www.pudn.com > study_CIP51.rar > timer.c


#include 
 
#include "tdp.h" 
 
#define TIMER0_COUNT	0xDC11  	/*10000h - ((11059200Hz/(12*FREQ)) - 17 )*/ 
 
static xdata unsigned timer0_tick; 
 
/*Timer T0 Interrupt service func*/ 
static void timer0_isr(void) interrupt 1 using 1 { 
	P1 |= 0x80; 
	TR0 = 0;								/* Stop T0 */ 
	TL0 = TL0 + (TIMER0_COUNT & 0x00FF); 
	TH0 = TH0 + (TIMER0_COUNT >> 8); 
	TR0 = 1;								/* start T0 */ 
	timer0_tick++; 
	P1 &= ~0x80; 
} 
 
/* Timer T0 Initialization*/ 
void timer0_initialize(void) { 
	INT_DISABLE;							/* close interrupt */ 
	timer0_tick = 0; 
	TR0 = 0;								/* Stop T0 */ 
	TMOD &= ~0x0F;							/* set T0 Working Type */ 
	TMOD |= 0x01; 
	TL0 = (TIMER0_COUNT & 0x00FF); 
	TH0 = (TIMER0_COUNT >> 8 ); 
	PT0 = 0; 
	ET0 = 1; 
	TR0 = 1; 
	TR0 = 1;								/* start T0 */ 
	INT_ENABLE;								/* open Int */ 
} 
 
/* Timer T0 tick func. */ 
unsigned timer0_count(void) { 
	xdata unsigned t; 
	INT_DISABLE; 
	t = timer0_tick; 
	INT_ENABLE; 
	return (t); 
} 
 
/* TIMER T0 elapse COunter */ 
unsigned timer0_elapsed_count (unsigned count) { 
	return (timer0_count() - count); 
} 
 
/* Timer T0 Waiting */ 
void timer0_wait(unsigned count) { 
	xdata unsigned start_count; 
	start_count = timer0_count(); 
	while (timer0_elapsed_count(start_count) <= count){} 
}