pic24_timer.c

Go to the documentation of this file.
00001 /*
00002  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00003  * All rights reserved.
00004  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00005  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00006  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00007  *
00008  * Permission to use, copy, modify, and distribute this software and its
00009  * documentation for any purpose, without fee, and without written agreement is
00010  * hereby granted, provided that the above copyright notice, the following
00011  * two paragraphs and the authors appear in all copies of this software.
00012  *
00013  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00014  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00015  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00016  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00021  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00022  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00023  *
00024  * Please maintain this header in its entirety when copying/modifying
00025  * these files.
00026  *
00027  *
00028  */
00029 
00030 
00031 
00032 // Documentation for this file. If the \file tag isn't present,
00033 // this file won't be documented.
00038 #include "pic24_all.h"
00039 
00040 /*********************************
00041  * Function private to this file *
00042  *********************************/
00043 
00044 
00053 uint16 msToU16Ticks(uint16 u16_ms, uint16 u16_pre) {
00054 // Use a float internally for precision purposes to accomodate wide range of FCY, u16_pre
00055   float f_ticks = FCY;
00056   uint16 u16_ticks;
00057   f_ticks = (f_ticks*u16_ms)/u16_pre/1000L;
00058   ASSERT(f_ticks < 65535.5);
00059   u16_ticks = roundFloatToUint16(f_ticks);  //back to integer
00060   return u16_ticks;
00061 }
00062 
00071 uint16 usToU16Ticks(uint16 u16_us, uint16 u16_pre) {
00072   // Use a float internally for precision purposes to accomodate wide range of FCY, u16_pre
00073   float f_ticks = FCY;
00074   uint16 u16_ticks;
00075   f_ticks = (f_ticks*u16_us)/u16_pre/1000000L;
00076   ASSERT(f_ticks < 65535.5);
00077   u16_ticks = roundFloatToUint16(f_ticks);  //back to integer
00078   return u16_ticks;
00079 
00080 }
00081 
00090 uint32 usToU32Ticks(uint32 u32_us, uint16 u16_pre) {
00091 // Use a float internally for precision purposes to accomodate wide range of FCY, u16_pre
00092   float f_ticks = FCY;
00093   uint32 u32_ticks;
00094   f_ticks = (f_ticks*u32_us)/u16_pre/1000000L;
00095   u32_ticks = roundFloatToUint32(f_ticks);  //back to integer
00096   return u32_ticks;
00097 }
00098 
00099 
00100 
00108 uint16 getTimerPrescaleBits(uint8 u8_TCKPS) {
00109   uint16 au16_prescaleValue[] = { 1, 8, 64, 256 };
00110   ASSERT(u8_TCKPS <= 3);
00111   return au16_prescaleValue[u8_TCKPS];
00112 }
00113 
00119 uint32 ticksToMs (uint32 u32_ticks, uint16 u16_tmrPre) {
00120   //because of the wide range of the numbers, use a float for precision
00121   float f_ticks;
00122   uint32 u32_timeMs;
00123 
00124   f_ticks = u32_ticks;   //convert to float
00125   f_ticks = ((f_ticks * u16_tmrPre)/FCY) * 1000;
00126   u32_timeMs = roundFloatToUint32(f_ticks);  //back to int32
00127   return (u32_timeMs);
00128 }
00129 
00130 
00136 uint32 ticksToUs (uint32 u32_ticks, uint16 u16_tmrPre) {
00137   //because of the wide range of the numbers, use a float for precision
00138   float f_ticks;
00139   uint32 u32_timeUs;
00140 
00141   f_ticks = u32_ticks;   //convert to float
00142   f_ticks = ((f_ticks * u16_tmrPre)/FCY) * 1000000L;
00143   u32_timeUs = roundFloatToUint32(f_ticks);  //back to int32
00144   return (u32_timeUs);
00145 }
00146 
00152 uint32 ticksToNs (uint32 u32_ticks, uint16 u16_tmrPre) {
00153   //because of the wide range of the numbers, use a float for precision
00154   float f_ticks;
00155   uint32 u32_timeNs;
00156 
00157   f_ticks = u32_ticks;   //convert to float
00158   f_ticks = ((f_ticks * u16_tmrPre)/FCY) * 1000000000L;
00159   u32_timeNs = roundFloatToUint32(f_ticks);  //back to int32
00160   return (u32_timeNs);
00161 }
00162 
00171 uint32 computeDeltaTicksLong(uint16 u16_start, uint16 u16_end, uint16 u16_tmrPR, uint16 u16_oflows) {
00172   uint32 u32_deltaTicks;
00173   if (u16_oflows == 0) u32_deltaTicks = u16_end - u16_start;
00174   else {
00175     //compute ticks from start to timer overflow
00176     u32_deltaTicks = (u16_tmrPR + 1) - u16_start;
00177     //add ticks due to overflows =  (overflows -1) * ticks_per_overflow
00178     u32_deltaTicks += ((((uint32) u16_oflows)- 1) * (((uint32)u16_tmrPR) + 1)) ;
00179     //now add in the delta due to the last capture
00180     u32_deltaTicks += u16_end;
00181   }
00182   return (u32_deltaTicks);
00183 }
00184 
00192 uint16 computeDeltaTicks(uint16 u16_start, uint16 u16_end, uint16 u16_tmrPR) {
00193   uint16 u16_deltaTicks;
00194   if (u16_end >= u16_start) u16_deltaTicks = u16_end - u16_start;
00195   else {
00196     //compute ticks from start to timer overflow
00197     u16_deltaTicks = (u16_tmrPR + 1) - u16_start;
00198     //now add in the delta from overflow to u16_end
00199     u16_deltaTicks += u16_end;
00200   }
00201   return (u16_deltaTicks);
00202 }

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9