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

Committer:
dontknowhow
Date:
Mon Mar 27 11:35:25 2017 +0000
Revision:
0:058d32b78e5d
Child:
1:2666729acca1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dontknowhow 0:058d32b78e5d 1 #include "NbAnalogIn.h"
dontknowhow 0:058d32b78e5d 2
dontknowhow 0:058d32b78e5d 3 NbAnalogIn::NbAnalogIn(){
dontknowhow 0:058d32b78e5d 4
dontknowhow 0:058d32b78e5d 5 NVIC_EnableIRQ(ADC_IRQn);
dontknowhow 0:058d32b78e5d 6
dontknowhow 0:058d32b78e5d 7 LPC_SC->PCONP |= (1 << 12); // turn power for ADC on
dontknowhow 0:058d32b78e5d 8
dontknowhow 0:058d32b78e5d 9 // set peripheral clock registers to provide clock for ADC (bits 24 & 25)
dontknowhow 0:058d32b78e5d 10 // set to 00 for PCLK = CCLK
dontknowhow 0:058d32b78e5d 11 LPC_SC->PCLKSEL0 &= ~(0x3 << 24); // clear bits
dontknowhow 0:058d32b78e5d 12 LPC_SC->PCLKSEL0 |= (0x1 << 24); // set bits
dontknowhow 0:058d32b78e5d 13
dontknowhow 0:058d32b78e5d 14 //set P1.30 as AD0.4
dontknowhow 0:058d32b78e5d 15 LPC_PINCON->PINSEL3 |= (3<<28);
dontknowhow 0:058d32b78e5d 16
dontknowhow 0:058d32b78e5d 17
dontknowhow 0:058d32b78e5d 18 //Set PINMODE of AD0.4 as no pull-up/down resistors
dontknowhow 0:058d32b78e5d 19 LPC_PINCON->PINMODE3 &= ~(3<<28);
dontknowhow 0:058d32b78e5d 20 LPC_PINCON->PINMODE3 |= (2<<28);
dontknowhow 0:058d32b78e5d 21
dontknowhow 0:058d32b78e5d 22
dontknowhow 0:058d32b78e5d 23 /* set the A/D control register */
dontknowhow 0:058d32b78e5d 24
dontknowhow 0:058d32b78e5d 25 // select 4, clkdiv = 7, enable, don't start yet
dontknowhow 0:058d32b78e5d 26 LPC_ADC->ADCR = (1 << 4) | (7 << 8) | (1 << 21) | (0 << 24) ;
dontknowhow 0:058d32b78e5d 27
dontknowhow 0:058d32b78e5d 28 printf("constructed \n");
dontknowhow 0:058d32b78e5d 29
dontknowhow 0:058d32b78e5d 30 }
dontknowhow 0:058d32b78e5d 31
dontknowhow 0:058d32b78e5d 32
dontknowhow 0:058d32b78e5d 33 unsigned int NbAnalogIn::read() {
dontknowhow 0:058d32b78e5d 34
dontknowhow 0:058d32b78e5d 35 /* disable interrupts */
dontknowhow 0:058d32b78e5d 36 LPC_ADC->ADINTEN &= ~(1 << 4);
dontknowhow 0:058d32b78e5d 37 LPC_ADC->ADINTEN = 0;
dontknowhow 0:058d32b78e5d 38
dontknowhow 0:058d32b78e5d 39 LPC_ADC->ADCR |= (1 << 24) ; // start conversion
dontknowhow 0:058d32b78e5d 40
dontknowhow 0:058d32b78e5d 41 while((LPC_ADC->ADSTAT & (1<<4)) == 0);
dontknowhow 0:058d32b78e5d 42
dontknowhow 0:058d32b78e5d 43 return (unsigned int)((LPC_ADC->ADGDR >> 4) & 0xFFF);
dontknowhow 0:058d32b78e5d 44 }
dontknowhow 0:058d32b78e5d 45
dontknowhow 0:058d32b78e5d 46 void NbAnalogIn::readNb(){
dontknowhow 0:058d32b78e5d 47 /* enable interrupts */
dontknowhow 0:058d32b78e5d 48 LPC_ADC->ADINTEN = (1 << 4);
dontknowhow 0:058d32b78e5d 49
dontknowhow 0:058d32b78e5d 50 LPC_ADC->ADCR |= (1 << 24) ; // start conversion
dontknowhow 0:058d32b78e5d 51 }