STM32——流水灯
宗旨:技术的学习是有限的,分享的精神是无限的。
stm32f10x_conf.h:打开stm32f10x_gpio.h和stm32f10x_rcc.h;
stm32f10x_gpio.c 和 stm32f10x_rcc.c加入工程模板中,只说重点。
【stm32f10x_rcc.c用于配置系统时钟 和外设时钟,由于每个外设都要配置时钟,所以它是每个外设都需要用到的库文件。】
// 新建led.h led.c
#ifndef _LED_H_
#define _LED_H_#include "stm32f10x.h"#define ON 0
#define OFF 1#define LED1(a) if (a) \GPIO_SetBits(GPIOC,GPIO_Pin_3);\else \GPIO_ResetBits(GPIOC,GPIO_Pin_3)#define LED2(a) if (a) \GPIO_SetBits(GPIOC,GPIO_Pin_4);\else \GPIO_ResetBits(GPIOC,GPIO_Pin_4)#define LED3(a) if (a) \GPIO_SetBits(GPIOC,GPIO_Pin_5);\else \GPIO_ResetBits(GPIOC,GPIO_Pin_5)void LED_GPIO_Config(void);#endif /* _LED_H_ */
#include "led.h"void LED_GPIO_Config(void)
{GPIO_InitTypeDef GPIO_InitStructure; /*定义一个GPIO_InitTypeDef类型的结构体*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); /*开启GPIOC的外设时钟*/GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; /*选择要控制的GPIOC引脚*/GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*设置引脚模式为通用推挽输出*/GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*设置引脚速率为50MHz*/GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_SetBits(GPIOC, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5); /*关闭所有led灯 */
}
#include "stm32f10x.h"
#include "led.h"void Delay(__IO u32 count) //简单的延时函数
{while(count--);
}int main(void)
{LED_GPIO_Config(); /*LED 端口初始化:配置引脚,时钟,输入输出方式,速率 */while(1){LED1(ON ); Delay(0x0FFFEF);LED1(OFF ); LED2(ON );Delay(0x0FFFEF);LED2(OFF );LED3(ON );Delay(0x0FFFEF);LED3(OFF );}return 0;
}