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.

Using a DS18B20 with a 16F684

Status
Not open for further replies.

Hawk1943

New Member
Hi everyone...

I'm struggling to get a section of code up and running and was therefore wondering if anyone else has anyone on here has any experiance using the DS18B20...

Only I'm trying to communicate with it using a 16F684 but at the moment I feel the Maxim Example Code that I have used from Application Note 162 to be slightly incorrect...

I've attached the code I have so far and the Maxim Application Note 162 that I am referring to along with the DS18B20 Data Sheet...

At the moment I am seeing the output signals on the Scope and the timmings at the moment appear to be correct, but i'm unsure about the read_temperature function that I have... as I do not appear to be sending out a 0x44 to start the temperature conversion... but the datasheet does not seem to mention it in the example code...

I have also attached the schematic circuit that I am currently using...

Only the time scale that I have to complete this side of things is rapidly coming to an end... I only have 28 days left to get it receiving the correct temperature...
 

Attachments

  • TempSensor.c
    7.2 KB · Views: 203
  • AN162.pdf
    130.6 KB · Views: 318
  • DS18B20.pdf
    219 KB · Views: 332
  • data logger - ver 1.0.1.pdf
    196.8 KB · Views: 442
There is no need to drive the data line high and you should set the data line low before setting to output. See this code,
Code:
unsigned char reset (void)
{
	unsigned char present;
	[COLOR="red"]DQ = 0;		// Pull DQ line Low   Swapped
	TRISC &= ~0x01;	// Set port to output Swapped[/COLOR]
	time_delay (40); 	// Leave low for 500uS
	TRISC |= 0x01;	// Set port to input
	[COLOR="red"]//DQ = 1; 	// Not Needed[/COLOR]
	[COLOR="red"]time_delay (10); 	// Wait 120uS [/COLOR]for present signal
	present = DQ; 	// Get present signal
	[COLOR="red"]time_delay (30); 	// Wait 360uS [/COLOR]for end of time slot
	return (present); 	// Present signal returned
}

Also, you're not completing time slots. The reset slot should be 480 low, 120 floating (pulled high) followed by a 360 delay to complete it. Same with your other time slots, you're not completing them.

Your read bit is, well, wrong, you don't even set the port to output to start the time slot. Try,
Code:
unsigned char read_bit (void)
{
    unsigned char i;
    DQ = 0;             // pull DQ line low to start time slot
    TRISC &= ~0x01;     // Set port to output
    __delay_us(1);      // Wait required time
    TRISC |= 0x01;      // Set port to input
    __delay_us(15);     // Wait for response
    i=DQ;               //read bit	
    __delay_us(45);     //complete slot
    return(i);          //return value
}
Your write bit needs to be similarly changed.
Code:
void write_bit (char b)
{
    unsigned char i;
    DQ = 0;             // pull DQ line low to start time slot
    TRISC &= ~0x01;     // Set port to output
    if (b==1)           //if were writing a 1 then - also acts as 1uS delay
        TRISC |= 1;     // return DQ high
    delay_us(60);       // Wait 60uS for end of time slot
    TRISC |= 1;         // return DQ high
}
Note, I used the built in delays. To use them correctly you need to add this line to the top of your code,
Code:
#define _XTAL_FREQ 8000000
I'm assuming you are using the internal 8MHz clock.

Give it a go and if you're still having problems post back.

Mike.
 
Last edited:
Thanks for that Pommie...

This is what my main code looks like...

Which is all working fine...

Would adding: -

#define _XTAL_FREQ 8000000

Cause me any problems with the other timming sequences that I have at the moment fully working?

I'm currently adding your changes to the code and will let you know how I get on...
 

Attachments

  • Main.c
    5.9 KB · Views: 257
  • Serial.c
    3.1 KB · Views: 218
Pommie,

I can not get the code to compile...

it does not like this delay function: -

__delay_us(1); // Wait required time

What should I do?
 
Pommie,

I can not get the code to compile...

it does not like this delay function: -

__delay_us(1); // Wait required time

What should I do?

I don't understand why that is. I just compiled this fine,
Code:
#include <pic.h>
  
__CONFIG(WDTDIS & INTIO & PWRTEN & MCLRDIS & UNPROTECT & BORDIS); 

#define _XTAL_FREQ 8000000

main()
{
   while(1)
   {
     [COLOR="red"]__delay_us(1)[/COLOR];      
   }
}

Edit, maybe you need to include pic.h.

Mike.
 
Last edited:
I've attached the code as it currently stands...

I have had to comment out sections of code just to get it to compile...

Some of the code has been replaced with my time delay function...

At the moment when I hit the letter J to read the temperature the code just hangs and does nothing...

Where as before it used to output all zero's as can be seen in the attached text document...

The other letters such as K & L output correctly providing that the letter J is not pressed first...
 

Attachments

  • 5th July 2010.zip
    233 KB · Views: 156
  • 2nd July 2010 - 2.TXT
    371 bytes · Views: 152
Last edited:
Ok...

We have a problem...

My original code outputs a string of data on the port pin...

The modified code that you gave me does not output any data...

Which can not be good news...

I've attached both versions to see if we can get any of them to actually work...
 

Attachments

  • TempSensor.c
    7.2 KB · Views: 195
  • TempSensor (modified).c
    11.5 KB · Views: 168
Ok...

We have a problem...

Your original code was just wrong. I tried to point you in the right direction, you ignored it. Ask your lecturers for the answer. I'm done.

Mike.
 
Hey Mike (pommie)...

Why are you being like that...

Your original code was just wrong. I tried to point you in the right direction, you ignored it. Ask your lecturers for the answer. I'm done.

I didn't ignore you... I took your advise and could not get the code to work, so I came back for help... as I just do not know what went wrong...

Only you did say...
Give it a go and if you're still having problems post back.

So i'm not sure how I managed to upset you... but as I obviously did I must therefore sincerely appologise to you...

Only I was rushed for time this morning as I was trying to test the code before heading out to work...

Only shortly after posting the last message to you the whole unit died on me and I was unable to get the serial comms back up and running...

I wrote a quick programme to test to processor and I managed to turn an output on and off every 840uS, however I could not get any code to run that used the serial comms, even though the code had already been used and worked perfectly... it was as if that side of things was dead... I tried using a 9-way feed back connector to test the serial comms to the computer and was able to echo back everything I sent using hyperterminal so I know that side of things appears Ok...

But I could see on the scope that when I pressed a key on the keyboard the PIC recieved it and appeard to be transmitting out... but hyperterminal was not apparently seeing it as if the internal clock was no longer running at 8MHz... only the previous chip went funny on me and started running at 9.6MHz and I was unable to get it to stabalise at 8MHz...

Only I'm not an Electronics Student... I am a Forensic Science Student... who is trying to design a data logger to assist with a third year project... to see if the degredation of cartilage can be used to determine the post mortem interval of buried remains...

I understand that I can purchase single channel devices to do the same job, but I need to record three channels at the same time... only the university is financially bankrupt and does not have the funds to assist me with the project, I only have a £25.00 budget to purchase porcine hind trotters...

Time is rapidly running out for me... hence the reason I am so stressed... I only have till the end of July to get a prototype up and running, leaving me two weeks to test the code and tweak it as required... otherwise my third year project is in jeperdy...

Mike (Pommie) can you please spend a little bit more of your valuable time assisting me as I am so confused with the code that I just do not know what to do in order to get it to work...

What information do you require from me, that will enable you to assist me further...
 
Last edited:
Hi Jim,

I admit I'm a bit frustrated trying to help you too. I've come across your plea for help on three or four different Forums now. I offered basically the same advice as Mike (Pommie) in post # 29 on this Forum.Microchip thread.

I'm sorry I can't be of assistance.

Good luck on your project.

Kind regards, Mike

<added>

I will offer one more piece of advice: Stop trying to use that particular Maxim application note because it's not correct (it doesn't even match their assembler application note).
 
Last edited:
Hi again Jim,

I looked back through that Forum.Microchip thread at all the posts made while I was sleeping last night and I discovered that TempSensor.c in post #38 was moving in the right direction. Your read_bit() and write_bit() functions looked good but your read_byte() and write_byte() functions were incorrect. So it seems you took one step forward and then took two steps backward while I was sleeping (lol). When I awoke I saw your more recent posts and code and it looked like you weren't making any progress.

These routines look correct;
Code:
unsigned char read_bit (void)
{
    unsigned char i;
    
    DQ = 0;             // preset output latch to '0'
    TRISC &= ~0x01;     // Set port to output - present a '0' on the buss
    __delay_us(1);      // Wait required time
    TRISC |= 0x01;      // Set port to input - present a '1' on the buss
    __delay_us(15);     // Wait for response
    i=DQ;               // read bit    
    __delay_us(45);     // complete slot
    return(i);          // return value
}
Code:
void write_bit (char b)
{
    unsigned char i;
    
    DQ = 0;             // preset output latch to '0'
    TRISC &= ~0x01;     // Set port to output - present a '0' on the buss
    if (b==1)           // if were writing a 1 then - also acts as 1uS delay
        TRISC |= 1;     // return DQ high
    _delay_us(60);      // Wait 60uS for end of time slot
    TRISC |= 1;         // return DQ high
}
The read_byte() and write_byte() routines were incorrect. Should look something like this;
Code:
unsigned char read_byte (void)
{
    unsigned char i;
    unsigned char value = 0;

    for (i=0; i<8; i++)  // reads in 8 bits of data
    {
        value >>= 1;     //
        if (read_bit())  //
          value |= 128;  //
    }
    return (value);      // return value
}
Code:
void write_byte (char val)
{
    unsigned char i;
    unsigned char temp;
    
    for (i=0; i<8; i++)     // writes byte one bit at a time
    {
        temp =  val & 1;     // copy that bit to temp
        write_bit(temp);   // write bit
        val >>= 1;          // prep for next bit
    }
}
I did not check out the other one-wire routines...

Regards, Mike, K8LH
 
Last edited:
I will offer one more piece of advice: Stop trying to use that particular Maxim application note because it's not correct (it doesn't even match their assembler application note).

My problem is that I cannot any longer read or understand assembly language, so I didn't even spot that... Thanks...

Only everything that I have done is directly based on that article...

I have this discussion on more than one forum as I am so desperate to find a solution as time is rapidly running out for me... and with regrette I have been forced to take such drastic actions in order to get an answer as quickly as I can... only now it's having a detremental effect as everyone who tried to assist me is now getting upset with me... so help is rapidly declining...

But I'm just so tired... I'm not sleeping and I'm so stressed about this whole project that it's now getting to me...

When I awoke I saw your more recent posts and code and it looked like you weren't making any progress.

That just about sums it up... :)

You could says that's exactly how I feel about the entire project I just want to take a hammer to the whole lot... :mad:
 
Last edited:
Anyway I know have two days off work, so I'll push on with the advice everyone has given me and see if I can get anything to work again...
 
OK...

Not sure if the 16F684 is working any longer...

So I have replaced it with the 16F636 as I know that this is a BRAND NEW CHIP... arrived by mistake from microchip as I had accidently ordered the wrong device...

But thankfully both are pin for pin compatible except for the fact the the 16F636 does not have an A/D...

I have got the 16F636 now transmitting data but not at the same baud rate as before as the characters are a a tad strange... I have attached what I am seeing on the scope but I just cannot translate it... I know BAUD = bits/time

But I'm not sure how many bits are being transmitted...

It should be the letter A followed by a carriage return, new line and > indicating ready... but I beleive the carriage return, new line and > are then also re-sent out again...

The total time for 1 message is - 6.4mS in duration with the smallest bit being 80uS in duration...
 

Attachments

  • Source Code.zip
    65.9 KB · Views: 125
  • m__DSC0025.jpg
    m__DSC0025.jpg
    80.3 KB · Views: 219
I forgot the mention...

I have ripped out all the code... i've gone back to the working Serial Comms Code just to get something talking...
 
Just to make things easier...

I'm only transmitting the letter 'U' (0x55) continuously as I beleive it should give me 1/2 the baud rate if I am actually transmitting at 4800,8,N,1

The total time for a single letter 'U' is 820uS with the smallest bit being 90uS

Therefore the binary equivalent of 0x55 is 01010101

So what I am seeing on screen needs to be inverted...

So I make it 10 bits in total with 1 start bit and 1 stop bit...

10 bits / 820uS = 12195

This just don't seem right...
 

Attachments

  • startup.zip
    62.8 KB · Views: 124
  • m__DSC0026.jpg
    m__DSC0026.jpg
    79.3 KB · Views: 185
Ok...

I'm there...

Just got the 16F636 to output everything correctly to hyperterminal...

So i'm now about to add the DS18B20 Code...

Bit by bit to see what works if anything...

But the site will not allow me to upload the code as it stands...

Just went back to edit post and was then able to upoad the code...
 

Attachments

  • 6th July 2010 - Changed to use printf.zip
    88.1 KB · Views: 123
Last edited:
Ok...

Were back to the original error messages...

I've used the code that was last posted, so hopefully we can use that as a suitable stating point...

But the code will not compile...

This is the error message that I am seeing...

Error [192] C:\Documents and Settings\Owner\Desktop\16F684 - Temperature Sensor\6th July 2010\6th July 2010 - Changed to use printf\Source Code\DS18B20.c; 74.31 undefined identifier "_XTAL_FREQ"

Error [192] C:\Documents and Settings\Owner\Desktop\16F684 - Temperature Sensor\6th July 2010\6th July 2010 - Changed to use printf\Source Code\DS18B20.c; 96.29 undefined identifier "_XTAL_FREQ"

Warning [361] C:\Documents and Settings\Owner\Desktop\16F684 - Temperature Sensor\6th July 2010\6th July 2010 - Changed to use printf\Source Code\DS18B20.c; 119.1 function declared implicit int

Basically all three lines point to there being a problem with the way I am using the following line of code...

Code:
_delay_us(60);      // Wait 60uS for end of time slot

I've attached the code... so that perhaps you might spare some more of your valuable time assisting me...
 

Attachments

  • 6th July 2010 - Changed to use printf.zip
    52.3 KB · Views: 116
Got it sorted...

I was setting

Code:
#define _XTAL_FREQ 8000000

in main.c when it should have been set in DS18B20.c (new version of TempSensor.c)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top