lpcware

How to read 6 analog inputs through ADC continuously and print them to the console screen

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Getdan35 on Thu Nov 14 06:28:19 MST 2013
/*
===============================================================================
Name        : main.c
Author      : $(author)
Version     :
Copyright   : $(copyright)
Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <stdio.h>

/* A/D Converter 0 (ADC0) */
#define AD0_BASE_ADDR  0x40034000
#define AD0CR          (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x00))
#define AD0GDR         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x04))
#define AD0INTEN       (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x0C))
#define AD0DR0         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x10))
#define AD0DR1         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x14))
#define AD0DR2         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x18))
#define AD0DR3         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x1C))
#define AD0DR4         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x20))
#define AD0DR5         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x24))
#define AD0DR6         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x28))
#define AD0DR7         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x2C))
#define AD0STAT        (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x30))
#define ADTRIM         (*(volatile unsigned long *)(AD0_BASE_ADDR + 0x34))

int ADC_Val1, ADC_Val2;
double v1,v2;

// TODO: insert other definitions and declarations here
void initADC()
{
LPC_SC->PCONP |= (1<<12); //Turn on ADC power and clock
LPC_PINCON->PINSEL1 |= (1<<14); //P0.23 is connected to AD0.0
LPC_PINCON->PINSEL1 |= (1<<16); //P0.24 is connected to AD0.1
LPC_PINCON->PINSEL1 |= (1<<18); //P0.25 is connected to AD0.2
LPC_PINCON->PINSEL1 |= (1<<20); //P0.26 is connected to AD0.3
LPC_PINCON->PINSEL3 |= (3<<28); //P1.30 is connected to AD0.4
LPC_PINCON->PINSEL3 |= (3<<30); //P1.31 is connected to AD0.5
LPC_PINCON->PINSEL0 |= (2<<6);  //P0.3 is connected to AD0.6
LPC_PINCON->PINSEL0 |= (2<<4);  //P0.2 is connected to AD0.7

AD0CR |= (1<<0); // select operating mode
AD0CR |= (1<<21);// set to 1 so converter is operational

}
int readADC()
{
AD0CR |= (1<<24);//START CONVERSION
while((AD0CR & (1<<31)==0));//WAIT FOR END OF CONVERSION
return ((AD0GDR>>4)&0xfff);//SHIFT TO RIGHT 4 BITS GLOBAL REGISTER
}


int main(void) {

    // TODO: insert code here
printf("ADC TEST");
initADC();
    volatile static int i = 0 ; // Force the counter to be placed into memory


    while(1)
{
ADC_Val1=readADC();
v1 = (3.13/4096)*ADC_Val1;
printf("ADC0: %.2lf\n", v1);
}
    return 0 ;
}
Can read one input ok but not sure how to go about reading 6 at same time?

Please help

Outcomes