使用C++进行STM32开发

张开发
2026/4/4 8:26:01 15 分钟阅读
使用C++进行STM32开发
使用方法还是以上述博客中的项目模板为例只需要将main.c文件改名为main.cpp就可以在 main 中使用 C 特性了。比如实现一个简单的class LEDclass LED{public:LED(GPIO_TypeDef *port, uint16_t pin, bool polarity false): port_(port), pin_(pin), polarity_(polarity), state_(false){}void on(){if (this-polarity_ true) {GPIO_SetBits(this-port_, this-pin_);} else {GPIO_ResetBits(this-port_, this-pin_);}this-state_ true;}void off(){if (this-polarity_ true) {GPIO_ResetBits(this-port_, this-pin_);} else {GPIO_SetBits(this-port_, this-pin_);}this-state_ false;}void toggle(){if (this-state_ true) {this-off();} else {this-on();}}bool state() const{return this-state_;}private:GPIO_TypeDef *port_;uint16_t pin_;bool polarity_; // true-高电平点亮false-低电平点亮bool state_; // 当前状态true-点亮false-灭};其余初始化代码不变定义LED led{GPIOC, GPIO_PIN_13}然后将死循环中的反转电平改为调用led.toggle()即可。完整的程序如下所示/** ************************************************** STM32 blink gcc demo** CPU: STM32F103C8* PIN: PA1** *************************************************/#include stm32f10x.hclass LED;void delay(int x){for (int i 0; i x; i) {for (int j 0; j 1000; j)__NOP();}}int main(){GPIO_InitTypeDef gpioDef;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);gpioDef.GPIO_Mode GPIO_Mode_Out_PP;gpioDef.GPIO_Pin GPIO_Pin_13;gpioDef.GPIO_Speed GPIO_Speed_10MHz;GPIO_Init(GPIOC, gpioDef);auto led LED{GPIOC, GPIO_Pin_13};while (true) {led.toggle();delay(500);}}烧录可以看到开发板上的 LED 正常闪烁。注意事项中断函数特殊处理由于 C 存在符号修饰特性因此当定义中断服务函数的时候一定要显式添加extern C关键字否则可能会导致中断异常extern C void TIM2_IRQHandler(){// your code here}一个一劳永逸的方法是在stm32f10x_it.h文件中直接声明好所有的中断函数ST 官方已做好适配/********************************************************************************* file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.h* author MCD Application Team* version V3.6.0* date 20-September-2021* brief This file contains the headers of the interrupt handlers.******************************************************************************* attention** Copyright (c) 2011 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************//* Define to prevent recursive inclusion -------------------------------------*/#ifndef __STM32F10x_IT_H#define __STM32F10x_IT_H#ifdef __cplusplusextern C {#endif/* Includes ------------------------------------------------------------------*/#include stm32f10x.h/* Exported types ------------------------------------------------------------*//* Exported constants --------------------------------------------------------*//* Exported macro ------------------------------------------------------------*//* Exported functions ------------------------------------------------------- *//* System exception handlers */void NMI_Handler(void);void HardFault_Handler(void);void MemManage_Handler(void);void BusFault_Handler(void);void UsageFault_Handler(void);void SVC_Handler(void);void DebugMon_Handler(void);void PendSV_Handler(void);void SysTick_Handler(void);/* Peripheral interrupt handlers */void WWDG_IRQHandler(void);void PVD_IRQHandler(void);void TAMPER_IRQHandler(void);void RTC_IRQHandler(void);void FLASH_IRQHandler(void);void RCC_IRQHandler(void);void EXTI0_IRQHandler(void);void EXTI1_IRQHandler(void);void EXTI2_IRQHandler(void);void EXTI3_IRQHandler(void);void EXTI4_IRQHandler(void);void DMA1_Channel1_IRQHandler(void);void DMA1_Channel2_IRQHandler(void);void DMA1_Channel3_IRQHandler(void);void DMA1_Channel4_IRQHandler(void);void DMA1_Channel5_IRQHandler(void);void DMA1_Channel6_IRQHandler(void);void DMA1_Channel7_IRQHandler(void);void ADC1_2_IRQHandler(void);void USB_HP_CAN1_TX_IRQHandler(void);void USB_LP_CAN1_RX0_IRQHandler(void);void CAN1_RX1_IRQHandler(void);void CAN1_SCE_IRQHandler(void);void EXTI9_5_IRQHandler(void);void TIM1_BRK_IRQHandler(void);void TIM1_UP_IRQHandler(void);void TIM1_TRG_COM_IRQHandler(void);void TIM1_CC_IRQHandler(void);void TIM2_IRQHandler(void);void TIM3_IRQHandler(void);void TIM4_IRQHandler(void);void I2C1_EV_IRQHandler(void);void I2C1_ER_IRQHandler(void);void I2C2_EV_IRQHandler(void);void I2C2_ER_IRQHandler(void);void SPI1_IRQHandler(void);void SPI2_IRQHandler(void);void USART1_IRQHandler(void);void USART2_IRQHandler(void);void USART3_IRQHandler(void);void EXTI15_10_IRQHandler(void);void RTC_Alarm_IRQHandler(void);void USBWakeUp_IRQHandler(void);#ifdef __cplusplus}#endif#endif /* __STM32F10x_IT_H */当有.cpp文件中需要定义中断服务函数的时候直接#include stm32f10x_it.h即可。STL 适用建议正常情况下STL 中的各种容器和算法可以正常使用如vectoralgorithm等。但是由于嵌入式环境中内存资源有限缺少内存管理器所以尽量避免使用vectorstring等涉及到动态内存分配的容器而array等不涉及动态内存分配的静态容器可以正常使用。

更多文章