一、步骤:
1、使能RCC时钟;
2、配置GPIO引脚;
3、控制引脚电平。
二、代码:
//bsp_led.h
#define __BSP_LED_H
#include"stm32f10x.h"
#define LED_R_GPIO_PIN GPIO_Pin_5
#define LED_G_GPIO_PIN GPIO_Pin_0
#define LED_B_GPIO_PIN GPIO_Pin_1
#define LED_RGB_GPIO_PORT GPIOB
#define LED_RGB_GPIO_CLK RCC_APB2Periph_GPIOB
#define ON 1
#define OFF 0
#define LED_R(a) if(a)GPIO_ResetBits(LED_RGB_GPIO_PORT, LED_R_GPIO_PIN);else GPIO_SetBits(LED_RGB_GPIO_PORT, LED_R_GPIO_PIN);
#define LED_G(a) if(a)GPIO_ResetBits(LED_RGB_GPIO_PORT, LED_G_GPIO_PIN);else GPIO_SetBits(LED_RGB_GPIO_PORT, LED_G_GPIO_PIN);
#define LED_B(a) if(a)GPIO_ResetBits(LED_RGB_GPIO_PORT, LED_B_GPIO_PIN);else GPIO_SetBits(LED_RGB_GPIO_PORT, LED_B_GPIO_PIN);
void LED_GPIO_Config(void);
#endif //__BSP_LED_H
//bsp_led.c
#include"bsp_led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(LED_RGB_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = LED_R_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_RGB_GPIO_PORT, &GPIO_InitStruct);
}
//点亮其他两灯操作类似
//main.c
#include"stm32f10x.h"
#include"bsp_led.h"
void Delay(uint32_t d)
{
for(;d!=0;d--);
}
int main()
{
int ans=50;
LED_GPIO_Config();
while(ans--)
{
//GPIO_SetBits(LED_RGB_GPIO_PORT, LED_R_GPIO_PIN);
LED_R(ON);
Delay(0xFFFFF);
//GPIO_ResetBits(LED_RGB_GPIO_PORT, LED_R_GPIO_PIN);
LED_R(OFF);
Delay(0xFFFFF);
}
GPIO_SetBits(LED_RGB_GPIO_PORT, LED_R_GPIO_PIN);
}