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.
Diff: acd_nrf52_saadc.cpp
- Revision:
- 8:b4afe08821f5
- Parent:
- 7:1093819c3bbf
- Child:
- 9:a1eacc3f0cbb
--- a/acd_nrf52_saadc.cpp Thu Sep 28 14:42:00 2017 +0000 +++ b/acd_nrf52_saadc.cpp Thu Sep 28 16:37:24 2017 +0000 @@ -1,23 +1,59 @@ +/* + * Made by Jurica Resetar and Karlo Milicevic @ aconno, 2017 + * jurica_resetar@yahoo.com + * aconno.de + * All rights reserved + * + */ + #include "acd_nrf52_saadc.h" -// add and remove analog channels -> +/- maxcnt +uint8_t NRF52_SAADC::channelCounter = 0; +int16_t NRF52_SAADC::data[sizeof(int16_t)*NUM_OF_CHANNELS] = {0}; -NRF52_SAADC::NRF52_SAADC(){ - NRF_SAADC->ENABLE = 1; - memset(data, 0, sizeof(data)); // Just to make sure data is zero - NRF_SAADC->RESULT.PTR =(uint32_t)data; - //NRF_SAADC->RESULT.MAXCNT = 0; +NRF52_SAADC::NRF52_SAADC(uint8_t analogIn): channel(channelCounter){ + if(!channelCounter){ + // Do this for the first object only + NRF_SAADC->ENABLE = 1; + NRF_SAADC->RESULT.PTR =(uint32_t)data; // Pass pointer to results buffer + } + if(channelCounter < 8){ + NRF_SAADC->CH[channel].PSELP = analogIn; + NRF_SAADC->CH[channel].CONFIG = 0x00020000; // reset + channelCounter++; + NRF_SAADC->RESULT.MAXCNT = channelCounter; + } +} + +NRF52_SAADC::NRF52_SAADC(uint8_t pPin, uint8_t nPin): channel(channelCounter){ + if(!channelCounter){ + // Do this for the first object only + NRF_SAADC->ENABLE = 1; + NRF_SAADC->RESULT.PTR =(uint32_t)data; // Pass pointer to results buffer + } + if(channelCounter < 8){ + NRF_SAADC->CH[channel].PSELP = pPin; + NRF_SAADC->CH[channel].PSELN = nPin; + NRF_SAADC->CH[channel].CONFIG = 0x00125000; // differential input, Gain 1 + channelCounter++; + NRF_SAADC->RESULT.MAXCNT = channelCounter; + } } NRF52_SAADC::~NRF52_SAADC(){ NRF_SAADC->ENABLE = 0; } +int16_t NRF52_SAADC::read(){ + updateData(); + return data[channel]; +} + void NRF52_SAADC::updateData(){ NRF_SAADC->TASKS_START = 1; while(!NRF_SAADC->EVENTS_STARTED); NRF_SAADC->TASKS_SAMPLE = 1; - for(uint8_t i = 0; i < NRF_SAADC->RESULT.MAXCNT; ++i){ + for(uint8_t i = 0; i < channelCounter; ++i){ while(!NRF_SAADC->EVENTS_RESULTDONE); while(!NRF_SAADC->EVENTS_DONE); while(!NRF_SAADC->EVENTS_END); @@ -27,20 +63,7 @@ while(!NRF_SAADC->EVENTS_STOPPED); } -bool NRF52_SAADC::addChannel(uint8_t pin){ - if(NRF_SAADC->RESULT.MAXCNT < 8){ - int channel = NRF_SAADC->RESULT.MAXCNT; - NRF_SAADC->CH[channel].PSELP = pin; // Input positive pin is VDD = 9 - NRF_SAADC->CH[channel].PSELN = pin+1; // Input positive pin is VDD = 9 - NRF_SAADC->CH[channel].CONFIG = 0x00125000; // differential input - //NRF_SAADC->CH[channel].CONFIG = 0x00020000; // reset - NRF_SAADC->RESULT.MAXCNT++; - return 0; - } - return 1; -} - void NRF52_SAADC::calibrate(){ NRF_SAADC->TASKS_CALIBRATEOFFSET = 1; while(!NRF_SAADC->EVENTS_CALIBRATEDONE); -} \ No newline at end of file +}