/** TIMER0.C ********************************************************** Timer0 dient als Zeitbasis Er läuft als TIMER im Modus 1. um den Timer zu konfigurieren, definiert man das Makro CLK_TCK. Test: (1) am PC CLK_TCK wird auf 18.2 gesetzt. Der Timer selbst wird durch den Timer des PC auf Adresse 40:6C simuliert. (2) am UC-Simulator wenn SIMUL definiert ist, wird der Timer beschleunigt *******************************************************************/ #ifdef LDEBUG #define CPU517a #include #define hide #else #include "bios.h" #define hide static #endif #include "timer0.h" #if defined (CPU517) || defined(CPU517a) #include "arith.h" #endif /* CLOCKMHZ ist die Taktfrequenz des Oszillators in MHZ Defaultwert: 12 MHz */ #ifndef CLOCKMHZ #define CLOCKMHZ 12 #endif #ifndef CLK_TCK #define CLK_TCK 100 #endif /* TIMERKORR korrigiert die Verlängerung der Timer-Periode durch PUSH-Operationen am Beginn der Interupt-Routine im Timer-Modus 1 */ #define TIMERKORR 19 #define TIMERLOAD (65536-(1000000*CLOCKMHZ)/(12*CLK_TCK) - TIMERKORR) /* Anzahl der Timerperioden pro Tag */ #define TIMER_1TAG (unsigned long)(60*60*24*CLK_TCK) /** LOCAL **********************************************************/ /* i_time ------------------------------------------------- zählt die Anzahl der Timer-Ticks seit der Initialisierung des UC Ein Tick ist je nach Konfiguration 1ms, 10ms, 100ms ----------------------------------------------------------*/ hide volatile unsigned long i_timer0_time=0; hide unsigned long i_timer0_timeout; #ifdef __C51__ void i_timer0 (void) interrupt 1 { TL0 = TIMERLOAD % 256; TH0 = TIMERLOAD / 256; i_timer0_time++; if (i_timer0_time==TIMER_1TAG) i_timer0_time=0; /* #ifndef LDEBUG if (i_timer0_time%ADCZYKLUS==0) adc_start(); #endif */ } /** PUBLIC **********************************************************/ void timer0_init (void) { /* Timer 0 zählt mit 1 ms Periodendauer */ TMOD &= 0xf0; TMOD |= 0x01; TL0 = TIMERLOAD % 256; TH0 = TIMERLOAD / 256; TR0=1; ET0=1; EAL=1; } #else #include #include /* #define CLK_TCK 18.2 */ #define CLOCKPTR_PC (unsigned long far *)MK_FP(0x40,0x6c) #endif /* /* __C51__ */ */ void timer0_set (unsigned long ticks) { #ifdef __C51__ ET0=0; TR0=0; i_timer0_time = ticks; timer0_init(); ET0=1; #else unsigned long far *fp = CLOCKPTR_PC; *fp = ticks; #endif } /** PUBLIC **********************************************************/ unsigned long timer0_get (void) { unsigned long ticks; #ifdef __C51__ ET0=0; ticks = i_timer0_time; ET0=1; return ticks; #else return *CLOCKPTR_PC; #endif } void timer0_timeout_set (unsigned int ticks) { i_timer0_timeout = timer0_get(); i_timer0_timeout += ticks; } void timer0_timeout_set_s (unsigned int s) { timer0_timeout_set((unsigned int)(s*CLK_TCK)); } unsigned char timer0_timeout (void) { return i_timer0_timeout < timer0_get(); }