【嵌入式】STM32输出SPWM实现逆变
一、SPWM逆变原理逆变是一个成熟的技术,无论是单极性逆变还是双极性逆变,都离不开SPWM调制。对于H桥全桥逆变,使用的是双极性SPWM逆变,因此本文只针对双极性逆变进行讲解,对于其硬件原理,可以参照【单相全桥逆变电路工作过程】。SPWM逆变的精髓就是【冲量等效原理】——将一周期正弦波等分为N段,每段的长度为T,可以算出任意一段的积分面积S(即正弦波在T时间段内与横轴围成的面积),如果在每一段T时
一、SPWM逆变原理
逆变是一个成熟的技术,无论是单极性逆变还是双极性逆变,都离不开SPWM调制。对于H桥全桥逆变,使用的是双极性SPWM逆变,因此本文只针对双极性逆变进行讲解,对于其硬件原理,可以参照【单相全桥逆变电路工作过程】。
SPWM逆变的精髓就是【冲量等效原理】——将一周期正弦波等分为N段,每段的长度为T,可以算出任意一段的积分面积S(即正弦波在T时间段内与横轴围成的面积),如果在每一段T时间段内,SPWM波与横轴围成的面积都等于T内正弦波的定积分S,那么在后级对SPWM进行滤波后即可得到正弦波。
如上图所示,只要能控制单片机输出这样的SPWM波,即可通过滤波得到正弦波。
那么如何让单片机输出这样的波形呢?主要分以下几步:
1.确定载波频率fb
载波即下图中红线标出的波形,占空比的调整都在每个载波周期内进行。对于单片机,我们只需要控制在某个载波周期内,高低电平所占的时间即可。一般来讲,载波频率越高,滤出的正弦波越细腻,能调出的正弦波频率上限也越高。
2.确定正弦波频率fs
3.得到正弦波一周波分段N
N=fb/fs ,由此可见,正弦波频率越低,N越大,基波频率越低,N越小。
4.分段计算正弦波积分S,得出PWM占空比序列
这里的分段计算,就是在整个正弦波一周期内,依次计算。计算出每段S后,另PWM与横轴围成的面积等于S,即可得出该段的PWM占空比。一周期计算完成后,即得出了PWM的占空比序列。
5.单片机输出SPWM
得到PWM序列后,其实就是个一维数组,写入单片机后,在每个定时器中断中更新占空比即可。
二、SPWM序列生成
掌握以上原理后,需要先通过软件计算生成spwm序列,根据以上原理,编写matlab程序如下:
%-----硬件设置-------------------------------------
timerCLK = 160000000;%timer时钟,160MHz/1分频
ARR = 7999;
baseRate = timerCLK / (ARR + 1);%载波频率
%-----正弦波设置------------------------------------
F = 25;%频率
T = 1/F;%周期
ampScale = 0.85;%调幅
N = round(baseRate / F);%一周波分多少段
stepTime = 1/F/N;%积分步进值
%-----计算------------------------------------------
CCR = (1:1:N)';
for i=1:1:N
area = double(quad(@(x)sin(2*pi*F*x),(i-1)*stepTime,i*stepTime));
CCR(i) = round((ampScale*area*ARR / stepTime + ARR) / 2);
end
这里的timerCLK即STM32 TIMER1的时钟频率,ARR即定时器重载值,这样就得到了载波频率。
然后对正弦波的各个参数进行设置,最后的for循环便计算了每一段的正弦波积分面积,并根据积分面积计算出高电平计数CCR。这样一来,将CCR序列放入单片机中,在每个定时器中断更新CCR即可。
三、单片机程序
1.时钟配置
双极性的全桥逆变需要生成互补极性的PWM,利用STM32CubeMx配置时钟1如下:
配置时钟分频为0,即不分频,时钟频率就是HCLK频率160MHz;配置ARR为8000-1,即和上述matlab程序吻合;
配置PWM为模式1,占空比pulse可以乱配,因为程序一运行就从CCR序列取数来更新pulse了;正负极性通道的极性配置为高电平,当计数值大于CCR时反转为低电平。
最后,还要使能配置TIM1的捕获/比较中断,即在一个ARR周期内,当计数值大于CCR后触发中断,进而在中断函数中更新CCR值,从而实现SPWM。
2.中断函数
//一周期内分多少段
#define SPWM_N 800
//序列计数
static int spwmCnt = 0;
//SPWM计数
static int spwmData[SPWM_N] = {
4013,4040,4067,4093,4120,4147,4173,4200,4227,4253,4280,4307,4333,4360,4386,4413,4439,4466,4492,
4519,4545,4571,4598,4624,4650,4676,4703,4729,4755,4781,4807,4833,4858,4884,4910,4936,4961,4987,
5012,5038,5063,5089,5114,5139,5164,5189,5214,5239,5264,5289,5313,5338,5363,5387,5411,5436,5460,
5484,5508,5532,5555,5579,5603,5626,5650,5673,5696,5719,5742,5765,5788,5811,5833,5855,5878,5900,
5922,5944,5966,5988,6009,6031,6052,6073,6094,6115,6136,6157,6178,6198,6218,6238,6258,6278,6298,
6318,6337,6356,6376,6395,6414,6432,6451,6469,6488,6506,6524,6542,6559,6577,6594,6611,6628,6645,
6662,6678,6695,6711,6727,6743,6758,6774,6789,6805,6820,6834,6849,6864,6878,6892,6906,6920,6933,
6947,6960,6973,6986,6999,7011,7023,7035,7047,7059,7071,7082,7093,7104,7115,7126,7136,7146,7156,
7166,7176,7185,7194,7203,7212,7221,7229,7238,7246,7254,7261,7269,7276,7283,7290,7296,7303,7309,
7315,7321,7327,7332,7337,7342,7347,7352,7356,7360,7364,7368,7371,7375,7378,7381,7384,7386,7388,
7391,7392,7394,7396,7397,7398,7399,7399,7400,7400,7400,7400,7399,7399,7398,7397,7396,7394,7392,
7391,7388,7386,7384,7381,7378,7375,7371,7368,7364,7360,7356,7352,7347,7342,7337,7332,7327,7321,
7315,7309,7303,7296,7290,7283,7276,7269,7261,7254,7246,7238,7229,7221,7212,7203,7194,7185,7176,
7166,7156,7146,7136,7126,7115,7104,7093,7082,7071,7059,7047,7035,7023,7011,6999,6986,6973,6960,
6947,6933,6920,6906,6892,6878,6864,6849,6834,6820,6805,6789,6774,6758,6743,6727,6711,6695,6678,
6662,6645,6628,6611,6594,6577,6559,6542,6524,6506,6488,6469,6451,6432,6414,6395,6376,6356,6337,
6318,6298,6278,6258,6238,6218,6198,6178,6157,6136,6115,6094,6073,6052,6031,6009,5988,5966,5944,
5922,5900,5878,5855,5833,5811,5788,5765,5742,5719,5696,5673,5650,5626,5603,5579,5555,5532,5508,
5484,5460,5436,5411,5387,5363,5338,5313,5289,5264,5239,5214,5189,5164,5139,5114,5089,5063,5038,
5012,4987,4961,4936,4910,4884,4858,4833,4807,4781,4755,4729,4703,4676,4650,4624,4598,4571,4545,
4519,4492,4466,4439,4413,4386,4360,4333,4307,4280,4253,4227,4200,4173,4147,4120,4093,4067,4040,
4013,3987,3960,3933,3907,3880,3853,3827,3800,3773,3747,3720,3693,3667,3640,3614,3587,3561,3534,
3508,3481,3455,3429,3402,3376,3350,3324,3297,3271,3245,3219,3193,3167,3142,3116,3090,3064,3039,
3013,2988,2962,2937,2911,2886,2861,2836,2811,2786,2761,2736,2711,2687,2662,2637,2613,2589,2564,
2540,2516,2492,2468,2445,2421,2397,2374,2350,2327,2304,2281,2258,2235,2212,2189,2167,2145,2122,
2100,2078,2056,2034,2012,1991,1969,1948,1927,1906,1885,1864,1843,1822,1802,1782,1762,1742,1722,
1702,1682,1663,1644,1624,1605,1586,1568,1549,1531,1512,1494,1476,1458,1441,1423,1406,1389,1372,
1355,1338,1322,1305,1289,1273,1257,1242,1226,1211,1195,1180,1166,1151,1136,1122,1108,1094,1080,
1067,1053,1040,1027,1014,1001,989,977,965,953,941,929,918,907,896,885,874,864,854,844,834,824,
815,806,797,788,779,771,762,754,746,739,731,724,717,710,704,697,691,685,679,673,668,663,658,
653,648,644,640,636,632,629,625,622,619,616,614,612,609,608,606,604,603,602,601,601,600,600,
600,600,601,601,602,603,604,606,608,609,612,614,616,619,622,625,629,632,636,640,644,648,653,
658,663,668,673,679,685,691,697,704,710,717,724,731,739,746,754,762,771,779,788,797,806,815,
824,834,844,854,864,874,885,896,907,918,929,941,953,965,977,989,1001,1014,1027,1040,1053,1067,
1080,1094,1108,1122,1136,1151,1166,1180,1195,1211,1226,1242,1257,1273,1289,1305,1322,1338,1355,
1372,1389,1406,1423,1441,1458,1476,1494,1512,1531,1549,1568,1586,1605,1624,1644,1663,1682,1702,
1722,1742,1762,1782,1802,1822,1843,1864,1885,1906,1927,1948,1969,1991,2012,2034,2056,2078,2100,
2122,2145,2167,2189,2212,2235,2258,2281,2304,2327,2350,2374,2397,2421,2445,2468,2492,2516,2540,
2564,2589,2613,2637,2662,2687,2711,2736,2761,2786,2811,2836,2861,2886,2911,2937,2962,2988,3013,
3039,3064,3090,3116,3142,3167,3193,3219,3245,3271,3297,3324,3350,3376,3402,3429,3455,3481,3508,
3534,3561,3587,3614,3640,3667,3693,3720,3747,3773,3800,3827,3853,3880,3907,3933,3960,3987,
};
//比较中断服务函数
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim1)
{
TIM1->CCR1 = spwmData[spwmCnt++];
if(spwmCnt >= SPWM_N)
{
spwmCnt = 0;
}
}
}
最后,在程序中开启PWM输出即可:
HAL_TIM_PWM_Start_IT(&htim1,TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start_IT(&htim1,TIM_CHANNEL_1);
四、验证
程序运行后用逻辑分析仪观察PWM,PWMN的波形如下,其占空比是以正弦规律变化的。
最后,将SPWM输出接入全桥逆变电路,经过滤波后,用示波器测量输出端波形,是比较完美的正弦波。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)