Read nRF51822(BLE Chip) VDD voltage and return 100 to 0% charging level
Dependents: BLE_Temp_Vdd_via_UART_TY BLE_EddystoneBeacon_w_ACC_TY51822 BLE_LoopbackUART_low_pwr_w_RTC1 BLE_Paired_Server ... more
This is only for nRF51822.
You don't need any hardware circuit. Just read internal Vdd voltage using ADC function inside of the chip.
Diff: nRF51_Vdd.cpp
- Revision:
- 1:7da210f6aff1
- Parent:
- 0:11c9f031975d
- Child:
- 2:9b1e219a7695
--- a/nRF51_Vdd.cpp Sun Jan 24 03:53:32 2016 +0000 +++ b/nRF51_Vdd.cpp Wed May 04 05:46:07 2016 +0000 @@ -7,7 +7,7 @@ * http://www.page.sannet.ne.jp/kenjia/index.html * http://mbed.org/users/kenjiArai/ * Created: January 23rd, 2016 - * Revised: January 24th, 2016 + * Revised: May 1st, 2016 * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE @@ -17,18 +17,31 @@ */ #include "nRF51_Vdd.h" +#include "nrf_delay.h" + +nRF51_Vdd::nRF51_Vdd(float vdd100percent, float vddzeropercent, uint8_t adc_use){ + v100p = vdd100percent; + v0p = vddzeropercent; + use_adc_for_others = adc_use; +} nRF51_Vdd::nRF51_Vdd(float vdd100percent, float vddzeropercent){ v100p = vdd100percent; v0p = vddzeropercent; + use_adc_for_others = USE_ADC; } nRF51_Vdd::nRF51_Vdd(void){ v100p = 3.3f; v0p = 1.8f; + use_adc_for_others = USE_ADC; } float nRF51_Vdd::read_real_value(void){ + NRF_ADC->POWER = 1; + nrf_delay_us(5); + NRF_ADC->EVENTS_END = 0; + NRF_ADC->TASKS_STOP = 1; reg0 = NRF_ADC->ENABLE; // save register value reg1 = NRF_ADC->CONFIG; // save register value reg2 = NRF_ADC->RESULT; // save register value @@ -41,16 +54,27 @@ NRF_ADC->EVENTS_END = 0; NRF_ADC->TASKS_START = 1; while (!NRF_ADC->EVENTS_END) {;} - wrk = NRF_ADC->RESULT; // 10 bit result - NRF_ADC->ENABLE = reg0; // recover register value - NRF_ADC->CONFIG = reg1; // recover register value - NRF_ADC->RESULT = reg2; // recover register value + wrk = NRF_ADC->RESULT; // 10 bit result NRF_ADC->EVENTS_END = 0; + NRF_ADC->TASKS_STOP = 1; + if (use_adc_for_others == USE_ADC){ + NRF_ADC->ENABLE = reg0; // recover register value + NRF_ADC->CONFIG = reg1; // recover register value + NRF_ADC->RESULT = reg2; // recover register value + NRF_ADC->EVENTS_END = 0; + } else { + NRF_ADC->CONFIG = 0x18; + NRF_ADC->EVENTS_END = 1; + NRF_ADC->TASKS_STOP = 1; + NRF_ADC->ENABLE = 0; + nrf_delay_us(5); + NRF_ADC->POWER = 0; + nrf_delay_us(5); + } return ((float)wrk / 1024 * 1.2 * 3.0); } -uint8_t nRF51_Vdd::read(void) -{ +uint8_t nRF51_Vdd::read(void){ wrk_vdd = read_real_value(); if (wrk_vdd <= v0p){ return 0;