/**@brief Function for handling the ADC interrupt. * * @details This function will fetch the conversion result from the ADC, convert the value into * percentage and send it to peer. */ void ADC_IRQHandler(void) { if (nrf_adc_conversion_finished()) { uint8_t adc_result; uint16_t batt_lvl_in_milli_volts; uint8_t percentage_batt_lvl; uint32_t err_code; nrf_adc_conversion_event_clean(); adc_result = nrf_adc_result_get(); batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) + DIODE_FWD_VOLT_DROP_MILLIVOLTS; percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts); err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl); if ( (err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_BUFFERS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } } }
/**@brief Function for handling the ADC interrupt. * * @details This function will fetch the conversion result from the ADC, convert the value into * percentage and send it to peer. */ void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event) { if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { nrf_saadc_value_t adc_result; uint16_t batt_lvl_in_milli_volts; uint8_t percentage_batt_lvl; uint32_t err_code; 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); batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) + DIODE_FWD_VOLT_DROP_MILLIVOLTS; percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts); err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl); if ( (err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_PACKETS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } } }
/**@brief Function for handling the ADC interrupt. * @details This function will fetch the conversion result from the ADC, convert the value into * percentage and send it to peer. */ void ADC_IRQHandler(void) { if (NRF_ADC->EVENTS_END != 0) { uint8_t adc_result; uint16_t batt_lvl_in_milli_volts; uint8_t percentage_batt_lvl; uint32_t err_code; NRF_ADC->EVENTS_END = 0; adc_result = NRF_ADC->RESULT; NRF_ADC->TASKS_STOP = 1; batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) + DIODE_FWD_VOLT_DROP_MILLIVOLTS; percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts); err_code = ble_bas_battery_level_update(&bas, percentage_batt_lvl); if ( (err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_BUFFERS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } } }
/**@brief Function for handling the ADC interrupt. * * @details This function will fetch the conversion result from the ADC, convert the value into * percentage and send it to peer. */ void ADC_IRQHandler(void) { uint32_t err_code; uint16_t adc_value; uint8_t percentage_batt_lvl; uint16_t batt_lvl_in_milli_volts; nrf_adc_conversion_event_clean(); nrf_adc_stop(); adc_value = nrf_adc_result_get(); batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_value); percentage_batt_lvl = battery_level_in_percent(batt_lvl_in_milli_volts); err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl); if ( (err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_PACKETS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } }
uint16_t battery_adc_read_once(void) { uint32_t adc_result = 0; uint16_t batt_lvl_in_milli_volts=0; battery_start(); nrf_delay_ms(10); while( !(NRF_ADC->EVENTS_END) ) { nrf_delay_ms(10); } NRF_ADC->EVENTS_END = 0; adc_result = NRF_ADC->RESULT; NRF_ADC->TASKS_STOP = 1; #if DEBUG_UART_EN // DbgPrintf("adc_result:%d\r\n",adc_result); #endif //*batt_lvl_in_milli_volts = ((uint32_t)(ADC_RESULT_IN_MILLI_VOLTS(adc_result)) & 0xffff); batt_lvl_in_milli_volts = ((uint32_t)(ADC_RESULT_IN_MILLI_VOLTS(adc_result)) & 0xffff); #if DEBUG_UART_EN // DbgPrintf("milli_volts:%d\r\n",batt_lvl_in_milli_volts); #endif //when charging, battery voltage need adjust // if(charger_status()!= NoCharge) { //batt_lvl_in_milli_volts += BATTERY_VOLTAGE_ADJUSTMENT; // } return batt_lvl_in_milli_volts; }
//ADC interrupt handler void ADC_IRQHandler(void) { if (NRF_ADC->EVENTS_END != 0) { uint8_t adc_result; NRF_ADC->EVENTS_END = 0; adc_result = NRF_ADC->RESULT; NRF_ADC->TASKS_STOP = 1; __SUPERCAP_VOLTAGE = ADC_RESULT_IN_MILLI_VOLTS(adc_result); } }
//ADC interrupt handler void ADC_IRQHandler(void) { if (NRF_ADC->EVENTS_END != 0) { uint8_t adc_result; NRF_ADC->EVENTS_END = 0; adc_result = NRF_ADC->RESULT; NRF_ADC->TASKS_STOP = 1; if(_ADC_measurement==_SUPERCAP_MEASURE) { __SUPERCAP_VOLTAGE = ADC_RESULT_IN_MILLI_VOLTS(adc_result); } else { __SOIL_HUMIDITY = adc_result; nrf_gpio_pin_clear(GPIO2); } } }
/** * @brief ADC interrupt handler. */ static void adc_event_handler(nrf_drv_adc_evt_t const * p_event) { uint32_t err_code; uint16_t adc_sum_value = 0; uint16_t adc_average_value; uint16_t adc_result_millivolts; uint8_t adc_result_percent; sd_clock_hfclk_release(); //Release the external crystal adc_event_counter++; NRF_LOG_INFO(" ADC event counter: %d\r\n", adc_event_counter); if (p_event->type == NRF_DRV_ADC_EVT_DONE) { uint32_t i; for (i = 0; i < p_event->data.done.size; i++) { NRF_LOG_INFO("Sample value %d: %d\r\n", i+1, p_event->data.done.p_buffer[i]); adc_sum_value += p_event->data.done.p_buffer[i]; //Sum all values in ADC buffer } adc_average_value = adc_sum_value / p_event->data.done.size; //Calculate average value from all samples in the ADC buffer NRF_LOG_INFO("Average ADC value: %d\r\n", adc_average_value); adc_result_millivolts = ADC_RESULT_IN_MILLI_VOLTS(adc_average_value); //Transform the average ADC value into millivolts value NRF_LOG_INFO("ADC result in millivolts: %d\r\n", adc_result_millivolts); adc_result_percent = battery_level_in_percent(adc_result_millivolts); //Transform the millivolts value into battery level percent. NRF_LOG_INFO("ADC result in percent: %d\r\n", adc_result_percent); //Send the battery level over BLE err_code = ble_bas_battery_level_update(&m_bas, adc_result_percent); //Send the battery level over BLE if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_PACKETS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)) { APP_ERROR_HANDLER(err_code); //Assert on error } } }
static uint16_t adc_read_blocking() { while (NRF_ADC->BUSY == 1) { // __asm("nop"); } NRF_ADC->EVENTS_END = 0; sd_nvic_DisableIRQ(ADC_IRQn); NRF_ADC->INTENCLR = ADC_INTENCLR_END_Enabled; NRF_ADC->TASKS_START = 1; adc_config(); NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; while (NRF_ADC->EVENTS_END == 0) { // __asm("nop"); } uint16_t batt_lvl_in_milli_volts; uint16_t result = NRF_ADC->RESULT; batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(result); result = battery_level_in_percent(batt_lvl_in_milli_volts); NRF_ADC->EVENTS_END = 0; NRF_ADC->TASKS_STOP = 1; return result; }
/**@brief Function for getting battery level using ADC. * @details This function also converts adc register value to percentage level. * * @param[out] bl Battery level in percentage. * * @retval NRF_SUCCES is measurement were properly taken, NRF_ERROR_BUSY otherwise. */ uint32_t take_battery_level(uint8_t * bl) { uint32_t err_code = NRF_ERROR_BUSY; if (NRF_ADC->EVENTS_END != 0) { uint8_t adc_result; uint16_t batt_lvl_in_milli_volts; NRF_ADC->EVENTS_END = 0; adc_result = NRF_ADC->RESULT; NRF_ADC->TASKS_STOP = 1; batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) + DIODE_FWD_VOLT_DROP_MILLIVOLTS; *bl = battery_level_in_percent(batt_lvl_in_milli_volts); NRF_ADC->TASKS_START = 1; err_code = NRF_SUCCESS; } return err_code; }