要输出50Hz正弦波这个数组需占用(1/50Hz)*31.25K=625
那么可以获得SPWM每个递增占空比为sin(0:2*pi/625:2*pi)*127+128
程序示例:
#include
#include
unsigned char SPWM_D[625];
unsigned int k;
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
OCR1A=SPWM_D[k];
if(k<624)k++;
else k=0;
}
上面程指SPWM数组及在中断控制SPWM输出
SPWM数组计算:
unsigned int i;
float j;
for(i=0;i<625;i++)
{
j=i*2*PI/625;
SPWM_D[i]=sin(j)*127+128;
};
输出波形:
500) {this.resized=true; this.width=500; this.alt='这是一张缩略图,点击可放大。\n按住CTRL,滚动鼠标滚轮可自由缩放';this.style.cursor='hand'}" onclick="if(!this.resized) {return true;} else {window.open('http://u.dianyuan.com/bbs/u/59/2045691194495961.jpg?x-oss-process=image/watermark,g_center,image_YXJ0aWNsZS9wdWJsaWMvd2F0ZXJtYXJrLnBuZz94LW9zcy1wcm9jZXNzPWltYWdlL3Jlc2l6ZSxQXzQwCg,t_20');}" onmousewheel="return imgzoom(this);">
500) {this.resized=true; this.width=500; this.alt='这是一张缩略图,点击可放大。\n按住CTRL,滚动鼠标滚轮可自由缩放';this.style.cursor='hand'}" onclick="if(!this.resized) {return true;} else {window.open('http://u.dianyuan.com/bbs/u/59/2045691194496242.jpg?x-oss-process=image/watermark,g_center,image_YXJ0aWNsZS9wdWJsaWMvd2F0ZXJtYXJrLnBuZz94LW9zcy1wcm9jZXNzPWltYWdlL3Jlc2l6ZSxQXzQwCg,t_20');}" onmousewheel="return imgzoom(this);">
500) {this.resized=true; this.width=500; this.alt='这是一张缩略图,点击可放大。\n按住CTRL,滚动鼠标滚轮可自由缩放';this.style.cursor='hand'}" onclick="if(!this.resized) {return true;} else {window.open('http://u.dianyuan.com/bbs/u/59/2045691194496251.jpg?x-oss-process=image/watermark,g_center,image_YXJ0aWNsZS9wdWJsaWMvd2F0ZXJtYXJrLnBuZz94LW9zcy1wcm9jZXNzPWltYWdlL3Jlc2l6ZSxQXzQwCg,t_20');}" onmousewheel="return imgzoom(this);">
在上图中你看到很漂亮的正弦波形了吗?