Subversion Repositories EngineBay2

Rev

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

Rev 38 Rev 39
Line 39... Line 39...
39
 
39
 
40
/* Private macro -------------------------------------------------------------*/
40
/* Private macro -------------------------------------------------------------*/
41
/* USER CODE BEGIN PM */
41
/* USER CODE BEGIN PM */
42
#define ADC_CHANNELS 7
42
#define ADC_CHANNELS 7
43
 
43
 
-
 
44
#define ADC_MAP_CHAN 2
-
 
45
 
-
 
46
#define ADC_PRESSURE_CHAN 3
-
 
47
 
-
 
48
#define ADC_REF_CHAN 5
-
 
49
 
-
 
50
#define ADC_TEMP_CHAN 6
-
 
51
 
44
// 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
45
// freq = 5000/60 * 2 = 166Hz.
53
// freq = 5000/60 * 2 = 166Hz.
46
// the TIM2 counter counts in 10uS increments,
54
// the TIM2 counter counts in 10uS increments,
47
// TODO this is wrong algo. Accept FIRST pulse, skip shorter pulses
55
// TODO this is wrong algo. Accept FIRST pulse, skip shorter pulses
48
// Accept the first pulse with over 2.5mS (1/400 sec)  duration as the closure
56
// Accept the first pulse with over 2.5mS (1/400 sec)  duration as the closure
Line 77... Line 85...
77
volatile char TimerFlag = 0;
85
volatile char TimerFlag = 0;
78
 
86
 
79
volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1
87
volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1
80
volatile char NoSerialIn = 0;
88
volatile char NoSerialIn = 0;
81
 
89
 
-
 
90
// scale for filtered samples
-
 
91
#define Scale 1024.0
-
 
92
 
82
// storage for ADC
93
// storage for ADC
83
uint16_t ADC_Samples[ADC_CHANNELS];
94
uint16_t ADC_Samples[ADC_CHANNELS];
84
 
95
 
-
 
96
uint32_t FILT_Samples[ADC_CHANNELS]; // filtered ADC samples * Scale
-
 
97
 
-
 
98
 
85
#define Scale 1024.0
99
#define NOM_VREF 3.3
-
 
100
// initial ADC vref
-
 
101
float  adc_vref   = NOM_VREF;
-
 
102
 
-
 
103
// internal bandgap voltage reference
-
 
104
const float STM32REF = 1.2;           // 1.2V typical
-
 
105
 
-
 
106
// scale factor initially assuming
86
const float ADC_Scale = 3.3 / (Scale * 4096.0); // convert to a voltage
107
float ADC_Scale = 1/(Scale * 4096) * NOM_VREF ;
87
 
108
 
88
uint32_t FILT_Samples[ADC_CHANNELS]; // filtered ADC samples * 1024
-
 
89
// Rev counter processing from original RevCounter Project
109
// Rev counter processing from original RevCounter Project
90
uint16_t RPM_Diff = 0;
110
uint16_t RPM_Diff = 0;
91
uint16_t RPM_Count_Latch = 0;
111
uint16_t RPM_Count_Latch = 0;
92
// accumulators
112
// accumulators
93
uint16_t RPM_Pulsecount = 0;
113
uint16_t RPM_Pulsecount = 0;
Line 150... Line 170...
150
    {
170
    {
151
      FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2;
171
      FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2;
152
    }
172
    }
153
}
173
}
154
 
174
 
-
 
175
 
-
 
176
/****!
-
 
177
 * @brief this reads the reference voltage within the STM32L151
-
 
178
 * Powers up reference voltage and temperature sensor, waits 3mS  and takes reading
-
 
179
 * Requires that the ADC be powered up
-
 
180
 */
-
 
181
 
-
 
182
 
-
 
183
void
-
 
184
CalibrateADC (void)
-
 
185
{
-
 
186
  float adc_val = FILT_Samples[ADC_REF_CHAN]  ;       // as set up in device config
-
 
187
 
-
 
188
  float adc_vref = STM32REF * ( 4096.0 * Scale)/  adc_val; // the estimate for checking
-
 
189
 
-
 
190
  ADC_Scale = 1/(Scale * 4096) * adc_vref ;
-
 
191
 
-
 
192
 
-
 
193
}
-
 
194
 
-
 
195
 
-
 
196
 
155
void
197
void
156
ProcessRPM (int instance)
198
ProcessRPM (int instance)
157
{
199
{
158
// compute the timer values
200
// compute the timer values
159
// snapshot timers
201
// snapshot timers
Line 175... Line 217...
175
          unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES;
217
          unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES;
176
          if (next_count == RPM_Count_Val)
218
          if (next_count == RPM_Count_Val)
177
            {
219
            {
178
              break; // completed loop
220
              break; // completed loop
179
            }
221
            }
180
          char pulse_level = RPM_Level[RPM_Count_Latch];
-
 
181
          base_time = RPM_Time[RPM_Count_Latch];
222
          base_time = RPM_Time[RPM_Count_Latch];
182
          new_time = RPM_Time[next_count];
223
          new_time = RPM_Time[next_count];
183
          RPM_Count_Latch = next_count;
224
          RPM_Count_Latch = next_count;
184
 
225
 
185
          RPM_Pulsewidth = new_time - base_time; // not wrapped
226
          RPM_Pulsewidth = new_time - base_time; // not wrapped
186
 
227
 
187
          // if the pulse was low,
228
          // if the pulse was low,
188
          if (pulse_level == 0 && RPM_Pulsewidth > BREAKER_MIN)
229
          if (RPM_Pulsewidth > BREAKER_MIN)
189
            {
230
            {
190
 
231
 
191
              RPM_Diff = new_time - last_dwell_end;
232
              RPM_Diff = new_time - last_dwell_end;
192
 
233
 
193
              RPM_Period[RPM_Period_Ptr] = RPM_Diff;
234
              RPM_Period[RPM_Period_Ptr] = RPM_Diff;
Line 303... Line 344...
303
void
344
void
304
ProcessBatteryVoltage (int instance)
345
ProcessBatteryVoltage (int instance)
305
{
346
{
306
  float reading = FILT_Samples[instance] * ADC_Scale;
347
  float reading = FILT_Samples[instance] * ADC_Scale;
307
  reading = reading * 7.8125; // real voltage
348
  reading = reading * 7.8125; // real voltage
308
  reading = reading * 51.15; // 1023/20
349
  reading = reading * 51.15; // PLC scaling =  1023/20
309
 
350
 
310
  plx_sendword (PLX_Volts);
351
  plx_sendword (PLX_Volts);
311
  PutCharSerial (&uc1, instance);
352
  PutCharSerial (&uc1, instance);
312
  plx_sendword ((uint16_t) reading);
353
  plx_sendword ((uint16_t) reading);
313
 
354
 
314
}
355
}
315
 
356
 
316
/****!
-
 
317
 * @brief this reads the reference voltage within the STM32L151
-
 
318
 * Powers up reference voltage and temperature sensor, waits 3mS  and takes reading
-
 
319
 * Requires that the ADC be powered up
-
 
320
 */
-
 
321
 
-
 
322
uint32_t ADC_VREF_MV = 3300;           // 3.300V typical
-
 
323
const uint16_t STM32REF_MV = 1224;           // 1.224V typical
-
 
324
 
-
 
325
void
-
 
326
CalibrateADC (void)
-
 
327
{
-
 
328
  uint32_t adc_val = FILT_Samples[5];       // as set up in device config
-
 
329
  ADC_VREF_MV = (STM32REF_MV * 4096) / adc_val;
-
 
330
}
-
 
331
 
357
 
332
void
358
void
333
ProcessCPUTemperature (int instance)
359
ProcessCPUTemperature (int instance)
334
{
360
{
-
 
361
   // this is defined in the STM32F103 reference manual . #
335
  int32_t temp_val;
362
  // V25 = 1.43 volts
-
 
363
  // Avg_slope = 4.3mV /degree C
-
 
364
  // temperature = {(V25 - VSENSE) / Avg_Slope} + 25
336
 
365
 
337
  uint16_t TS_CAL30 = *(uint16_t*) (0x1FF8007AUL); /* ADC reading for temperature sensor at 30 degrees C with Vref = 3000mV */
-
 
338
  uint16_t TS_CAL110 = *(uint16_t*) (0x1FF8007EUL); /* ADC reading for temperature sensor at 110 degrees C with Vref = 3000mV */
-
 
339
  /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */
366
  /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */
340
 
367
 
341
  temp_val = FILT_Samples[5];
368
  float temp_val = FILT_Samples[ADC_TEMP_CHAN] * ADC_Scale;
342
 
-
 
343
  /* renormalise temperature value to account for different ADC Vref  : normalise to that which we would get for a 3000mV reference */
369
  /* renormalise temperature value to account for different ADC Vref  : normalise to that which we would get for a 3000mV reference */
344
  temp_val = temp_val * ADC_VREF_MV / (Scale * 3000UL);
370
  temp_val = (1.43- temp_val) / 4.3e-3 + 25;
-
 
371
 
-
 
372
  int32_t result = temp_val  ;
-
 
373
 
-
 
374
//  int32_t result = 800 * ((int32_t) temp_val - TS_CAL30);
-
 
375
//  result = result / (TS_CAL110 - TS_CAL30) + 300;
345
 
376
 
346
  int32_t result = 800 * ((int32_t) temp_val - TS_CAL30);
-
 
347
  result = result / (TS_CAL110 - TS_CAL30) + 300;
-
 
348
 
377
 
349
  if (result < 0)
-
 
350
    {
-
 
351
      result = 0;
-
 
352
    }
-
 
353
  plx_sendword (PLX_FluidTemp);
378
  plx_sendword (PLX_FluidTemp);
354
  PutCharSerial (&uc1, instance);
379
  PutCharSerial (&uc1, instance);
355
  plx_sendword (result / 10);
380
  plx_sendword (result);
356
 
381
 
357
}
382
}
358
 
383
 
359
// the MAP sensor is giving us a reading of
384
// the MAP sensor is giving us a reading of
360
// 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016)
385
// 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016)
Line 367... Line 392...
367
 
392
 
368
void
393
void
369
ProcessMAP (int instance)
394
ProcessMAP (int instance)
370
{
395
{
371
// Using ADC_Samples[3] as the MAP input
396
// Using ADC_Samples[3] as the MAP input
372
  float reading = FILT_Samples[3] * ADC_Scale;
397
  float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale;
373
  reading = reading * 2.016;      // real voltage
398
  reading = reading * 2.016;      // real voltage
374
  // values computed from slope / intercept of map.ods
399
  // values computed from slope / intercept of map.ods
375
  //reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5
400
  //reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5
376
  // using a pressure gauge.
401
  // using a pressure gauge.
377
  reading = (reading) * 150 + 326;
402
  reading = (reading) * 150 + 326;
Line 389... Line 414...
389
 
414
 
390
void
415
void
391
ProcessOilPress (int instance)
416
ProcessOilPress (int instance)
392
{
417
{
393
// Using ADC_Samples[2] as the MAP input
418
// Using ADC_Samples[2] as the MAP input
394
  float reading = FILT_Samples[2] * ADC_Scale;
419
  float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale;
395
  reading = reading * 2.00; // real voltage
420
  reading = reading * 2.00; // real voltage
396
  reading = (reading - 0.5) * 512 / 4;  // this is 1023 * 100/200
421
  reading = (reading - 0.5) * 512 / 4;  // this is 1023 * 100/200
397
 
422
 
398
  plx_sendword (PLX_FluidPressure);
423
  plx_sendword (PLX_FluidPressure);
399
  PutCharSerial (&uc1, instance);
424
  PutCharSerial (&uc1, instance);
Line 460... Line 485...
460
 
485
 
461
  HAL_SPI_MspInit (&hspi1);
486
  HAL_SPI_MspInit (&hspi1);
462
 
487
 
463
  HAL_ADC_MspInit (&hadc1);
488
  HAL_ADC_MspInit (&hadc1);
464
 
489
 
465
  HAL_ADC_Start_DMA (&hadc1, ADC_Samples, ADC_CHANNELS);
490
  HAL_ADC_Start_DMA (&hadc1, (uint32_t *)ADC_Samples, ADC_CHANNELS);
466
 
491
 
467
  HAL_ADC_Start_IT (&hadc1);
492
  HAL_ADC_Start_IT (&hadc1);
468
 
493
 
469
  HAL_TIM_Base_MspInit (&htim4);
494
  HAL_TIM_Base_MspInit (&htim4);
470
  HAL_TIM_Base_Start_IT (&htim4);
495
  HAL_TIM_Base_Start_IT (&htim4);
Line 670... Line 695...
670
  }
695
  }
671
  /** Configure Regular Channel
696
  /** Configure Regular Channel
672
  */
697
  */
673
  sConfig.Channel = ADC_CHANNEL_0;
698
  sConfig.Channel = ADC_CHANNEL_0;
674
  sConfig.Rank = ADC_REGULAR_RANK_1;
699
  sConfig.Rank = ADC_REGULAR_RANK_1;
675
  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
700
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
676
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
701
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
677
  {
702
  {
678
    Error_Handler();
703
    Error_Handler();
679
  }
704
  }
680
  /** Configure Regular Channel
705
  /** Configure Regular Channel
Line 695... Line 720...
695
  }
720
  }
696
  /** Configure Regular Channel
721
  /** Configure Regular Channel
697
  */
722
  */
698
  sConfig.Channel = ADC_CHANNEL_3;
723
  sConfig.Channel = ADC_CHANNEL_3;
699
  sConfig.Rank = ADC_REGULAR_RANK_4;
724
  sConfig.Rank = ADC_REGULAR_RANK_4;
700
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
-
 
701
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
725
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
702
  {
726
  {
703
    Error_Handler();
727
    Error_Handler();
704
  }
728
  }
705
  /** Configure Regular Channel
729
  /** Configure Regular Channel
706
  */
730
  */
707
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
731
  sConfig.Channel = ADC_CHANNEL_4;
708
  sConfig.Rank = ADC_REGULAR_RANK_5;
732
  sConfig.Rank = ADC_REGULAR_RANK_5;
709
  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
-
 
710
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
733
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
711
  {
734
  {
712
    Error_Handler();
735
    Error_Handler();
713
  }
736
  }
714
  /** Configure Regular Channel
737
  /** Configure Regular Channel
Line 719... Line 742...
719
  {
742
  {
720
    Error_Handler();
743
    Error_Handler();
721
  }
744
  }
722
  /** Configure Regular Channel
745
  /** Configure Regular Channel
723
  */
746
  */
724
  sConfig.Channel = ADC_CHANNEL_4;
747
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
725
  sConfig.Rank = ADC_REGULAR_RANK_7;
748
  sConfig.Rank = ADC_REGULAR_RANK_7;
726
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
749
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
727
  {
750
  {
728
    Error_Handler();
751
    Error_Handler();
729
  }
752
  }