Hello, I have a problem with a temperature sensor ds18b20. How can I display the temperature on a LCD display?
#include <htc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 8000000
#endif
#include "lcd.h"
#define Skip_ROM 0xCC
#define Convert_T 0x44
#define Read_scratchpad 0xBE
#define Port_18B20 RA0
#define Tx_18B20 TRISA0 = 0
#define Rx_18B20 TRISA0 = 1
unsigned char purva;
unsigned char vtora;
unsigned char treta;
unsigned char y;
unsigned char reset();
void write(char WRT);
unsigned char read();
int main(void)
{
unsigned temp;
unsigned short tempL, tempH, fraction;
unsigned int i = 0;
lcd_init();
lcd_goto(0);
lcd_puts("DS18B20"); //LCD display for the starting part
lcd_goto(0x40);
lcd_puts("Degree= ");
while(1) // create an infinite loop
{
if(!reset())
{
write(Skip_ROM);
write(Convert_T);
__delay_ms(750);
reset();
write(Skip_ROM);
write(Read_scratchpad);
tempL = read(); //read low temp value
tempH = read(); //read high temp value
i=((unsigned int)tempH << 8 ) | (unsigned int)tempL; //put both value in one variable
y=i%100;
purva=(i-y)/100;
treta=i%10;
vtora=(y-treta)/10;
lcd_goto(0x40);
}
}
while(1) continue;
}
unsigned char reset()
{
Tx_18B20; // Tris = 0 (output)
Port_18B20 = 0; // set pin# to low (0)
__delay_us(480); // 1 wire require time delay
Rx_18B20; // Tris = 1 (input)
__delay_us(60); // 1 wire require time delay
if (Port_18B20 == 0) // if there is a presence pluse
{
__delay_us(480);
return 0; // return 0 ( 1-wire is presence)
}
else
{
__delay_us(480);
return 1; // return 1 ( 1-wire is NOT presence)
}
}
void write(char WRT)
{
char i,Cmd;
Cmd=WRT;
Rx_18B20; // set pin# to input (1)
for(i = 0; i < 8; i++)
{
if((Cmd & (1<<i))!= 0)
{
// write 1
Tx_18B20; // set pin# to output (0)
Port_18B20 = 0; // set pin# to low (0)
__delay_us(1); // 1 wire require time delay
Rx_18B20; // set pin# to input (release the bus)
__delay_us(60); // 1 wire require time delay
}
else
{
//write 0
Tx_18B20; // set pin# to output (0)
Port_18B20 = 0; // set pin# to low (0)
__delay_us(60); // 1 wire require time delay
Rx_18B20; // set pin# to input (release the bus)
}
}
}
unsigned char read()
{
char i,result = 0;
Rx_18B20; // TRIS is input(1)
for(i = 0; i < 8; i++)
{
Tx_18B20; // TRIS is output(0)
Port_18B20 = 0; // genarate low pluse for 2us
__delay_us(2);
Rx_18B20; // TRIS is input(1) release the bus
if(Port_18B20 != 0)
result |= 1<<i;
__delay_us(60); // wait for recovery time
}
return result;
}
#include <htc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 8000000
#endif
#include "lcd.h"
#define Skip_ROM 0xCC
#define Convert_T 0x44
#define Read_scratchpad 0xBE
#define Port_18B20 RA0
#define Tx_18B20 TRISA0 = 0
#define Rx_18B20 TRISA0 = 1
unsigned char purva;
unsigned char vtora;
unsigned char treta;
unsigned char y;
unsigned char reset();
void write(char WRT);
unsigned char read();
int main(void)
{
unsigned temp;
unsigned short tempL, tempH, fraction;
unsigned int i = 0;
lcd_init();
lcd_goto(0);
lcd_puts("DS18B20"); //LCD display for the starting part
lcd_goto(0x40);
lcd_puts("Degree= ");
while(1) // create an infinite loop
{
if(!reset())
{
write(Skip_ROM);
write(Convert_T);
__delay_ms(750);
reset();
write(Skip_ROM);
write(Read_scratchpad);
tempL = read(); //read low temp value
tempH = read(); //read high temp value
i=((unsigned int)tempH << 8 ) | (unsigned int)tempL; //put both value in one variable
y=i%100;
purva=(i-y)/100;
treta=i%10;
vtora=(y-treta)/10;
lcd_goto(0x40);
}
}
while(1) continue;
}
unsigned char reset()
{
Tx_18B20; // Tris = 0 (output)
Port_18B20 = 0; // set pin# to low (0)
__delay_us(480); // 1 wire require time delay
Rx_18B20; // Tris = 1 (input)
__delay_us(60); // 1 wire require time delay
if (Port_18B20 == 0) // if there is a presence pluse
{
__delay_us(480);
return 0; // return 0 ( 1-wire is presence)
}
else
{
__delay_us(480);
return 1; // return 1 ( 1-wire is NOT presence)
}
}
void write(char WRT)
{
char i,Cmd;
Cmd=WRT;
Rx_18B20; // set pin# to input (1)
for(i = 0; i < 8; i++)
{
if((Cmd & (1<<i))!= 0)
{
// write 1
Tx_18B20; // set pin# to output (0)
Port_18B20 = 0; // set pin# to low (0)
__delay_us(1); // 1 wire require time delay
Rx_18B20; // set pin# to input (release the bus)
__delay_us(60); // 1 wire require time delay
}
else
{
//write 0
Tx_18B20; // set pin# to output (0)
Port_18B20 = 0; // set pin# to low (0)
__delay_us(60); // 1 wire require time delay
Rx_18B20; // set pin# to input (release the bus)
}
}
}
unsigned char read()
{
char i,result = 0;
Rx_18B20; // TRIS is input(1)
for(i = 0; i < 8; i++)
{
Tx_18B20; // TRIS is output(0)
Port_18B20 = 0; // genarate low pluse for 2us
__delay_us(2);
Rx_18B20; // TRIS is input(1) release the bus
if(Port_18B20 != 0)
result |= 1<<i;
__delay_us(60); // wait for recovery time
}
return result;
}