Hello all,
I am trying to read temperature from DS18B20 sensor using the OneWire protocol (non parasitic) and a PIC18F4550 (Aptinex development board with a built-in DS18B20). I've read both OneWire and DS18B20 datasheets, then I tried to look up some example codes just to understand how it all works, but the more I tried to understand the main function part, the more confused I got. I think I understand how reset, write and read functions work (clear explanations in the datasheet), but the part that confuses me the most is how to actually read data from the sensor. Below is a code that I found somewhere on the net, and after I build it, the result is in the pic. Also, after 3 seconds, the screen goes blank. The LCD module is 100% working. I really do not understand what is going on this part:
tempL = read();
tempH = read();
tempL >>= 4;
tempH <<= 4;
tempH += tempL;
tempL = DEC2BCD(tempH);
Could someone please give any insights what is going on in that part and why the outcome is like that. Any help will be greatly appreciated!


I am trying to read temperature from DS18B20 sensor using the OneWire protocol (non parasitic) and a PIC18F4550 (Aptinex development board with a built-in DS18B20). I've read both OneWire and DS18B20 datasheets, then I tried to look up some example codes just to understand how it all works, but the more I tried to understand the main function part, the more confused I got. I think I understand how reset, write and read functions work (clear explanations in the datasheet), but the part that confuses me the most is how to actually read data from the sensor. Below is a code that I found somewhere on the net, and after I build it, the result is in the pic. Also, after 3 seconds, the screen goes blank. The LCD module is 100% working. I really do not understand what is going on this part:
tempL = read();
tempH = read();
tempL >>= 4;
tempH <<= 4;
tempH += tempL;
tempL = DEC2BCD(tempH);
Could someone please give any insights what is going on in that part and why the outcome is like that. Any help will be greatly appreciated!
Code:
#define _XTAL_FREQ 8000000
#include <xc.h>
#include <stdio.h>
#include "delay.h"
#include "LCD_4bit_config.h"
#define _XTAL_FREQ 8000000
#define DEC2BCD(dec) (((dec / 10) << 4) + (dec % 10))
#define Skip_ROM 0xCC // 0b11001100
#define Convert_temperature 0x44 // 0b01000100
#define Read_scratchpad 0xBE // 0b10111110
#define Write_scratchpad 0x4E
#define Data_pin LATB0
unsigned short tempL, tempH;
char reset()
{
TRISB0 = 0; // Drive bus low
__delay_us(480); // Delay 480us
TRISB0 = 1; // Release bus
__delay_us(70); // Delay 70us
if (Data_pin == 0) // Sample bus
{
__delay_us(410);
return 0; // Device present
}
else
{
__delay_us(410);
return 1; // No device present
}
}
write(char a)
{
char i;
Data_pin = 1;
for (i=0;i<8;i++)
{
if ((a & (1<<i)) != 0 )
{
TRISB0 = 0; // Write 1 bit
Data_pin = 0;
__delay_us(6);
TRISB0 = 1;
__delay_us(64);
}
else
{
TRISB0 = 0; // Write 0 bit
Data_pin = 0;
__delay_us(60);
TRISB0 = 1;
__delay_us(10);
}
}
}
char read()
{
char i, result = 0;
TRISB0 = 1; //RB0 as input
for (i=0;i<8;i++)
{
TRISB0 = 0; // Drive bus low
Data_pin = 0; // Generate low pulse for 2us
__delay_us(6);
TRISB0 = 1; // Release the bus
__delay_us(9);
if (Data_pin != 0) // Sample bus to read bit from slave
{
result |= 1<<i;
__delay_us(55);
}
return result;
}
}
main()
{
char s[20];
OSCCON = 0b11110100; // Set internal oscillator to 8 MHz
ADCON1 = 0x0F; // Set all pins as digital I/O
CMCON = 0x07; // Set all comparators as digital I/O
TRISD = 0x00; // Set PORTD as digital OUTPUT for the LCD
LATD = 0; // Clear PORTD
TRISB = 0xFF; // Set PORTB as digital INPUT
LATB = 0; // Clear PORTB
LCD_init(); // Initialize LCD module
LCD_clr();
while (1)
{
if (!reset())
{
write(Skip_ROM);
write(Convert_temperature);
delay_ms(750);
reset();
write(Skip_ROM);
write(Read_scratchpad);
tempL = read();
tempH = read();
tempL >>= 4;
tempH <<= 4;
tempH += tempL;
tempL = DEC2BCD(tempH);
sprintf(s,"%d %d",tempL,tempH);
LCD_setCursor(1,1);
LCD_writeString(s);
}
}
}

