// **************************************************************
// *** 实验: Timer/Counter ***
// *** 2007年10月13日 ***
// *** 目标MCU:MEGA168v 晶振:内部 8MHZ ***
// *** fuse_config:CLKDIV8 disabled ***
// **************************************************************
//set_bit:PORTD |=(1<<PORTD3);
//clr_bit:PORTD &=~(1<<PORTD3);
#include<iom168v.h>
#include<macros.h>
#include<myfun.h>
void PORT_Init(void)
{
DDRD = 0xFF;
}
//*************************************************//
//******************* TC **********************//
//*************************************************//
unsigned int counter=0;
unsigned int sec=0;
//prescale:8;WGM: Normal;
//desired value: 5KHz
//actual value: 5.009KHz (0.2%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x10; //set count
TCCR0A = 0x00;
TIMSK0 = 0x01; //enable timer overflow interrupt
TCCR0B = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 0x10; //set count
counter++;
if(counter == 4000)
sec++;
}
//*************************************************//
//****************** TC_over **********************//
//*************************************************//
void main(void)
{
CLI();
PORT_Init();
timer0_init();
SEI();
while(1) ;
}