Domanda
Ho bisogno del vostro aiuto qualificato! Sono programmazione in C ++, utilizzando un PIC 18F87J50 e tenta di connettersi al mio DS18B20 Port H0!
Credo che la mia programmazione di fondo è corretto così .... il problema che ho (credo di avere), è durante l'esecuzione di un comando ROM, sto cercando per il codice ROM a 64 bit.
Il primo byte mi dovrebbe dire quale famiglia la componente appartiene (28h). I successivi 48 bit mi dovrebbero dare un serial uniq solo per questo componente. L'ultimo viene utilizzato per un CRC.
sto pensando a destra quando si fa in questo modo:
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);
}
Se io chiedo per il codice in ROM_CODE [1-6] dovrei ottenere il numero uniq ?? should'nt I ??
Cordiali saluti!
Soluzione
Bene, il modo migliore per accedere al numero di serie è probabilmente da copiare in un buffer separato utilizzando 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];
...
Il &ROM_CODE[1]
è lì per andare a prendere l'indirizzo del 2 ° elemento in ROM_CODE
. ROM_CODE+1
può anche funzionare, ma il mio C è un tocco arrugginita.
Il nulla ( '\ 0') viene aggiunto alla fine come C utilizza stringhe null-terminated . Questo farà in modo che sia compatibile con le routine di libreria C e comunemente usato idiomi C.
Si potrebbe anche accedervi direttamente dalla matrice. Ma che sarà più difficile da lavorare e improbabile che sia valsa la pena a meno che non si davvero bisogno che 6 byte di memoria.
A seconda di come la vostra applicazione è sofisticata, si consiglia di avvolgere questo in una classe. Passare il buffer di 8 caratteri per il costruttore e quindi utilizzare metodi come getFamily()
/ getSerial()
per recuperare le informazioni desiderate.
Per una molto semplice applicazione, però, che è un bel po 'di codice aggiuntivo per semplificare qualcosa che è già molto maneggevole.
Altri suggerimenti
Ecco il codice che dovrebbe consentire di leggere l'ID del dispositivo. Credo che il codice è stato eseguito a digiunare qui è un codice che ho usato per interfacciarsi con il 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;
}