-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tbs
292 lines (233 loc) · 9.16 KB
/
main.tbs
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'This Tibbo BASIC application is a sample code for testing Tibbit#62 (1-wire/signle-wire)
'This sample is written in Tibbo BASIC.
'This application is designed in a way that expects CH1 of Tibbit to be set into Single-wire mode and
' CH2 to be set into 1-wire. Therefore, on CH1 yo can connect only one DHT22 sensor whereas on CH2 you can connect multiple 1-wire temperature sensors.
' To simplify the test, it is assumed that we would like to provide 5V power to sensors from IO1. Therefore, we need to turn the output power on (IO1).
include "global.tbh"
dim tbt_channel as byte
dim temp, hum as real
dim timer as byte
'Uncomment the following line if you want to use imperial units
'#define USE_IMPERIAL_UNITS
#ifdef USE_IMPERIAL_UNITS
#define UNITS TBT62_IMPERIAL_UNITS
#else
#define UNITS TBT62_METRIC_UNITS
#endif
sub on_sys_init()
dim Sensor_ID as string(8)
dim i as byte
dim voltage as string(2)
' -----------------------Upgrade Tibbit Firmware-----------------------
' ----------------------------Only for test----------------------------
' Place the Tibbit on one of the slots that can handle i2c.
' pass the SCL,SDA,-MCLR io numbers into the pic_lvp_start function, respectively.
' Add the firmware file as hex file to the root directory and add it to the resource files.
' Comment out this part of code to be safe while operating after LVP upgrade.
' Upgrade code:
'pic_lvp_start(PL_IO_NUM_11_TX1, PL_IO_NUM_10_RX1, PL_IO_NUM_1) ' TPP3G2- on S5
'if not pic_lvp_upload_firmware("Tibbit62_FW_00_02.hex") then
' sys.halt
'end if
'-----------------------End of Upgrading Firmware-----------------------
timer = 0
' Initializing the Tibbit
' This process has debugprints for testing purposes
sys.debugprint("TBT62 initialize - ")
select case tbt62_init("MAIN", TBT62_S1_DATA, TBT62_S1_CLK, TBT62_S1_RESET, TBT62_S1_BUSY, UNITS, tbt_channel, YES)
case TBT62_INIT_OK:
sys.debugprint("Success" + CR_LF)
case TBT62_INIT_ERROR_SI2C_NO_EMPTY_CHANNEL:
sys.debugprint("No empty SI2C channel!" + CR_LF)
exit sub
case TBT62_INIT_ERROR_NO_VALID_VERSION:
sys.debugprint("No Tibbit installed or wrong version!" + CR_LF)
exit sub
case TBT62_INIT_ERROR_FAULT_CHECK_3V3:
sys.debugprint("3.3V Fault!" + CR_LF)
case TBT62_INIT_ERROR_FAULT_CHECK_VCC:
sys.debugprint("VCC Fault!" + CR_LF)
case TBT62_INIT_ERROR_POWER_FAULT_CHECK:
sys.debugprint("Power Fault!" + CR_LF)
end select
' Set VCC_OUT on
' This is capable of outputting 5V power on IO1 to sensors
sys.debugprint("Set VCC on - ")
if not tbt62_vcc_control(TBT62_VCC_ON, tbt_channel) then
sys.debugprint("fault!" + CR_LF)
exit sub
else
sys.debugprint("succesful" + CR_LF)
end if
dim voltager as real
' Read the VCC voltage with ADC
sys.debugprint("Get VCC voltage - ")
voltager = tbt62_get_VCC_voltage(tbt_channel)
if voltager = -1000 then
sys.debugprint("Command failed!" + CR_LF)
exit sub
end if
sys.debugprint("Voltage Vcc : " + ftostr(voltager, FTOSTR_MODE_PLAIN, 3) + "V" + CR_LF)
' Get voltage 3V3
sys.debugprint("Get voltage 3v3 - ")
voltager = tbt62_get_3V3_voltage(tbt_channel)
if voltager = -1000 then
sys.debugprint("Command failed!" + CR_LF)
exit sub
end if
sys.debugprint("Voltage 3.3V : " + ftostr(voltager, FTOSTR_MODE_PLAIN, 3) + "V" + CR_LF)
' Get power fault
sys.debugprint("Get power fault - ")
dim powerFault as byte
powerFault = tbt62_get_power_fault(tbt_channel)
if powerFault = 255 then
sys.debugprint("Command failed!" + CR_LF)
exit sub
end if
sys.debugprint("Power Fault status : " + str(powerFault) + CR_LF)
' Set Channel 1 as Single-wire MODE
sys.debugprint("Single Wire test" + CR_LF)
sys.debugprint("Set mode Single Wire for channel 1 - ")
if not tbt62_set_mode(TBT62_SINGLE_WIRE, TBT62_CHANNEL_1, tbt_channel) then
sys.debugprint("Command failed!" + CR_LF)
exit sub
else
sys.debugprint("success" + CR_LF)
end if
sys.debugprint("Get Temperature and Humidity - ")
select case tbt62_sw_get_temp_hum(temp, hum, DHT22, TBT62_CHANNEL_1, tbt_channel)
case TBT62_NO_FAULT:
sys.debugprint("success" + CR_LF)
#ifndef USE_IMPERIAL_UNITS
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°C, Hum: " + ftostr(hum, FTOSTR_MODE_PLAIN, 5) + "% RH" + CR_LF)
#else
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°F, Hum: " + ftostr(hum, FTOSTR_MODE_PLAIN, 5) + "% RH" + CR_LF)
#endif
case TBT62_FAULT_TIBBIT:
sys.debugprint("Command failed!" + CR_LF)
' exit sub
case TBT62_FAULT_CONTROLSUM:
sys.debugprint("Checksum fault!" + CR_LF)
' exit sub
case TBT62_FAULT_NO_SENSOR
sys.debugprint("No sensor!" + CR_LF)
end select
' OneWire mode
sys.debugprint("OneWire test" + CR_LF)
sys.debugprint("Select channel 2 - ")
if not tbt62_send_command(TBT62_CMD_SELECTCHANNEL + TBT62_CHANNEL_2, tbt_channel) then
sys.debugprint("send command fault!" + CR_LF)
exit sub
else
sys.debugprint("success" + CR_LF)
end if
sys.debugprint("Set mode OneWire for channel 2 - ")
if not tbt62_set_mode(TBT62_ONE_WIRE, TBT62_CHANNEL_2, tbt_channel) then
sys.debugprint("send command fault!" + CR_LF)
exit sub
else
sys.debugprint("success" + CR_LF)
end if
' Search devices and request temperature from found temperature sensors
'Find first device
sys.debugprint("Find first device - ")
if not tbt62_send_command(TBT62_CMD_SEARCHFIRST, tbt_channel) then
sys.debugprint(" send command fault!" + CR_LF)
exit sub
end if
while (true)
' Wait BUSY
sys.debugprint("finding. Wait BUSY - ")
while (tbt62_checkBusy(tbt_channel) = false)
wend
' Read results of search sensor
sys.debugprint("Read ID - ")
if not tbt62_read_data(Sensor_ID, 8, tbt_channel) then
sys.debugprint("send command fault!" + CR_LF)
exit sub
end if
' Check Sensor ID
if (Sensor_ID = (chr(255) + chr(255) + chr(255) + chr(255) + chr(255) + chr(255) + chr(255) + chr(255))) then
sys.debugprint(" Sensors not found!" + CR_LF)
exit while
end if
sys.debugprint("Sensor ID: " + tbt62_ID_Hex(Sensor_ID) + CR_LF)
' Get temperature
sys.debugprint("Get temperature..." + CR_LF)
select case tbt62_ow_get_temperature(temp, Sensor_ID, TBT62_CHANNEL_2, tbt_channel)
case TBT62_NO_FAULT:
#ifndef USE_IMPERIAL_UNITS
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°C" + CR_LF)
#else
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°F" + CR_LF)
#endif
case TBT62_FAULT_TIBBIT:
sys.debugprint(" send command fault!" + CR_LF)
exit sub
case TBT62_FAULT_CONTROLSUM:
sys.debugprint("Control sum fault!" + CR_LF)
case TBT62_FAULT_NO_SENSOR
sys.debugprint("No sensor!" + CR_LF)
end select
'Find next device
sys.debugprint("Find next device - ")
if not tbt62_send_command(TBT62_CMD_SEARCHNEXT, tbt_channel) then
sys.debugprint(" send command fault!" + CR_LF)
exit sub
end if
wend
sys.debugprint("--------End of Sys_Init--------" + CR_LF)
sys.debugprint("***************************************" + CR_LF)
end sub
' In on_sys_timer() sensors are periodically polled
sub on_sys_timer()
if timer < 20 then
timer = timer + 1
else
timer = 0
' Single Wire MODE
sys.debugprint("--------Single Wire test on channel 1- DHT22 connected" + CR_LF)
sys.debugprint("Get temp and hum - ")
select case tbt62_sw_get_temp_hum(temp, hum, DHT22, TBT62_CHANNEL_1, tbt_channel)
case TBT62_NO_FAULT:
sys.debugprint("success" + CR_LF)
#ifndef USE_IMPERIAL_UNITS
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°C, Hum: " + ftostr(hum, FTOSTR_MODE_PLAIN, 5) + "% RH" + CR_LF)
#else
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°F, Hum: " + ftostr(hum, FTOSTR_MODE_PLAIN, 5) + "% RH" + CR_LF)
#endif
case TBT62_FAULT_TIBBIT:
sys.debugprint("send command fault!" + CR_LF)
' exit sub
case TBT62_FAULT_CONTROLSUM:
sys.debugprint("Control sum fault!" + CR_LF)
' exit sub
case TBT62_FAULT_NO_SENSOR
sys.debugprint("No sensor!" + CR_LF)
end select
sys.debugprint("--------End of Single Wire test" + CR_LF)
' Get temperature of a specified sensor
' Change SID below if you know your sensor's SID. If you don't know it, choose from the scanned IDs printed in on_sys_init() scanning phase...
dim SID as string(8)
dim tmp as real
SID="\x28\x6A\xDF\x03\x00\x00\x00\xAB" 'Change this address to match the sensor you want to filter out from the connected sensors on bus
sys.debugprint("--------OneWire test. Get temperature" + CR_LF)
sys.debugprint("--------Sensor ID:286ADF03000000AB" + CR_LF)
select case tbt62_ow_get_temperature(temp, SID, TBT62_CHANNEL_2, tbt_channel)
case TBT62_NO_FAULT:
#ifndef USE_IMPERIAL_UNITS
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°C" + CR_LF)
#else
sys.debugprint("Temp: " + ftostr(temp, FTOSTR_MODE_PLAIN, 5)+ "°F" + CR_LF)
#endif
case TBT62_FAULT_TIBBIT:
sys.debugprint(" send command fault!" + CR_LF)
case TBT62_FAULT_CONTROLSUM:
sys.debugprint("Control sum fault!" + CR_LF)
case TBT62_FAULT_NO_SENSOR
sys.debugprint("No sensor!" + CR_LF)
end select
sys.debugprint("--------End of reading specific One-wire sensor" + CR_LF)
sys.debugprint("***************************************" + CR_LF)
end if
end sub