在avrstudio4.09里面进行调试:
这个是溢出中断程序
void timer0_ovf_isr(void) 经仔细检查,进入此行时cycle=2089
{
TCNT0 = 236; 此时cycle=2140
a++; 此时cycle=2142
if(a<4)
PORTA=a;
else
{delay(2);
PORTA=a;
a=0;}
}
51个时钟周期啊,cpu干什么去了??这样搞得时间很不准啊!!如果用来编写显示时间的程序,要出大问题了!要不就把那段时间也算进去,那位高手能说一下吗??
整个程序在这里
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
void port_init(void);
void timer0_init(void);
void timer0_ovf_isr(void);
void init_devices(void);
void delay (unsigned int dt);
unsigned char a;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x03;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:8
// WGM: Normal
// desired value: 1Hz
// actual value: Out of range
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 251; /*INVALID SETTING*/; //set count
OCR0 = 0x00 /*INVALID SETTING*/; //set compare
TCCR0 = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 236; /*INVALID SETTING*/; //reload counter value
a++;
if(a<4)
PORTA=a;
else
{delay(2);
PORTA=a;
a=0;}
}
//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 = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//精确延时程序(us) 延时(dt+2)/2 us
#pragma ctask delay
void delay (unsigned int dt)
{
while(dt)
dt--;
asm("nop");
}
void main(void)
{
init_devices();
timer0_init();
while(1)
{asm("nop");
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
高手来解答下.
如果用匹配比较中断是不是也要浪费51个周期啊?
一周热门 更多>