كود:
/*
Digital Thermometer using PIC16F877 and LM35
Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
*/
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
// Define Messages
char message0[] = "LCD Initialized";
char message1[] = "Room Temperature";
// String array to store temperature value to display
char *tempC = "000.0";
char *tempF = "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)
// 48 is the decimal character code value for displaying 0 on LCD
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);
if (tempinF/10000)
tempF[0] = tempinF/10000 + 48;
else tempF[0] = ' ';
tempF[1] = (tempinF/1000)%10 + 48; // Extract tens digit
tempF[2] = (tempinF/100)%10 + 48;
tempF[4] = (tempinF/10)%10 + 48;
// print temperature on LCD
Lcd_Out(2, 10, tempF);
}
void main() {
ADCON0 = 0 ; // Connect AN0 to ADC module
ADCON1 = 0b00000101; // Connect AN0, select Vref=AN3 = 1.19V
TRISB = 0b00000000; // PORTC All Outputs
TRISA = 0b00001001; // PORTA All Outputs, Except RA0and RA3
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,message0);
Delay_ms(1000);
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 symbol
// if you see greek alpha letter try typing 178 instead of 223
Lcd_Chr(2,7,'C');
Lcd_Chr(2,16,'F');
do {
temp_value = ADC_Read(0);
temp_value = temp_value*1168;
tempinC = temp_value/1000;
tempinC = tempinC*10;
tempinF = 9*tempinC/5 + 3200;
Display_Temperature();
Delay_ms(1000); // Temperature sampling at 1 sec interval
} while(1);
}