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 | }
|