00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00038 #include "pic24_all.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 int32 roundFloatToUint32(float f_x) {
00051 uint32 u32_y;
00052
00053 u32_y = f_x;
00054 if ((f_x - u32_y) < 0.5) return u32_y;
00055 else return u32_y+1;
00056 }
00057
00058 int16 roundFloatToUint16(float f_x) {
00059 uint16 u16_y;
00060
00061 u16_y = f_x;
00062 if ((f_x - u16_y) < 0.5) return u16_y;
00063 else return u16_y+1;
00064 }
00065
00066
00075 uint16 msToU16Ticks(uint16 u16_ms, uint16 u16_pre) {
00076
00077 float f_ticks = FCY;
00078 uint16 u16_ticks;
00079 f_ticks = (f_ticks*u16_ms)/u16_pre/1000L;
00080 ASSERT(f_ticks < 65535.5);
00081 u16_ticks = roundFloatToUint16(f_ticks);
00082 return u16_ticks;
00083 }
00084
00093 uint16 usToU16Ticks(uint16 u16_us, uint16 u16_pre) {
00094
00095 float f_ticks = FCY;
00096 uint16 u16_ticks;
00097 f_ticks = (f_ticks*u16_us)/u16_pre/1000000L;
00098 ASSERT(f_ticks < 65535.5);
00099 u16_ticks = roundFloatToUint16(f_ticks);
00100 return u16_ticks;
00101
00102 }
00103
00112 uint32 usToU32Ticks(uint32 u32_us, uint16 u16_pre) {
00113
00114 float f_ticks = FCY;
00115 uint32 u32_ticks;
00116 f_ticks = (f_ticks*u32_us)/u16_pre/1000000L;
00117 u32_ticks = roundFloatToUint32(f_ticks);
00118 return u32_ticks;
00119 }
00120
00121
00122
00130 uint16 getTimerPrescaleBits(uint8 u8_TCKPS) {
00131 uint16 au16_prescaleValue[] = { 1, 8, 64, 256 };
00132 ASSERT(u8_TCKPS <= 3);
00133 return au16_prescaleValue[u8_TCKPS];
00134 }
00135
00141 uint32 ticksToMs (uint32 u32_ticks, uint16 u16_tmrPre) {
00142
00143 float f_ticks;
00144 uint32 u32_timeMs;
00145
00146 f_ticks = u32_ticks;
00147 f_ticks = ((f_ticks * u16_tmrPre)/FCY) * 1000;
00148 u32_timeMs = roundFloatToUint32(f_ticks);
00149 return (u32_timeMs);
00150 }
00151
00152
00158 uint32 ticksToUs (uint32 u32_ticks, uint16 u16_tmrPre) {
00159
00160 float f_ticks;
00161 uint32 u32_timeUs;
00162
00163 f_ticks = u32_ticks;
00164 f_ticks = ((f_ticks * u16_tmrPre)/FCY) * 1000000L;
00165 u32_timeUs = roundFloatToUint32(f_ticks);
00166 return (u32_timeUs);
00167 }
00168
00174 uint32 ticksToNs (uint32 u32_ticks, uint16 u16_tmrPre) {
00175
00176 float f_ticks;
00177 uint32 u32_timeNs;
00178
00179 f_ticks = u32_ticks;
00180 f_ticks = ((f_ticks * u16_tmrPre)/FCY) * 1000000000L;
00181 u32_timeNs = roundFloatToUint32(f_ticks);
00182 return (u32_timeNs);
00183 }
00184
00193 uint32 computeDeltaTicksLong(uint16 u16_start, uint16 u16_end, uint16 u16_tmrPR, uint16 u16_oflows) {
00194 uint32 u32_deltaTicks;
00195 if (u16_oflows == 0) u32_deltaTicks = u16_end - u16_start;
00196 else {
00197
00198 u32_deltaTicks = (u16_tmrPR + 1) - u16_start;
00199
00200 u32_deltaTicks += ((((uint32) u16_oflows)- 1) * (((uint32)u16_tmrPR) + 1)) ;
00201
00202 u32_deltaTicks += u16_end;
00203 }
00204 return (u32_deltaTicks);
00205 }
00206
00214 uint16 computeDeltaTicks(uint16 u16_start, uint16 u16_end, uint16 u16_tmrPR) {
00215 uint16 u16_deltaTicks;
00216 if (u16_end >= u16_start) u16_deltaTicks = u16_end - u16_start;
00217 else {
00218
00219 u16_deltaTicks = (u16_tmrPR + 1) - u16_start;
00220
00221 u16_deltaTicks += u16_end;
00222 }
00223 return (u16_deltaTicks);
00224 }