-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sensors.spin
145 lines (120 loc) · 5.86 KB
/
Sensors.spin
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
CON
_clkmode=xtal1+pll16x
_xinfreq=5_000_000
device=$32
datapin = 1 'SDA
clockPin = 0 'SCL
obj
SC : "Screen"
SN : "Simple_Numbers"
CN : "Converter"
Str : "STRINGS2"
var
long X
pub PrintDeg
SC.Init
getHeading
SC.print(SN.decf(||X/10,3))
SC.print(string("."))
SC.print(SN.dec(||X//10))
pub PrintHeading
SC.Print(getDirection)
pub StandBy
start
send($32)
send($83) 'sleep mode
stop
pub Resume'fix this, or we waste 4.5 ma!
start
send($32)
send($84) 'exit sleep mode to standby mode
stop
waitcnt(clkfreq/1000 + cnt) 'wait milisecond
start
send($32)
send($75) 'endter run mode
stop
pub PrintTemp| var1,toInt,final,tdec
SC.InIt
start
send($91)
'var1:=(receive(true) << 4) | (receive(true)>>4)
var1:=(receive(true)<<4) | (receive(true)>>4)
stop
toInt:=CN.StrToDec(SN.dec(var1))
'var1:=var1*0.0625
'((var1*625)*9/5)+32
' final:=FM.FMul(var1,0.0625)
final:=(((var1*625)*9)/5)+320000
SC.print(SN.dec((final/10000)))
SC.print(string("."))
if final/10000 < 100
tdec:=2
else
tdec:=3
SC.print(Str.parse(SN.dec(final),tdec,1))
' sc.position(2,0)
' sc.print(SN.dec(final))
pub getHeading|Y,Z
start
send($32)
send($50) 'get tilt heading info!
stop
waitcnt(clkfreq/1000 + cnt) 'wait milisecond
start
send($33)
X:=(receive(true) << 8) | (receive(true))
Y:=(receive(true) << 8) | (receive(true))
Z:=(receive(true) << 8) | (receive(false))
stop
~~X
X:=X
pub getDirection| heading
heading:=X/10
if heading => 0 and heading=<44
return string("N ")
if heading => 45 and heading=< 89
return string("NE")
if heading => 90 and heading=< 134
return string("E ")
if heading =>135 and heading=<179
return string("SE")
if heading => 180 and heading=<224
return string("S ")
if heading=> 225 and heading=<269
return string("SW")
if heading=> 270 and heading=<314
return string("W ")
if heading=>315 and heading=<360
return string("NW")
PRI send(value) ' I²C Send data - 4 Stack Longs
value := ((!value) >< 8)
repeat 8
dira[dataPin] := value
dira[clockPin] := false
dira[clockPin] := true
value >>= 1
dira[dataPin] := false
dira[clockPin] := false
result := !(ina[dataPin])
dira[clockPin] := true
dira[dataPin] := true
PRI receive(aknowledge) ' I²C receive data - 4 Stack Longs
dira[dataPin] := false
repeat 8
result <<= 1
dira[clockPin] := false
result |= ina[dataPin]
dira[clockPin] := true
dira[dataPin] := aknowledge
dira[clockPin] := false
dira[clockPin] := true
dira[dataPin] := true
PRI start ' 3 Stack Longs
outa[dataPin] := false
outa[clockPin] := false
dira[dataPin] := true
dira[clockPin] := true
PRI stop ' 3 Stack Longs
dira[clockPin] := false
dira[dataPin] := false