avr定时器做的正弦波

时间:2022-09-02 15:01:13

2010-04-19 16:53:00

实物照片如下

avr定时器做的正弦波

RC电路的电阻为1K与10K时的波形分别如下

avr定时器做的正弦波

avr定时器做的正弦波

仿真图片如下:

avr定时器做的正弦波

程序如下:

#include <iom16v.h>
#include <macros.h>
#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)
    {};
}