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:
Dautor
Date:
2017-08-23
Revision:
4:616c6590bbd2
Parent:
3:fcada8b3b567
Child:
6:57e342aaaca7

File content as of revision 4:616c6590bbd2:

#include "acd_nrf52_saadc.h"

// add and remove analog channels -> +/- maxcnt

NRF52_SAADC::NRF52_SAADC(){
    NRF_SAADC->ENABLE = 1;
    memset(data, 0, sizeof(data));
    NRF_SAADC->RESULT.PTR =(uint32_t)data;
    NRF_SAADC->RESULT.MAXCNT = 0;
}

NRF52_SAADC::~NRF52_SAADC(){
    NRF_SAADC->ENABLE = 0;
}

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)
    {
        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);
}

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].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);
}