-
Notifications
You must be signed in to change notification settings - Fork 5
/
Si7021.gcb
88 lines (65 loc) · 1.99 KB
/
Si7021.gcb
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
#chip mega328p,16
#option explicit
#include <millis.h> ' Include the Library
#define SAMPLERATE 4000 ' Flash rate in mS
#define hi2c_BAUD_RATE 100
#define hi2c_DATA PORTC.4
#define hi2c_CLOCK PORTC.5
hi2cMode Master
Dim Si7021_HighByte, Si7021_LowByte as byte
Dim SiRaw Alias Si7021_HighByte, Si7021_LowByte as Word
Dim Humidity,Temperature,TemperatureC,TemperatureF as Word
DIM Temperature as WORD
Dim Hundredths as Byte
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
#define USART_DELAY 10 ms
Dim CurMs, LstMs as word ' declare working variables
' Main ' This loop runs over and over forever.
LstMs = 0
CurMs = 0
' Main ' This loop runs over and over forever.
Do
CurMs = millis()
if CurMs - LstMs >= SAMPLERATE then ' required Time has Elapsed
Read_Humidity
Read_Temperature
LstMs = CurMs ' And Record Toggle Time
end if
Loop
Sub Read_Humidity
Hi2CStart
HI2cSend (0x80) 'Write
Hi2cSend (0xF5)
Hi2CReStart
HI2CSend (0x81) 'read
wait 50 ms ' for conversion ( could be less)
Hi2CReStart
HI2CSend (0x81) 'read
Hi2CReceive (Si7021_HighByte, ACK)
Hi2CReceive (Si7021_Lowbyte, NACK)
Hi2Cstop
'now do the maths
Humidity = (([long]SiRaw * 125) / 65536) -6
; Locate 1,12
If Humidity < 10 then hserPrint "0"
hserPrint "Humidity="+str(Humidity) : hserPrint "%"
HSerPrintCRLF
End Sub
Sub Read_Temperature
Hi2CStart
HI2CSend (0x80) 'Write
Hi2cSend (0xE0)
Hi2CReStart
HI2CSend (0x81) 'read
Hi2CReceive (Si7021_HighByte, ACK)
Hi2CReceive (Si7021_Lowbyte, NACK)
Hi2Cstop
wait 20 ms
TemperatureC = (([long]SiRaw * 17572) / 65536) - 4685
TemperatureF = (([long]TemperatureC * 9) /5) + 3200
Hundredths = TemperatureF % 100 'Modulud Divide
TemperatureF = TemperatureF / 100
; Locate 2,14
hserPrint "Temp.="+str(TemperatureC) : hserPrint "." : hserPrint str(Hundredths)+" "
End Sub