Fast AnalogIn module which asks for a single non blocking reading and causes and interrupt when done.

NbAnalogIn.cpp

Committer:
dontknowhow
Date:
2017-03-27
Revision:
0:058d32b78e5d
Child:
1:2666729acca1

File content as of revision 0:058d32b78e5d:

#include "NbAnalogIn.h"

NbAnalogIn::NbAnalogIn(){
    
    NVIC_EnableIRQ(ADC_IRQn);
    
    LPC_SC->PCONP |= (1 << 12); // turn power for ADC on
    
    // set peripheral clock registers to provide clock for ADC (bits 24 & 25)
    // set to 00 for PCLK = CCLK
    LPC_SC->PCLKSEL0 &= ~(0x3 << 24); // clear bits
    LPC_SC->PCLKSEL0 |= (0x1 << 24);  // set bits
    
    //set P1.30 as AD0.4
    LPC_PINCON->PINSEL3 |= (3<<28);
    

    //Set PINMODE of AD0.4 as no pull-up/down resistors
    LPC_PINCON->PINMODE3 &= ~(3<<28);
    LPC_PINCON->PINMODE3 |=  (2<<28);
    
    
    /* set the A/D control register */
    
    // select 4, clkdiv = 7, enable, don't start yet
    LPC_ADC->ADCR  = (1 << 4) | (7 << 8) | (1 << 21) | (0 << 24) ;
    
    printf("constructed \n");
    
}


unsigned int NbAnalogIn::read() {
    
    /* disable interrupts */
    LPC_ADC->ADINTEN &= ~(1 << 4);
    LPC_ADC->ADINTEN = 0;
    
    LPC_ADC->ADCR  |= (1 << 24) ; // start conversion
    
    while((LPC_ADC->ADSTAT & (1<<4)) == 0);
    
    return (unsigned int)((LPC_ADC->ADGDR >> 4) & 0xFFF);
}

void NbAnalogIn::readNb(){
    /* enable interrupts */
    LPC_ADC->ADINTEN = (1 << 4);
    
    LPC_ADC->ADCR  |= (1 << 24) ; // start conversion
}