用M48内部时钟做程序时间为1秒但实际为1.6秒,请各位大哥指教(有具体代码)
//各位大哥,小弟有一问题向大家指教,用定时器做灯闪的程序,原本意图是//1秒钟闪一下,但最终结果是1.6秒闪一下(用秒表计时的,大概测一下)
//ICC-AVR application builder : 2009-4-16 14:56:33
// Target : M48
// Crystal: 1.0000Mhz
// 实验目的使PB6驱动的LED灯1秒钟闪一下//
#include <iom48v.h>
#include <macros.h>
unsigned char j;
unsigned char i;
void port_init(void)
{
PORTB = 0x40;
DDRB= 0x40;
PORTC = 0x00; //m103 output only
DDRC= 0x00;
PORTD = 0x00;
DDRD= 0x00;
}
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 10mSec
// actual value:9.984mSec (0.2%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x64; //set count
TCCR0A = 0x00;
TCCR0B = 0x03; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
j=1;
//TIMER0 has overflowed
}
//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;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
while(1)
{
if(j==1)
{
j=0;
i++;
if(i>=100)//计数1秒
{
i=0;
PORTB^=0x40;//端口反转一次
}
}
}
//insert your functional code here...
}
//实际做出来的动作大约是1.6秒闪一下,请各位大哥指教,熔丝里设置的是8M,8分频 #pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
if(++i>=100)//计数1秒
{
i=0;
PORTB^=0x40;//端口反转一次
}
TCNT0 = 0x64;//TIMER0 has overflowed
}
void main(void)
{
init_devices();
while(1); //insert your functional code here...
}
试试这样会不会好一点。 结果还是一样的 对了,忘了写“TCNT0 = 0x64;”了,请问大虾这个是怎么算出来的 代码是自动生成的,没想到会有错误。
页:
[1]