STM32 PWM通过RC低通滤波转双极性SPWM测试
- 📍参考内容《利用是stm32cubemx实现双极性spwm调制 基于stm32f407vet6》
- 📺相关视频链接:
https://www.bilibili.com/video/BV16S4y147hB/?spm_id_from=333.788
双极性SPWM调制讲解以及基于stm32的代码生成
✨理论相关知识,可以参考上面的相关内容。本文只是对该功能进行验证。
-
🌿测试硬件对象:
STM32F427
,不局限于型号,任意一个高级定时器都可以进行功能测试。 -
🔖二阶低通滤波器参考电路:
-
🌿TIM1高级定时器带死区时间控制参数的PWM互补输出波形:
-
🌿死区时间:
-
🌿经过二阶RC低通滤波后的波形。(橙色为PWM输入波形,红色为经过二阶RC滤波后的输出波形)
-
🔰二阶RC滤波和一阶RC滤波波形对比
🛠STM32CubeMX工程配置
- 🌿开启一路互补PWM输出。(具体频率参数可以根据需求自行配置)
- 🌿死区时间设置:
- 👉死区时间计算,可以参考我的另外一篇文章,有较为详细方法《HAL STM32G4 +TIM1 3路PWM互补输出+VOFA波形演示》
- 🌿另外配置一个20khz的定时器中断,用来根据正弦规律改占空比。(频率和上面的PWM频率参数一致)
📑业务代码
- 🌿在main函数中开启定时器PWM输出。
c
/* 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);
/* USER CODE END 2 */
- 🌿在定时器2,中断回调函数中,注入定时器1的PWM通道计数器的的比较值:
c
uint16_t spwmcnt=0;//数组计数值:0~399;
float M=0.8;//调制比M:0~1
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 + M*spwm_group[spwmcnt] ;//20khz的频率改变400次,20000/400=50hz的正弦波;
spwmcnt++;
if(spwmcnt==400)spwmcnt=0;
}
}
📚工程源码
c
链接:https://pan.baidu.com/s/1foLw5GocQhpzJVN_2qlLag?pwd=9r9x
提取码:9r9x