My Template app for TinyBLE.
Dependencies: mbed BLE_API nRF51822 eMPL_MPU6050
battery.h@2:45d3ae0a3b3c, 2020-08-22 (annotated)
- Committer:
- arils
- Date:
- Sat Aug 22 02:20:03 2020 +0000
- Revision:
- 2:45d3ae0a3b3c
- Parent:
- 1:cea2f48b1aff
Template Version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arils | 1:cea2f48b1aff | 1 | |
arils | 1:cea2f48b1aff | 2 | |
arils | 1:cea2f48b1aff | 3 | #ifndef __BATTERY_H__ |
arils | 1:cea2f48b1aff | 4 | #define __BATTERY_H__ |
arils | 1:cea2f48b1aff | 5 | |
arils | 1:cea2f48b1aff | 6 | #include "mbed.h" |
arils | 1:cea2f48b1aff | 7 | |
arils | 1:cea2f48b1aff | 8 | class Battery { |
arils | 1:cea2f48b1aff | 9 | public: |
arils | 1:cea2f48b1aff | 10 | Battery(PinName pin) { |
arils | 1:cea2f48b1aff | 11 | uint32_t n = (uint32_t) pin; |
arils | 1:cea2f48b1aff | 12 | channel = 1 << (1 + n); |
arils | 1:cea2f48b1aff | 13 | } |
arils | 1:cea2f48b1aff | 14 | |
arils | 1:cea2f48b1aff | 15 | float read() { |
arils | 1:cea2f48b1aff | 16 | uint32_t pre_enable_register = NRF_ADC->ENABLE; |
arils | 1:cea2f48b1aff | 17 | uint32_t pre_config_register = NRF_ADC->CONFIG; |
arils | 1:cea2f48b1aff | 18 | |
arils | 1:cea2f48b1aff | 19 | |
arils | 1:cea2f48b1aff | 20 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
arils | 1:cea2f48b1aff | 21 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
arils | 1:cea2f48b1aff | 22 | (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos) | |
arils | 1:cea2f48b1aff | 23 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
arils | 1:cea2f48b1aff | 24 | (channel << ADC_CONFIG_PSEL_Pos) | |
arils | 1:cea2f48b1aff | 25 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
arils | 1:cea2f48b1aff | 26 | |
arils | 1:cea2f48b1aff | 27 | NRF_ADC->TASKS_START = 1; |
arils | 1:cea2f48b1aff | 28 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) { |
arils | 1:cea2f48b1aff | 29 | } |
arils | 1:cea2f48b1aff | 30 | |
arils | 1:cea2f48b1aff | 31 | uint16_t value = NRF_ADC->RESULT; |
arils | 1:cea2f48b1aff | 32 | |
arils | 1:cea2f48b1aff | 33 | NRF_ADC->ENABLE = pre_enable_register; |
arils | 1:cea2f48b1aff | 34 | NRF_ADC->CONFIG = pre_config_register; |
arils | 1:cea2f48b1aff | 35 | |
arils | 1:cea2f48b1aff | 36 | return (float)value * (1.0f / (float)0x3FF) * 1.2 * 12.2 / 2.2; |
arils | 1:cea2f48b1aff | 37 | } |
arils | 1:cea2f48b1aff | 38 | |
arils | 1:cea2f48b1aff | 39 | private: |
arils | 1:cea2f48b1aff | 40 | uint32_t channel; |
arils | 1:cea2f48b1aff | 41 | }; |
arils | 1:cea2f48b1aff | 42 | |
arils | 1:cea2f48b1aff | 43 | #endif // __BATTERY_H__ |