ConfigTimer01.c

00001 /***************************************************************************** 00002 * * 00003 * ********** * 00004 * ************ * 00005 * *** *** * 00006 * *** +++ *** * 00007 * *** + + *** * 00008 * *** + CHIPCON CC1010 * 00009 * *** + + *** HAL - ConfigTimer01 * 00010 * *** +++ *** * 00011 * *** *** * 00012 * *********** * 00013 * ********* * 00014 * * 00015 ***************************************************************************** 00016 * * 00017 ***************************************************************************** 00018 * Author: ROH * 00019 ***************************************************************************** 00020 * Revision history: * 00021 * * 00022 * $Log: ConfigTimer01.c,v $ 00023 * Revision 1.1 2002/10/14 13:04:30 tos 00024 * Initial version in CVS. 00025 * 00026 * * 00027 ****************************************************************************/ 00028 00029 #include <chipcon/hal.h> 00030 #include <chipcon/partCStack.h> 00031 00032 //---------------------------------------------------------------------------- 00033 // ulong halConfigTimer01(...); 00034 // 00035 // Description: 00036 // This function configures timer 0 or 1 (depending on the value given in 00037 // _option_ as either an interrupt timer (an interrupt is generated at 00038 // certain intervals in time, as specified by _period) or an interrupt 00039 // pulse counter (an interrupt when _period_ number of pulses have 00040 // been detected on P3.4/P3.5 for timer0/timer1.) 00041 // Some timer settings (with long timeouts) require that the user 00042 // initializes timer register in the interrupt service routine (ISR.) This 00043 // should be done by using the appropriate version of the 00044 // ISR_TIMERx_ADJUST(m) macro with the word pointed to by _modulo_ as an 00045 // argument. The _modulo_ argument takes a pointer to a word, if it is 00046 // NULL, many timer settings will be unavailable. It is the responsibility 00047 // of the programmer to make sure that the appropriate timer ISR has been 00048 // declared (and that it begins with the obligatory ISR_TIMERx_ADJUST(m) 00049 // macro). The timer must be started with macro TIMERx_RUN(TRUE). 00050 // 00051 // Arguments: 00052 // byte options 00053 // Options indicating which timer to configure and how. Different 00054 // constants for _option_ is defined below. 00055 // ulong period 00056 // The desired period between interrupts in microseconds in timer 00057 // mode, or the number of counted pulses between interrupts in counter 00058 // mode. 00059 // Maximum period achivable is ca. 65535 us!!! 00060 // word clkFreq 00061 // The XOSC clock frequency in kHz. 00062 // word xdata* modulo 00063 // A pointer to a word (in xdata) which after the function has returned 00064 // contains the value to supply to the obligatory macro invocation of 00065 // ISR_TIMERx_ADJUST(m) at the start of the timer ISR. 00066 // 00067 // Return value: 00068 // ulong 00069 // In timer mode, the actual period in microseconds between interrupts 00070 // or zero if the period is impossible to achieve. 00071 // In counter mode, zero if the supplied count value is impossible to 00072 // achieve, otherwise one. 00073 //---------------------------------------------------------------------------- 00074 ulong halConfigTimer01(byte options, ulong period, word clkFreq, xdata word *modulo) { 00075 xdata byte ckcon, tmod, thx, tlx; 00076 00077 *modulo=0; 00078 if (options&TIMER01_INT_PULSE_COUNTER) { 00079 // Pulse counter 00080 if (!(period&0xFFFFFF00)) { // <256 00081 tmod=0x06; // Mode 2 (8-bit auto-reload) + clocked by T0/T1 00082 thx=tlx=0-(byte)period; 00083 } else { 00084 tmod=0x05; // Mode 1 (16-bit) + clocked by T0/T1 00085 *modulo=0-(word)period; 00086 thx=(*modulo)>>8; tlx=(*modulo)&0xFF; 00087 } 00088 00089 // Setup hardware registers 00090 if (options&TIMER1) { 00091 TMOD=(TMOD&0x0F)|(tmod<<4); 00092 TH1=thx; TL1=tlx; 00093 } else { 00094 TMOD=(TMOD&0xF0)|tmod; 00095 TH0=thx; TL0=tlx; 00096 } 00097 return 1; 00098 } else { 00099 // Timer 00100 period=period*(clkFreq/4000); 00101 00102 if (period&0xFFFF0000) { // >65535 00103 // Using CLK/12 00104 ckcon=0; 00105 if ((period/=3)&0xFFFF0000) // >65535 00106 return 0; 00107 } else { 00108 // Using CLK/4 00109 ckcon=1; 00110 } 00111 00112 // Choose mode 00113 if (!(period&0xFFFFFF00)) { // <256 00114 tmod=0x02; // Mode 2 (8-bit auto-reload) + clocked by CLK/4 or CLK/12 00115 thx=tlx=0-(byte)period; 00116 } else { 00117 tmod=0x01; // Mode 1 (16-bit) + clocked by CLK/4 or CLK/12 00118 *modulo=0-(word)period; 00119 thx=(*modulo)>>8; tlx=(*modulo)&0xFF; 00120 } 00121 00122 // Setup hardware registers 00123 if (options&TIMER1) { 00124 TMOD=(TMOD&0x0F)|(tmod<<4); 00125 TH1=thx; TL1=tlx; 00126 CKCON=(CKCON&~0x10) | ((ckcon&1)<<4); 00127 if (!(options&TIMER01_NO_INT_TIMER)) { 00128 TR1=0; 00129 TF1=0; 00130 ET1=1; 00131 EA=1; 00132 } 00133 } else { 00134 TMOD=(TMOD&0xF0)|tmod; 00135 TH0=thx; TL0=tlx; 00136 CKCON=(CKCON&~0x0F) | ((ckcon&1)<<3); 00137 if (!(options&TIMER01_NO_INT_TIMER)) { 00138 TR0=0; 00139 TF0=0; 00140 ET0=1; 00141 EA=1; 00142 } 00143 } 00144 00145 // Return actual period 00146 period*=(ckcon) ? 4000 : 12000; 00147 period/=clkFreq; 00148 00149 // >>> added by patrik spieß 00150 //startOffset= (TH0<<8) | TL0; 00151 //sendUlongToUart0(startOffset, 8); 00152 //UART0_WAIT_AND_SEND(32); 00153 // <<< added by patrik spieß 00154 00155 return period; 00156 } 00157 }

Generated on Fri Aug 27 10:04:02 2004 for Cheap Sensor Network by doxygen 1.3.8