Subversion Repositories EngineBay2

Rev

Rev 16 | Rev 18 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16 Rev 17
Line 68... Line 68...
68
volatile char NoSerialIn = 0;
68
volatile char NoSerialIn = 0;
69
 
69
 
70
// storage for ADC
70
// storage for ADC
71
uint16_t  ADC_Samples[6];
71
uint16_t  ADC_Samples[6];
72
 
72
 
-
 
73
#define Scale 1024.0
-
 
74
const float ADC_Scale = 3.3 / (Scale * 4096.0); // convert to a voltage
-
 
75
 
-
 
76
uint32_t  FILT_Samples[6]; // filtered ADC samples * 1024
73
// Rev counter processing from original RevCounter Project
77
// Rev counter processing from original RevCounter Project
74
unsigned int RPM_Diff = 0;
78
unsigned int RPM_Diff = 0;
75
unsigned int RPM_Count_Latch = 0;
79
unsigned int RPM_Count_Latch = 0;
76
// accumulators
80
// accumulators
77
unsigned int RPM_Pulsecount = 0;
81
unsigned int RPM_Pulsecount = 0;
Line 104... Line 108...
104
void plx_sendword(int x) {
108
void plx_sendword(int x) {
105
        PutCharSerial(&uc1, ((x) >> 6) & 0x3F);
109
        PutCharSerial(&uc1, ((x) >> 6) & 0x3F);
106
        PutCharSerial(&uc1, (x) & 0x3F);
110
        PutCharSerial(&uc1, (x) & 0x3F);
107
}
111
}
108
 
112
 
-
 
113
void init_ADC_filter()
-
 
114
{
-
 
115
        int i;
-
 
116
         for(i=0;i<6;i++)
-
 
117
         {
-
 
118
                FILT_Samples[i] = 0;
-
 
119
         }
-
 
120
}
-
 
121
 
-
 
122
void filter_ADC_samples()
-
 
123
{
-
 
124
 int i;
-
 
125
 for(i=0;i<6;i++)
-
 
126
 {
-
 
127
        FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2;
-
 
128
 }
-
 
129
 
-
 
130
 
-
 
131
 
-
 
132
}
-
 
133
 
-
 
134
 
109
void ProcessRPM(int instance) {
135
void ProcessRPM(int instance) {
110
// compute the timer values
136
// compute the timer values
111
// snapshot timers
137
// snapshot timers
112
        unsigned long RPM_Pulsewidth;
138
        unsigned long RPM_Pulsewidth;
113
        unsigned long RPM_Count_Val;
139
        unsigned long RPM_Count_Val;
Line 148... Line 174...
148
 
174
 
149
        }
175
        }
150
 
176
 
151
        if (RPM_Pulsecount > 0) {
177
        if (RPM_Pulsecount > 0) {
152
 
178
 
-
 
179
 
-
 
180
 
-
 
181
 
153
                // now have time for N pulses in clocks
182
                // now have time for N pulses in clocks
154
                // need to scale by 19.55: one unit is 19.55 RPM
183
                // need to scale by 19.55: one unit is 19.55 RPM
155
                // 1Hz is 60 RPM
184
                // 1Hz is 60 RPM
156
                Coded_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE)
185
                float new_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE)
157
                                / (RPM_FilteredWidth) + 0.5;
186
                                                / (RPM_FilteredWidth) + 0.5;
-
 
187
 
-
 
188
                Coded_RPM += (new_RPM * Scale - Coded_RPM)/4;
-
 
189
 
158
#if !defined MY_DEBUG
190
#if !defined MY_DEBUG
159
                // reset here unless we want to debug
191
                // reset here unless we want to debug
160
                RPM_Pulsecount = 0;
192
                RPM_Pulsecount = 0;
161
                RPM_FilteredWidth = 0;
193
                RPM_FilteredWidth = 0;
162
#endif
194
#endif
163
        }
195
        }
164
 
196
 
165
// send the current RPM calculation
197
// send the current RPM *calculation
166
        plx_sendword(PLX_RPM);
198
        plx_sendword(PLX_RPM);
167
        PutCharSerial(&uc1, instance);
199
        PutCharSerial(&uc1, instance);
168
        plx_sendword(Coded_RPM);
200
        plx_sendword(Coded_RPM/Scale);
169
}
201
}
170
 
202
 
171
uint8_t CHT_Timer = 0;
203
uint8_t CHT_Timer = 0;
172
 
204
 
173
// this uses a MAX6675 which is a simple 16 bit read
205
// this uses a MAX6675 which is a simple 16 bit read
Line 183... Line 215...
183
        {
215
        {
184
                CHT_Timer=0;
216
                CHT_Timer=0;
185
 
217
 
186
                   HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_RESET);
218
                   HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_RESET);
187
 
219
 
188
 
-
 
189
                   HAL_Delay(1);
-
 
190
 
-
 
191
                   HAL_SPI_Receive(&hspi1, buffer, 2, 2);
220
                   HAL_SPI_Receive(&hspi1, buffer, 2, 2);
192
 
221
 
-
 
222
                   HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET);
193
 
223
 
194
 
224
 
195
                   uint16_t obs = (buffer[0]<<8)| buffer[1];
225
                   uint16_t obs = (buffer[0]<<8)| buffer[1];
196
 
226
 
197
                   uint8_t  good = (obs & 4)==0;
227
                   uint8_t  good = (obs & 4)==0;
Line 199... Line 229...
199
                   {
229
                   {
200
                     Coded_CHT = obs>>5;
230
                     Coded_CHT = obs>>5;
201
                   }
231
                   }
202
                   else
232
                   else
203
                   {
233
                   {
204
                          Coded_CHT= 1000; // signal fail
234
                          Coded_CHT= 1024; // signal fail
205
                   }
235
                   }
206
        }
236
        }
207
 
237
 
208
        plx_sendword(PLX_X_CHT);
238
        plx_sendword(PLX_X_CHT);
209
        PutCharSerial(&uc1, instance);
239
        PutCharSerial(&uc1, instance);
210
        plx_sendword(Coded_CHT);
240
        plx_sendword(Coded_CHT);
211
     HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET);
-
 
212
 
241
 
213
}
242
}
214
 
243
 
-
 
244
// 1023 is 20.00 volts.
215
void ProcessBatteryVoltage(int instance)
245
void ProcessBatteryVoltage(int instance)
216
{
246
{
-
 
247
    float reading = FILT_Samples[instance] * ADC_Scale ;
-
 
248
    reading = reading * 7.8125; // real voltage
-
 
249
    reading = reading * 51.15 ; // 1023/20
-
 
250
 
-
 
251
 
217
        plx_sendword(PLX_Volts);
252
        plx_sendword(PLX_Volts);
218
        PutCharSerial(&uc1, instance);
253
        PutCharSerial(&uc1, instance);
219
        plx_sendword(ADC_Samples[instance]);
254
        plx_sendword((uint16_t)reading);
-
 
255
 
-
 
256
 
-
 
257
 
-
 
258
}
-
 
259
 
-
 
260
// the MAP sensor is giving us a reading of
-
 
261
// 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016)
-
 
262
// I believe the sensor reads  4.5V at 1000kPa and 0.5V at  0kPa
-
 
263
 
-
 
264
void ProcessMAP(int instance)
-
 
265
{
-
 
266
// Using ADC_Samples[3] as the MAP input
-
 
267
    float reading = FILT_Samples[3] * ADC_Scale;
-
 
268
    reading = reading * 2.016; // real voltage
-
 
269
    reading = (reading-0.5) * 1000/ 4;
-
 
270
 
-
 
271
        plx_sendword(PLX_MAP);
-
 
272
        PutCharSerial(&uc1, instance);
-
 
273
        plx_sendword((uint16_t)reading);
220
 
274
 
221
 
275
 
222
 
276
 
223
}
277
}
224
 
278
 
-
 
279
// the Oil pressi sensor is giving us a reading of
-
 
280
// 4.5 volts for 100 PSI or  2.25 volts at the ADC input (resistive divider by 2.016)
-
 
281
// I believe the sensor reads  4.5V at 100PSI and 0.5V at  0PSI
-
 
282
// an observation of 1024 is 200PSI, so observation of 512 is 100 PSI.
-
 
283
 
-
 
284
void ProcessOilPress(int instance)
-
 
285
{
-
 
286
// Using ADC_Samples[2] as the MAP input
-
 
287
    float reading = FILT_Samples[2] *  ADC_Scale ;
-
 
288
    reading = reading * 2.00 ; // real voltage
-
 
289
    reading = (reading-0.5) * 512 / 4;  // this is 1023 * 100/200
-
 
290
 
-
 
291
        plx_sendword(PLX_FluidPressure);
-
 
292
        PutCharSerial(&uc1, instance);
-
 
293
        plx_sendword((uint16_t)reading);
-
 
294
 
-
 
295
 
-
 
296
 
-
 
297
}
-
 
298
 
-
 
299
 
225
 
300
 
226
void ProcessTiming(int instance)
301
void ProcessTiming(int instance)
227
{
302
{
228
        plx_sendword(PLX_Timing);
303
        plx_sendword(PLX_Timing);
229
        PutCharSerial(&uc1, instance);
304
        PutCharSerial(&uc1, instance);
230
        plx_sendword(64-15); // make it negative
305
        plx_sendword(64-15); // make it negative
231
}
306
}
232
 
307
 
-
 
308
 
-
 
309
 
233
/* USER CODE END 0 */
310
/* USER CODE END 0 */
234
 
311
 
235
int main(void)
312
int main(void)
236
{
313
{
237
 
314
 
Line 289... Line 366...
289
// Start the counter
366
// Start the counter
290
        HAL_TIM_Base_Start(&htim2);
367
        HAL_TIM_Base_Start(&htim2);
291
// Start the input capture and the interrupt
368
// Start the input capture and the interrupt
292
        HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
369
        HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
293
 
370
 
-
 
371
        init_ADC_filter();
294
 
372
 
295
  /* USER CODE END 2 */
373
  /* USER CODE END 2 */
296
 
374
 
297
  /* Infinite loop */
375
  /* Infinite loop */
298
  /* USER CODE BEGIN WHILE */
376
  /* USER CODE BEGIN WHILE */
Line 335... Line 413...
335
          PutCharSerial(&uc2,(val&31) + 32);
413
          PutCharSerial(&uc2,(val&31) + 32);
336
 
414
 
337
 
415
 
338
 
416
 
339
  // send the observations
417
  // send the observations
-
 
418
         filter_ADC_samples();
340
                 ProcessRPM(0);
419
                 ProcessRPM(0);
341
                 ProcessCHT(0);
420
                 ProcessCHT(0);
342
                 ProcessBatteryVoltage(3);
421
                 ProcessBatteryVoltage(0); // Batt 1
343
                 ProcessBatteryVoltage(1);
422
                 ProcessBatteryVoltage(1); // Batt 2
344
 
-
 
-
 
423
//               ProcessBatteryVoltage(2); // oil pressure
-
 
424
//               ProcessBatteryVoltage(3); // MAP
-
 
425
                 ProcessBatteryVoltage(4); // temperature sensor
-
 
426
//               ProcessBatteryVoltage(5); // vrefint
-
 
427
                 ProcessMAP(0);
-
 
428
                 ProcessOilPress(0);
345
                 ProcessTiming(0);
429
                 ProcessTiming(0);
346
 
430
 
347
                 PutCharSerial(&uc1,PLX_Stop);
431
                 PutCharSerial(&uc1,PLX_Stop);
348
                }
432
                }
349
 
433
 
Line 425... Line 509...
425
 
509
 
426
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
510
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
427
    */
511
    */
428
  sConfig.Channel = ADC_CHANNEL_10;
512
  sConfig.Channel = ADC_CHANNEL_10;
429
  sConfig.Rank = 1;
513
  sConfig.Rank = 1;
430
  sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES;
514
  sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
431
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
515
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
432
  {
516
  {
433
    Error_Handler();
517
    Error_Handler();
434
  }
518
  }
435
 
519