/*
===============================================================================
Name : main.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/
#include "LPC17xx.h"
#include <cr_section_macros.h>
#define ADC_OVERRUN 0x40000000
#define ADC_DONE 0x80000000
#define ADC_Clk1000000/* set to 1Mhz */
volatile uint16_t adspl[50];
int main(void)
{
volatile static int i = 0;
volatile uint32_t regVal;
volatile uint32_t pclkdiv,pclk;
// AD initialize (AD0.0 1MHz polling read)
LPC_SC->PCONP |= (1 << 12);
LPC_PINCON->PINSEL1 &= ~0x0000C000; // P0.23 A0.0 function 01
LPC_PINCON->PINSEL1 |= 0x00004000;
LPC_PINCON->PINMODE1 &= ~0x0000C000; // No pull-up no pull-down (function 10)
LPC_PINCON->PINMODE1 |= 0x00008000;
pclkdiv = (LPC_SC->PCLKSEL0 >> 24) & 0x03;
switch ( pclkdiv ) {
case 0x00:
default:
pclk = SystemCoreClock/4;
break;
case 0x01:
pclk = SystemCoreClock;
break;
case 0x02:
pclk = SystemCoreClock/2;
break;
case 0x03:
pclk = SystemCoreClock/8;
break;
}
LPC_ADC->ADCR = ( 0x01 << 0 ) | ( ( pclk / ADC_Clk - 1 ) << 8 ) |
( 0 << 16 ) | ( 0 << 17 ) | ( 1 << 21 ) | ( 0 << 24 ) | ( 0 << 27 );
// for(i=0; i<50; i++) //simple ADC reading
// {
// LPC_ADC->ADCR |= (1 << 24); // start adc
// do
// {
// regVal = LPC_ADC->ADDR0; //check done bit
// }while(!(regVal & ADC_DONE));//wait until Done is set
// if ( regVal & ADC_OVERRUN ){adspl = 0x1000;}//overrun
// else{adspl = ( regVal >> 4 ) & 0xFFF;}
// }
for(i=0 ; i<50; i++) {
LPC_ADC->ADCR &= 0xFFFFFF00;
LPC_ADC->ADCR |= (1 << 24) | (1 << 0); // start adc
for(;;){
regVal = LPC_ADC->ADDR0; // read result of A/D conversion
if ( regVal & ADC_DONE )
break;
}
//LPC_ADC->ADCR &= 0xF8FFFFFF; // stop ADC now
if ( regVal & ADC_OVERRUN ) {
adspl = 0x1000;
continue;
}
adspl = ( regVal >> 4 ) & 0xFFF;
}
while(1)
{
i++ ;
}
return 0 ;
}
|