我在调试应用程序时遇到错误,我已经能够指出错误似乎发生在对函数 adc_gain_enum_to_real_gain()的调用中,但是我看不到为什么它出错了,我怀疑与传递/读取指针&adc_gain
有关。任何提示?
提前致谢!
void saadc_handler_interrupt(nrf_drv_saadc_evt_t const * const p_event)
{
uint32_t err_code;
uint16_t voltage;
nrf_saadc_value_t adc_result;
uint16_t tmp_voltage;
float adc_gain;
if (p_event->type == NRF_DRV_SAADC_EVT_CALIBRATEDONE)
{
m_adc_cal_in_progress = false;
}
else if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
adc_result = p_event->data.done.p_buffer[0];
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
APP_ERROR_CHECK(err_code);
err_code = adc_gain_enum_to_real_gain(ADC_GAIN, &adc_gain); //<===HERE!!
APP_ERROR_CHECK(err_code);
float tmp = adc_result / (( (1/6) / ADC_REFERENCE_VOLTAGE) * pow(2, ADC_RESOLUTION_BITS));
tmp_voltage = (uint16_t) ((tmp / m_battery_divider_factor) * 1000);
voltage = ( (tmp_voltage + 5) / 10) * 10; // Round the value.
NRF_LOG_INFO("Read value from saadc %d\nV",voltage);
batt_event_handler_adc(voltage);
}
//nrf_drv_saadc_uninit();
}
这样的功能是
uint32_t adc_gain_enum_to_real_gain(nrf_saadc_gain_t gain_reg, float * const real_val)
{
switch(gain_reg)
{
case NRF_SAADC_GAIN1_6: *real_val = 1 / (float)6;
break;
case NRF_SAADC_GAIN1_5: *real_val = 1 / (float)5;
break;
case NRF_SAADC_GAIN1_4: *real_val = 1 / (float)4;
break;
case NRF_SAADC_GAIN1_3: *real_val = 1 / (float)3;
break;
case NRF_SAADC_GAIN1_2: *real_val = 1 / (float)2;
break;
case NRF_SAADC_GAIN1: *real_val = 1;
break;
case NRF_SAADC_GAIN2: *real_val = 2;
break;
case NRF_SAADC_GAIN4: *real_val = 3;
break;
default: return M_BATT_STATUS_CODE_INVALID_PARAM;
};
return M_BATT_STATUS_CODE_SUCCESS;
}
答案 0 :(得分:1)
下面
float tmp = adc_result / (( (1/6) / ADC_REFERENCE_VOLTAGE) * pow(2, ADC_RESOLUTION_BITS));
(1/6)
将始终评估为0
。这是一个整数除法!
所以整个表达式(( (1/6) / ADC_REFERENCE_VOLTAGE) * pow(2, ADC_RESOLUTION_BITS))
会产生0.
(注意:此处为浮点),这反过来会在adc_result / ...
引发除零。
要解决此问题,请执行
(1./6)
或
(1/6.)
或
((float) 1/6)
或
(1 /(float) 6)
或上述任意组合。
不相关但是与整数除法相关的另一个麻烦原因是:
(tmp_voltage + 5) / 10
如上修复。