
#include <c8051F200.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <intrins.h>

unsigned int q,value,value1,temp,totvalue,e;
unsigned char a,b,c,d;	

unsigned char xdata array0H[10];
unsigned char xdata array0L[10];
unsigned int xdata currentIndex;


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;				
	
float bit_value,result;

void config(void);
void ldelay(int time);
void enable();
void lcdinit (void);
void lcd_command(unsigned char m);
void print(unsigned char k);
//void hi();
void adcdata();
void voltage();
void result_dsp();


void config(void)
{
	// Configure the PRTnMX Registers	
	PRT0MX = 0x00;	// PRT0MX: Initial Reset Value
	PRT1MX = 0x00;	// PRT1MX: Initial Reset Value
	PRT2MX = 0x00;	// PRT2MX: Initial Reset Value

	// Port configuration (1 = Push Pull Output)
	PRT0CF = 0x00;	// Output configuration for P0 
	PRT1CF = 0xFF;	// Output configuration for P1 
	PRT2CF = 0xFF;// Output configuration for P2 
	PRT3CF = 0xFF; // Output configuration for P3

	P0MODE = 0x00; // Input configuration for P0
	P1MODE = 0xFF; // Input configuration for P1
	P2MODE = 0xFF; //Input configuration for P2
	P3MODE = 0xFF; // Input configuration for P3

	CPT0CN = 0x00;	// Comparator 0 Control Register
	CPT1CN = 0x00;	// Comparator 1 Control Register

	OSCXCN = 0x30;	// EXTERNAL Oscillator Control Register	
	OSCICN = 0x14;	// Internal Oscillator Control Register

	REF0CN = 0x03;	// Reference Control Register

	SPI0CN = 0x00;	// SPI Control Register
	SPI0CFG = 0x00;	// SPI Configuration Register
	SPI0CKR = 0x00;	// SPI Clock Rate Register

	SCON = 0x00;		// Serial Port Control Register
	PCON = 0x00;		// Power Control Register
/*----------------
 ADC Configuration
 -----------------*/
	AMX0SL = 0xE7;	// AMUX Channel Select Register; Analog input at P0.3 at pin 16                                                                                                                                                                                                                                                                                                                           
	ADC0CF = 0x78;	// ADC Configuration Register
	ADC0CN = 0x40;	// ADC Control Register
	
	ADC0LTH = 0x00;	// ADC Less-Than High Byte Register
	ADC0GTH = 0x00;	// ADC Greater-Than High Byte Register

/*------------------
 Timer Configuration
 -------------------*/
	CKCON = 0x00;	// Clock Control Register
	TH0 = 0x00;		// Timer 0 High Byte
	TL0 = 0x00;		// Timer 0 Low Byte
	TH1 = 0x00;		// Timer 1 High Byte
	TL1 = 0x00;		// Timer 1 Low Byte
	TMOD = 0x00;		// Timer Mode Register
	TCON = 0x00;		// Timer Control Register

	RCAP2H = 0x00;	// Timer 2 Capture Register High Byte
	RCAP2L = 0x00;	// Timer 2 Capture Register Low Byte	
	TH2 = 0x00;		// Timer 2 High Byte	
	TL2 = 0x00;		// Timer 2 Low Byte	
    T2CON = 0x00;	// Timer 2 Control Register	

	RSTSRC = 0x00;	// Reset Source Register

	IE = 0x00;		//Interrupt Enable
	IP = 0x00;		//Interrupt Priority
	EIE1 = 0x00;	//Extended Interrupt Enable 1
	EIE2 = 0x00;	//Extended Interrupt Enable 2
	EIP1 = 0x00;	//Extended Interrupt Priority 1
	EIP2 = 0x00;	//Extended Interrupt Priority 2

	// Watchdog Timer Configuration
	WDTCN=0xde;	//disable software
	WDTCN=0xad;	//watchdog timer
}



void ldelay (int time)
{
	long y;
	for (y=0; y<time;y++)
	{
	}
}

void enable()
{
	E = 1;
	ldelay(5);
	E = 0;
	ldelay(5);
}

void lcdinit(void)
{
	E=0;
	RW=0;
	RS=0;

	P3=0x38; 	// 8bit, 2 lines, default font
	enable();

	P3=0x0C;	// on display
	enable();

	P3=0x06;	// entry mode, not to shift
	enable();

	P3=0x01;	// clear dispay and cursor return
	enable();

	P3=0x02;	// cursor return
	enable();
}

void lcd_command(unsigned char m)
{
	P3 = m;
	RS = 0;
	RW = 0;
	enable();
}
void print(unsigned char k) 	
{
	P3 = k;
	RS = 1;
	RW = 0;
	enable();	
}


void voltage()
{
	unsigned char words[] = "Voltage:";
	for(q=0;q<9;q++)	//9 characters stored
	{
		print (words[q]);
	}
}


{
	
		ADCINT = 0;
		value=0;

		ADBUSY = 1;
//		ldelay(100);
		
		while(ADCINT==0);		//Is conversion complete ?

		if((currentIndex==NULL) || (currentIndex==10))
		{
		
		currentIndex =0;
		}
		array0H[currentIndex]=ADC0H;
		array0L[currentIndex]=ADC0L;
		currentIndex ++;


		value = ADC0L;
		e= ADC0H; 
		totvalue= value + (e*256);
//		ldelay(100);

//		ldelay(100);
		ADCINT=0;
		ADBUSY=0;

}

void bit_resolution()
{

	bit_value = 3.0/(4096);
	result = totvalue*bit_value;
	
}


void one()		/*convert 1st value	A.xxx */

{
	temp=result;
	temp = temp%10;
	a = temp+48;
	
}

void dec1()		/*convert 1st decimal place value	x.Bxx */
{
	temp=result*10;
	temp = temp%10;
	b = temp+48;
}

void dec2()		/*convert 2nd decimal place value	x.xCx */
{
	
	temp = result*100;
	temp = temp%10;
	c = temp+48;
}

void dec3()		/*convert 3rd decimal place value	x.xxD */

{
	temp = result*1000;
	temp = temp%10;
	d = temp+48;
}


void convert_all()
{
	one();
	dec1();
	dec2();
	dec3();
}


void displayD()		/* display 3 digit number */
{
	RS = 1;

	P3 = a;
	enable();
	
	print(0x2E); 	/*display ".", ASCII code is 2E for "."*/

	P3 = b;
	enable();
	
	P3 = c;
	enable();
	
	P3 = d;
	enable(); 
}


void result_dsp()
{
	
	bit_resolution();
	convert_all();
	displayD();
}

void main()
{
	config();

	lcdinit();		//Initialize LCD

	ADCEN = 1; 		//Enable ADC
   
   	ldelay(50);

	lcd_command(0x80);	//LCD command to display on the first line

	voltage();		//Display "Voltage="

	lcd_command(0x02);	//LCD command to return the cursor home
	lcd_command(0xC0);	//LCD command to display on the second line

   	adcdata();

	result_dsp();

	print(0x56);	//Display "V"

  	ldelay(4000);
   
}       
	 