- /******************************************************
- //文件名称:利用T/C0的PWM功能产生正弦波
- //作 者:翟志刚
- //版 本:
- //日 期:2014.7.31
- //说 明:
- //修改记录:
- *******************************************************/
- #include <iom16v.h>
- #include <macros.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar auc_SinParam[] = {
- 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_LUT = 0;
- // T/C0 溢出中断服务
- #pragma interrupt_handler timer0_ovf:10
- void timer0_ovf(void)
- {
- OCR0 = auc_SinParam[X_LUT++];
- if(X_LUT > 128)
- X_LUT = 0;
- }
- void main(void)
- {
- DDRB=0x08; // PB3输出方式,作为OC0输出PWM波
- TCCR0=0x69; //快速PWM初始化
- OCR0=128;
- TIMSK=0x01; // 允许T/C0溢出中断
- SREG=0x80; // 开放全局中断
- while (1)
- {
- };
- }
复制代码
|