Esempio n. 1
0
// Lua: ow.select( id, buf[8])
static int ow_select( lua_State *L )
{
  uint8_t rom[8];
  size_t datalen;
  int numdata, i;
  unsigned id = luaL_checkinteger( L, 1 );
  const char *pdata;
  MOD_CHECK_ID( ow, id );

  if( lua_istable( L, 2 ) )
  {
    datalen = lua_objlen( L, 2 );
    if (datalen!=8)
      return luaL_error( L, "wrong arg range" );
    for( i = 0; i < datalen; i ++ )
    {
      lua_rawgeti( L, 2, i + 1 );
      numdata = ( int )luaL_checkinteger( L, -1 );
      lua_pop( L, 1 );
      if( numdata > 255 )
        return luaL_error( L, "wrong arg range" );
      rom[i] = (uint8_t)numdata;
    }
  }
  else
  {
    pdata = luaL_checklstring( L, 2, &datalen );
    if (datalen!=8)
      return luaL_error( L, "wrong arg range" );
    for( i = 0; i < datalen; i ++ ){
      rom[i] = pdata[i];
    }
  }

  onewire_select(id, rom);
  return 0;
}
Esempio n. 2
0
// return the temperature in C multiplied by 16
int read_DS1820()
{
  uint8 i;
  uint8 present = 0;
  uint8 dataread[12];
  int temp_read;

  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = onewire_reset();
  onewire_select(DS1820_addr);    
  onewire_write(0xBE,0);         // Read Scratchpad
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    dataread[i] = onewire_read();
  }
  temp_read = ((dataread[1] << 8) | dataread[0]);
  if (!is_DS18B20) {
    // temp_read is currently in half degrees.
    temp_read *= 8.0;
    //
    temp_read += - ( 8 * (dataread[7]- dataread[6]) )/dataread[7];
  }
  return temp_read;
}
Esempio n. 3
0
int ICACHE_FLASH_ATTR ds18b20_read(ds18b20_data *read) {
	byte i;
	byte present = 0;
	byte type_s;
	byte data[12];
	byte addr[8];
	byte crc;
	float celsius;
	//float fahrenheit;

	DS18B20_DBG("Look for OneWire Devices on PIN =%d", gpioPin);

	if (!onewire_search(gpioPin, addr)) {
		DS18B20_DBG("No more addresses.");
		onewire_reset_search(gpioPin);
		delay_ms(250);
		return;
	}

	DS18B20_DBG("ADDRESS = %02x %02x %02x %02x %02x %02x %02x %02x",
			addr[0], addr[1], addr[2], addr[3],
			addr[4], addr[5], addr[6], addr[7]);

	// the first ROM byte indicates which chip
	switch (addr[0]) {
		case 0x10:
			DS18B20_DBG("  Chip = DS18S20");  // or old DS1820
			type_s = 1;
			break;
		case 0x28:
			DS18B20_DBG("  Chip = DS18B20");
			type_s = 0;
			break;
		case 0x22:
			DS18B20_DBG("  Chip = DS1822");
			type_s = 0;
			break;
		default:
			DS18B20_DBG("Device is not a DS18x20 family device.");
			return;
	} 

	onewire_reset(gpioPin);
	onewire_select(gpioPin, addr);
	onewire_write(gpioPin, 0x44, 1);
        // start conversion, use ds.write(0x44,1) with parasite power on at the end

	delay_ms(1000);

	present = onewire_reset(gpioPin);
	onewire_select(gpioPin, addr);
	onewire_write(gpioPin, 0xBE, 1);   // Read Scratchpad

	//DS18B20_DBG("  Present = %d", present);
	for ( i = 0; i < 9; i++) {         // we need 9 bytes
		data[i] = onewire_read(gpioPin);
		DS18B20_DBG("  DATA: %02x ", data[i]);
	}
	crc = onewire_crc8(data, 8);
	DS18B20_DBG(" CRC=%d ", crc);

	if (crc != data[8]) {
		DS18B20_DBG("CRC is not valid!");
		return;
	}

	// Convert the data to actual temperature
	// because the result is a 16 bit signed integer, it should
	// be stored to an "int16_t" type, which is always 16 bits
	// even when compiled on a 32 bit processor.
	int16_t raw = (data[1] << 8) | data[0];
	if (type_s) {
		raw = raw << 3; // 9 bit resolution default
		if (data[7] == 0x10) {
			// "count remain" gives full 12 bit resolution
			raw = (raw & 0xFFF0) + 12 - data[6];
		}
	} else {
		byte cfg = (data[4] & 0x60);
		// at lower res, the low bits are undefined, so let's zero them
		if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
		else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
		else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
		//// default is 12 bit resolution, 750 ms conversion time
	}

	celsius = (float) raw / 16.0;
	//fahrenheit = celsius * 1.8 + 32.0;
	read->temp = celsius;

	DS18B20_DBG("Temperature = ");

	char *temp_string = (char*) os_zalloc(64);

	c_sprintf(temp_string, "%f", read->temp);
	DS18B20_DBG("  %s Celsius", temp_string);
	os_free(temp_string);

	//DS18B20_DBG(" %2.2f Fahrenheit", fahrenheit);

	return 1;
}
Esempio n. 4
0
void start_DS1820()
{
  onewire_reset();
  onewire_select(DS1820_addr);
  onewire_write(0x44,0);         // start conversion, with parasite power off at the end
}