利用PWM生成正弦波程序
扫描二维码
随时随地手机看文章
/*******************************************************************
实验名称:产生正弦波演示实验
器件:ATmega16L
晶振:外部 12M
接线方法:
1.PD5接低通滤波输入
2.用示波器测其输出波形
*******************************************************************/
#include
#include
#include
#pragma interrupt_handler timer1:9
char auc_SinParam[128]={//128点正弦波样本值
128,134,140,147,153,159,165,171,177,182,188,193,198,204,208,213,
218,222,226,230,233,237,240,242,245,247,249,251,252,253,254,254,
254,254,253,252,251,250,248,246,244,241,238,235,232,228,224,220,
215,211,206,201,196,191,185,179,174,168,162,156,150,144,137,131,
125,119,112,106,100,94,88,82,77,71,65,60,55,50,45,41,
36,32,28,24,21,18,15,12,10,8,6,5,4,3,2,2,
2,2,3,4,5,7,9,11,14,16,19,23,26,30,34,38,
43,48,52,57,63,68,74,79,85,91,97,103,109,116,122,128
};
char x=0;
//T/C1溢出中断服务
void timer1(void)
{
x=x+1;
if (x>128)
x=0;
OCR1A=auc_SinParam[x];//auc_SinParam[x];//取样点指针到比较匹配寄存器
}
void main(void)
{
DDRD=0xff;//PB3输出方式,作为OC0输出PWM波
TIMSK=0x04; //中断屏蔽位,没有开启任何中断
TIFR=0; //中断标志位
TCCR1A=0xA1; //开启PWM
TCCR1B=0x09;//系统时钟
SREG=0x80; //开启全局中断
OCR1A=128;//auc_SinParam[x];
}