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

Committer:
jurica238814
Date:
2017-10-04
Revision:
10:b763d9cfc063
Parent:
9:a1eacc3f0cbb

File content as of revision 10:b763d9cfc063:

/*
 * Made by Jurica Resetar and Karlo Milicevic @ aconno, 2017
 * jurica_resetar@yahoo.com
 * aconno.de  
 * All rights reserved 
 *
 */
  
#include "acd_nrf52_saadc.h"

uint8_t NRF52_SAADC::channelCounter = 0;
int16_t NRF52_SAADC::data[sizeof(int16_t)*NUM_OF_CHANNELS] = {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
        NRF_SAADC->RESOLUTION = 3;
        calibrate();
    }
    if(channelCounter < 8){
        NRF_SAADC->CH[channel].PSELP = analogIn + 1;     
        NRF_SAADC->CH[channel].CONFIG = 0x00021000; // 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
        NRF_SAADC->RESOLUTION = 3;
        calibrate();
    }
    if(channelCounter < 8){
        NRF_SAADC->CH[channel].PSELP = pPin + 1;     
        NRF_SAADC->CH[channel].PSELN = nPin + 1;     
        NRF_SAADC->CH[channel].CONFIG = 0x00121200; // 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 < channelCounter; ++i){
        while(!NRF_SAADC->EVENTS_RESULTDONE);
        while(!NRF_SAADC->EVENTS_DONE);
        while(!NRF_SAADC->EVENTS_END);
        while(NRF_SAADC->STATUS == 1); // while conversion is is progress
    }
    NRF_SAADC->TASKS_STOP = 1;
    while(!NRF_SAADC->EVENTS_STOPPED);
}

void NRF52_SAADC::calibrate(){
    NRF_SAADC->TASKS_CALIBRATEOFFSET = 1;
    while(!NRF_SAADC->EVENTS_CALIBRATEDONE);
}