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