sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char Message1[] = "VOLT";
unsigned int ADC_Value, DisplayVolt;

char *volt = "00.0";
const unsigned short TEMP_RESOLUTION = 12;
char *text = " 00.0";
unsigned temp;
bit oldstate;

void Display_Temperature(unsigned int temp2write) {
    const unsigned short RES_SHIFT = 4;
    char temp_whole;
    unsigned int temp_fraction;

    // check if temperature is negative
    if (temp2write & 0x8000) {
        text[0] = '-';
        temp2write = ~temp2write + 1;
    }
    // extract temp_whole
    temp_whole = temp2write >> RES_SHIFT ;

    // convert temp_whole to characters
    if (temp_whole/100)
        text[0] = temp_whole/100 + 48;
    else
        text[0] = ' ';

    text[1] = (temp_whole/10)%10 + 48; // Extract tens digit
    text[2] = temp_whole%10 + 48;      // Extract ones digit

    // extract temp_fraction and convert it to unsigned int
    temp_fraction = temp2write << (4-RES_SHIFT);
    temp_fraction &= 0x000F;
    temp_fraction *= 625;

    // convert temp_fraction to characters
    text[4] = temp_fraction/1000 + 48;     // Extract thousands digit

    // Display temperature on LCD
    Lcd_Out(2, 10, text);
}

void main() {
 INTCON = 0;                      // All interrupts disabled
 ANSEL = 0x04;                    // Pin RA2 is configured as an analog input
 TRISA = 0x04;
 ANSELH = 0;                      // Rest of pins are configured as digital
 ADCON1 = 0x82;                   // A/D voltage reference is VCC
 TRISA = 0xFF;                    // All port A pins are configured as inputs
 C1ON_bit = 0; // Disable comparators
 C2ON_bit = 0;
 TRISD0_bit = 1;                                // set RD0 pin as input
 oldstate = 0;

 Lcd_Init();        // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR);      // CLEAR display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1,1,Message1);
 Lcd_Out(1,13,"TEMP");
 Lcd_Chr(2,5,'V');
    // Print degree character, 'C' for Centigrades
    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,16,'C');
do {
  //Voltage meter
  ADC_Value = ADC_Read(2);
  DisplayVolt = ADC_Value * 2;
  volt[0] = DisplayVolt/1000 + 48;
  volt[1] = (DisplayVolt/100)%10 + 48;
  volt[3] = (DisplayVolt/10)%10 + 48;
  Lcd_Out(2,1,volt);
  delay_ms(500);   // Hold for 500 ms
          //--- perform temperature reading
        Ow_Reset(&PORTE, 2);       // Onewire reset signal
        Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
        Ow_Write(&PORTE, 2, 0x44); // Issue command CONVERT_T
        Delay_ms(750);
        Ow_Reset(&PORTE, 2);
        Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
        Ow_Write(&PORTE, 2, 0xBE); // Issue command READ_SCRATCHPAD
        temp = Ow_Read(&PORTE, 2);
        temp = (Ow_Read(&PORTE, 2) << 8) + temp;

        //--- Format and display result on Lcd
        Display_Temperature(temp);
        Delay_ms(100);
        ///////////////////////////////////////////////////////
   if (Button(&PORTD, 0, 1, 1)) {               // Detect logical one
      oldstate = 1;                              // Update flag
    }
    if (oldstate && Button(&PORTD, 0, 1, 0)) {   // Detect one-to-zero transition
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,4,"ERROR");
      oldstate = 0;                              // Update flag
    }
     if (Button(&PORTD, 0, 1, 1)) {               // Detect logical one
      Lcd_Cmd(_LCD_CLEAR);
      oldstate = 1;                              // Update flag
    }
 } while(1);
} // End main()