CSSE4011_BLE_IMU IMU Seeed Tiny Ble

Dependencies:   BLE_API_Tiny_BLE MPU6050-DMP-Seeed-Tiny-BLE mbed

Committer:
flywind
Date:
Wed Jun 10 09:46:16 2015 +0000
Revision:
0:f90c3452d779
CSSE4011_Seed-Tiny-BLE

Who changed what in which revision?

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