Measure temperature using DS1820 sensor. Use ‘1-wire’ protocol...
Temperature measurement is one of the most common operations performed by the microcontroller. A DS1820 temperature sensor is used here for measuring. It is capable of measuring temperature within the range of -55 °C to 125 °C with a 0.5 °C accuracy. To transfer data to the microcontroller, a special type of serial communication called 1-wire is used. Due to their simple and wide application, such sensors are run and controlled by functions stored in the One_Wire library. This library contains three functions in total:' Header****************************************************** program example_12 ' Program name dim LCD_RS as sbit at RB4_bit ' Lcd module connections LCD_EN as sbit at RB5_bit LCD_D4 as sbit at RB0_bit LCD_D5 as sbit at RB1_bit LCD_D6 as sbit at RB2_bit LCD_D7 as sbit at RB3_bit LCD_RS_Direction as sbit at TRISB4_bit LCD_EN_Direction as sbit at TRISB5_bit LCD_D4_Direction as sbit at TRISB0_bit LCD_D5_Direction as sbit at TRISB1_bit LCD_D6_Direction as sbit at TRISB2_bit LCD_D7_Direction as sbit at TRISB3_bit ' End Lcd module connections ' Set TEMP_RESOLUTION to the corresponding resolution of the DS18x20 sensor in use: ' 18S20: 9 (default setting can be 9,10,11 or 12); 18B20: 12 const TEMP_RESOLUTION as byte = 9 ' Constant TEMP_RESOLUTION is of byte type dim text as char[9] ' Variable text is of char type temp as word ' Variable temp is of word type sub procedure Display_Temperature( dim temp2write as word ) const RES_SHIFT = TEMP_RESOLUTION - 8 dim temp_whole as byte ' Variable temp_whole is of byte type temp_fraction as word ' Variable temp_fraction is of word type text = "000.0000" if (temp2write and 0x8000) then ' Check if temperature is negative text[0] = "-" temp2write = not temp2write + 1 end if temp_whole = word(temp2write >> RES_SHIFT) ' Extract temp_whole if ( temp_whole div 100 ) then ' Convert temp_whole to characters text[0] = temp_whole div 100 + 48 else text[0] = "0" end if text[1] = (temp_whole div 10) mod 10 + 48 ' Extract tens text[2] = temp_whole mod 10 + 48 ' Extract ones temp_fraction = word(temp2write << (4-RES_SHIFT)) ' Extract temp_fraction temp_fraction = temp_fraction and 0x000F ' and convert it to temp_fraction = temp_fraction * 625 ' unsigned int text[4] = word(temp_fraction div 1000) + 48 ' Extract thousands text[5] = word((temp_fraction div 100) mod 10 + 48) ' Extract hundreds text[6] = word((temp_fraction div 10) mod 10 + 48) ' Extract tens text[7] = word(temp_fraction mod 10) + 48 ' Extract ones Lcd_Out(2, 5, text) ' Print temperature on Lcd end sub main: ' Start of program ANSEL = 0 ' Configure analog pins as digital I/O ANSELH = 0 text = "000.0000" Lcd_Init() ' Initialize Lcd Lcd_Cmd(_LCD_CLEAR) ' Clear Lcd Lcd_Cmd(_LCD_CURSOR_OFF) ' Turn off cursor Lcd_Out(1, 1, " Temperature: ") Lcd_Chr(2,13,178) ' Print degree character, "C" for Centigrades ' Different LCD displays have different char code for degree Lcd_Chr(2,14,"C") ' If you see greek letter ‘alpha’ type 178 instead of 223 while 1 ' Temperature is read in the main loop 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_us(120) 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 Display_Temperature(temp) ' Format and display result on Lcd Delay_ms(520) ' 520 mS delay wend end. ' End of programIn order to make this example work properly, it is necessary to check the following libraries in the Library Manager prior to compiling: