下图为一般的单相全桥逆变器拓扑,其中4个开关管的SPWM控制通常通过MCU实现。本次采用STM32G474单片机,主频72MHZ,载波频率20KHZ,有几个关键步骤:
1.硬件部分
单片机:选择适合的单片机,如STM32,需要用到高级定时器的一些功能,由于T1,T2不能同时导通,T3,T4也不能同时导通,否则短路烧管子。所以T1与T2要用高级定时器的互补pwm输出,并给合适的死区时间,T3与T4同,最好用同一个高级定时器的两个通道,也可以只用一个通道,T1,T4相同,T2,T3相同,这种方式更简单。
H桥电路:包括四个MOSFET或IGBT,用于逆变器的全桥电路。
驱动电路:用于驱动H桥的MOSFET或IGBT,例如使用栅极驱动芯片。
电源:提供单片机和H桥的电源。
2.软件部分
2.1 初始化
系统初始化采用STM32CUBE MX来实现,借助此工具,可以快速生成初始化代码,并自动生成配置文件,极大地简化了初始化过程,提高了开发效率。主要用到两个定时器模块,一个高级定时器1(也可以定时器8)pwm互补输出;一个通用定时器,以正弦规律改占空比。初始化配置过程如下:
SYS配置:
RCC配置:
高级定时器timer1配置,如下:
要求载波频率为20Khz,则TIMER1的计数周期为主频72Mhz/20khz=3600,对应50us。
修改死区时间,确保上下管不会同时导通。死区时间配置是按照计数周期填写的,36个计数周期为500ns,配置如下:
接下来是生成的PWM配置 ,如下:
上图只是把初始占空比设置为1800/3600=50%,后续根据另一个定时器中断修改CRR的值,改变占空比。
2.2 生成SPWM波形
可以使用软件生成SPWM波形表。示例代码:
int spwm_group[400]={14,42,71,99,127,155,183,212,240,268,296,323,351,379,406,434,461, 489,516,543,570,596,623,649,676,702,728,754,779,805,830,855,880,904,928,953,976,1000,1023,1047,1069,1092,1114,1136,1158,1180,1201,1222,1242,1263,1283,1302,1322,1341,1360,1378,1396,1414,1431,1448,1464,1481,1497,1512,1527,1542,1556,1570,1584,1597,1610,1623,1635,1646,1658,1668,1679,1689,1698,1707,1716,1725,1732,1740,1747,1754,1760,1765,1771,1776,1780,1784,1788,1791,1793,1796,1797,1799,1800,1800,1800,1800,1799,1797,1796,1793,1791,1788,1784,1780,1776,1771,1765,1760,1754,1747,1740,1732,1725,1716,1707,1698,1689,1679,1668,1658,1646,1635,1623,1610,1597,1584,1570,1556,1542,1527,1512,1497,1481,1464,1448,1431,1414,1396,1378, 1360,1341,1322,1302,1283,1263,1242,1222,1201,1180,1158,1136,1114,1092,1069,1047,1023,1000,976,953,928,904,880,855,830,805,779,754,728,702,676,649,623,596,570,543,516,489,461,434,406,379,351,323,296,268,240,212,183,155,127,99,71,42,14,-14,-42,-71,-99,-127,-155,-183,-212,-240,-268,-296,-323,-351,-379,-406,-434,-461,-489,-516,-543,-570,-596,-623,-649,-676, -702,-728,-754,-779,-805,-830,-855,-880,-904,-928,-953,-976,-1000,-1023,-1047,-1069, -1092,-1114,-1136,-1158,-1180,-1201,-1222,-1242,-1263,-1283,-1302,-1322,-1341,-1360,-1378,-1396,-1414,-1431,-1448,-1464,-1481,-1497,-1512,-1527,-1542,-1556,-1570,-1584,-1597,-1610,-1623,-1635,-1646,-1658,-1668,-1679,-1689,-1698,-1707,-1716,-1725,-1732,-1740,-1747,-1754,-1760,-1765,-1771,-1776,-1780,-1784,-1788,-1791,-1793,-1796,-1797,-1799,-1800,-1800,-1800,-1800,-1799,-1797,-1796,-1793,-1791,-1788,-1784,-1780,-1776,-1771,-1765,-1760,-1754,-1747,-1740,-1732,-1725,-1716,-1707,-1698,-1689,-1679,-1668,-1658,-1646,-1635,-1623,-1610,-1597,-1584,-1570,-1556,-1542,-1527,-1512,-1497,-1481,-1464,-1448,-1431,-1414,-1396,-1378,-1360,-1341,-1322,-1302,-1283,-1263,-1242,-1222,-1201,-1180,-1158,-1136,-1114,-1092,-1069,-1047,-1023,-1000,-976,-953,-928,-904,-880,-855,-830,-805,-779,-754,-728,-702,-676,-649,-623,-596,-570,-543,-516,-489,-461,-434,-406,-379,-351,-323,-296,-268,-240,-212,-183,-155,-127,-99,-71,-42,-14, };
2.3 配置中断
使用通用定时器2中断来更新PWM占空比。定时器的中断频率决定了SPWM的频率。例如,假设目标SPWM频率是50Hz,采样点数为400,则定时器中断频率应设置为50Hz * 400 = 20kHz。
将timer2计数周期设置为3600,如下:
并开启中断,如下:
2.4 更新PWM占空比
在定时器中断服务程序中,从查找表中读取值,并更新PWM占空比。
在main.c中添加中断回调函数,如下:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) // 中断回调函数 { if(htim == &htim2) { TIM1->CCR1 = 1800.f + M*spwm_group[spwmcnt] ; spwmcnt++; if(spwmcnt == 400) spwmcnt = 0; } }
其中M为调制比在0,1之间,取0.8。
2.5 开启定时器中断,并发波
HAL_TIM_Base_Start_IT(&htim2); // 开启定时器2中断 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // 开启TIM1通道1的PWM波 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); // 开启TIM1通道1的互补PWM波
3. 全桥逆变控制
使用单片机的PWM输出控制H桥的上下臂开关。
4. 调试与测试
连接示波器,检查输出的SPWM波形,探头接下面两个引脚观察波形,PC0 ------> TIM1_CH1; PA7 ------> TIM1_CH1N。大约是50hz。
占空比上下互补,按正弦规律变化,(由于示波器原因只能看出一部分):
放大后死区时间为预设的500ns,如下:
下一步,连接负载,测试逆变器的实际运行情况,确保输出的交流电压和频率符合设计规范。