stm32使用固件库点亮LED灯
1. 项目:使用stm32f10x点亮LED灯,并实现流水灯效果。
2. 代码:
main.c
#include "stm32f10x.h"
#include "bsp_led.h"
/*延时函数*/
void Delay(uint32_t count)
{
for(;count!= 0; count--);
}
int main(void)
{
LED_GPIO_Config();
while(1)
{
// //方法1
// GPIO_SetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN); //关灯
// Delay(0xfffff);
// GPIO_ResetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN); /*开灯*/
// Delay(0xfffff);
//方法2
LED_G(OFF); //关灯
Delay(0xffffff);
LED_G(ON); /*开灯*/
Delay(0xffffff);
}
}
bsp_led.c
#include "bsp_led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(LED_G_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = LED_G_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
}
bsp_led.h
#ifndef __bsp_led_h #define __bsp_led_h #include "stm32f10x.h" //方法1 #define LED_G_GPIO_PIN GPIO_Pin_0 #define LED_G_GPIO_PORT GPIOB #define LED_G_GPIO_CLK RCC_APB2Periph_GPIOB //方法2 #define ON 1 #define OFF 0 #define LED_G(a) if(a) GPIO_SetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN); \ else GPIO_ResetBits(LED_G_GPIO_PORT,LED_G_GPIO_PIN); void LED_GPIO_Config(void); #endif /*__bsp_led_h*/



浙公网安备 33010602011771号