User manual
409
mikoPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
Library Example
This example reads the temperature using DS18x20 connected to pin RF6. After reset, MCU obtains temperature from
the sensor and prints it on the Lcd. Be sure to set Fosc appropriately in your project, to pull-up RF6 line and to turn off
the PORTF leds.
Copy Code To Clipboard
program OneWire;
// LCD module connections
var LCD_RS : sbit at LATB4_bit;
var LCD_EN : sbit at LATB6_bit;
var LCD_D4 : sbit at LATD4_bit;
var LCD_D5 : sbit at LATD5_bit;
var LCD_D6 : sbit at LATD6_bit;
var LCD_D7 : sbit at LATD7_bit;
var LCD_RS_Direction : sbit at TRISB4_bit;
var LCD_EN_Direction : sbit at TRISB6_bit;
var LCD_D4_Direction : sbit at TRISD4_bit;
var LCD_D5_Direction : sbit at TRISD5_bit;
var LCD_D6_Direction : sbit at TRISD6_bit;
var LCD_D7_Direction : sbit at TRISD7_bit;
// End LCD module connections
// 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 : byte = 9;
var text : array[9] of char;
temp : word;
procedure Display_Temperature( temp2write : word );
const RES_SHIFT = TEMP_RESOLUTION - 8;
var temp_whole : byte;
temp_fraction : word;
begin
text := ‘000.0000’;
// Check if temperature is negative
if (temp2write and 0x8000) then
begin
text[0] := ‘-’;
temp2write := not temp2write + 1;
end;
// Extract temp_whole
temp_whole := word(temp2write shr RES_SHIFT);
// Convert temp_whole to characters
if ( temp_whole div 100 ) then
text[0] := temp_whole div 100 + 48