源代码:
- #define F_CPU 9600000UL // 9.6 MHz
- #include <util/delay.h>
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- #define PWM_PRESCALE 110
- #define SAMPLE_RATE ((9600000/PWM_PRESCALE))
- #define DDS_ACCU_MAX 0x10000
- unsigned char DDS_value;
- unsigned int dds_diff;
- unsigned int cycle_counter;
- unsigned char phase;
- unsigned int dds_accu;
- unsigned char dds_count;
- unsigned char PORT_buf;
- const unsigned char sin_tbl [] PROGMEM = {
- 0x7f, 0x82, 0x85, 0x88, 0x8b, 0x8f, 0x92, 0x95,
- 0x98, 0x9b, 0x9e, 0xa1, 0xa4, 0xa7, 0xaa, 0xad,
- 0xb0, 0xb2, 0xb5, 0xb8, 0xbb, 0xbe, 0xc0, 0xc3,
- 0xc6, 0xc8, 0xcb, 0xcd, 0xd0, 0xd2, 0xd4, 0xd7,
- 0xd9, 0xdb, 0xdd, 0xdf, 0xe1, 0xe3, 0xe5, 0xe7,
- 0xe9, 0xea, 0xec, 0xee, 0xef, 0xf0, 0xf2, 0xf3,
- 0xf4, 0xf5, 0xf7, 0xf8, 0xf9, 0xf9, 0xfa, 0xfb,
- 0xfc, 0xfc, 0xfd, 0xfd, 0xfd, 0xfe, 0xfe, 0xfe,
- 0xfe, 0xfe, 0xfe, 0xfe, 0xfd, 0xfd, 0xfd, 0xfc,
- 0xfc, 0xfb, 0xfa, 0xf9, 0xf9, 0xf8, 0xf7, 0xf5,
- 0xf4, 0xf3, 0xf2, 0xf0, 0xef, 0xee, 0xec, 0xea,
- 0xe9, 0xe7, 0xe5, 0xe3, 0xe1, 0xdf, 0xdd, 0xdb,
- 0xd9, 0xd7, 0xd4, 0xd2, 0xd0, 0xcd, 0xcb, 0xc8,
- 0xc6, 0xc3, 0xc0, 0xbe, 0xbb, 0xb8, 0xb5, 0xb2,
- 0xb0, 0xad, 0xaa, 0xa7, 0xa4, 0xa1, 0x9e, 0x9b,
- 0x98, 0x95, 0x92, 0x8f, 0x8b, 0x88, 0x85, 0x82,
- 0x7f, 0x7c, 0x79, 0x76, 0x73, 0x6f, 0x6c, 0x69,
- 0x66, 0x63, 0x60, 0x5d, 0x5a, 0x57, 0x54, 0x51,
- 0x4e, 0x4c, 0x49, 0x46, 0x43, 0x40, 0x3e, 0x3b,
- 0x38, 0x36, 0x33, 0x31, 0x2e, 0x2c, 0x2a, 0x27,
- 0x25, 0x23, 0x21, 0x1f, 0x1d, 0x1b, 0x19, 0x17,
- 0x15, 0x14, 0x12, 0x10, 0x0f, 0x0e, 0x0c, 0x0b,
- 0x0a, 0x09, 0x07, 0x06, 0x05, 0x05, 0x04, 0x03,
- 0x02, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02,
- 0x02, 0x03, 0x04, 0x05, 0x05, 0x06, 0x07, 0x09,
- 0x0a, 0x0b, 0x0c, 0x0e, 0x0f, 0x10, 0x12, 0x14,
- 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23,
- 0x25, 0x27, 0x2a, 0x2c, 0x2e, 0x31, 0x33, 0x36,
- 0x38, 0x3b, 0x3e, 0x40, 0x43, 0x46, 0x49, 0x4c,
- 0x4e, 0x51, 0x54, 0x57, 0x5a, 0x5d, 0x60, 0x63,
- 0x66, 0x69, 0x6c, 0x6f, 0x73, 0x76, 0x79, 0x7c
- };
- unsigned char digital_signal_input=192;
- int accumulator,accumulator1,accumulator2;
- unsigned char ddc;
- unsigned char portb_buff;
- ISR(TIM0_COMPA_vect){
- PORTB=portb_buff;
- // 1order
- if (accumulator>=0x80){
- portb_buff|=(1<<PB2);
- ddc=0xff;
- }else{
- portb_buff&=~(1<<PB2);
- ddc=0x00;
- }
- accumulator=digital_signal_input-ddc+accumulator;
- phase = dds_accu>> 8; /* phase = DDS accumlator upper byte */
- digital_signal_input = pgm_read_byte (& sin_tbl [phase]); /* output value get from sin table */
- dds_accu += dds_diff;
- }
- int main(void){
- DDRB|=(1<<PB2);
-
- TCCR0A=(0b10<<WGM00); //CTC mode
- TCCR0B=(0b001<<CS00); //div by 1
- OCR0A=(PWM_PRESCALE-1); //
- TIMSK0=(1<<OCIE0A);
- dds_diff = DDS_ACCU_MAX * 1000 / SAMPLE_RATE; // set phase step in each sample
- sei();
- portb_buff=PORTB;
- while(1){
- };
- }
复制代码
|