Gente, por favor, nao sei mais oque fazer, estou usando o código abaixo para ler um ds1820 (temperatura) mas não sai dos 10.63.

Aguem já passou por algo parecido? 

Alguem tem um código funcionando para + e - graus?

Obrigado gente.

Sérgio

#include "OneWire.h"
int DS18S20_Pin = 3; //DS18S20 Signal pin on digital 3

//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 3
void setup(void) {
Serial.begin(9600);
}
void loop(void) {

float temperature = getTemp();
Serial.println(temperature);

delay(100); //just here to slow down the output so it is easier to read

}

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}

Exibições: 336

Responder esta

Respostas a este tópico

Essas são as rotinas que eu uso.

Tente adaptar para você, eu tenho aqui apenas os  DS1820, mas quando eu tinha os DS18B20 funcionava também.

#include <OneWire.h>

#define MAX_DS1820_SENSORS 15
byte total_sensores;
byte addr[MAX_DS1820_SENSORS][8];

/* DS18S20 Temperature chip i/o */
OneWire ds_0(3);

void setup()
{
Serial.begin(9600);
}

void loop()
{
ExecutarLeituras();
delay(1000);
}

float getTemperature(OneWire ds, byte* address, byte* data){
byte tr;
float Temperatura;
writeTimeToScratchpad(ds, address);
delay(700);
readTimeFromScratchpad(ds, address,data);

if (address[0] == 0x28)
{
tr = (data[1] 8) | data[0];
Temperatura = (float)tr / 16;
if (data[1] == 0xFF)
{
Temperatura = Temperatura * -1;
}
}
else
{
//put in temp all the 8 bits of LSB (least significant byte)
tr = data[0];
/*
Serial.print(" Data 0");
F Serial.print(data[0], HEX);
Serial.print(" ");
*/
//check for negative temperature
if (data[1] > 0x80)
{
tr = ~tr; //two's complement adjustment
tr = tr + 1;
tr = tr >> 1;
Temperatura = tr * - 1; //flip value negative.
}
else
{
//drop bit 0
Temperatura = tr >> 1;
}
//COUNT PER Celsius degree (10h)
int cpc = data[7];
//COUNT REMAIN (0Ch)
int cr = data[6];


//calculate the temperature based on this formula :
//TEMPERATURE = TEMP READ - 0.25 + (COUNT PER Celsius Degree - COUNT REMAIN) / (COUNT PER Celsius Degree)
Temperatura = Temperatura - (float)0.25 + (cpc - cr)/(float)cpc;
}
return Temperatura;
}

void EnviaAddressSensor(uint8_t *addr ,int LCD_PRINT, int SendSerial)
{
int i;
for (i=0;i<8;i++)
{
if (addr[i] < 9)
{
if (SendSerial)
{
Serial.print("0");
}
}
if (SendSerial)
{
Serial.print(addr[i],HEX);
}
if (i<7)
{
if (SendSerial)
{
Serial.print("-");
}
}
}
}

void assing_array(uint8_t *addr, uint8_t *Addr_A)
{
byte i;
for (i = 0; i < 8; i++)
{
addr[i] = Addr_A[i];
}
}

void ExecutarLeituras(void)
{
int Calc;
byte sensor, i;
Serial.print("L;");
ProcuraSensores(ds_0, 0);
for (sensor=0; sensor < total_sensores; sensor++)
{
Leitura_Temperatura(ds_0, sensor, true);
}

Serial.print("F:");
Serial.print(Calc);
Serial.print(";");
Serial.println("");
}

void writeTimeToScratchpad(OneWire ds, byte* address){
//reset the bus
ds.reset();
//select our sensor
ds.select(address);
//CONVERT T function call (44h) which puts the temperature into the scratchpad
ds.write(0x44,1);
//sleep a second for the write to take place
delay(1000);
}

void readTimeFromScratchpad(OneWire ds, byte* address, byte* data){
//reset the bus
ds.reset();
//select our sensor
ds.select(address);
//read the scratchpad (BEh)
ds.write(0xBE);
for (byte i=0;i<9;i++){
data[i] = ds.read();
}
}
void IdentificaSensor(int enviar_serial)
{
OneWire ds_i(9); // on pin 9
byte i;
byte sensor_found[8];
i = 0;
if (enviar_serial == 1)
{
Serial.print("I;");
}
while (ds_i.search(sensor_found))
{
if (enviar_serial == 1)
{
EnviaAddressSensor(sensor_found, true, true);
Serial.print(";");
}
i++;
}
if (i == 0)
{
if (enviar_serial == 1)
{
Serial.print("Sensor não Encontrado");
}
}

if (enviar_serial == 1)
{
Serial.println(";");
}
}

void ProcuraSensores(OneWire ds, int enviar_serial)
{
byte i;
byte sensor_found[8];
i = 0;
if (enviar_serial == 1)
{
Serial.print("D;");
}
while (ds.search(sensor_found))
{
assing_array(addr[i], sensor_found);
if (enviar_serial == 1)
{
EnviaAddressSensor(sensor_found, false, true);
Serial.print(";");
}
i++;
}
if (enviar_serial == 1)
{
Serial.println("");
}
total_sensores = i;
}

void PegaDadosSensores()
{
byte sensor, i;
byte data[12];
float Temperatura;

// Checa por novos sensores
Serial.print("S:");
Serial.print(total_sensores);
Serial.println(":");
ProcuraSensores(ds_0,0);
for (sensor=0; sensor < total_sensores; sensor++)
{
Temperatura = getTemperature(ds_0, addr[sensor],data);
Serial.print("S:");
EnviaAddressSensor(addr[sensor], false, true);
Serial.print(" ");
for (i = 0; i < 9; i++)
{
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" Temp ");
Serial.println(Temperatura);
}
Serial.println("S:F:");
}

float Leitura_Temperatura(OneWire ds, int sensor, int SendSerial)
{
float Temperatura;
byte data[12];
if (SendSerial)
{
Serial.print("T:");
EnviaAddressSensor(addr[sensor], false, true);
Serial.print(":");
}
Temperatura = getTemperature(ds, addr[sensor],data);
if (SendSerial)
{
Serial.print(Temperatura);
Serial.print(";");
}
return Temperatura;
}

Desculpe a demora em agradecer a ajuda, 

muito obrigado pelo código, mas você não vai acreditar...

o problema é a peça, que dizer o lote de peças.

Encontrei um post na internet onde alguem dizia que o lote b7 estava com problemas, fui até o loja e troquei,.,..resolvido.

É mole???

Obrigado

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço