- led.c:
- #include "led.h"
- void LED_GPIO_Config(void)
- {
- // 定义一个 GPIO_InitTypeDef 类型的结构体
- GPIO_InitTypeDef GPIO_InitStructure;
- // 开启 GPIOC 的外设时钟
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE);
- // 选择要控制的 GPIOC 引脚
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
- // 设置引脚模式为通用推挽输出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- // 设置引脚速率为 50Mhz
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // 调用库函数,初始化GPIOC
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- // 设置GPIOC3、GPIOC4、GPIOC5 为高电平
- GPIO_SetBits(GPIOC, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5);
- }
- led.h:
- #ifndef __LED_H
- #define __LED_H
- #include "stm32f10x.h"
- #include "stm32f10x_rcc.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
- main.c
- #include "stm32f10x.h"
- #include "led.c"
- void Delay(__IO u32 nCount)
- {
- for(; nCount !=0; nCount--);
- }
- int main(void)
- {
- LED_GPIO_Config();
- while (1)
- {
- LED1(ON);
- Delay(0x0FFFEF);
- LED1(OFF);
- Delay(0x0FFFEF);
- }
- }
|