#include <iom128v.h> #include <macros.h>
unsigned int pulse; //UART1 initialize // desired baud rate:9600 // actual baud rate:9615 (0.2%) // char size: 8 bit // parity: Disabled void uart1_init(void) { UCSR1B = 0x00; //disable while setting baud rate UCSR1A = 0x02; UCSR1C = 0x06; UBRR1L = 0x0c; //set baud rate lo UBRR1H = 0x00; //set baud rate hi UCSR1B = 0x18; }
void tranmsit(unsigned char data) { while(!(UCSR1A&(1<<UDRE1))); UDR1=data; } //TIMER0 initialize - prescale:1024 // WGM: Normal // desired value: 1Sec // actual value: 1.000Sec (0.0%) void timer0_init(void) { TCCR0 = 0x00; //stop ASSR = 0x08; //set async mode TCNT0 = 0xc0; //set count OCR0 = 0x20; TCCR0 = 0x07; //start timer } #pragma interrupt_handler int0_isr:2 void int0_isr(void) { pulse=pulse+1;//external interupt on INT0 }
#pragma interrupt_handler timer0_ovf_isr:17 void timer0_ovf_isr(void) { unsigned char a,b,c; unsigned int data; TCNT0 = 0xc0; //reload counter value data=TCNT1; a=data>>8; b=data; c=TCNT2; tranmsit(0xf2); tranmsit(0x04); tranmsit(a); tranmsit(b); a=pulse>>8; b=pulse; tranmsit(a); tranmsit(b); tranmsit(c); tranmsit(0xf2); TCNT1=0; pulse=0; TCNT2=0; }
//TIMER3 initialize - prescale:8 // WGM: 14) PWM fast, TOP=ICRn // desired value: 500Hz // actual value: 500.000Hz (0.0%) void timer3_init(void) { TCCR3B = 0x00; //stop TCNT3 = 0x00; OCR3A = 0x7c; OCR3B = 0x7c; OCR3C = 0x7c; ICR3 = 0xF9; TCCR3A = 0xF2; TCCR3B = 0x1A; //start Timer }
void main(void) { unsigned int i; CLI(); TCCR1B=0x07; TCCR2=0x07; EICRA = 0x02; //extended ext ints EIMSK = 0x01; DDRE=0x3a; timer0_init(); uart1_init(); timer3_init(); TIMSK = 0x01; //timer interrupt sources SEI(); for(;;); } 代码贴上来,大家帮忙测试测试,谢谢!
|