English 中文(简体)
One Wire Problem
原标题:

I need your qualified help! I m programing in C++, using a PIC 18F87J50 and trying to connect DS18B20 at my H0 Port!

I think my underlying programing is correct so.... the problem I have (think I have), is when performing a ROM Command, I m searching for the 64-bit ROM CODE.

The first byte should tell me what family the component belongs to (28h). The next 48 bits should give me a uniq serial for just that component. The last one is used for a CRC.

Am I thinking right when doing like this:

void Device_ID( uint8_t command ){
    uint8_t ROM_CODE[8]; // 1 byte CRC, 6 bytes SERIAL, 1 byte Family code
    uint8_t loop;
    static char container[8];

    OW_reset_pulse();
    OW_write_byte( command );

    for(loop = 0; loop < 8; loop++)     // 1 byte in per time = 64-bits
    {
        ROM_CODE[loop] = OW_read_byte(); 
    }

    HexToStrWithZeros(ROM_CODE[0], container);
    Display_Cls();
    Display_StringAt ("Family Code: ",5,6);
    Display_Buffer (container);
}

If I ask for the code in ROM_CODE[1-6] I should get the uniq number?? should nt I??

Kind Regards!

最佳回答

Well, the best way to access the serial number is probably to copy it into a separate buffer using strncpy.

#include <string.h>

...

char family;
char serial[7]; // Extra byte for null terminator
char checksum;

...

family = ROM_CODE[0];

strncpy(serial, &ROM_CODE[1], 6);
serial[6] =   ;

checksum = ROM_CODE[7];

...

The &ROM_CODE[1] is there to fetch the address of the 2nd element in ROM_CODE. ROM_CODE+1 may also work, but my C is a touch rusty.

The null ( ) is added at the end as C uses null-terminated strings. This will make sure it s compatible with C library routines and commonly used C idioms.

You could also access it directly from the array. But that ll be harder to work with and unlikely to be worth it unless you really need that 6 bytes of memory.

Depending on how sophisticated your app is, you may want to wrap this in a class. Pass the 8 character buffer to the constructor and then use methods such as getFamily() / getSerial() to retrieve the information you want.

For a very simple app though, that s a whole lot of extra code to simplify something that s already very manageable.

问题回答

Here is some code that should allow you to read the device ID. I think your code was running to fast here is some code that I used to interface with the DS18B20.

/****************************************************************************
* temperature.h
****************************************************************************/

#ifndef TEMP_H
#define TEMP_H

extern double read_temp ( void );
extern   void start_temp( void );
extern   void Device_ID ( void );

#endif

/****************************************************************************
* temperature.c
****************************************************************************/

void     reset_ow(void);
void     write_ow(uint8_t  b);
uint8_t  read_ow (void);

#define OW_TEMP_SIG LATHbits.LATH0
#define OW_TEMP_TRIS TRISHbits.TRISH0
#define OW_TEMP_SIG_IN PORTHbits.RH0
#define DIR_OUT 0
#define DIR_IN 1


void Device_ID( void )
{
    uint8_t loop; 
   uint8_t family; 
   uint8_t checksum; 
   uint8_t ROM_CODE[8];         // 1 byte CRC, 6 bytes SERIAL, 1 byte Family code

   reset_ow();
   write_ow(0x33); // READ ROM COMMAND DS18B20

    for(loop = 0; loop < 8; loop++) // 1 byte in per time = 64-bits
    {
        ROM_CODE[loop] = read_ow();
    }

    family = ROM_CODE[0];
    checksum = ROM_CODE[7];

  // add extra code to handle code
}

void start_temp(void) 
{
   uint8_t i;

   OW_TEMP_SIG=1;
   OW_TEMP_TRIS=DIR_OUT;
   for ( i=0;i<100;i++)
   {
       Delay_us(100);
   }
   reset_ow();
   write_ow(0xcc); // skip rom
   write_ow(0x44); // start t conv
}

double read_temp(void) 
{
   double temp=0;
   S16 itemp;

   reset_ow();
   write_ow(0xcc); // skip rom
   write_ow(0xbe); // read scratch pad
   itemp=read_ow();
   itemp|=(S16)read_ow()<<8;

   temp = itemp*(0.0625);
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
   return temp; 
}


void reset_ow(void)
{
   OW_TEMP_TRIS=DIR_OUT;
   OW_TEMP_SIG=0;
   Delay_us(250);
   Delay_us(250);
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
   Delay_us(250);
   Delay_us(250);
}

void write_ow(uint8_t b)
{
   uint8_t i;

   OW_TEMP_SIG=1;
   OW_TEMP_TRIS=DIR_OUT;
   for ( i=0;i<8;i++)
   {
      OW_TEMP_SIG=0;
      if ( b & 0x01 )
      {
         Delay_us(10);
         OW_TEMP_SIG=1;
      }
      Delay_us(70);
      OW_TEMP_SIG=1;
      Delay_us(10);
      b >>= 1;
   }
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
}

uint8_t read_ow(void)
{
   uint8_t b=0;
   uint8_t m;
   uint8_t i;

   m=1;
   for ( i=0;i<8;i++)
   {
      OW_TEMP_SIG=1;
      OW_TEMP_TRIS=DIR_OUT;
      OW_TEMP_SIG=0;        
      Delay_us(8);
      OW_TEMP_TRIS=DIR_IN;
      OW_TEMP_SIG=1;
      Delay_us(15);

      if ( 1 == OW_TEMP_SIG_IN )
      {
         b |= m;
      }
      m <<=1;
      Delay_us(60);
   }
   OW_TEMP_TRIS=DIR_IN;
   OW_TEMP_SIG=1;
   return b;
}




相关问题
Steps to read data from ARM microcontroller port

I am having trouble reading serial data from ARM LPC2378 microcontroller. Will I have to use UART or any GPIO port can be used?? is ayone having c code for it??

high speed tracing

I have an embedded board with a 32 Micro-controller, and an custom built OS, Unfortunately, as of now, connection to the PC is only through serial port, Internal memory is limited to 512KB. There are ...

Affordable, programmable device with gprs and simple sensors?

I ve got quite a fun challenge / work assignment. I m to monitor a couple of 5V light bulbs (warning lights) on a machine standing far out in no man s land. I m looking for an affordable device with ...

Alarm history stack or queue?

I m trying to develop an alarm history structure to be stored in non-volatile flash memory. Flash memory has a limited number of write cycles so I need a way to add records to the structure without ...

LD linker: Target address aligning but not address in ROM

I have a program that s resident in flash and will run from flash. Early in the program data segments are copied from flash to ram. I m using a linker script like (simplified): .text : { *(....

clear code for counting from 0 to 255 using 8-bit datatype

I was wondering if there is a clean way of counting from 0 to 255 using an 8 bit datatype, something like: for(uint8_t i(0);i<=255;++i) { .... } This obviously will not work but it makes it ...

热门标签