![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Attempting to create an open source, very simple (basically bluetooth notification light to start) smartwatch around the nRF51822.
Dependencies: BLE_API mbed nRF51822
The main branch of code for the simpleWatch project
battery.h@0:3e9f9b3ed7c8, 2016-05-11 (annotated)
- Committer:
- rjpope42
- Date:
- Wed May 11 18:29:00 2016 +0000
- Revision:
- 0:3e9f9b3ed7c8
Committing a PWM attempt as version 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rjpope42 | 0:3e9f9b3ed7c8 | 1 | |
rjpope42 | 0:3e9f9b3ed7c8 | 2 | |
rjpope42 | 0:3e9f9b3ed7c8 | 3 | #ifndef __BATTERY_H__ |
rjpope42 | 0:3e9f9b3ed7c8 | 4 | #define __BATTERY_H__ |
rjpope42 | 0:3e9f9b3ed7c8 | 5 | |
rjpope42 | 0:3e9f9b3ed7c8 | 6 | #include "mbed.h" |
rjpope42 | 0:3e9f9b3ed7c8 | 7 | |
rjpope42 | 0:3e9f9b3ed7c8 | 8 | class Battery { |
rjpope42 | 0:3e9f9b3ed7c8 | 9 | public: |
rjpope42 | 0:3e9f9b3ed7c8 | 10 | Battery(PinName pin) { |
rjpope42 | 0:3e9f9b3ed7c8 | 11 | uint32_t n = (uint32_t) pin; |
rjpope42 | 0:3e9f9b3ed7c8 | 12 | channel = 1 << (1 + n); |
rjpope42 | 0:3e9f9b3ed7c8 | 13 | } |
rjpope42 | 0:3e9f9b3ed7c8 | 14 | |
rjpope42 | 0:3e9f9b3ed7c8 | 15 | float read() { |
rjpope42 | 0:3e9f9b3ed7c8 | 16 | uint32_t pre_enable_register = NRF_ADC->ENABLE; |
rjpope42 | 0:3e9f9b3ed7c8 | 17 | uint32_t pre_config_register = NRF_ADC->CONFIG; |
rjpope42 | 0:3e9f9b3ed7c8 | 18 | |
rjpope42 | 0:3e9f9b3ed7c8 | 19 | |
rjpope42 | 0:3e9f9b3ed7c8 | 20 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
rjpope42 | 0:3e9f9b3ed7c8 | 21 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
rjpope42 | 0:3e9f9b3ed7c8 | 22 | (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos) | |
rjpope42 | 0:3e9f9b3ed7c8 | 23 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
rjpope42 | 0:3e9f9b3ed7c8 | 24 | (channel << ADC_CONFIG_PSEL_Pos) | |
rjpope42 | 0:3e9f9b3ed7c8 | 25 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
rjpope42 | 0:3e9f9b3ed7c8 | 26 | |
rjpope42 | 0:3e9f9b3ed7c8 | 27 | NRF_ADC->TASKS_START = 1; |
rjpope42 | 0:3e9f9b3ed7c8 | 28 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) { |
rjpope42 | 0:3e9f9b3ed7c8 | 29 | } |
rjpope42 | 0:3e9f9b3ed7c8 | 30 | |
rjpope42 | 0:3e9f9b3ed7c8 | 31 | uint16_t value = NRF_ADC->RESULT; |
rjpope42 | 0:3e9f9b3ed7c8 | 32 | |
rjpope42 | 0:3e9f9b3ed7c8 | 33 | NRF_ADC->ENABLE = pre_enable_register; |
rjpope42 | 0:3e9f9b3ed7c8 | 34 | NRF_ADC->CONFIG = pre_config_register; |
rjpope42 | 0:3e9f9b3ed7c8 | 35 | |
rjpope42 | 0:3e9f9b3ed7c8 | 36 | return (float)value * (1.0f / (float)0x3FF) * 1.2 * 12.2 / 2.2; |
rjpope42 | 0:3e9f9b3ed7c8 | 37 | } |
rjpope42 | 0:3e9f9b3ed7c8 | 38 | |
rjpope42 | 0:3e9f9b3ed7c8 | 39 | private: |
rjpope42 | 0:3e9f9b3ed7c8 | 40 | uint32_t channel; |
rjpope42 | 0:3e9f9b3ed7c8 | 41 | }; |
rjpope42 | 0:3e9f9b3ed7c8 | 42 | |
rjpope42 | 0:3e9f9b3ed7c8 | 43 | #endif // __BATTERY_H__ |