Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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