【利用stm32实现单极性倍频SPWM调制(电赛必备)-哔哩哔哩
利用stm32cubemx生成单极性倍频spwm调制,首先打开cubemx选择mcu型号,配置时钟等,很多教程讲得很好,我这就跳过这些;
接下来来到定时器的配置,对于全桥电路,需要驱动四个开关管,所以需要选择一个高级定时器(定时器1或8)的两路互补输出,我们这里选择定时器1的通道1,2(最好选择同一个定时器的两个通道);
预分频值设成0,ARR设成8399,选择中心对齐计数模式1,计数是从0到8399再到0是一个周期,所以生成的pwm波的频率是168M/(2*(8399+1))/(0+1)=10kHZ;由于是单极倍频,实际把spwm波给到全桥上时,起到20kHZ的效果。
占空比可以随便给一个,这里给个中间值4200,如果没有硬件死区记得给死区,不然容易烧管子。
接下来配置中断改变占空比来实现spwm调制。这里我们选择TIM2的中断(也可以其他的)。
这里是20kHZ的中断,注意计数方式默认向上计数就好。记得把中断勾上
接下来工程命名与存储路径的选择,这里也略过。
生成代码转到keil5上(其他开发环境自行参考)
在main函数里相应位置加上以下几行代码。
/* USER CODE BEGIN 2 */HAL_TIM_Base_Start_IT(&htim2);//开启定时器2中断HAL_TIM_PWM_Start (&htim1,TIM_CHANNEL_1);//开启四路pwm波HAL_TIMEx_PWMN_Start (&htim1,TIM_CHANNEL_1);HAL_TIM_PWM_Start (&htim1,TIM_CHANNEL_2);HAL_TIMEx_PWMN_Start (&htim1,TIM_CHANNEL_2);/* USER CODE END 2 */
然后就是要在中断回调函数里改占空比来生成spwm,这里我们采用正弦数组表的方法(也可以直接用正弦公式计算)
数组表如上所示,在合适位置加上如下代码
/* USER CODE BEGIN PV */uint16_t spwmcnt=0;int spwm_group[400]={33,99,165,231,297,362,428,494,559,624,690,755,819,884,948,1013,1076,1140,1203,1266,1329,1392,1454,1515,1577,1638,1698,1758,1818,1877,1936,1994,2052,2110,2166,2223,2278,2333,2388,2442,2495,2548,2600,2652,2703,2753,2802,2851,2899,2946,2993,3039,3084,3129,3172,3215,3257,3298,3339,3378,3417,3455,3492,3528,3564,3598,3632,3664,3696,3727,3757,3786,3814,3841,3868,3893,3917,3940,3963,3984,4005,4024,4042,4060,4076,4092,4106,4119,4132,4143,4153,4163,4171,4178,4184,4190,4194,4197,4199,4200,4200,4199,4197,4194,4190,4184,4178,4171,4163,4153,4143,4132,4119,4106,4092,4076,4060,4042,4024,4005,3984,3963,3940,3917,3893,3868,3841,3814,3786,3757,3727,3696,3664,3632,3598,3564,3528,3492,3455,3417,3378,3339,3298,3257,3215,3172,3129,3084,3039,2993,2946,2899,2851,2802,2753,2703,2652,2600,2548,2495,2442,2388,2333,2278,2223,2166,2110,2052,1994,1936,1877,1818,1758,1698,1638,1577,1515,1454,1392,1329,1266,1203,1140,1076,1013,948,884,819,755,690,624,559,494,428,362,297,231,165,99,33,-33,-99,-165,-231,-297,-362,-428,-494,-559,-624,-690,-755,-819,-884,-948,-1013,-1076,-1140,-1203,-1266,-1329,-1392,-1454,-1515,-1577,-1638,-1698,-1758,-1818,-1877,-1936,-1994,-2052,-2110,-2166,-2223,-2278,-2333,-2388,-2442,-2495,-2548,-2600,-2652,-2703,-2753,-2802,-2851,-2899,-2946,-2993,-3039,-3084,-3129,-3172,-3215,-3257,-3298,-3339,-3378,-3417,-3455,-3492,-3528,-3564,-3598,-3632,-3664,-3696,-3727,-3757,-3786,-3814,-3841,-3868,-3893,-3917,-3940,-3963,-3984,-4005,-4024,-4042,-4060,-4076,-4092,-4106,-4119,-4132,-4143,-4153,-4163,-4171,-4178,-4184,-4190,-4194,-4197,-4199,-4200,-4200,-4199,-4197,-4194,-4190,-4184,-4178,-4171,-4163,-4153,-4143,-4132,-4119,-4106,-4092,-4076,-4060,-4042,-4024,-4005,-3984,-3963,-3940,-3917,-3893,-3868,-3841,-3814,-3786,-3757,-3727,-3696,-3664,-3632,-3598,-3564,-3528,-3492,-3455,-3417,-3378,-3339,-3298,-3257,-3215,-3172,-3129,-3084,-3039,-2993,-2946,-2899,-2851,-2802,-2753,-2703,-2652,-2600,-2548,-2495,-2442,-2388,-2333,-2278,-2223,-2166,-2110,-2052,-1994,-1936,-1877,-1818,-1758,-1698,-1638,-1577,-1515,-1454,-1392,-1329,-1266,-1203,-1140,-1076,-1013,-948,-884,-819,-755,-690,-624,-559,-494,-428,-362,-297,-231,-165,-99,-33,}; void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//中断回调函数 20k{if(htim==&htim2){TIM1->CCR1 = 4200.f + spwm_group[spwmcnt] ;TIM1->CCR2 = 4200.f - spwm_group[spwmcnt] ;spwmcnt++;if(spwmcnt==400)spwmcnt=0;}}/* USER CODE END PV */
对整套工程编译,没有报错,可以正常烧录使用。
怎么检验是不是正常的spwm波呢,首先在固定占空比下看pwm波是不是10kHZ的,然后准备两个一样的RC滤波器,选择两路通道接上,并用示波器的两个通道同时看两个滤波器中电容两端波形,理论上CH1和CH1N为反相的50HZ正弦波,2通道也是,另外CH1与CH2反相,CH1与CH2N同相。
如果有问题,欢迎留言交流,顺便把工程上传了,供大家下载(没有会员的就没必要特意开了哈)。
https://download.csdn.net/download/m0_65265936/86340818
如有错误,欢迎指正。