//************************ Functions and variables declerations.******************
#include <reg51.h> //Pre-processor commands.
#include <absacc.h>
unsigned char mess[] = "Temp in :";
unsigned char mess2[] ="Temp select :";
unsigned char x; //Create character x.
unsigned char ADC(); //Create ADC.
unsigned char ADC1(); // Create ADC1
unsigned char temp; //Create character value.
unsigned char temp1;
#define PPIA 0x4000 //Port A.
#define PPIB 0x4001 //Port B.
#define PPIC 0x4002 //Port C not being used.
#define PPICNTRL 0x4003 //8255 Control Register.
sfr ADCLK=0xF2; //Clock control register.
sfr ADCON=0xF3; //Control register.
sfr ADDL=0xF4; //Data low byte.
sfr ADDH=0xF5; //Data high byte.
sfr ADCF=0xF6; //Configuration register, defines which ADC.
sbit LED = P2^0 ;
void TLCD(unsigned char value);
void LCD_String(unsigned char*string1); //create function for lcd string.
void LCD_init(); //create function for lcd initialization.
void LCD_data(); //create function for lcd data.
void Delay(unsigned int mstime); //Prototype Functions
void MSDelay(unsigned int mstime); //Prototype Functions used in between Characters.
//************************ LCD *********************************************************
void LCD_init() //LCD initialization function.
{ //
XBYTE[PPIA] = 0x38; //Initialize LCD 16x2.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Start small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Start delay.
//
XBYTE[PPIA] = 0x0E; //DIsplay on cursor off.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Small small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Start delay.
//
XBYTE[PPIA] = 0x01; //Clear Display.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Start small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Small delay.
//
XBYTE[PPIA] = 0x80; //Line 1.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Start small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Start delay.
}
void LCD_init2() //LCD initialization function.
{ //
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Start small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Start delay.
//
XBYTE[PPIA] = 0x0E; //DIsplay on cursor off.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Small small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Start delay.
//
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Start small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Small delay.
//
XBYTE[PPIA] = 0xc0; //Line 2.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //Start small delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //Start delay.
}
void LCD_data(unsigned char value) //LCD data function.
{ //
XBYTE[PPIA] = value; //Send value to LCD.
XBYTE[PPIB] = 0x05; //EN = 1 , RS = 1 , RW = 0.
MSDelay(150); //Start small delay.
XBYTE[PPIB] = 0x01; //EN = 0 , RS = 1 , RW = 0.
MSDelay(150); //Start delay.
}
void LCD_String(unsigned char*string1) //String function.
{ //
while(*string1) //Loop here until the string is all send to the
{ //data of the LCD. This will display the meaasge
LCD_data (*string1); //that is called with the string.
MSDelay(250);
string1++; //Increment string.
} //
} //
void MSDelay(unsigned int mstime) //milliseconds delay.
{ //
unsigned int i,j; //nested loops.
for(i=0;i<mstime;i++) //outer loop.
for(j=0;j<100;j++); //inner loop.
} //
//************************ ADC Actual temp********************************************
unsigned char ADC() //Analog to Digital Conversion Function.
{ //
ADCON=ADCON & 0xF8; //Select CH ADC.
ADCON = ADCON|0x08; //Mask.
while((ADCON&0x10)!=0x10); //Wait ADEOC=1 bit4.
ADCON=ADCON & 0xEF; //Clear ADEOC flag=0.
return(ADDH); //Return parameter/value to main.
} //
void TLCD(unsigned char value) //Alg function.
{ //
unsigned char x; //character x.
unsigned char dec1,dec2,dec3; //character for decimal.
unsigned char units, tens, hundreds; //character for tens and units and hundreds.
//
x=value/10; //divide value by 10.
dec1=value%10; //place value for decimal 1.
dec2=x%10; //
dec3=x/10; //
units=dec1|0x30; //OR units.
tens=dec2|0x30; //OR tens.
hundreds=dec3|0x30; //OR hundreds.
LCD_data(hundreds); //Send data to LCD.
Delay(100); //Small delay.
LCD_data(tens); //Send data to LCD.
Delay(100); //Small delay.
LCD_data(units); //Send data to LCD.
Delay(100); //Small delay.
} //
void Delay(unsigned int mstime) //milliseconds delay.
{ //
unsigned int i,j; //nested loops.
for(i=0;i<mstime;i++) //outer loop.
for(j=0;j<100;j++); //inner loop.
} //
//********************** MAIN PROG **************************************
void main(void) //Main Function Block.
{
XBYTE[PPICNTRL]=0x80; //Control word ALL Ports o/p.
LCD_init(); //Initialize LCD.
LCD_String(mess); //Send string message.
LCD_init2(); //Initialize LCD.
LCD_String(mess2);
while(1) //Loop here.
{ //
ADCF=0x01; //P1.0
ADCON=0x20; //Enable ADC
ADCLK=0x01; //Time=1.33us.
MSDelay(150);
XBYTE[PPIA] = 0xFE,0x32,0x00,0x0B; //Line 2.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //delay.
temp=ADC(); //Get Value from ADC.
TLCD(temp); //Convert value to be writen on LCD.
ADCF=0x02; //P1.1
ADCON=0x20; //Enable ADC
ADCLK=0x01; //Time=1.33us.
MSDelay(150);
XBYTE[PPIA] = 0xFE,0x32,0x01,0x0B; //Line 2.
MSDelay(100);
XBYTE[PPIB] = 0x04; //EN = 1 , RS = 0 , RW = 0.
MSDelay(100); //delay.
XBYTE[PPIB] = 0x00; //EN = 0 , RS = 0 , RW = 0.
MSDelay(100); //delay.
temp1=ADC1(); //Get Value from ADC.
TLCD(temp1); //Convert value to be writen on LCD //
if ((ADC1 == ADC))
{
LED= 1; // switch on p2.1
}
else if((ADC1) != (ADC))
{
LED= 0; // switch on p2.1
}
}
}
//************************ ADC Actual temp********************************************
unsigned char ADC1() //Analog to Digital Conversion Function.
{ //
ADCON=ADCON & 0xF8; //Select CH ADC.
ADCON = ADCON|0x08; //Mask.
while((ADCON&0x10)!=0x10); //Wait ADEOC=1 bit4.
ADCON=ADCON & 0xEF; //Clear ADEOC flag=0.
return(ADDH); //Return parameter/value to main.
} //
void TLCD1(unsigned char value) //Alg function.
{ //
unsigned char x; //character x.
unsigned char dec1,dec2,dec3; //character for decimal.
unsigned char units, tens, hundreds; //character for tens and units and hundreds.
//
x=value/10; //divide value by 10.
dec1=value%10; //place value for decimal 1.
dec2=x%10; //
dec3=x/10; //
units=dec1|0x30; //OR units.
tens=dec2|0x30; //OR tens.
hundreds=dec3|0x30; //OR hundreds.
LCD_data(hundreds); //Send data to LCD.
Delay(100); //Small delay.
LCD_data(tens); //Send data to LCD.
Delay(100); //Small delay.
LCD_data(units); //Send data to LCD.
Delay(100); //Small delay.
} //
void Delay1(unsigned int mstime) //milliseconds delay.
{ //
unsigned int i,j; //nested loops.
for(i=0;i<mstime;i++) //outer loop.
for(j=0;j<100;j++); //inner loop.
}