/*--------------------------------------------------*/
/* main.c */ /* FYQ,2007,01,03 */ /* 生成正弦波 */ /*--------------------------------------------------*/ /* */ /* AVR系统的处理器为: ATMega16 */ /* AVR系统的晶振频率: 7.3728Mhz */ /* */ /*--------------------------------------------------*/ /**************************************************** 程序修改为OC2输出,PD7脚(管脚21) 2007,10,31 ****************************************************/ #include "defines.h" #include <avr/io.h> #include <inttypes.h> #include <avr/interrupt.h> #include <stdlib.h> #include <stdio.h> #include <avr/pgmspace.h> #include <avr/sleep.h> #include "sin_table.h" #include "delay.h" #define DISABLE_TC2 (TCCR1B&=~_BV(CS22)) #define ENABLE_TC2 (TCCR1B|=_BV(CS22)) // -----------------全局变量----------------- uint8_t key_value; uint8_t x_sw=0x00; uint16_t x_lutext=0; uint16_t x_lut; // x_sw表(8倍):x_sw=ROUND(8*N_sample*f*510/FCK) void io_init(void) { DDRD=0xFF; PORTD=0xFF; DDRA=0xC0; // PA0~PA5为输入,PA6~PA7为输出 PORTA=0x00; // PORTA关上拉 } //定义按键输入 void key_init(void) { DDRA=0xFF; PORTA=0xFF;//_BV(PA0)|_BV(PA1)|_BV(PA2)|_BV(PA3); } void LedOnOff(void) { uint8_t i; for(i=0;i<3;i++) { PORTA^=_BV(PA7); delay_nms(500); } } void tc2_init() { TIMSK|=_BV(OCIE2); // 使能T/C2溢出中断 TCCR2|=_BV(CS20); // 分频:1 TCCR2|=_BV(COM21)|_BV(COM20); // 比较匹配时置位OC2计数值为0xFF时清零OC2 TCCR2|=_BV(WGM21)|_BV(WGM20); // 快速PWM模式 } SIGNAL(SIG_OUTPUT_COMPARE2) { x_lutext+=x_sw; x_lut=(uint8_t)(((x_lutext+4)>>3)&(0x007F)); // 数据规格化,0x007F=127 OCR2=pgm_read_byte(&sin_table[x_lut]); // 从flash中取出正弦数据 // 更新OCR2 } // 读键盘 uint8_t read_key(void) { uint8_t key_v; key_v=(PINA&0x3F); return key_v; } int main(void) { io_init(); tc2_init(); LedOnOff(); sei(); // 开总的中断允许 // set_sleep_mode(SLEEP_MODE_IDLE); while(1) { key_value=read_key(); switch(key_value){ case 0x00: {x_sw=0;DISABLE_TC2;};break; case 0x01: {x_sw=1;ENABLE_TC2;};break; case 0x02: {x_sw=2;ENABLE_TC2;};break; case 0x04: {x_sw=3;ENABLE_TC2;};break; case 0x08: {x_sw=4;ENABLE_TC2;};break; case 0x10: {x_sw=5;ENABLE_TC2;};break; case 0x20: {x_sw=6;ENABLE_TC2;};break; default: break; } // sleep_mode(); } return 0; }
|
|