SAADC library and drivers for nrf52832.
Dependents: acd52832_SAADC_Differential_input_EPD acd52832_Car_battery_ch acd52832_Car_battery_ch_2
Library to use aconno drivers for Nordic Semiconductor nrf52832 SAADC.
acd_nrf52_saadc.cpp@7:1093819c3bbf, 2017-09-28 (annotated)
- Committer:
- jurica238814
- Date:
- Thu Sep 28 14:42:00 2017 +0000
- Revision:
- 7:1093819c3bbf
- Parent:
- 6:57e342aaaca7
- Child:
- 8:b4afe08821f5
Small changes added into initialization for a differential input. Refactoring TODO!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dautor | 1:2d6e904c6843 | 1 | #include "acd_nrf52_saadc.h" |
Dautor | 1:2d6e904c6843 | 2 | |
Dautor | 1:2d6e904c6843 | 3 | // add and remove analog channels -> +/- maxcnt |
Dautor | 1:2d6e904c6843 | 4 | |
Dautor | 3:fcada8b3b567 | 5 | NRF52_SAADC::NRF52_SAADC(){ |
Dautor | 1:2d6e904c6843 | 6 | NRF_SAADC->ENABLE = 1; |
jurica238814 | 6:57e342aaaca7 | 7 | memset(data, 0, sizeof(data)); // Just to make sure data is zero |
Dautor | 3:fcada8b3b567 | 8 | NRF_SAADC->RESULT.PTR =(uint32_t)data; |
jurica238814 | 6:57e342aaaca7 | 9 | //NRF_SAADC->RESULT.MAXCNT = 0; |
Dautor | 1:2d6e904c6843 | 10 | } |
Dautor | 1:2d6e904c6843 | 11 | |
Dautor | 1:2d6e904c6843 | 12 | NRF52_SAADC::~NRF52_SAADC(){ |
Dautor | 2:1e7d6ab5b04b | 13 | NRF_SAADC->ENABLE = 0; |
Dautor | 1:2d6e904c6843 | 14 | } |
Dautor | 1:2d6e904c6843 | 15 | |
Dautor | 3:fcada8b3b567 | 16 | void NRF52_SAADC::updateData(){ |
Dautor | 1:2d6e904c6843 | 17 | NRF_SAADC->TASKS_START = 1; |
Dautor | 1:2d6e904c6843 | 18 | while(!NRF_SAADC->EVENTS_STARTED); |
Dautor | 1:2d6e904c6843 | 19 | NRF_SAADC->TASKS_SAMPLE = 1; |
jurica238814 | 6:57e342aaaca7 | 20 | for(uint8_t i = 0; i < NRF_SAADC->RESULT.MAXCNT; ++i){ |
Dautor | 4:616c6590bbd2 | 21 | while(!NRF_SAADC->EVENTS_RESULTDONE); |
Dautor | 4:616c6590bbd2 | 22 | while(!NRF_SAADC->EVENTS_DONE); |
Dautor | 4:616c6590bbd2 | 23 | while(!NRF_SAADC->EVENTS_END); |
Dautor | 4:616c6590bbd2 | 24 | while(NRF_SAADC->STATUS == 1); // while conversion is is progress |
Dautor | 4:616c6590bbd2 | 25 | } |
Dautor | 4:616c6590bbd2 | 26 | NRF_SAADC->TASKS_STOP = 1; |
Dautor | 4:616c6590bbd2 | 27 | while(!NRF_SAADC->EVENTS_STOPPED); |
Dautor | 1:2d6e904c6843 | 28 | } |
Dautor | 3:fcada8b3b567 | 29 | |
Dautor | 3:fcada8b3b567 | 30 | bool NRF52_SAADC::addChannel(uint8_t pin){ |
jurica238814 | 6:57e342aaaca7 | 31 | if(NRF_SAADC->RESULT.MAXCNT < 8){ |
Dautor | 3:fcada8b3b567 | 32 | int channel = NRF_SAADC->RESULT.MAXCNT; |
Dautor | 3:fcada8b3b567 | 33 | NRF_SAADC->CH[channel].PSELP = pin; // Input positive pin is VDD = 9 |
jurica238814 | 7:1093819c3bbf | 34 | NRF_SAADC->CH[channel].PSELN = pin+1; // Input positive pin is VDD = 9 |
jurica238814 | 7:1093819c3bbf | 35 | NRF_SAADC->CH[channel].CONFIG = 0x00125000; // differential input |
jurica238814 | 7:1093819c3bbf | 36 | //NRF_SAADC->CH[channel].CONFIG = 0x00020000; // reset |
Dautor | 3:fcada8b3b567 | 37 | NRF_SAADC->RESULT.MAXCNT++; |
Dautor | 3:fcada8b3b567 | 38 | return 0; |
Dautor | 3:fcada8b3b567 | 39 | } |
Dautor | 3:fcada8b3b567 | 40 | return 1; |
Dautor | 3:fcada8b3b567 | 41 | } |
Dautor | 3:fcada8b3b567 | 42 | |
Dautor | 3:fcada8b3b567 | 43 | void NRF52_SAADC::calibrate(){ |
Dautor | 3:fcada8b3b567 | 44 | NRF_SAADC->TASKS_CALIBRATEOFFSET = 1; |
Dautor | 3:fcada8b3b567 | 45 | while(!NRF_SAADC->EVENTS_CALIBRATEDONE); |
Dautor | 3:fcada8b3b567 | 46 | } |