One problem with the wire

I need your qualified help! I am programming in C ++ using the PIC 18F87J50 and trying to connect the DS18B20 to my H0 port!

I think that my base program is correct, therefore ... I have a problem (I think I have), when you execute the ROM command, I am looking for the 64-bit ROM code.

The first byte should indicate to me in which family the component belongs (28h). The next 48 bits should give me a series of uniq just for this component. The latter is used for CRC.

I think correctly when I do 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], should I get the uniq number? should'nt i ??

Yours faithfully!

+4
source share
2 answers

Well, the best way to access the serial number is probably to copy it to 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] = '\0'; checksum = ROM_CODE[7]; ... 

&ROM_CODE[1] you need to find the address of the second element in ROM_CODE . ROM_CODE+1 may also work, but my C is a rusty touch.

Zero ('\ 0') is added at the end, because C uses null-terminated strings . This will ensure compatibility with C library routines and commonly used C idioms.

You can also access it directly from the array. But it will be harder to work with and is hardly worth it unless you really need that 6 bytes of memory.

Depending on how complex your application is, you can wrap this in a class. Pass the 8-character buffer to the constructor, and then use methods like getFamily() / getSerial() to get the information you need.

For a very simple application, however, there is a lot of extra code to simplify something that is already very manageable.

+2
source

Here is the code that should allow you to read the device identifier. I think your code is fast, here is some code that I used to interact 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; } 
0
source

All Articles