新建工程配置

工程配置过程详见零死角玩转stm32 第11章

点亮led灯

新建文件夹

把固件库版本的新建工程作为模板,位置:G:\开发板学习\野火指南者F103\1-书籍配套例程-F103VE指南者\11-新建工程—固件库版本。在use文件夹下新建led文件夹,在led文件夹中新建led.c和ed.h文件。
image.png

编写程序

led流水灯
main.c

  1. #include "stm32f10x.h"
  2. #include "bsp_led.h"
  3. void Delay( uint32_t count)
  4. {
  5. for(;count != 0;count--);
  6. }
  7. void GreenLed(uint32_t t)
  8. {
  9. LED_G(ON);
  10. LED_B(OFF);
  11. LED_R(OFF);
  12. Delay(t);
  13. }
  14. void BlueLed(uint32_t t)
  15. {
  16. LED_G(OFF);
  17. LED_B(ON);
  18. LED_R(OFF);
  19. Delay(t);
  20. }
  21. void RedLed(uint32_t t)
  22. {
  23. LED_G(OFF);
  24. LED_B(OFF);
  25. LED_R(ON);
  26. Delay(t);
  27. }
  28. int main(void)
  29. {
  30. uint32_t t = 0xFFFFFF;
  31. LED_GPIO_Config();
  32. while(1)
  33. {
  34. GreenLed(t);
  35. BlueLed(t);
  36. RedLed(t);
  37. }
  38. }

led.c

  1. //bsp:board support package
  2. #include "bsp_led.h"
  3. void LED_GPIO_Config(void)
  4. {
  5. GPIO_InitTypeDef GPIO_InitStruct;
  6. RCC_APB2PeriphClockCmd(LED_G_GPIO_CLK , ENABLE);
  7. GPIO_InitStruct.GPIO_Pin = LED_G_GPIO_Pin;
  8. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  9. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  10. GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
  11. GPIO_InitStruct.GPIO_Pin = LED_B_GPIO_Pin;
  12. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  13. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  14. GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
  15. GPIO_InitStruct.GPIO_Pin = LED_R_GPIO_Pin;
  16. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  17. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  18. GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
  19. }

led.h

  1. #ifndef __BSP_LED_H
  2. #define __BSP_LED_H
  3. #include "stm32f10x.h"
  4. #define LED_G_GPIO_Pin GPIO_Pin_0
  5. #define LED_B_GPIO_Pin GPIO_Pin_1
  6. #define LED_R_GPIO_Pin GPIO_Pin_5
  7. #define LED_G_GPIO_PORT GPIOB
  8. #define LED_G_GPIO_CLK RCC_APB2Periph_GPIOB
  9. #define ON 1
  10. #define OFF 0
  11. #define LED_G(a) if(a) \
  12. GPIO_ResetBits(LED_G_GPIO_PORT, LED_G_GPIO_Pin); \
  13. else GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_Pin);
  14. #define LED_B(a) if(a) \
  15. GPIO_ResetBits(LED_G_GPIO_PORT, LED_B_GPIO_Pin); \
  16. else GPIO_SetBits(LED_G_GPIO_PORT, LED_B_GPIO_Pin);
  17. #define LED_R(a) if(a) \
  18. GPIO_ResetBits(LED_G_GPIO_PORT, LED_R_GPIO_Pin); \
  19. else GPIO_SetBits(LED_G_GPIO_PORT, LED_R_GPIO_Pin);
  20. void LED_GPIO_Config(void);
  21. #endif /* __BSP_LED_H */

小技巧:

Doxygen 注释规范

image.png
这是一种名为“Doxygen”的注释规范,如果在工程文件中按照这种规范去注释,可以使用 Doxygen 软件自动根据注释生成帮助文档。
语雀内容

keil5代码编写自动补充

image.png
在edit/configuration /Text Completion中,勾选第一个 struct/class members即可

关闭keil5动态检查

image.png
在edit/configuration
image.png
不要勾选Enable,就可以关闭语法检查。

更改字体

image.png
在edit/configuration
image.png
字体推荐选择 GB2312
image.png
这里可以修改具体的关键字颜色、字体