#include "LPC214x.h"

#define ADC_CLK		1000000		/* set to 1Mhz */

#include "serial.h"
#include "rprintf.h"
#include "target.h"

void delay_ms(int x);

int main(void)
{
	init_serial0(19200);
	rprintf_devopen(putc_serial0);
	rprintf("Hello!\n\n");

	PINSEL0 = 0x10000000;

	AD0CR = ( 0x01 << 0 ) | 	
	( 1 << 3 ) | //Turn analog 3 on
	( ( Fpclk / ADC_CLK - 1 ) << 8 ) |
	( 0 << 16 ) | 		// BURST = 0, no BURST, software controlled
	( 0 << 17 ) |  		// CLKS = 000, 11 clocks/10 bits 
	( 0 << 18 ) |		//^
	( 0 << 19 ) |		//^
	( 1 << 21 ) |  		// PDN = 1, normal operation 
	( 0 << 22 ) |  		// TEST1:0 = 00 
	( 0 << 24 );  		// START = 0 A/D conversion stops

	while(1) {
		AD0CR |= (1 << 24); //Start Conversion
		delay_ms(1); //Give it time to convert(I doubt this is necessary?)
		rprintf("%luH", ((AD0DR3 >> 6) & 0x000003ff) * 0x40);
		AD0CR &= (1 << 24);  //Stop Conversion
		delay_ms(50);
	}
}	

void delay_ms(int x)
//Rough, not meant to be accurate
{
    int a,b;
    for(a=0;a<x;a++){
        for(b=0;b<3000;b++);
    }
}
