
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051F200.h>
#include <stdlib.h>
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
	unsigned int q;

	sbit RS = P2^5; 	// RS (Register Select) for Port 2.5
	sbit RW = P2^6; 	// RW for Port 2.6
	sbit E = P2^7; 		// Enable for Port 2.7

	sbit DB0 = P3^0;	//DB = Data bus 
	sbit DB1 = P3^1;
	sbit DB2 = P3^2;
	sbit DB3 = P3^3;
	sbit DB4 = P3^4;
	sbit DB5 = P3^5;
	sbit DB6 = P3^6;
	sbit DB7 = P3^7;

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void config(void);
void delay (unsigned int duration);
void enable();
void lcdinit (void);
void command(unsigned char m);
void print(unsigned char k);
void hi();
void adcdata(void);

//------------------------------------------------------------------------------------
// Generating Delay
//------------------------------------------------------------------------------------
void delay (unsigned int duration)
{
   while (duration--!=0);
}

//------------------------------------------------------------------------------------
// Enable
//------------------------------------------------------------------------------------
void enable()
{
	E = 1;
	delay(150);
	E = 0;
	delay(150);
}
//------------------------------------------------------------------------------------
// Initialize LCD
//------------------------------------------------------------------------------------
void lcdinit(void)
{
	E=0;
	RW=0;
	RS=0;
	P3=0x38; 	// 8bit, 2 lines, default font
	enable();
	P3=0x0C;	// on dispaly
	enable();
	P3=0x06;	// entry mode, not to shift
	enable();
	P3=0x01;	// clear dispay and cursor return
	enable();
	P3=0x02;	// cursor return
	enable();
}

//------------------------------------------------------------------------------------
// Command Character to LCD 
//------------------------------------------------------------------------------------
void command(unsigned char m)
{
	P3 = m;
	RS = 0;
	RW = 0;
	enable();
}

//------------------------------------------------------------------------------------
// Print Character to LCD
//------------------------------------------------------------------------------------
void print(unsigned char k) 	
{
	P3 = k;
	RS = 1;
	RW = 0;
	enable();	
}

void adcdata(void)
{
	unsigned int adc_sum = 0; unsigned int adc_count = 0;
	
	for(adc_count = 0;adc_count < 16;adc_count++)
	{
		ADCINT = 0;
		ADBUSY = 1;

		while(!ADCINT);		//conversion complete ?

		adc_sum += ADC0H;	//build an average
	}

	adc_sum >>= 4;			//	/16 => 8 BIT result
	
	print(adc_sum);		
	
	
}

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main()
{
  config();

  lcdinit();		//Initialize LCD
  command(0x80);

   ADCEN = 1; 		//enable ADC
   
   while(1)			//endless loop
   {
   	delay(500000);
   	adcdata();
   	delay(500000);
   }
   

}       
