示例#1
0
文件: ds18b20.c 项目: alivehex/gpior
//*----------------------------------------------------------------------------
//* 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;
}
示例#2
0
//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);
}
示例#3
0
//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);
}
示例#4
0
// 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 */
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 */
示例#7
0
int8_t ds_read_temperature(ds_code device_code, uint16_t* temperature)
{
  // reset
  if(ds_presence_reset() == DS_FALSE) return -1;

  // match the device
  if(device_code==NULL) ds_write_command(DS_COMM_SKIP_ROM);
  //TODO: else ds_matchRom(device_code);

  // read all scratchpad
  ds_write_command(DS_COMM_READ_SCRATCHPAD);
  uint8_t scratchpad[9];
  for(uint8_t i =0; i<9; i++)
    {
      if(ds_read_byte(&(scratchpad[i]))==DS_FALSE) return -2;
    }

  // check CRC code
  if(simpleCrc(scratchpad,9)!=0) return -3;

  // get temperature from the scratchpad
  (*temperature) = (uint16_t)(scratchpad[0])+((uint16_t)(scratchpad[1])<<8);

  return DS_TRUE;
}
示例#8
0
文件: ds18b20.c 项目: alivehex/gpior
//*----------------------------------------------------------------------------
//* 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);
}
/* 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 */
示例#10
0
//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);
}
/* 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 */
示例#12
0
int8_t ds_setResolution(ds_code device_code,ds_resolution_t res)
{
  // frstly read schradpad becouse if you write you must write all rom
  // including even teperature alarms

  // reset bus firsth
  if(ds_presence_reset() == DS_FALSE){return DS_FALSE;}

  // match device
  if(device_code==NULL) ds_write_command(DS_COMM_SKIP_ROM);
  //TODO:  else ds_matchRom(device_code);

  // read scratchpad
  ds_write_command(DS_COMM_READ_SCRATCHPAD);
  uint8_t scratchpad[9];
  for(uint8_t i =0; i<sizeof(scratchpad); i++)
    {
      if(ds_read_byte(&(scratchpad[i]))==DS_FALSE) return DS_FALSE;
    }
  if(simpleCrc(scratchpad,sizeof(scratchpad))!=0) return DS_FALSE;

  // reset the bus
  if(ds_presence_reset() == DS_FALSE) return DS_FALSE;

  // match device
  if(device_code==NULL) ds_write_command(DS_COMM_SKIP_ROM);
  //TODO:else ds_matchRom(device_code);

  // write to scratchpad
  ds_write_command(DS_COMM_WRITE_SCRATCHPAD);

  // writing to scratch pad
  ds_write_command(scratchpad[2]);
  ds_write_command(scratchpad[3]);
  ds_write_command((uint8_t)res);

  return DS_TRUE;
}