刚学AVR,关于T0定时器做延时用,注意红色标记的地方。
//ICC-AVR application builder : 2010-1-4 10:37:07
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x01;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void Delay_ms(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x83; //set count
OCR0 = 0x7D; //set compare
TCCR0 = 0x03; //start timer
while ((TIFR & 0x01) == 0); // 为什么TIFR的D0位不会溢出????
TIFR &= ~ (1<<TOV0);
TCCR0 = 0x00; //stop
}
void Delay_Xms(unsigned int i)
{
unsigned int x;
for (x=0; x<i; x++)
{
Delay_ms();
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
while (1)
{
PORTA &= ~(1<<0);
Delay_Xms(1000);
PORTA |= (1<<0);
}
} |