Datasheet
Library Example
This example reads the temperature using DS18x20 connected to pin PORTB.2.
After reset, MCU obtains temperature from the sensor and prints it on the Lcd. Make
sure to pull-up PORTB.2 line and to turn off the PORTB leds.
program OneWire
' Lcd module connections
dim LCD_RS as sbit at PORTD.B2
LCD_EN as sbit at PORTD.B3
LCD_D4 as sbit at PORTD.B4
LCD_D5 as sbit at PORTD.B5
LCD_D6 as sbit at PORTD.B6
LCD_D7 as sbit at PORTD.B7
LCD_RS_Direction as sbit at DDRD.B2
LCD_EN_Direction as sbit at DDRD.B3
LCD_D4_Direction as sbit at DDRD.B4
LCD_D5_Direction as sbit at DDRD.B5
LCD_D6_Direction as sbit at DDRD.B6
LCD_D7_Direction as sbit at DDRD.B7
' End Lcd module connections
' OneWire pinout
dim OW_Bit_Write as sbit at PORTB.B2
OW_Bit_Read as sbit at PINB.B2
OW_Bit_Direction as sbit at DDRB.B2
' end OneWire definition
' Set TEMP_RESOLUTION to the corresponding resolution of used
DS18x20 sensor:
' 18S20: 9 (default setting can be 9,10,11,or 12)
' 18B20: 12
const TEMP_RESOLUTION as byte = 12
dim text as byte[9]
temp as word
sub procedure Display_Temperature( dim temp2write as word )
const RES_SHIFT = TEMP_RESOLUTION - 8
dim temp_whole as byte
temp_fraction as word
text = "000.0000"
' check if temperature is negative
if (temp2write and 0x8000) then
text[0] = "-"
temp2write = not temp2write + 1
end if
285
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroBasic PRO for AVR
CHAPTER 6