AVR_定时器使用范例.doc
时间:07-29 13:12
查看:1346次
下载:162次
简介:
本文简单的介绍了ATMEGA16的定时器的操作方法。
//ICC-AVR application builder : 2007-6-19 下午 04:14:03 // Target : M16 // Crystal: 11.059Mhz
#include <iom16v.h> #include <macros.h> #include "stdio.h" unsigned int Freq = 0;
//TIMER1 initialize - prescale:1024 void timer1_init(void) { TCCR1B = 0x00; //stop TCNT1H = 0xD5; //setup TCNT1L = 0xD1; ICR1H = 0x2A; ICR1L = 0x2F; TCCR1A = 0x00; TCCR1B = 0xC5; //start Timer }
#pragma interrupt_handler timer1_capt_isr:6 void timer1_capt_isr(void) { Freq++; }
#pragma interrupt_handler timer1_ovf_isr:9 void timer1_ovf_isr(void) { TCNT1H = 0xD5; TCNT1L = 0xD1; printf("%d",Freq); Freq = 0; while(!(UCSRA&0x20)); UDR = 0x0d; }
// desired baud rate: 9600 void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x06; UBRRL = 0x47; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0x18; }
void init_devices(void) { CLI(); //disable all interrupts timer1_init(); uart0_init();
MCUCR = 0x00; GICR = 0x00; TIMSK = 0x24; //timer interrupt sources SEI(); //re-enable interrupts }
void main(void) { init_devices(); while(1); }
|