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.
Ok...

Code Compiles and Runs Ok...

But...

When I press the letter J to run reset ()... I'm not seeing the port pin transmitting any data whilst looking on the scope...

I presume I should see port RC0 going "Hi - Low" as the data is transmitted...

I'm attaching the code as it stands so that perhaps you can advise me further as to how to progress...
 

Attachments

  • 6th July 2010 - Changed to use printf.zip
    134.7 KB · Views: 100
Are you sure you're getting to that "J" section of code? Maybe echo that character back to Hyperterminal just to make sure you actually made it there?

Your reset() function is incorrect. Should be;

Code:
unsigned char reset (void)
{
    unsigned char present;
    DQ = 0;                 //
    TRISC &= ~0x01;    // Set port to output
    __delay_us(500);     // Leave low for 500uS
    TRISC |= 0x01;        // Set port to input
    __delay_us(60);     // Wait 60uS for present signal
    present = DQ;         // Get present signal
    __delay_us(500);     // Wait 500uS for end of time slot
    return (present);     // Present signal returned
}
I'm not a fan of the HiTech product. You might want to simulate those delay functions to make sure they're giving you what you expect...

Regards, Mike
 
Last edited:
Hi Mike...

I was just tweaking the code... when you replied... :0
The switch statement is working fine... As I have Letters I, O & P transmitting the letters via putch ('A')...

After the total collapse yesterday... I've carefully made my way to this point...
 

Attachments

  • DS18B20 - A.c
    6.4 KB · Views: 120
Just double checked...

Code:
			   switch (uart_data)
			   {

					case 'J':
							reset ();
							//putch ('A');
							break;

					case 'K':
							read_bit ();
							//putch ('B');
							break;

					case 'L':
							Read_ROM ();
							//putch ('C');
							break;
					case 'I':
							putch ('A');
							break;
					case 'O':
							putch ('B');
							break;
					case 'P':
							putch ('C');
							break;

					default:
							newline();
				} 	 // end of switch

This is all working fine...
 
The compiler does not like me to use TRISC0.0 for some strange reason, I generated a page full of error messages...

So I changed the code but I'm unsure how to re-do this line of code...

Code:
//   data = PORTC.0;     // capture Rx bit

The full code listing is here, but also attached is the fully compiled code: -

Code:
#include	<htc.h>
#include <stdio.h>
#include "Serial.h"
#include "Defines.h"


/*==================================================================================
**
** The 16F636 should be running at 8MHz using the internal oscillator
** Not sure at this stage how long it takes to run or what each count is
**
** =================================================================================
*/

/*==================================================================================
**
** Each communication cycle must begin with a reset from the 16F684
** To do this the port needs to be pulled low for 480uS then released
** The DS18B20 will respond by pulling the port low to indicate it's present
**
** 0 indicates device present, 1 indicates no device present
**
** DQ is on RC0
**
** Time Slot (Min 60uS - Max 120uS)
** Recovery Time - 1uS
** Reset Time (Min 480uS - Max 960uS)
** Present Detect 60uS
**
** One cycle appears to be 12uS
**
** =================================================================================
*/

/*==================================================================================
**
** RESET - this should ideally reset the DS18B20 and recieve a Presence Pulse
**
** =================================================================================
*/

unsigned char reset (void)
{
	
	unsigned char present;

	DQ = 0;			// Pull DQ line Low
	TRISC &= ~0x01;		// Set port to output
	__delay_us(500); 	// Leave low for 500uS
	TRISC |= 0x01;		// Set port to input
	__delay_us(60); 	// Wait 60uS for present signal
	present = DQ; 		// Get present signal
	__delay_us(500); 	// Wait 500uS for end of time slot
	return (present); 	// Present signal returned
}

/*==================================================================================
**
** READ_BIT - the time_delay required for a read bit is 15uS
**
** =================================================================================
*/

unsigned char read_bit (void)
{
    unsigned char data;

    PORTC &= ~0x01;		 // preset output latch to '0'
    TRISC &= ~0x01;		// Set port to output - present a '0' on the bus
    __delay_us(1);      // Wait required time
    __delay_us(1);      // Wait required time
    TRISC &= 0x01;      // Set port to input - present a '1' on the bus
    __delay_us(15);     // Wait for response
 //   data = PORTC.0;     // capture Rx bit
    __delay_us(45);     // complete time slot
    return(data);       // return value
}

/*==================================================================================
**
** WRITE_BIT - writes a bit to the one-wire bus
**
** =================================================================================
*/

void write_bit (char b)
{
    unsigned char i;

    PORTC &= ~0x01;		 // preset output latch to '0'
    TRISC &= ~0x01;		// Set port to output - present a '0' on the bus
    if (b==1)           // if were writing a 1 then - also acts as 1uS delay
    TRISC &= 0x01;      // Set port to input - present a '1' on the bus
    __delay_us(60);     // Wait 60uS for end of time slot
}

/*==================================================================================
**
** READ_BYTE - reads a byte from the one-wire bus
**
** =================================================================================
*/

unsigned char read_byte (void)
{
    unsigned char data;
    unsigned char count = 0;
	unsigned char bt = 0;
    

    while (count < 8)  // reads in 8 bits of data
    {
        data >> = 1;     //
        bt = read_bit();  //
        if (bt) data |= 128;  //
	count ++;
    }
    return (data);      // return value
}

/*==================================================================================
**
** WRITE_BYTE - writes a byte to the one-wire bus
**
** =================================================================================
*/

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
    }
}

/*==================================================================================
**
** READ_ROM - finds the 64-bit ROM code when only a single device is conected
**
** =================================================================================
*/

void Read_ROM(void)
{
	int n;
	static char dat[9];

	printf("\nReading ROM Code\n");
	reset();
	write_byte(0x33);
	for (n=0;n<8;n++){dat[n]=read_byte();}
	printf("\n ROM Code = %X%X%X%X%X%X%X%X\n",dat[7],dat[6],dat[5],dat[4],dat[3],dat[2],dat[1],dat[0]);
}

/*==================================================================================
**
** READ_ROM - finds the 64-bit ROM code when only a single device is conected
**
** =================================================================================
*/

void Read_TEMP(void)
{
	char temp_lo;
	char temp_hi;
	

	reset ();
	write_byte (0xCC);	// Send "skip rom"
	write_byte (0x44);	// Send "convert"
	while (!read_bit());	// wait for convert complete
	
	reset ();
	write_byte (0xCC);	// Send "skip rom"
	write_byte (0xBE);	// Send "read scratchpad"

	temp_lo = read_byte();
	temp_hi = read_byte();
	reset ();
}
 

Attachments

  • 6th July 2010 - Part Working Code.zip
    134.9 KB · Views: 101
Just a quick note to THANK everyone for there participation...

After much trial and error I have finally got the unit talking...

I am seeing a string of data coming out of the unit, so I just need to now translate that data to see how accurate it is...

So I just like to send out a big THANK YOU to all that has helped on this particular thread...

Basically I had to etch a new PCB today as I was unable to programme the unit any longer due to errors, once I reprogrammmed the PIC she instantly came to life...

So it's onwards and upwards with the project...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top