前言
PWM合成正弦波,原理什么的不详细说了,概括一下就是 PWM有效面积的积分 = 正弦波的有效面积。PWM的频率越快,细分的越多,锯齿也就越不明显。
做法是:首先利用正弦波取点软件,取点1000个,生成一个正弦波的数组。
PWM波的频率(F_PWM)与正弦波频率(F_SIN)之间的对应关系与采样点数(S_NUM)有着密切的关系,即: F_SIN=F_PWM/S_NUM
S_NUM 在这里为1000,因为取了1000个点
先用TIM1高级定时器来生成一个PWM波作为载波,我用的是72M主频,分频系数0,TIM_Period填1000(这个1000就是PWM的总周期,要大于等于正弦波数组的满值)
再用TIM2来生成一段与正弦波能量等效的PWM载波 ,TIM2配置的是 分频系数0,计数值1440,得到TIM2的频率:72M/分频1(即分频器实际的分频为 分频系数+1)/1440 = 50000Hz ,即20us进入一次中断。
根据公式可以计算出F_SIN=F_PWM/S_NUM=50000Hz/1000=50Hz
先上代码:
#include "stm32f10x.h"
#include "bsp_rcc.h"
#include "bsp_tim.h"
#include "bsp_AdvanceTim.h"
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
};
int main(void)
{
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
rcc_systempclock_init( RCC_PLLMul_9 ); //9倍频,时钟总线为72Mhz
AdvanceTim_GPIO_Config(); //初始化高级定时器出PWM波的GPIO
AdvanceTim_Mode_Config(); //初始化高级定时器,用来产生载波
TIM2_NVIC_Config(); //TIM2的中断优先级
timer2_init(); //初始化TIM2用来改变载波
while(1)
{
}
}
bsp_AdvanceTim.c的内容如下:
#include "bsp_AdvanceTim.h"
void AdvanceTim_GPIO_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
void AdvanceTim_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //配置时基结构体,声明一个结构体变量方便传参
TIM_OCInitTypeDef TIM_OCInitStructure; //配置输出比较结构体,声明一个结构体变量方便传参
//TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //配置有关刹车和死区结构体,声明一个结构体变量方便传参
//=====================时基初始化======================//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //开TIMER1外设时钟
TIM_TimeBaseStructure.TIM_Prescaler = 0;
// 计数器计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 1000;
// 时钟分频因子 - 一分频,配置死区时间需要用到
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4;
// 重复寄存器的值,没有用到,不管
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
//====================================================//
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM1通道1输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; //互补通道使能
TIM_OCInitStructure.TIM_Pulse = 0; //占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; //互补通道也是高电平有效
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空闲状态 低电平
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //互补通道空闲状态 低电平
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //初始化TIM1通道1输出PWM
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能TIM1 输出比较1的预装载使能 想要改变占空比 得先输出完当前周期的波形之后 到下个波形才按照新的占空比(更新事件发生后才改变占空比)
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
bsp_tim.c的内容如下:
#include "bsp_tim.h"
void TIM2_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void timer2_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
// 计数器计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// 自动重装载寄存器的值 填入10000 实际上会计数10000+1次 因为10000需要-- 10001次才能发生下溢
TIM_TimeBaseStructure.TIM_Period = 1440-1;
// 时钟分频因子,配置死区时间需要用到
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
// 重复寄存器的值,没有用到,不管
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
// 清楚TIM2上溢中断标志位
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
// 使能TIM2上溢中断
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
// 开启TIM2
TIM_Cmd(TIM2,ENABLE);
}
void timer2_it_init(void)
{
TIM2_NVIC_Config();
timer2_init();
}
这个代码输出的效果就是,PA8引脚输出PWM波形,但是这个波形还需要通过一个低通滤波器之后,才能看到正弦波的波形。
电路如下:
在箭头处挂示波器,可以观察到正弦波的波形。
代码的原理就类似红外遥控那种,载波频率如果很高,那么合成出来的正弦波的锯齿就越不明显。同时,TIM2的频率也十分重要,他决定了正弦波的频率。
举例来说:
比如我现在要生成50Hz的正弦波,那么我取了1000个点,也就意味着,在50Hz即20ms这个总周期内,我需要跑完数组的1000个点,那么20ms/1000 = 20us ,所以TIM2的中断时间就必须是20us。
每隔20us,TIM2进入中断服务函数,查表之后,把TIM1生成的PWM的占空比更新一次,在没到20us这段期间,PWM还是按照当前的占空比不断输出,所以TIM1的频率越高,每20us中,含有的方波数就越多,精度也就越高,锯齿就会越细。当到达20us之后,更新占空比,PWM又按照新的占空比去输出波形。当数组1000个内容输出完之后,一个完整的正弦波就出来了,但是要注意的是这个正弦波没有负半周(以GND为参考点的话),因为我们的MCU输出不了负电压。
这个正弦波完整输出一次的时间就是20us(占空比改变一次)*1000(个点) = 20000us = 20ms 即50Hz。
我个人理解的基本原理就是这样,如果有误,欢迎指出。