-
Notifications
You must be signed in to change notification settings - Fork 24
/
ow_get_temps.jal
170 lines (135 loc) · 4.09 KB
/
ow_get_temps.jal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
--
-- $Id: ow_get_temps.jal,v 1.3 2014/01/01 02:32:47 cvsusers Exp $
--
-- Get the temperature reading from each of the devices found on the 1-wire bus.
--
--
-- Dallas on-line docs conversion/display routine for DS18S20.
--
-- Arguments are:-
--
-- temp_msb -- Most significant byte of device scratch-pad.
-- temp_lsb -- Least significant byte of device scratch-pad.
--
-- This routine is specific to the DS18S20 temperature sensor, but can
-- easily be adapted for use for any other, similar device.
--
procedure DS18S20_Disp (byte in temp_msb, byte in temp_lsb) is
const byte tout[] = "Degrees C: ";
const byte minus[] = "-";
const byte point[] = ".";
var byte fract = 0;
var bit halfdeg at temp_lsb : 0;
if (halfdeg) then
fract = 5; -- If half-degree bit is set, print.
end if
if (temp_msb <= 0x80) then
temp_lsb = (temp_lsb/2); -- Shift to get whole degree.
end if
temp_msb = temp_msb & 0x80; -- Mask all but the sign bit.
if (temp_msb >= 0x80) then
temp_lsb = (! temp_lsb) + 1; -- Twos complement.
end if
if (temp_msb >= 0x80) then
temp_lsb = (temp_lsb/2); -- Shift to get whole degree.
end if
print_string(xport, tout); -- Print prefix "Deg C".
if (temp_msb >= 0x80) then
-- temp_lsb = ((-1)*temp_lsb); -- (original C code)
print_string(xport, minus); -- Add "-" prefix for minus temperatures.
end if
print_byte_dec(xport, temp_lsb); -- Print temperature in degrees C.
print_string(xport, point); -- Print decimal point.
print_byte_dec(xport, fract); -- Print fractional part.
print_crlf(xport);
return;
end procedure
--
-- Initialize the 1-wire bus to address the specific device currently
-- pointed to by the serial number stored in the ROM_NO array.
--
procedure AddressDevice is
var byte i = 0;
-- Reset the bus. Return an error if there are no devices found.
if (! OWReset()) then
ow_perror(0); -- OWERROR(OWERROR_NO_DEVICES_ON_NET);
return;
end if
-- Send the "Match ROM" command and then the serial number of
-- the device to be addressed.
OWWriteByte(MATCH_ROM);
-- for SN_LENGTH using i loop
while (i < SN_LENGTH) loop
OWWriteByte(ROM_NO[i]);
i = i + 1;
end loop
return;
end procedure
--
-- Display the temperature for all of the devices stored in the FamilySN[]
-- array.
--
procedure DisplayStored is
var byte i = 0;
-- for (DevCount) using i loop
-- for (SN_LENGTH) using j loop
while (i < DevCount) loop
var byte j = 0;
while (j < SN_LENGTH) loop
print_byte_hex(xport, FamilySN[i].sn[j]);
j = j + 1;
end loop
const byte tdst[] = ":\n\r\t ";
print_string(xport, tdst);
DS18S20_Disp(FamilySN[i].tmsb, FamilySN[i].tlsb);
i = i + 1;
end loop
print_crlf(xport);
return;
end procedure
--
-- Get temperatures.
--
-- Takes the device list found in the global array FamilySN[] and interrogates
-- each device in turn for the temperature reading.
-- Stores the temperature read from the device back into the FamilySN[] array
-- of records.
--
procedure GetTemperatures is
var byte i = 0, j = 0;
-- for (DevCount - 1) using i loop
while (i < DevCount) loop
if (OWDEBUG) then
const byte gtinf[] = "GetTemp ";
print_string(xport, gtinf);
print_byte_dec(xport, DevCount);
const byte spc[] = " ";
print_string(xport, spc);
print_byte_dec(xport, i);
print_crlf(xport);
end if
owSerialNum(i, TRUE); -- Get serial number for this device from
-- the FamilySN array and put it into the
-- ROM_NO working array.
-- Send the "Temperature Convert" command to initialize a reading
-- from the addressed device.
AddressDevice();
OWWriteByte(T_CONVERT);
delay_1s(1); -- Give conversion time to finish.
-- Send the "Read Scratch-Pad" command to initiate transfer the actual
-- temperature data.
AddressDevice();
OWWriteByte(READ_SPAD);
-- Store the temperature data from scratch-pad memory into
-- the record structure for this device.
FamilySN[i].tlsb = OWReadByte();
FamilySN[i].tmsb = OWReadByte();
i = i + 1;
end loop
-- Reset the bus to let all devices know that we're done.
if (! OWReset()) then
ow_perror(0); -- OWERROR(OWERROR_NO_DEVICES_ON_NET);
return;
end if
return;
end procedure