#include "lpc21xx.h"
#define Fosc 12000000
#define Fcclk (Fosc*5)
#define Fcco (Fcclk*4)
#define Fpclk (Fcclk/4)
#define UART_BPS 9600 //SET BAUD RATE
//function prototype
void DECtoASCII(unsigned int data);
void Delay(unsigned char Ticks);
void init_UART0(void);
void UART0_Sendbyte(unsigned char data);
void UART0_Sendstr(unsigned char *str);
void ADC_init(void);
unsigned char ADC_Conversion(unsigned char channel);
////////////////
unsigned int ADC_Data1=0;
unsigned int ADC_Data2=0;
unsigned char TEMP[5];
double data;
///////////////
void DECtoASCII(unsigned int data)
{
signed int i,local[5]={0,0,0,0,0};
for(i=0;i<4;i++)
{ local[i]=data%10;
data= data/10;
}
for(i=4;i>=0;i--)
{
UART0_Sendbyte(local[i]+48);
}
}
void Delay(unsigned char Ticks)
{
unsigned int i;
if(Ticks==0)
{
Ticks=1;
}
for(;Ticks>0;Ticks--)
{
for(i=0;i<60000;i++);
}
}
void init_UART0(void)
{
unsigned int Baud16;
U0LCR=0X83;
Baud16=(Fpclk/16)/9600;
U0DLM=Baud16/256;
U0DLL=Baud16%256;
U0LCR=0X03;
}
void UART0_Sendbyte(unsigned char data)
{
U0THR=data;
while((U0LSR&0X40)==0);
}
void UART0_Sendstr(unsigned char *str)
{
while(*str)
{
UART0_Sendbyte(*str++);
}
}
void ADC_init(void)
{
//PINSEL0=0X01000000; //AD0.1 PIN SELECTED
ADCR&=0X00000000;
ADCR|=0X00000E00; //CLKDIV=PCLK/(ADCCLK-1) =15/1000000-1 ADCCLK=E
ADCR&=0XFFF1FFFF; //NO BURST IE BURST=0; SOFTWARE CONTROLLED
ADCR&=0XFF3FFFFF; //TEST 1:0 00 NORMAL MODE
ADCR&=0XF7FFFFFF; //EDGE =0 CONVERSION ON FALLING EDGE
ADCR|=0X00200000; //PDN=1 ACTIVE ADC MODULE
}
unsigned char ADC_Conversion(unsigned char channel)
{
unsigned int temp=0;
ADCR=((ADCR&0XFFFFFF00)|channel);
ADCR|=(1<<24);
while((ADDR&0X80000000)==0);//wait for the conversion to complete
//temp=ADDR;
temp=(unsigned int)(ADDR>>6)&0x03ff;
return temp;
}
int main()
{ unsigned int val;
PINSEL0=0X05 ;// enable UART0 RX AND TX PINS
PINSEL1=0X05; //ENABLE AD0.1 AND AD0.2
PINSEL2=0;
VPBDIV=0X01;//15 MHZ
init_UART0();
ADC_init();
while(1)
{
UART0_Sendstr("ADC");
val=ADC_Conversion(1);
DECtoASCII(val);
}
}