Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DS18B20 with PIC18f452 source code problem

Status
Not open for further replies.

Jellie7r

New Member
i am connecting a DS18B20 to a PIC 18452. i have written a code in C for it. The DQ line is connected to the RA1 pin of the PIC18f452.

The code looks fine i think ( i had reference from the MAXIM site) but i can't seem to define my DQ as my RA1 pin. Dunno what went wrong. i am then displaying the temperature result on the Hyper Terminal of Windows through my USART using a MAX232 ic.

If anyone can help me solve the bug i would be really Greatful. Thanks.
 

Attachments

  • ds18b20_with_pic18_in_c.c
    7 KB · Views: 868
You wouldn't write a 1 to a pin using the 1-wire protocol. This is a mistake. It is an open collector design and you should have an external pullup resistor or the port B internal pullups (if the electric specs match, I would avoid it personally).

Then using the pin is a matter of:
LATA1=0; //only do this once

TRISA1=1; //this puts a 1 on the bus

(delay)...

TRISA1=0; //puts a 0 on the bus

Better yet, if you're not using A4 for an external secondary oscillator xtal I strongly recommend using that pin. It's an open collector pin, so this is just perfect. You need only assign the TRIS reg. Then assigning LATA1=0 makes the PIC pull it down, LATA1=1 makes it float and it will be pulled up by the external resistor (or pulled down by the slave's output).
 
Thanks
i had connected the external pull up resistor...
if i replace my DQ with TRISA1, would it work? is this what u meant?


say refering to the code below...

LATA1=0; //only define this one once at start that's it?

void delay(int useconds)
{
int s;
for (s=0; s<useconds;s++);
}

//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-24)/16 = 28.5 - we use 29. Presence checked
// another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
//*/
unsigned char ow_reset(void)
{
unsigned char presence; TRISA1 = 0; //pull DQ line low
delay(29); // leave it low for 480us
TRISA1 = 1; // allow line to return high
delay(3); // wait for presence
presence = TRISA1; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned

} // 0=presence, 1 = no part




and if i were to use A4 pin, then do i assign LATA1 instead of DQ and TRIS? sorry i am quite vague bout the lower part of your post...

Thanks
 
A4 is electrically different than other port pins in regards to output. It can pull the pin to ground when you write a 0 to the reg like any other pin. However it has no transistor to pull it up when you write a 1 to the reg. It is essentially tristated even though TRISA4 may be 0.

Be sure that for all other pins on PortA you may do, any time you set just one bit you assign the LAT, not the port itself. LATA0=1 not RA0=1. The reason being is that the RA0=0 means the hardware will read the current value of all PortA, even those defined as inputs, modifies, and writes it back to the port. So A1 may still be an input but may get a "1" written to it. That means when you change it to an output again, it will output a 1 instead of the 0 you wrote to it at the top of the code. The safer thing is to assign LATA1=0 before every time you assign TRISA1=0, but it's still not universally safe since ISRs can happen between the two instructions which write to another RAx pin.
 
thanks again. can i just reconfirm...

that means if i were to use any other A pin besides A4, the codes will be like beneath...

//OW_RESET (using pin 1)

unsigned char ow_reset(void)
{
unsigned char presence;
LATA1 = 0 //do it everytime before TRISA1=0
TRISA1 = 0; //pull DQ line low
delay(29); // leave it low for 480us
TRISA1 = 1; // allow line to return high
delay(3); // wait for presence
presence = TRISA1; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned

} // 0=presence, 1 = no part



and if i were to use A4,

//OW_RESET

unsigned char ow_reset(void)
{
unsigned char presence;
LATA4 = 0; //pull DQ line low
delay(29); // leave it low for 480us
LATA4 = 1; // allow line to return high
delay(3); // wait for presence
presence = LATA4; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned

} // 0=presence, 1 = no part



is the above or bottom code correct? if wrong what are the mistakes. thanks again.
 
Jellie7r said:
Top:
presence = TRISA1; // get presence signal

Bottom:
presence = LATA4; // get presence signal

These two lines are in error. To read A1 you would use
presence = RA1;

For the bottom you would use:
presence = RA4;

The LATA4 value is the output latch- which is what you last wrote to it before turning the pin into an input, rather than the logical value of the input pin which you needed. Thus you would always use the registers- RA4, PORTA- for reading inputs.
 
i have done according to what you suggest but when i compile i keep getting the error symbol LATA1/TRISA1 not defined, need lvalue.how do i get rid of it? i have attached the updated file here anyway.
thanks.
 

Attachments

  • ds18b20_with_pic_in_c_3.c
    5.2 KB · Views: 588
Hmm, not sure why you'd get that.
I don't build exactly that way. Instead of doing:
#include <p18F452.h>

I always do
#include <pic18.h>

and in HiTide select the chip with Project->Global Options->Compiler. I'm not sure if that's related to the issue though.

I sort of recall that assigning a single bit of a TRIS reg used to be unsupported. I could be wrong though. In any case it works for sure in newer versions.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top