pic16f913 and ds18b20

Thread Starter

climb123

Joined Jan 24, 2013
30
If this can wait till I get back from Orlando, Florida next week, I can post an example of one wire and the Dallas temperature sensors. I need to drag that project out anyway.
I can wait until the end of next week.
I change delay time for write(Convert_T); to 750ms, and start to show me A.
 

t06afre

Joined May 11, 2009
5,934
I can wait until the end of next week.
I change delay time for write(Convert_T); to 750ms, and start to show me A.
Do you think you could zip down you project and post this with your schematic? In the toolbar select project->Package in zip. And you will get a zip file with all the project files
 

t06afre

Joined May 11, 2009
5,934
This is from the datasheet
Five of the pins of PORTA can be configured as analog
inputs. These pins, RA5 and RA<3:0>, are configured
as analog inputs on device power-up and must be
reconfigured by the user to be used as I/O’s. This is
done by writing the appropriate values to the CMCON0​
and ANSEL registers (see Example 3-1).
I searched you code. But I can not find any writing to ANSEL neither CMCON0. I told you the importance of setting ANSEL and CMCON0. But you so far ignored this. That make me kind of sad.
 

Thread Starter

climb123

Joined Jan 24, 2013
30
Did you mean that:
Rich (BB code):
#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)
{	

	 ANS0=0	;
//ANSEL=0;
	unsigned temp;
	unsigned short tempL, tempH, fraction;
	 int i=0  ;	
	 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(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
			lcd_goto(0x40);	
			itoa(buffer,i,10);
			lcd_puts(buffer);
			__delay_ms(2000);	
			}
	}	
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;
}
 
Last edited:

Thread Starter

climb123

Joined Jan 24, 2013
30
I can't simulate, I show on the LCD tempL, and i write for tempH=10, and the display show me 000 0000 0101 0000
Rich (BB code):
tempL = read();			//read low temp value
	tempH = read();			//read high temp value
	tempH = 10;
i=((unsigned int)tempH << 8 ) | (unsigned int)tempL;		//put both value in one variable
		
    for (int idx = 0; idx < 16; idx++)
				 {
				  	display[idx] = (i & (1<<idx))?'1':'0';
				 }	
				display[16]='\0';
				lcd_goto(0x40);
				lcd_puts(display);
 

thatoneguy

Joined Feb 19, 2009
6,359
Can you put the sensor on a port B input, or disable comparators?

In simulation, does the simulator show a signal output on the 1 wire pin?
 

Thread Starter

climb123

Joined Jan 24, 2013
30
I use PORTB for data to LCD. I try to disable comparators. I make changes on real board not in simulation
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Here is demo code for the DS18S20. My pic is the p18f14k22. You will need to modify the code a bit. Specifically configuration.

Be sure to check config.h and lcd.h for the port bits and delay.h for your OSC speed you will be using for your project.

I also have some code for multiple one wire devices but I have yet to put together a demo.
 

Attachments

Last edited:
Top