Rev 46 | Rev 48 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 46 | Rev 47 | ||
---|---|---|---|
Line 51... | Line 51... | ||
51 | 51 | ||
52 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
52 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
53 | // freq = 5000/60 * 2 = 166Hz. |
53 | // freq = 5000/60 * 2 = 166Hz. |
54 | // the TIM2 counter counts in 10uS increments, |
54 | // the TIM2 counter counts in 10uS increments, |
55 | // Need to accumulate low level for a 400th of a second before accepting it as a pulse |
55 | // Need to accumulate low level for a 400th of a second before accepting it as a pulse |
56 | #define ACCUM_MAX (RPM_COUNT_RATE / 500) |
56 | #define ACCUM_MAX (RPM_COUNT_RATE / 400) |
- | 57 | ||
- | 58 | #define GLITCH_MAX (RPM_COUNT_RATE / 2000) |
|
57 | 59 | ||
58 | #define RPM_AVERAGE 4 |
60 | #define RPM_AVERAGE 4 |
59 | 61 | ||
60 | // wait for about 1 second to decide whether or not starter is on |
62 | // wait for about 1 second to decide whether or not starter is on |
61 | 63 | ||
Line 178... | Line 180... | ||
178 | unsigned short RPM_Pulsewidth; |
180 | unsigned short RPM_Pulsewidth; |
179 | // current RPM pulse next slot index |
181 | // current RPM pulse next slot index |
180 | unsigned short RPM_Count_Val; |
182 | unsigned short RPM_Count_Val; |
181 | 183 | ||
182 | // accumulator for pulse widths |
184 | // accumulator for pulse widths |
183 | unsigned short RPM_Accumulator = ACCUM_MAX; |
185 | static unsigned short RPM_Accumulator = ACCUM_MAX; |
184 | // Next state of pulse high/low |
186 | // Next state of pulse high/low |
185 | unsigned char RPM_State = 1; |
187 | static unsigned char RPM_State = 1; |
186 | // Current state of pulse high/low |
188 | // Current state of pulse high/low |
187 | unsigned char RPM_State_Curr = 1; |
189 | static unsigned char RPM_State_Curr = 1; |
188 | 190 | ||
189 | __disable_irq(); // copy the counter value |
191 | __disable_irq(); // copy the counter value |
190 | RPM_Count_Val = RPM_Count; |
192 | RPM_Count_Val = RPM_Count; |
191 | __enable_irq(); |
193 | __enable_irq(); |
192 | // do calculations |
194 | // do calculations |
Line 202... | Line 204... | ||
202 | unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES; |
204 | unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES; |
203 | if (next_count == RPM_Count_Val) |
205 | if (next_count == RPM_Count_Val) |
204 | { |
206 | { |
205 | break; // completed loop |
207 | break; // completed loop |
206 | } |
208 | } |
207 | char pulse_level = RPM_Level[RPM_Count_Latch]; |
209 | char pulse_level = (RPM_Time[RPM_Count_Latch] & RPM_FLAG) ? 1 : 0; |
208 | base_time = RPM_Time[RPM_Count_Latch]; |
210 | base_time = RPM_Time[RPM_Count_Latch] & ~RPM_FLAG; |
209 | new_time = RPM_Time[next_count]; |
211 | new_time = RPM_Time[next_count] & ~RPM_FLAG; |
210 | RPM_Count_Latch = next_count; |
212 | RPM_Count_Latch = next_count; |
211 | 213 | ||
212 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
214 | RPM_Pulsewidth = new_time - base_time; |
- | 215 | ||
- | 216 | if (pulse_level == 0 && (RPM_Pulsewidth > ACCUM_MAX)) |
|
- | 217 | RPM_State = 1; |
|
- | 218 | if (pulse_level == 1) |
|
- | 219 | RPM_State = 0; |
|
- | 220 | #if 0 |
|
213 | RPM_State_Curr = RPM_State; |
221 | RPM_State_Curr = RPM_State; |
214 | // Count up/down |
222 | // Count up/down |
215 | if (pulse_level == 0) |
223 | if (pulse_level == 0) |
216 | { |
224 | { |
217 | int next = RPM_Accumulator - RPM_Pulsewidth; |
- | |
218 | if (next < 0) // going to cross zero |
225 | if (RPM_Pulsewidth > RPM_Accumulator) // going to cross zero |
219 | { |
226 | { |
220 | RPM_State = 0; |
227 | RPM_State = 0; |
221 | RPM_Accumulator = 0; |
228 | RPM_Accumulator = 0; |
222 | } |
229 | } |
223 | else |
230 | else |
224 | { |
231 | { |
225 | RPM_Accumulator = next; |
232 | RPM_Accumulator -= RPM_Pulsewidth; |
226 | } |
233 | } |
227 | } |
234 | } |
228 | else |
235 | else |
229 | { |
236 | { |
230 | int next = RPM_Accumulator + RPM_Pulsewidth; |
237 | if (RPM_Accumulator + RPM_Pulsewidth > ACCUM_MAX) |
231 | if (next > ACCUM_MAX) |
- | |
232 | { |
238 | { |
233 | RPM_State = 1; |
239 | RPM_State = 1; |
234 | RPM_Accumulator = ACCUM_MAX; |
240 | RPM_Accumulator = ACCUM_MAX; |
235 | } |
241 | } |
236 | else |
242 | else |
237 | { |
243 | { |
238 | RPM_Accumulator = next; |
244 | RPM_Accumulator += RPM_Pulsewidth; |
239 | } |
245 | } |
240 | } |
246 | } |
241 | 247 | #endif |
|
242 | // low pulse has reached at least minimum width, count it. |
248 | // low pulse has reached at least minimum width, count it. |
243 | if ((RPM_State == 0) && (RPM_State_Curr = 1)) |
249 | if ((RPM_State == 1) && (RPM_State_Curr == 0)) |
244 | { |
250 | { |
245 | RPM_Diff = new_time - last_dwell_end; |
251 | RPM_Diff = new_time - last_dwell_end; |
246 | 252 | ||
247 | RPM_Period[RPM_Period_Ptr] = RPM_Diff; |
253 | RPM_Period[RPM_Period_Ptr] = RPM_Diff; |
248 | RPM_Period_Ptr = (RPM_Period_Ptr + 1) % RPM_AVERAGE; |
254 | RPM_Period_Ptr = (RPM_Period_Ptr + 1) % RPM_AVERAGE; |
249 | if (RPM_Pulsecount < RPM_AVERAGE) |
255 | if (RPM_Pulsecount < RPM_AVERAGE) |
250 | RPM_Pulsecount++; // count one pulse |
256 | RPM_Pulsecount++; // count one pulse |
251 | last_dwell_end = new_time; |
257 | last_dwell_end = new_time; |
252 | } |
258 | } |
- | 259 | RPM_State_Curr = RPM_State; |
|
253 | } |
260 | } |
254 | } |
261 | } |
255 | 262 | ||
256 | if (RPM_Pulsecount == RPM_AVERAGE) |
263 | if (RPM_Pulsecount == RPM_AVERAGE) |
257 | { |
264 | { |
Line 265... | Line 272... | ||
265 | 272 | ||
266 | Coded_RPM = (Scale * 30.0 * RPM_AVERAGE * RPM_COUNT_RATE) / (19.55 * RPM_FilteredWidth); |
273 | Coded_RPM = (Scale * 30.0 * RPM_AVERAGE * RPM_COUNT_RATE) / (19.55 * RPM_FilteredWidth); |
267 | 274 | ||
268 | #if !defined MY_DEBUG |
275 | #if !defined MY_DEBUG |
269 | // reset here unless we want to debug |
276 | // reset here unless we want to debug |
270 | RPM_Pulsecount = 0; |
277 | RPM_Pulsecount = 0; |
271 | RPM_FilteredWidth = 0; |
- | |
272 | #endif |
278 | #endif |
273 | } |
279 | } |
274 | 280 | ||
275 | // send the current RPM *calculation |
281 | // send the current RPM *calculation |
276 | plx_sendword(PLX_RPM); |
282 | plx_sendword(PLX_RPM); |