找回密码
 立即注册
首页 业界区 安全 使用C++进行STM32开发

使用C++进行STM32开发

篙菠 2026-1-17 01:40:02
传统的STM32开发,使用的是C语言。C++中的一些高级特性,如引用、面向对象等,可以极大地提高代码的可读性和易维护性。
前置条件

开发板:STM32F103C8T6
开发环境:vscode+EIDE插件
编译器:arm-none-eabi-gcc v15.2.0
开发环境的适用方法,可以参考这篇博客:vscode+edie插件配置STM32开发环境,彻底摆脱KEIL
使用方法

还是以上述博客中的项目模板为例:
只需要将 main.c 文件改名为 main.cpp,就可以在main中使用C++特性了。
比如实现一个简单的 class LED :
  1. class LED
  2. {
  3. public:
  4.     LED(GPIO_TypeDef *port, uint16_t pin, bool polarity = false)
  5.         : port_(port), pin_(pin), polarity_(polarity), state_(false)
  6.     {
  7.     }
  8.     void on()
  9.     {
  10.         if (this->polarity_ == true) {
  11.             GPIO_SetBits(this->port_, this->pin_);
  12.         } else {
  13.             GPIO_ResetBits(this->port_, this->pin_);
  14.         }
  15.         this->state_ = true;
  16.     }
  17.     void off()
  18.     {
  19.         if (this->polarity_ == true) {
  20.             GPIO_ResetBits(this->port_, this->pin_);
  21.         } else {
  22.             GPIO_SetBits(this->port_, this->pin_);
  23.         }
  24.         this->state_ = false;
  25.     }
  26.     void toggle()
  27.     {
  28.         if (this->state_ == true) {
  29.             this->off();
  30.         } else {
  31.             this->on();
  32.         }
  33.     }
  34.     bool state() const
  35.     {
  36.         return this->state_;
  37.     }
  38. private:
  39.     GPIO_TypeDef *port_;
  40.     uint16_t pin_;
  41.     bool polarity_; // true-高电平点亮,false-低电平点亮
  42.     bool state_; // 当前状态:true-点亮,false-灭
  43. };
复制代码
其余初始化代码不变,定义 LED led{GPIOC, GPIO_PIN_13} 然后将死循环中的反转电平改为调用 led.toggle() 即可。
完整的程序如下所示:
  1. /*
  2. * ************************************************
  3. *
  4. *              STM32 blink gcc demo
  5. *
  6. *  CPU: STM32F103C8
  7. *  PIN: PA1
  8. *
  9. * ************************************************
  10. */
  11. #include "stm32f10x.h"
  12. class LED;
  13. void delay(int x)
  14. {
  15.     for (int i = 0; i < x; i++) {
  16.         for (int j = 0; j < 1000; j++)
  17.             __NOP();
  18.     }
  19. }
  20. int main()
  21. {
  22.     GPIO_InitTypeDef gpioDef;
  23.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  24.     gpioDef.GPIO_Mode  = GPIO_Mode_Out_PP;
  25.     gpioDef.GPIO_Pin   = GPIO_Pin_13;
  26.     gpioDef.GPIO_Speed = GPIO_Speed_10MHz;
  27.     GPIO_Init(GPIOC, &gpioDef);
  28.     auto led = LED{GPIOC, GPIO_Pin_13};
  29.     while (true) {
  30.         led.toggle();
  31.         delay(5000);
  32.     }
  33. }
复制代码
烧录,可以看到开发板上的LED正常闪烁。
注意事项

中断函数特殊处理

由于C++存在符号修饰特性,因此当定义中断服务函数的时候,一定要显示添加 extern "C" 关键字,否则可能会导致中断异常:
  1. extern "C" void TIM2_IRQHandler(){
  2.     // your code here
  3. }
复制代码
一个一劳永逸的方法是,在 stm32f10x_it.h 文件中直接声明好所有的中断函数,ST官方已做好适配:
  1. /**
  2. ******************************************************************************
  3. * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.h
  4. * @author  MCD Application Team
  5. * @version V3.6.0
  6. * @date    20-September-2021
  7. * @brief   This file contains the headers of the interrupt handlers.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * Copyright (c) 2011 STMicroelectronics.
  12. * All rights reserved.
  13. *
  14. * This software is licensed under terms that can be found in the LICENSE file
  15. * in the root directory of this software component.
  16. * If no LICENSE file comes with this software, it is provided AS-IS.
  17. *
  18. ******************************************************************************
  19. */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __STM32F10x_IT_H
  22. #define __STM32F10x_IT_H
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* Includes ------------------------------------------------------------------*/
  27. #include "stm32f10x.h"
  28. /* Exported types ------------------------------------------------------------*/
  29. /* Exported constants --------------------------------------------------------*/
  30. /* Exported macro ------------------------------------------------------------*/
  31. /* Exported functions ------------------------------------------------------- */
  32. /* System exception handlers */
  33. void NMI_Handler(void);
  34. void HardFault_Handler(void);
  35. void MemManage_Handler(void);
  36. void BusFault_Handler(void);
  37. void UsageFault_Handler(void);
  38. void SVC_Handler(void);
  39. void DebugMon_Handler(void);
  40. void PendSV_Handler(void);
  41. void SysTick_Handler(void);
  42. /* Peripheral interrupt handlers */
  43. void WWDG_IRQHandler(void);
  44. void PVD_IRQHandler(void);
  45. void TAMPER_IRQHandler(void);
  46. void RTC_IRQHandler(void);
  47. void FLASH_IRQHandler(void);
  48. void RCC_IRQHandler(void);
  49. void EXTI0_IRQHandler(void);
  50. void EXTI1_IRQHandler(void);
  51. void EXTI2_IRQHandler(void);
  52. void EXTI3_IRQHandler(void);
  53. void EXTI4_IRQHandler(void);
  54. void DMA1_Channel1_IRQHandler(void);
  55. void DMA1_Channel2_IRQHandler(void);
  56. void DMA1_Channel3_IRQHandler(void);
  57. void DMA1_Channel4_IRQHandler(void);
  58. void DMA1_Channel5_IRQHandler(void);
  59. void DMA1_Channel6_IRQHandler(void);
  60. void DMA1_Channel7_IRQHandler(void);
  61. void ADC1_2_IRQHandler(void);
  62. void USB_HP_CAN1_TX_IRQHandler(void);
  63. void USB_LP_CAN1_RX0_IRQHandler(void);
  64. void CAN1_RX1_IRQHandler(void);
  65. void CAN1_SCE_IRQHandler(void);
  66. void EXTI9_5_IRQHandler(void);
  67. void TIM1_BRK_IRQHandler(void);
  68. void TIM1_UP_IRQHandler(void);
  69. void TIM1_TRG_COM_IRQHandler(void);
  70. void TIM1_CC_IRQHandler(void);
  71. void TIM2_IRQHandler(void);
  72. void TIM3_IRQHandler(void);
  73. void TIM4_IRQHandler(void);
  74. void I2C1_EV_IRQHandler(void);
  75. void I2C1_ER_IRQHandler(void);
  76. void I2C2_EV_IRQHandler(void);
  77. void I2C2_ER_IRQHandler(void);
  78. void SPI1_IRQHandler(void);
  79. void SPI2_IRQHandler(void);
  80. void USART1_IRQHandler(void);
  81. void USART2_IRQHandler(void);
  82. void USART3_IRQHandler(void);
  83. void EXTI15_10_IRQHandler(void);
  84. void RTC_Alarm_IRQHandler(void);
  85. void USBWakeUp_IRQHandler(void);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif /* __STM32F10x_IT_H */
复制代码
当有 .cpp 文件中需要定义中断服务函数的时候,直接 #include "stm32f10x_it.h" 即可。
STL 适用建议

正常情况下,STL中的各种容器和算法可以正常使用,如 vector  等。但是由于嵌入式环境中内存资源有限,缺少内存管理器,所以 尽量避免使用 vector string 等涉及到动态内存分配的容器,而· array 等不涉及动态内存分配的静态容器可以正常使用。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

2026-1-18 23:00:48

举报

2026-1-20 07:47:16

举报

7 天前

举报

您需要登录后才可以回帖 登录 | 立即注册