Digital Temperature Meter Using PIC16F688
Digital Temperature Meter Using PIC16F688
void Display_Temperature() {
// convert Temp to characters
if (!C_Neg) {
if (tempinC/1000)
// 48 is the decimal character code value for displaying 0 on LCD
tempC[0] = tempinC/1000 + 48;
else tempC[0] = ' ';
}
tempC[1] = (tempinC/100)%10 + 48; // Extract tens digit
tempC[2] = (tempinC/10)%10 + 48; // Extract ones digit
if (!F_Neg) {
if (tempinF/1000)
tempF[0] = tempinF/1000 + 48;
else tempF[0] = ' ';
}
tempF[1] = (tempinF/100)%10 + 48; // Extract tens digit
tempF[2] = (tempinF/10)%10 + 48;
tempF[4] = tempinF%10 + 48;
// print temperature on LCD
Lcd_Out(2, 10, tempF);
}
void main() {
TRISC = 0x00 ;
TRISA = 0b00001100; // RA2, RA3 Inputs, Rest O/P's
ANSEL = 0b00000000;
PORTA = 0b00000000; // Start with Everything Low
PORTC = 0b00000000; // Start with Everything Low
CMCON0 = 0b00000111;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
BackLight = 1;
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
// if you see greek alpha letter try typing 178 instead of 223
Lcd_Chr(2,7,'C');
Lcd_Chr(2,16,'F');
// Interrupt Setup
OPTION_REG = 0x00; // Clear INTEDG, External Interrupt on falling edge
INTCON.INTF = 0; // Clear interrupt flag prior to enable
INTCON.INTE = 1; // enable INT interrupt
INTCON.GIE = 1; // enable Global interrupts
do {
//--- perform temperature reading
Ow_Reset(&PORTA, 5); // Onewire reset signal
Ow_Write(&PORTA, 5, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 5, 0x44); // Issue command CONVERT_T
INTCON.GIE = 1; // 1-wire library disables interrpts
Delay_ms(600);
Ow_Reset(&PORTA, 5);
Ow_Write(&PORTA, 5, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 5, 0xBE); // Issue command READ_SCRATCHPAD
if(C_Neg) {
tempinF = 320-9*tempinC/5;
if (tempinF < 0) {
F_Neg = 1;
tempF[0] = '-';
tempinF = abs(tempinF);
}
else F_Neg = 0;
}
else tempinF = 9*tempinC/5 + 320;
//--- Format and display result on Lcd
Display_Temperature();
} while(1);
}