Locator mobile version

Dependencies:   BLE_API mbed nRF51822

Fork of WeatherStation by Weather man

Committer:
PostaL
Date:
Mon Nov 09 23:09:16 2015 +0000
Revision:
2:654ee4b3950f
Added battery service and reading for tiny ble

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PostaL 2:654ee4b3950f 1
PostaL 2:654ee4b3950f 2
PostaL 2:654ee4b3950f 3 #ifndef __BATTERY_H__
PostaL 2:654ee4b3950f 4 #define __BATTERY_H__
PostaL 2:654ee4b3950f 5
PostaL 2:654ee4b3950f 6 #include "mbed.h"
PostaL 2:654ee4b3950f 7
PostaL 2:654ee4b3950f 8 class Battery {
PostaL 2:654ee4b3950f 9 public:
PostaL 2:654ee4b3950f 10 Battery(PinName pin) {
PostaL 2:654ee4b3950f 11 uint32_t n = (uint32_t) pin;
PostaL 2:654ee4b3950f 12 channel = 1 << (1 + n);
PostaL 2:654ee4b3950f 13 }
PostaL 2:654ee4b3950f 14
PostaL 2:654ee4b3950f 15 float read() {
PostaL 2:654ee4b3950f 16 uint32_t pre_enable_register = NRF_ADC->ENABLE;
PostaL 2:654ee4b3950f 17 uint32_t pre_config_register = NRF_ADC->CONFIG;
PostaL 2:654ee4b3950f 18
PostaL 2:654ee4b3950f 19
PostaL 2:654ee4b3950f 20 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
PostaL 2:654ee4b3950f 21 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
PostaL 2:654ee4b3950f 22 (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos) |
PostaL 2:654ee4b3950f 23 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
PostaL 2:654ee4b3950f 24 (channel << ADC_CONFIG_PSEL_Pos) |
PostaL 2:654ee4b3950f 25 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
PostaL 2:654ee4b3950f 26
PostaL 2:654ee4b3950f 27 NRF_ADC->TASKS_START = 1;
PostaL 2:654ee4b3950f 28 while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
PostaL 2:654ee4b3950f 29 }
PostaL 2:654ee4b3950f 30
PostaL 2:654ee4b3950f 31 uint16_t value = NRF_ADC->RESULT;
PostaL 2:654ee4b3950f 32
PostaL 2:654ee4b3950f 33 NRF_ADC->ENABLE = pre_enable_register;
PostaL 2:654ee4b3950f 34 NRF_ADC->CONFIG = pre_config_register;
PostaL 2:654ee4b3950f 35
PostaL 2:654ee4b3950f 36 return (float)value * (1.0f / (float)0x3FF) * 1.2 * 12.2 / 2.2;
PostaL 2:654ee4b3950f 37 }
PostaL 2:654ee4b3950f 38
PostaL 2:654ee4b3950f 39 private:
PostaL 2:654ee4b3950f 40 uint32_t channel;
PostaL 2:654ee4b3950f 41 };
PostaL 2:654ee4b3950f 42
PostaL 2:654ee4b3950f 43 #endif // __BATTERY_H__