pic16f913 and ds18b20

ErnieM

Joined Apr 24, 2011
8,377
I do note that the first call to itoa() right inside main() you pass i=0, and if it displays zero then it is correct.
 

Thread Starter

climb123

Joined Jan 24, 2013
30
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.
Could you explain, what did you mean.
 

tshuck

Joined Oct 18, 2012
3,534
Could you explain, what did you mean.
Don't try reading the sensor. Try displaying a number you put into your code that won't change and work your logic out that way.
i.e.
Rich (BB code):
buffer[0] = '1';
buffer[1] = '2';
buffer[2] = '3';
buffer[3] = '4';
 

tshuck

Joined Oct 18, 2012
3,534
I try this, show me 1234
I'm assuming this is supposed to be in the past tense:confused:

This means you have a problem with either your sensor, or your storage of the data...try reading a know analog value(i.e.read the voltage off of a voltage divider), if that works, you have a problem with your sensor...
 

t06afre

Joined May 11, 2009
5,934
If you have a PICKIT 3 you should also be able to debug. And also so I am 100% sure. Then you code something like this
Rich (BB code):
itoa(buffer,12345,10);
           lcd_puts(buffer);
    	__delay_ms(5000);
Do you get the correct number in the display. From what I can see I think your problem is from reading the data out of the sensor. Perhaps you should post that part of the code also. What is your source for this part of the code
 

Thread Starter

climb123

Joined Jan 24, 2013
30
Yes I get the correct number in the display. This is the whole part of code, without the init of LCD

Rich (BB code):
#include <htc.h>
#include <stdlib.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 reset();
void write(char WRT);
unsigned char read();

int main(void)
{	
		
	unsigned temp;
	unsigned short tempL, tempH, fraction;
	 int i=0  ;	
	unsigned char buffer [20];
	lcd_init();
	lcd_goto(0);
	lcd_puts("DS18B20");  //LCD display for the starting part
	while(1) 	// create an infinite loop
	{		
			if(!reset())
			{
			write(Skip_ROM);		
			write(Convert_T);		
			__delay_ms(500);
			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);	
			itoa(buffer,i,10);
			lcd_puts(buffer);
			__delay_ms(5000);
			}	

	}	
while(1) continue;
}


unsigned char reset()
{
	Tx_18B20; // Tris = 0 (output)
	Port_18B20 = 0; // set pin# to low (0)
	__delay_us(550); // 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(550);
		return 0; // return 0 ( 1-wire is presence)
		} 
		else 
		{
		__delay_us(550);
		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(10); // 1 wire require time delay
			Rx_18B20; // set pin# to input (release the bus)
			__delay_us(80); // 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(80); // 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(10);
		Rx_18B20; // TRIS is input(1) release the bus
		if(Port_18B20 != 0) 
		result |= 1<<i;
		__delay_us(80); // wait for recovery time
		}
	return result;
}
 

tshuck

Joined Oct 18, 2012
3,534
try changing the read() to return a known value... your one-wire interface may be having issues...
i.e.
Rich (BB code):
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(10);
		Rx_18B20; // TRIS is input(1) release the bus
		if(Port_18B20 != 0) 
		result |= 1<<i;
		__delay_us(80); // wait for recovery time
		}
	//return result;
        return 12;
}
 

t06afre

Joined May 11, 2009
5,934
I think your problems are a quite common error. After a reset a PIC will by default set pins as analog input. If such is an option. Check what the datasheet say about the the ANSEL and CMCON0 register. The ANSEL register should cleared but not sure then it comes to CMCON0. Confer with the datasheet
 

tshuck

Joined Oct 18, 2012
3,534
LCD shows me 3084
that's what you should have gotten if it's working.
(12 << 8) | 12 ) = 3084

Your problem lies in your interface/sensor.... Try double checking your timings for the 1-wire interface are met and that the transmitting device is using the same protocol...
 

tshuck

Joined Oct 18, 2012
3,534
I think your problems are a quite common error. After a reset a PIC will by default set pins as analog input. If such is an option. Check what the datasheet say about the the ANSEL and CMCON0 register. The ANSEL register should cleared but not sure then it comes to CMCON0. Confer with the datasheet
This should be taken into account in the LCD_init(), but it is worth looking in to...
 
Top