static dsStatus ds2431_read_memory (DALLAS_CONTEXT *dc, unsigned int address, unsigned char *data, int bufsiz) { dsStatus stat; unsigned int i; if (!dc->in_mask) { return dsError_NoDevice; } if (address + bufsiz > 0x90) { return dsError_NoDevice; } stat = ds_reset(dc); if (stat != dsError_None) { return stat; } /* Only one device; skip ROM check */ ds_write_byte(dc, SKIP_ROM); ds_write_byte(dc, READ_MEMORY); ds_write_byte(dc, address); ds_write_byte(dc, address >> 8); for (i = 0; i < bufsiz; i++) { data[i] = (unsigned char)ds_read_byte(dc); } return ds_reset(dc); } /* ds2431_read_memory */
//*---------------------------------------------------------------------------- //* Name : ds_write_match_rom() //* Brief : Write match rom command and 64 bit rom datas //* Argument : DS_REG pointer //* Return : Error = -1, OK = 0 //*---------------------------------------------------------------------------- static void ds_write_match_rom(DS18B20_REG_T *ds) { int i; uint8 *c = &ds->ds_id; // Write commands ds_write_byte(DS_CMD_MATCH_ROM); // Write 64 bis ROM for(i = 0; i < 8; i ++) { ds_write_byte(*(c ++)); } }
//return current mahr 可以读写 float ds_get_acr(void) { float acr; while(!ds_reset()); ds_write_byte(CMD_ONEWIRE_SKIP_ROM); ds_write_byte(CMD_ONEWIRE_READ); ds_write_byte(CMD_ADDR_ACR); uint8_t msb =ds_read_byte(); acr=(float)(msb); return(acr); }
//return current ma float ds_get_current(void) { float current; while(!ds_reset()); ds_write_byte(CMD_ONEWIRE_SKIP_ROM); ds_write_byte(CMD_ONEWIRE_READ); ds_write_byte(CMD_ADDR_CURRENT); uint16_t msb =ds_read_byte(); int16_t lsb =ds_read_byte(); int16_t t = (msb<<8)|lsb; current=(float)(t*0.07813); return(current); }
//return volt v float ds_get_volt(void) { float volt; while(!ds_reset()); ds_write_byte(CMD_ONEWIRE_SKIP_ROM); ds_write_byte(CMD_ONEWIRE_READ); ds_write_byte(CMD_ADDR_VOLT); uint16_t msb =ds_read_byte(); uint16_t lsb =ds_read_byte(); uint16_t t = (msb<<8)|lsb; volt=(float)(t>>5)*0.00488; return(volt); }
// return temperature C float ds_get_temperature(void) { float temperature; while(!ds_reset()); ds_write_byte(CMD_ONEWIRE_SKIP_ROM); ds_write_byte(CMD_ONEWIRE_READ); ds_write_byte(CMD_ADDR_TEMP); uint16_t msb =ds_read_byte(); uint16_t lsb =ds_read_byte(); uint16_t t = (msb<<8)|lsb; temperature=(float)t/32*0.125; return(temperature); }
/* Read data from the ROM (which contains the S/N). */ static dsStatus ds_read_rom_64bit_single (DALLAS_CONTEXT *dc, int port, unsigned char *data) { int x; dsStatus stat; if (!dc->in_mask) { return dsError_NoDevice; } stat = ds_reset(dc); if (stat != dsError_None) { return stat; } ds_write_byte(dc, READ_ROM); for (x = 0; x < 8; x++) { *(data + x) = (unsigned char)ds_read_byte(dc); } if (*data != DS2430_CODE && *data != DS2431_CODE) { return dsError_WrongDevice; } return calcCRC8rom(data) ? dsError_CRCError : dsError_None; } /* ds_read_rom_64bit_single */
//*---------------------------------------------------------------------------- //* Name : ds_read_all_ram() //* Brief : Read all DS18B20 RAM register //* Argument : DS_REG pointer //* Return : Error = -1, OK = 0 //*---------------------------------------------------------------------------- static ds_read_all_ram(DS18B20_REG_T *ds) { int i; uint8 *c = NULL; if(ds == NULL) { return -1; } // reset DS18B20 if(ds_reset() < 0) { return -1; } ds_write_match_rom(ds); ds_write_byte(DS_CMD_READ_RAM); c = &ds->temp_lsb; for(i = 0; i < 9; i ++) { *(c ++) = ds_read_byte(); } // reset DS18B20 if(ds_reset() < 0) { return -1; } // check crc here if(ds->ram_crc7 != crc7(0, &ds->temp_lsb, 8)) { return -1; } // calculate conver time return ds_calculate_conver_time(ds); }
//*---------------------------------------------------------------------------- //* Name : ds_read_all_rom() //* Brief : Read all DS18B20 ROM registers //* Argument : DS_REG pointer //* Return : Error = -1, OK = 0 //*---------------------------------------------------------------------------- static int ds_read_all_rom(DS18B20_REG_T *ds) { int i; if(ds == NULL) { return -1; } // reset DS18B20 if(ds_reset() < 0) { return -1; } // Write commands ds_write_byte(DS_CMD_READ_ROM); // read 8 + 48 + 8 bits ds->ds_id = ds_read_byte(); for(i = 0; i < 6; i++) { ds->serial_num[i] = ds_read_byte(); } ds->rom_crc7 = ds_read_byte(); // check crc7 here if(ds->rom_crc7 != crc7(0, (unsigned char *)ds, 7)) { return -1; } return 0; }
/* Read data from the memory. * This will work for commands READ MEMORY, READ SCRATCHPAD, * READ STATUS, READ APPLICATION REG */ dsStatus ds_read_string (DALLAS_CONTEXT *dc, unsigned int command, unsigned int address, unsigned char *data, int bufsiz) { unsigned int i; if (!dc->in_mask) { return dsError_NoDevice; } ds_write_byte(dc, command); ds_write_byte(dc, address); for (i = 0; i < bufsiz; i++) { *(data + i) = (unsigned char)ds_read_byte(dc); } return ds_reset(dc); } /* ds_read_string */
int ds_search_rom(uint8_t *id, uint8_t mask) { uint8_t r, i, j; uint8_t crc, b1, b2; /* Reset the sensor(s) */ r = ds_reset(); if(r != DS_OK) return(r); /* Transmit the SEARCH ROM command */ ds_write_byte(0xF0); /* An ID is made up of 8 bytes (7 + CRC) */ for(crc = i = 0; i < 8; i++) { *id = 0; for(j = 0; j < 8; j++) { /* Read the bit and its complement */ b1 = ds_read_bit(); b2 = ds_read_bit(); /* Both bits should never be 1 */ if(b1 && b2) return(DS_ERROR); /* Both bits are 0 when two or more sensors * respond with a different value */ if(b1 == b2) { b1 = mask & 1; mask >>= 1; /* Test if this is the last ID on the bus */ if(!b1) r = DS_MORE; } /* Let the sensors know which direction we're going */ ds_write_bit(b1); *id >>= 1; if(b1) *id |= 1 << 7; } /* Update the CRC, compare with last byte */ if(i != 7) crc = _crc_ibutton_update(crc, *id); else if(crc != *id) return(DS_BADCRC); id++; }
//*---------------------------------------------------------------------------- //* Name : ds_start_con() //* Brief : Write command "Start convert tempreture" //* Argument : DS_REG pointer //* Return : Error = -1, OK = 0 //*---------------------------------------------------------------------------- int DS_start_con(DS18B20_REG_T * ds) { if(ds == NULL) { return -1; } // reset first if(ds_reset() < 0) { return -1; } // write match rom command ds_write_match_rom(ds); // write convert command ds_write_byte(DS_CMD_START_CON); return 0; }
//*---------------------------------------------------------------------------- //* Name : ds_configcon_type() //* Brief : Config conver BIT type //* Argument : DS_TYPE //* Return : Error = -1, OK = 0 //*---------------------------------------------------------------------------- static int ds_config_con_type(DS18B20_REG_T * ds, DS_TEMP_T type) { uint8 buf = 0; if(ds == NULL) { return -1; } // reset DS18B20 if(ds_reset() < 0) { return -1; } buf |= (type << 5); // Write commands ds_write_match_rom(ds); ds_write_byte(DS_CMD_WRITE_RAM); ds_write_byte(ds->usr_byte[0]); ds_write_byte(ds->usr_byte[1]); ds_write_byte(buf); // reset DS18B20 if(ds_reset() < 0) { return -1; } // read back if(ds_read_all_ram(ds) < 0) { return -1; } // check if write successed if(ds->config != (buf | 0x1f)) { return -1; } return 0; }
/* Read data from the memory. */ static dsStatus ds2430_read_memory (DALLAS_CONTEXT *dc, unsigned int address, unsigned char *data, int bufsiz) { dsStatus stat; if (!dc->in_mask) { return dsError_NoDevice; } address &= 0x1F; stat = ds_reset(dc); if (stat != dsError_None) { return stat; } /* Only one device; skip ROM check */ ds_write_byte(dc, SKIP_ROM); return ds_read_string(dc, READ_MEMORY, address, data, bufsiz ); } /* ds2430_read_memory */
/* Read the 64-bit (8-byte) application area of some devices. */ dsStatus ds_read_application_area (DALLAS_CONTEXT *dc, unsigned int address, unsigned char *data, int bufsiz) { unsigned int x; dsStatus stat; if (!dc->in_mask) { return dsError_NoDevice; } stat = ds_reset(dc); if (stat != dsError_None) { return stat; } ds_write_byte(dc, SKIP_ROM); ds_write_byte(dc, READ_STATUS); ds_write_byte(dc, 0); /* Validation byte */ x = ds_read_byte(dc); if (x != 0xFC) { return dsError_NotProgrammed; } stat = ds_reset(dc); if (stat != dsError_None) { return stat; } ds_write_byte(dc, SKIP_ROM); ds_write_byte(dc, READ_APPLICATION); /* LSB of address */ ds_write_byte(dc, address & 0xFF); for (x = 0; x < bufsiz; x++) { *(data + x) = (unsigned char)ds_read_byte(dc); } return dsError_None; } /* ds_read_application_area */