AVR單片機(jī)的TC0定時(shí)溢出例程
定時(shí)公式:Time=PRE*(MAX-TCNT0+1)/F_cpu單位S ,其中,PRE為與分頻數(shù)
本文引用地址:http://m.butianyuan.cn/article/201611/321461.htm#include
#include
void port_init(void)
{
PORTA = 0xFF;
DDRA
PORTB = 0xFF;
DDRB
PORTC = 0xFF; //m103 output only
DDRC
PORTD = 0xFF;
DDRD
}
//TIMER0 initialize - prescale:8/256
// WGM: Normal
// desired value: 1mSec/20ms
// actual value:
void timer0_init(void)
{
TCCR0 = 0x00; //stop
//TCNT0 = 0x83; //set count T=PRE*(MAX-TCNT0+1)/F_cpu=8*(255-130)/1MHz=1ms
TCNT0 = 0xB2; //set count T=PRE*(MAX-TCNT0+1)/F_cpu=256*(255-178+1)/1MHz=19.968ms
OCR0
//TCCR0 = 0x02; //start timer
TCCR0 = 0x04; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
//TCNT0 = 0x83; //reload counter value 重載TCNT0,使TC0重復(fù)從0x83-0xff計(jì)數(shù)
TCNT0 = 0xB2; //reload counter value 重載TCNT0,使TC0重復(fù)從0xB2-0xff計(jì)數(shù)
PORTB^=BIT(7)|BIT(6)|BIT(5);//翻轉(zhuǎn)PB6/PB7口,實(shí)現(xiàn)兩個(gè)LED燈的1ms間隔亮滅
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
}
評(píng)論