كود:
/*
Digital Thermometer using PIC16F877A and LM35 sensor at AN0
Vref+=2.5V at AN3 , Vref-=Vss
Eng.F.Abdelaziz
http://www.eeecb.com/vb/index.php
*/
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
// Define Messages
char message1[] = "Temperature:-";
// String array to store temperature value to display
char *tempC = "000.0";
// Variables to store temperature values
unsigned int tempinF, tempinC;
unsigned long temp_value;
void Display_Temperature() {
// convert Temp to characters
if (tempinC/10000)
tempC[0] = tempinC/10000 + 48;
else tempC[0] = ' ';
tempC[1] = (tempinC/1000)%10 + 48; // Extract tens digit
tempC[2] = (tempinC/100)%10 + 48; // Extract ones digit
// convert temp_fraction to characters
tempC[4] = (tempinC/10)%10 + 48; // Extract tens digit
// print temperature on LCD
Lcd_Out(2, 1, tempC);
}
void main() {
ADCON1 = 0b00000001; // Connect AN3 , select Vref=1.2V
TRISD = 0b00000000; // PORTD All Outputs
TRISA = 0xFF; // PORTA All Inputs
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,message1); // Write message1 in 1st row
// Print degree character
Lcd_Chr(2,6,223);
//Lcd_Chr(2,15,223);
// different LCD displays have different char code for degree
// if you see greek alpha letter try typing 178 instead of 223
Lcd_Chr(2,7,'C');
while(1) {
temp_value = ADC_Read(0);
temp_value = temp_value*244;
tempinC = temp_value/10;
Display_Temperature();
Delay_ms(1000);
}
}