This blog post explains how to interface adc with cortex m4 development board
Procedure to interface ADC with Cortex M4
- Power: In the PCONP register, set the PCADC bit
- Configure pins P0.23 (CH0), P0.24 (CH1) and P0.25 (CH2) as analog pins using their respective IOCON registers i.e. IOCON_P0_23, IOCON_P0_24 and IOCON_P0_25.
- Select ADC conversion Clock rate (400 KHz-12bit conversion) using ADC control register.
- Enable Power Down (Normal CPU mode)
- Select the Channel and Start conversion
- Wait for the DONE bit. If the DONE bit is HIGH, the conversion is completed
- Read the data register and store them in a variable
- Do the Steps 5 to 7 for the remaining 2 channels.
- Write these data’s in HyperTerminal via UART0
C code to Interface ADC with LPC4088 Cortex M4
#include <LPC407x_8x_177x_8x.h>
#include "uart0.h"
#include <stdlib.h>
unsigned int CH0;
unsigned char Str[5];
#define ADC_MAX_VAL 4095
#define ADC_MAX_VOLT 3.3
#define VAL_PER_VOLT (ADC_MAX_VAL /ADC_MAX_VOLT )
void init_adc(void);
unsigned int ADC_Getdata(unsigned char channel);
void delay_ms(long ms);
void itoa(unsigned int val, unsigned char *str);
int main(void)
{
init_adc();
init_serial();
while(1)
{
CH0=ADC_Getdata(0); //Get data from channel0, calculate temp value
CH0=((float)CH0/VAL_PER_VOLT)*100;
itoa(CH0,Str); //Convert the val to string
UART0_puts((unsigned char *)"Temperature:"); //Step 9-Send the string to UART0
UART0_puts(Str); //Space
UART0_Txmt('\r'); //Carriage return, Enter Key
delay_ms(500);
}
}
void init_adc(void)
{
//1. set the PCADC bit
LPC_SC->PCONP |= (1 << 12); /* enable power to ADC*/
//2. Configure pins P0.23, P0.24 and P0.25 as ADC input pins
LPC_IOCON->P0_23 = 1; /* Pin P0.23 used as ADC0, IN0 */
LPC_IOCON->P0_24 = 1; /* Pin P0.24 used as ADC0, IN1 */
LPC_IOCON->P0_25 = 1; /* Pin P0.25 used as ADC0, IN2 */
//3. ADC conversion rate = 400Khz
//30MHz/12.4MHz = 2.4-1 = 1 (8 to 15)
//4. Enable PDN bit (21 bit)
LPC_ADC->CR = 0;
LPC_ADC->CR |= ((1<<21) | (1<<8));
}
unsigned int ADC_Getdata(unsigned char channel)
{
//5.Select the Channel and Start conversion
LPC_ADC->CR |= (1<<24) | (1<<channel);
//6. Wait for the DONE bit (31). If the DONE bit is HIGH, the conversion is completed
while((LPC_ADC->DR[channel] & 0x80000000)==0);
//Deselect the channel and Stop Conversion
LPC_ADC->CR &= ~((1<<24) | (1<<channel));
//7. Read the data
return ((LPC_ADC->DR[channel]>>4)&0xFFF);
}
void delay_ms(long ms) // delay 1 ms per count @ CCLK 120 MHz
{
long i,j;
for (i = 0; i < ms; i++ )
for (j = 0; j < 26659; j++ );
}
//Int to string (ascii)
void itoa(unsigned int val, unsigned char *str)
{
//Separate the single digit and add 0x30 (where the ascii '0' starts
str[3] = val%10+'0';
//Get the remainder
val = val/10;
//Separate the single digit of remainder ie 10th digit of val
str[2] = val%10+'0';
//Get the remainder
val = val/10;
//Separate the single digit of remainder ie 100th digit of val
str[1] = val%10+'0';
//Get the remainder, its the 1000th digit
str[0] = val/10+'0';
//End the string with NULL character
str[4] = '\0';
}