pic16f913 and ds18b20

Thread Starter

climb123

Joined Jan 24, 2013
30
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;
}
 

ErnieM

Joined Apr 24, 2011
8,415
Zero: Learn to use code tags

One: get temperature reading

Two: Convert reading to a string

Three: Send string to LCD
 

Thread Starter

climb123

Joined Jan 24, 2013
30
y=i%100;
purva=(i-y)/100;
treta=i%10;
vtora=(y-treta)/10;

Here I convert 2 byte in a number, but i don't know how to convert to a string.
 

takao21203

Joined Apr 28, 2012
3,702
http://pic.hitechworld.org/data/hitachi_lcd.html

Have a look my LCD code. It is 4bit, but maybe it is of use to you since you can see how I handle strings. And I also have a putchar function.

All this LCD stuff is a lot of detail work, and you need to work this out for yourself. No one can know what's best for you.

Other can show you their code, but they can not do the work for you.

If you can not convert an unsigned char into a C string you need to learn more C at first.
 

spinnaker

Joined Oct 29, 2009
7,830
Begin at the beginning.First get a message to display on the LCD. Don't worry about the temperature. For example display the classic Hello World. Once you have that you can move on.
 

Thread Starter

climb123

Joined Jan 24, 2013
30
I know how to write on LCD, but a can't show the temp, when I write
y=i%100;
purva=(i-y)/100;
treta=i%10;
vtora=(y-treta)/10;
t1=(0x30+purva);
t2=(0x30+vtora);
t3=(0x30+treta);
lcd_putch("t1");
lcd_putch("t2");
lcd_putch("t3");
the lcd show ||
 

spinnaker

Joined Oct 29, 2009
7,830
Then your LCD is not working because you are literally displaying t1t2t3.

Try

lcd_putch(t1);
lcd_putch(t2);
lcd_putch(t3);
 

Thread Starter

climb123

Joined Jan 24, 2013
30
This is the code for lcd, but it show me only 000. Is anyone working with this sensor to help me with the program?

buffer[0]=i/100+'0';
buffer[1]=(i%100)/10+'0';
buffer[2]=i %10+'0';
buffer[3]=0;
lcd_init();
lcd_goto(0);
lcd_puts("DS18B20"); //LCD display for the starting part
lcd_goto(0x40);
lcd_puts(buffer);
 

t06afre

Joined May 11, 2009
5,934
Does the LCD display the "ds18b20" string correct. Also you have been told to use the itoa() function. Please look in the Hi-Tech C install folder. Here you will find a folder named docs. And inside here you will find a nice manual. Look up the itoa() function. And no more nonsense please
 

t06afre

Joined May 11, 2009
5,934
First show how you use the itoa function. Second then we ask question in order to help you. It is rude to just ignore those questions. So once more. Can you confirm that this line do as it should.
Rich (BB code):
lcd_puts("DS18B20"); //LCD display for the starting part
 

Thread Starter

climb123

Joined Jan 24, 2013
30
Sorry. Yes lcd_puts("DS18B20") displayed correctly.
This is the code for itoa
Rich (BB code):
int main(void)
{	
		
	unsigned temp;
	unsigned short tempL, tempH, fraction;
	unsigned int i = 0 ;	
	unsigned char buffer [20];
	lcd_init();
	lcd_goto(0);
	lcd_puts("DS18B20");  //LCD display for the starting part
	lcd_goto(0x40);
	itoa(buffer,i,20);
	lcd_puts(buffer);
	__delay_ms(5000);


	while(1) 	// create an infinite loop
	{		
			if(!reset())
			{
			write(Skip_ROM);		
			write(Convert_T);		
			__delay_ms(1000);
			
			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

			lcd_goto(0x40);
 

t06afre

Joined May 11, 2009
5,934
Hi try this
Rich (BB code):
itoa(buffer,12345,10);
The prototype fro the itoa function is
Rich (BB code):
char * itoa (char * buf, int val, int base)
You got the two first correct. But the int base. Shall be set to the number system you want. Use 10 for decimal number, and 16 for hexadecimal numbers. I think 8 or 2 will give you octal or binary numbers. If the test line work. You can suspect something is wrong with the reading of the temperature sensor. Will you be able to use the hardware debugger in MPLAB?
 

tshuck

Joined Oct 18, 2012
3,534
I have pickit3 on sure electronics, but when i try to connect to MPLAB can't find my device, so i can't use debuger
Try hard-coding your output to be displayed to the LCD. Instead of reading the sensor, give the part of your code that formats your data for output something like 1234, that way, you can work on just getting your LCD formatting right.
 
Top