An example of testing the micro:bit battery without any external components

Dependencies:   microbit

Fork of microbit-component-display by BBC

Committer:
JonnyA
Date:
Thu Sep 28 18:23:33 2017 +0000
Revision:
1:6c9081f485dd
Parent:
0:0c37474c8541
Repurpose a simple display example into a battery voltage one. Thanks to Marcelo's article https://os.mbed.com/users/MarceloSalazar/notebook/measuring-battery-voltage-with-nordic-nrf51x/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonnyA 0:0c37474c8541 1 /* See
JonnyA 0:0c37474c8541 2 * http://lancaster-university.github.io/microbit-docs/advanced/
JonnyA 0:0c37474c8541 3 * for docs about using the micro:bit library
JonnyA 0:0c37474c8541 4 */
JonnyA 0:0c37474c8541 5 #include "MicroBit.h"
JonnyA 0:0c37474c8541 6
JonnyA 0:0c37474c8541 7 MicroBitDisplay display;
JonnyA 0:0c37474c8541 8
JonnyA 1:6c9081f485dd 9 /*
JonnyA 1:6c9081f485dd 10 * This is a simple program that let us use the ADC to read the input voltage.
JonnyA 1:6c9081f485dd 11 * - thanks Marcelo: https://os.mbed.com/users/MarceloSalazar/notebook/measuring-battery-voltage-with-nordic-nrf51x/
JonnyA 1:6c9081f485dd 12 */
JonnyA 1:6c9081f485dd 13
JonnyA 1:6c9081f485dd 14
JonnyA 1:6c9081f485dd 15 void vdd_analogin_init(void)
JonnyA 1:6c9081f485dd 16 {
JonnyA 1:6c9081f485dd 17 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
JonnyA 1:6c9081f485dd 18 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
JonnyA 1:6c9081f485dd 19 (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
JonnyA 1:6c9081f485dd 20 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
JonnyA 1:6c9081f485dd 21 (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) |
JonnyA 1:6c9081f485dd 22 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
JonnyA 1:6c9081f485dd 23 }
JonnyA 1:6c9081f485dd 24
JonnyA 1:6c9081f485dd 25 uint16_t vdd_analogin_read_u16(void)
JonnyA 1:6c9081f485dd 26 {
JonnyA 1:6c9081f485dd 27 NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
JonnyA 1:6c9081f485dd 28 NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos;
JonnyA 1:6c9081f485dd 29 NRF_ADC->TASKS_START = 1;
JonnyA 1:6c9081f485dd 30 while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {};
JonnyA 1:6c9081f485dd 31 return (uint16_t)NRF_ADC->RESULT; // 10 bit
JonnyA 1:6c9081f485dd 32 }
JonnyA 1:6c9081f485dd 33
JonnyA 0:0c37474c8541 34 int main()
JonnyA 0:0c37474c8541 35 {
JonnyA 1:6c9081f485dd 36 float value;
JonnyA 1:6c9081f485dd 37 char buffer[5];
JonnyA 1:6c9081f485dd 38 vdd_analogin_init();
JonnyA 1:6c9081f485dd 39
JonnyA 1:6c9081f485dd 40 while(1) {
JonnyA 1:6c9081f485dd 41 value = (float)vdd_analogin_read_u16();
JonnyA 1:6c9081f485dd 42 value = (value * 3.6) / 1024.0;
JonnyA 1:6c9081f485dd 43 //May seem risky, but device will die before it reaches 10V ;)
JonnyA 1:6c9081f485dd 44 sprintf(buffer, "%.2f", value);
JonnyA 1:6c9081f485dd 45 display.scroll(buffer);
JonnyA 1:6c9081f485dd 46 wait(3.0);
JonnyA 1:6c9081f485dd 47 }
JonnyA 0:0c37474c8541 48 }