赞
踩
对于功率方向,输出SPWM波是必须要掌握的
工程:
stm32生成spwm代码Keil工程链接资源
引用spwm波定义:
PWM波形就是指占空比可变的波形;SPWM波形是指脉冲宽度按正弦规律变化且和正弦波等效的PWM波形。两者的区别在于PWM改变输出方波的占空比,来改变等效输出电压;SPWM调制脉冲方式,脉冲宽度时间占空比按正弦规律排列,这样输出波形经过适当的滤波可以做到正弦波输出。
1、cubeMX部分
pwm做载波,首先输出pwm波
启用一个定时器做定时器中断,目的是在一定时间,变化PWM占空比,模拟正弦波的幅度变化。
生成代码
2、keil代码编写
先取点,1000个模拟点
在软件中设置好,生成模拟点
int size=1000;
uint16_t sin_value[] = {500,503,506,509,512,515,518,521,525,528,531,534,537,540,543,547,
550,553,556,559,562,565,568,572,575,578,581,584,587,590,593,596,
599,602,606,609,612,615,618,621,624,627,630,633,636,639,642,645,
648,651,654,657,660,663,666,669,672,675,678,681,684,686,689,692,
695,698,701,704,707,710,712,715,718,721,724,726,729,732,735,738,
740,743,746,749,751,754,757,759,762,765,767,770,773,775,778,781,
783,786,788,791,793,796,798,801,803,806,808,811,813,816,818,821,
823,825,828,830,833,835,837,839,842,844,846,849,851,853,855,857,
860,862,864,866,868,870,872,875,877,879,881,883,885,887,889,891,
893,895,896,898,900,902,904,906,908,909,911,913,915,917,918,920,
922,923,925,927,928,930,931,933,935,936,938,939,941,942,944,945,
946,948,949,951,952,953,955,956,957,958,960,961,962,963,964,966,
967,968,969,970,971,972,973,974,975,976,977,978,979,980,981,981,
982,983,984,985,985,986,987,987,988,989,989,990,991,991,992,992,
993,993,994,994,995,995,996,996,996,997,997,997,998,998,998,998,
999,999,999,999,999,999,999,999,999,999,1000,999,999,999,999,999,
999,999,999,999,999,998,998,998,998,997,997,997,996,996,996,995,
995,994,994,993,993,992,992,991,991,990,989,989,988,987,987,986,
985,985,984,983,982,981,981,980,979,978,977,976,975,974,973,972,
971,970,969,968,967,966,964,963,962,961,960,958,957,956,955,953,
952,951,949,948,946,945,944,942,941,939,938,936,935,933,931,930,
928,927,925,923,922,920,918,917,915,913,911,909,908,906,904,902,
900,898,896,895,893,891,889,887,885,883,881,879,877,875,872,870,
868,866,864,862,860,857,855,853,851,849,846,844,842,839,837,835,
833,830,828,825,823,821,818,816,813,811,808,806,803,801,798,796,
793,791,788,786,783,781,778,775,773,770,767,765,762,759,757,754,
751,749,746,743,740,738,735,732,729,726,724,721,718,715,712,710,
707,704,701,698,695,692,689,686,684,681,678,675,672,669,666,663,
660,657,654,651,648,645,642,639,636,633,630,627,624,621,618,615,
612,609,606,602,599,596,593,590,587,584,581,578,575,572,568,565,
562,559,556,553,550,547,543,540,537,534,531,528,525,521,518,515,
512,509,506,503,500,496,493,490,487,484,481,478,474,471,468,465,
462,459,456,452,449,446,443,440,437,434,431,427,424,421,418,415,
412,409,406,403,400,397,393,390,387,384,381,378,375,372,369,366,
363,360,357,354,351,348,345,342,339,336,333,330,327,324,321,318,
315,313,310,307,304,301,298,295,292,289,287,284,281,278,275,273,
270,267,264,261,259,256,253,250,248,245,242,240,237,234,232,229,
226,224,221,218,216,213,211,208,206,203,201,198,196,193,191,188,
186,183,181,178,176,174,171,169,166,164,162,160,157,155,153,150,
148,146,144,142,139,137,135,133,131,129,127,124,122,120,118,116,
114,112,110,108,106,104,103,101,99,97,95,93,91,90,88,86,
84,82,81,79,77,76,74,72,71,69,68,66,64,63,61,60,
58,57,55,54,53,51,50,48,47,46,44,43,42,41,39,38,
37,36,35,33,32,31,30,29,28,27,26,25,24,23,22,21,
20,19,18,18,17,16,15,14,14,13,12,12,11,10,10,9,
8,8,7,7,6,6,5,5,4,4,3,3,3,2,2,2,
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,
3,3,3,4,4,5,5,6,6,7,7,8,8,9,10,10,
11,12,12,13,14,14,15,16,17,18,18,19,20,21,22,23,
24,25,26,27,28,29,30,31,32,33,35,36,37,38,39,41,
42,43,44,46,47,48,50,51,53,54,55,57,58,60,61,63,
64,66,68,69,71,72,74,76,77,79,81,82,84,86,88,90,
91,93,95,97,99,101,103,104,106,108,110,112,114,116,118,120,
122,124,127,129,131,133,135,137,139,142,144,146,148,150,153,155,
157,160,162,164,166,169,171,174,176,178,181,183,186,188,191,193,
196,198,201,203,206,208,211,213,216,218,221,224,226,229,232,234,
237,240,242,245,248,250,253,256,259,261,264,267,270,273,275,278,
281,284,287,289,292,295,298,301,304,307,310,313,315,318,321,324,
327,330,333,336,339,342,345,348,351,354,357,360,363,366,369,372,
375,378,381,384,387,390,393,397,400,403,406,409,412,415,418,421,
424,427,431,434,437,440,443,446,449,452,456,459,462,465,468,471,
474,478,481,484,487,490,493,496
};
开启pwm输出和定时器中断:
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_Base_Start_IT(&htim2);
调用定时器中断回调函数:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
static int i = 0;
if(++i == size)i = 0;
if (htim->Instance == htim2.Instance){
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, sin_value[i]); //由向量表修改占空比
}
}
最后即可输出SPWM,经过硬件滤波就能输出完美的正弦波
正弦波的频率计算:
定时器中断的为依准: 72MHz / psc(预分频值) / 计数值 / 取点数
我设置的是psc=0;计数值72-1;
所以72000000/72/1000=1KHz,即正弦波输出的频率为1KHz;
前面的载波PWM的频率为72000000/1000=72KHz;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。