ConfigTimer23.c

00001 /***************************************************************************** 00002 * * 00003 * ********** * 00004 * ************ * 00005 * *** *** * 00006 * *** +++ *** * 00007 * *** + + *** * 00008 * *** + CHIPCON CC1010 * 00009 * *** + + *** HAL - ConfigTimer23 * 00010 * *** +++ *** * 00011 * *** *** * 00012 * *********** * 00013 * ********* * 00014 * * 00015 ***************************************************************************** 00016 * * 00017 ***************************************************************************** 00018 * Author: ROH * 00019 ***************************************************************************** 00020 * Revision history: * 00021 * * 00022 * $Log: ConfigTimer23.c,v $ 00023 * Revision 1.4 2004/01/08 18:57:04 tos 00024 * Corrected limit algorithm for Timer23 function. 00025 * 00026 * Revision 1.3 2003/08/11 15:02:38 tos 00027 * Corrected reentrant usage in library: reentrant only if using large memory model. 00028 * 00029 * Revision 1.2 2003/07/29 11:25:32 tos 00030 * Made halWait() and halConfigTimer23() reentrant. 00031 * 00032 * Revision 1.1 2002/10/14 13:04:30 tos 00033 * Initial version in CVS. 00034 * 00035 * * 00036 ****************************************************************************/ 00037 00038 #include <chipcon/hal.h> 00039 00040 00041 //---------------------------------------------------------------------------- 00042 // ulong halConfigTimer23(...); 00043 // 00044 // Description: 00045 // This function configures timer 2 or 3 (depending on the value given in 00046 // _option_ as either an interrupt timer (an interrupt is generated at 00047 // certain intervals in time, as specified by _period) or a pulse width 00048 // modulator (PWM). 00049 // If _period_ is specified as 0, then, in timer mode the timeout peiod will 00050 // be set to the maximum possible, and in PWM mode the period will be 00051 // set as long as possible. Using the PWM mode of timer 2/3 overrides the 00052 // normal operation of ports P3.4/P3.5 and can thus not be used in conjunction 00053 // with timer 0/1 configured as counters. The duty cycle is set to 50% (128/255) 00054 // initially in PWM mode. 00055 // The timer/PWM must be started with macro TIMERx_RUN(TRUE). 00056 // 00057 // Arguments: 00058 // byte options 00059 // Options indicating which timer to configure and how. Different 00060 // constants for _option_ is defined below. 00061 // ulong period 00062 // The desired period between interrupts in microseconds. In PWM mode 00063 // the duty cycle will be set as close to 50% as possible. This duty 00064 // cycle can be changed (in an ISR or at any other time) by using the 00065 // appropriate PWMx_SET_DUTY_CYCLE(...) macro. If _period_ 00066 // is 0, then the maximum period possible will be set. The period can 00067 // also be adjusted dynamically with the PWMx_SET_PERIOD(...) macro. 00068 // word clkFreq 00069 // The XOSC clock frequency in kHz. 00070 // 00071 // Return value: 00072 // ulong 00073 // The actual period in microseconds or zero if the desired period is 00074 // too high. 00075 //---------------------------------------------------------------------------- 00076 #if (__MODEL__ == 0) // Memory model: 0 = SMALL, 1 = COMPACT, 2 = LARGE 00077 ulong halConfigTimer23(byte options, ulong period, word clkFreq) { 00078 #else 00079 ulong halConfigTimer23(byte options, ulong period, word clkFreq) reentrant { 00080 #endif 00081 00082 byte tx, txpre, mode; 00083 00084 // Calculate achievable period: 00085 if (period >= 0x10000) { 00086 period = (((period>>4)*(clkFreq>>4))+(127500>>8))/(255000>>8); 00087 } else { 00088 period = ((period*clkFreq)+127500)/255000; 00089 } 00090 00091 if (period) 00092 period--; 00093 00094 mode=0; 00095 00096 if (options&TIMER23_PWM) { 00097 // PWM mode selected 00098 if (period&0xFFFFFF00) // >255 00099 return 0; 00100 txpre=(byte)period; 00101 tx=128; 00102 mode=1; 00103 } else { 00104 // Timer mode selected 00105 if (period&0xFFFF0000) // >63535 00106 return 0; 00107 txpre=(byte)((word)period>>8); 00108 tx=(byte)period; 00109 } 00110 00111 if (options&TIMER3) { 00112 // Timer 3 selected 00113 if (options&TIMER23_INT_TIMER) { 00114 EXIF&=~0x80; // Clear interrupt flag 00115 ET3=1; // Enable timer interrupt 00116 EA=1; // Enable global interrupt 00117 } 00118 T3PRE=txpre; 00119 T3=tx; 00120 TCON2=(TCON2&~0x0C)|(mode<<2); 00121 } else { 00122 // Timer 2 selected 00123 if (options&TIMER23_INT_TIMER) { 00124 EXIF&=~0x20; // Clear interrupt flag 00125 ET2=1; // Enable timer interrupt 00126 EA=1; // Enable global interrupt 00127 } 00128 T2PRE=txpre; 00129 T2=tx; 00130 TCON2=(TCON2&~0x03)|mode; 00131 } 00132 00133 period=(period+1)*255000/clkFreq; 00134 00135 return period; 00136 } 00137 00142 ulong halCalcTimerPeriod(ulong period, word clkFreq, xdata TIMER_DATA *periodData) { 00143 00144 // Calculate achievable period: 00145 if (period >= 0x10000) { 00146 period = (((period>>4)*(clkFreq>>4))+(127500>>8))/(255000>>8); 00147 } else { 00148 period = ((period*clkFreq)+127500)/255000; 00149 } 00150 00151 if (period) { 00152 period--; 00153 } 00154 00155 periodData->txpre= (byte)((word)period>>8); 00156 periodData->tx= (byte)period; 00157 00158 period=(period+1)*255000/clkFreq; 00159 return period; 00160 } 00161 00162 #if (__MODEL__ == 0) // Memory model: 0 = SMALL, 1 = COMPACT, 2 = LARGE 00163 void halConfigTimerFast23(byte options, xdata TIMER_DATA *periodData) { 00164 #else 00165 void halConfigTimerFast23(byte options, xdata TIMER_DATA *periodData) reentrant { 00166 #endif 00167 00168 if (options&TIMER23_PWM) { 00169 return; 00170 } 00171 00172 if (options&TIMER3) { 00173 // Timer 3 selected 00174 if (options&TIMER23_INT_TIMER) { 00175 EXIF&=~0x80; // Clear interrupt flag 00176 ET3=1; // Enable timer interrupt 00177 EA=1; // Enable global interrupt 00178 } 00179 T3PRE= periodData->txpre; 00180 T3= periodData->tx; 00181 TCON2=(TCON2&~0x0C); 00182 } else { 00183 // Timer 2 selected 00184 if (options&TIMER23_INT_TIMER) { 00185 EXIF&=~0x20; // Clear interrupt flag 00186 ET2=1; // Enable timer interrupt 00187 EA=1; // Enable global interrupt 00188 } 00189 T2PRE= periodData->txpre; 00190 T2= periodData->tx; 00191 TCON2=(TCON2&~0x03); 00192 } 00193 }

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