avr定時器做的正弦波
RC電路的電阻為1K與10K時的波形分別如下
仿真圖片如下:
程序如下:
#include
#include
#define uchar unsigned char
uchar auc_SinParam[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}; // 128點正弦波樣本值
uchar x_SW = 8,X_LUT = 0;
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
X_LUT += x_SW; // 新樣點指針
if (X_LUT > 127) X_LUT -= 128;// 樣點指針調整
OCR0 = auc_SinParam[X_LUT];// 取樣點指針到比較匹配寄存器
}
void main(void)
{
CLI(); //關閉所有中斷
DDRB=0x08;// PB3輸出方式,作為OC0輸出PWM波
TCCR0=0x69;
OCR0=128;
TIMSK=0x01;// 允許T/C0溢出中斷
SEI();// 開放全局中斷
while(1)
{};
}
評論