Home PICPIC Code PIC16F877 and DS1820 temperature sensor example

PIC16F877 and DS1820 temperature sensor example

In this example we connect the ever popular DS18B20 temperature sensor to our PIC, we will then display the temperature on our LCD

We used the DS18S20 in our example this was connected to PORT E pin 2 of our PIC16F877, once again we had a 16×2 LCD connected to PORT B.

Schematic

PIC16F877 and DS1820 example

PIC16F877 and DS1820 example

Code


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;

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12
unsigned short TEMP_RESOLUTION = 9;
unsigned temp;
char *text = "000.00";
int i;
int colona;

void Read_Temperature()
{
// 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_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;
}

void Display_Temperature()
{
unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp2write;
unsigned int temp_fraction;

temp2write = temp;
// 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;

text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
text[2] =  temp_whole%10     + 48;             // Extract ones digit
text[3] = '.';
// 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
// Print temperature on LCD
Lcd_Out(1, 1, text);
// Print degree character and'C' for Celsius
Lcd_Chr_CP(0xDF);                             // 223 ASCII for degree symbol on my LCD
Lcd_Chr_CP('C');
}

void main()
{
CMCON |=7;
ADCON1 = 0x0D;
TRISE.B2 = 1;                                  // Configure RE2 pin as input

Lcd_Init();                                    // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off

// Main loop
do
{
Read_Temperature();
Display_Temperature();
Delay_ms(100);
} while (1);
}

Links
DS1820 20pcs

DS1820 Temperature Module

You may also like