Probleme mit DS18S20

Gast #1619467
Lesenswert?

Hallo,

der DS18S20 liefert mir eine falsche Temperaturwert züruck. Bei temp_MSB 
kriege ich 0xFF und temp_LSB kriege ich 0xBF. Wenn das umgerechnet wird, 
komme ich dann zu -32°C.

Könnte jemand mir helfen?
1
float Read_Temperature(void)
2
{
3
  unsigned char  Scratchpad[9];
4
  //unsigned char buffer[2];
5
  unsigned char temp_LSB,temp_MSB;
6
  char i;
7
  float final_temp;
8
  signed int temp;
9

10
  if(DS18_SendReset(3))
11
  {
12
    DS18_WriteByte(0xCC); //Skip ROM 
13
    DS18_WriteByte(0x44); //Start conversion
14
    Delay_us(670);
15
    DS18_SendReset(3);
16
    DS18_WriteByte(0xCC); //Skip ROM
17
    DS18_WriteByte(0xBE); //Read Scratchpad
18

19
      for(i=0;i<9;i++)
20
      {
21
        Scratchpad[i]=DS18_ReadByte_1();
22
      }
23
  }
24

25
/*****************************************************************************
26
Read temperature from Scratchpad and convert the reading into °C
27
*****************************************************************************/
28

29
temp_MSB = Scratchpad[1];
30
temp_LSB = Scratchpad[0];
31
test = temp_LSB;
32
if(temp_MSB <= 0x80)
33
{
34
  temp_LSB = (temp_LSB/2);
35
}
36

37
temp_MSB = temp_MSB & 0x80;
38

39
if(temp_MSB >= 0x80)
40
{
41
  temp_LSB = (~temp_LSB)+1;
42
}
43

44
if(temp_MSB >= 0x80)
45
{
46
  temp_LSB = (temp_LSB/2);
47
}
48

49
if(temp_MSB >= 0x80)
50
{
51
  temp = ((-1)*temp_LSB);
52
}
53

54
final_temp= ((float)temp);//*10 ;//Final Temperature in °C x10
55
                //e.g. 21.5°C = 21
56

57
return final_temp;
Gast #1619491
Lesenswert?

Du musst erst mal eine Messung triggern und warten bis er fertig ist.

Messung anstossen geht mit:
CONVERT T [44h]

Und dann:
If the DS18S20 is powered by an
external supply, the master can issue read-time slots after the Convert 
T command and the DS18S20 will
respond by transmitting 0 while the temperature conversion is in 
progress and 1 when the conversion is
done.
Gast #1619671
Lesenswert?

Ok ... das mit dem Reset war eine falsche Vermutung meinerseits.

Hab nochmal in dem Code nachgesehen, den ich habe.

1. Reset
2. Skip Rom
3. Start Conversion
4. Solange warten, bis dir Konvertierung fertig ist
5. Reset
6. Skip Rom
7. Read scratchpad - wobei man hier nach 2 byte auch aufhören kann
8. Reset

Ausschalten und Ruhe gönnen nicht vergessen. Die Temperatur driftet 
sonst.
Gast #1619804
Lesenswert?

Hallo leute,

ich bin zurzeit nicht mehr am Arbeitsplatz,hab schon Feierabend gemacht. 
Mit euren Vorschlägen werde ich morgen versuchen,den Sensor zum laufen 
bringen.

Ich werde euch morgen sagen,ob es funktioniert.

Wünsche euch einen schönen Feierabend.

Bis morgen leut!

Mfg
Gast #1620551
Lesenswert?

Hallo leut,

ich hab den Vorschlag von MagIO genommen und hab versucht,den Sensor zum 
laufen zu bringen.

Jetzt liefert der DS18S20 mir temp_MSB = 0x00 und temp_LSB = 0x2F . Nun 
kommt die Frage : Wie errechnet man aus diesen Werten die Temperatur in 
°C (Celsius) .

Muss mann ertsmal temp_LSB invertieren, eins addieren und dann durch 
zwei teilen?

Oder mann nimmt einfach temp_LSB und teilt es durch 2?


temp_LSB = 0x2F d.h 67 in Decimal /2 = 23.5
temp_MSB = 0x00 d.h positive Temperatur

Die gelieferte Temperatur ergibt sich dann : + 23.5°C , richtig?


Hier ist noch mal die modifizierte Code :
1
float Read_Temperature(void)
2
{
3
  unsigned char  Scratchpad[9];
4
  //unsigned char buffer[2];
5
  unsigned char temp_LSB,temp_MSB;
6
  unsigned char i,j;
7
  float final_temp;
8
  signed int temp;
9

10
  /*for(j=0;j<9;j++)
11
  {
12
    Scratchpad[j]=0;
13
  }
14
  */
15
  if(DS18_SendReset(3))
16
  {
17
    DS18_SetPortOutput();
18
    DS18_WriteByte(0xCC); //Skip ROM 
19
    DS18_WriteByte(0x44); //Start conversion
20
    for(j=0;j<128;j++)
21
    {Delay_us(40000);}
22
    //while(!DS18_Read()){}
23
    DS18_SendReset(3);
24
    DS18_WriteByte(0xCC); //Skip ROM
25
    DS18_WriteByte(0xBE); //Read Scratchpad
26

27
      for(i=0;i<9;i++)
28
      {
29
        Scratchpad[i]=DS18_ReadByte_1();
30
      }
31
    
32
  }
33

34
/*****************************************************************************
35
Read temperature from Scratchpad and convert the reading into °C
36
*****************************************************************************/
37
if(DS18_SendReset(3))
38
{
39
temp_MSB = Scratchpad[1];
40
temp_LSB = Scratchpad[0];
41
test = temp_LSB;
42
  
43
  
44
  if(temp_MSB <= 0x80)
45
  {
46
  temp_LSB = (temp_LSB/2);
47
  }
48

49
  temp_MSB = temp_MSB & 0x80;
50

51
  if(temp_MSB >= 0x80)
52
  {
53
  temp_LSB = (~temp_LSB)+1;
54
  }
55

56
  if(temp_MSB >= 0x80)
57
  {
58
  temp_LSB = (temp_LSB/2);
59
  }
60

61
  if(temp_MSB >= 0x80)
62
  {
63
  temp = ((-1)*temp_LSB);
64
  }
65

66
final_temp= ((float)temp);//*10 ;//Final Temperature in °C x10
67
                //e.g. 21.5°C = 215
68
}
69

70
return final_temp;
71

72
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren