Skip to content

Commit

Permalink
drv_adc: fix divide by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Feb 9, 2021
1 parent f72c817 commit d2d3333
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/drivers/drv_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,27 @@ uint16_t readADC1(int channel) {

float adc_read(int channel) {
switch (channel) {
case 0: //vbat
case 0: {
//vbat
const float raw_value = readADC1(channel);
#ifdef DEBUG
lpf(&debug.adcfilt, (float)readADC1(channel), 0.998);
lpf(&debug.adcfilt, raw_value, 0.998);
#endif
return (float)readADC1(channel) * ((float)(ADC_SCALEFACTOR * (profile.voltage.actual_battery_voltage / profile.voltage.reported_telemetry_voltage)));

case 1: //reference
return raw_value * ((float)(ADC_SCALEFACTOR * (profile.voltage.actual_battery_voltage / profile.voltage.reported_telemetry_voltage)));
}
case 1: {
//reference
const float raw_value = readADC1(channel);
#ifdef DEBUG
lpf(&debug.adcreffilt, (float)readADC1(channel), 0.998);
lpf(&debug.adcreffilt, raw_value, 0.998);
#endif
return vref_cal / (float)readADC1(channel);

if (raw_value == 0) {
// avoid devision by zero below
return 0;
}
return vref_cal / raw_value;
}
default:
return 0;
}
Expand Down

0 comments on commit d2d3333

Please sign in to comment.