#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0xff;
DDRA = 0xff;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:256
// WGM: CTC
// desired value: 10mSec
// actual value: 10.027mSec (-0.3%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
//TCNT0 = 0x16; //set count
OCR0 = 0xEA; //set compare
TCCR0 = 0x0C; //start timer
}
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
static unsigned char i,count1s;
count1s++;
if(count1s==100)
{
count1s=0;
i++;
}
switch(i)
{
case 0:
break;
case 1:
PORTA=0XFE;
break;
case 2:
PORTA=0XFF;
i=0;
break;
default:
i=0;
break;
}
//compare occured TCNT0=OCR0
}
//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 = 0x00;
TIMSK = 0x02; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main()
{
init_devices();
while(1);
}
此程序的意思是每个中断定时10MS,1S时间变化一次.
但实际结果,5,6S时间才变化一次,我想问为什么?