forked from LairdCP/BL600-Applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.temperature.sensor.sb
165 lines (135 loc) · 6.31 KB
/
ts.temperature.sensor.sb
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
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Poll Temperature sensor
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
//The temperature sensor is polled this timeout
#define TEMPERATURE_POLL_MS (2000)
//******************************************************************************
// Library Import
//******************************************************************************
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim adRpt$ as string //Contains the most recent Advertise report
dim scRpt$ as string //Contains the most recent scan report
dim stRsp$ as string //Uart rx data is stored here
dim rc //Return code from function calls
dim inconn //Will be 0 for no connection, otherwise nonzsero
dim oldtmp
//******************************************************************************
// Initialisse Global Variable
//******************************************************************************
inconn = 0 //No connection yet
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//==============================================================================
//==============================================================================
sub AssertResCode(rc,byval msg$ as string)
if(rc!=0)then
print "\n";msg$;" failed with ";integer.h' rc
else
print "\n";msg$;" OK"
endif
endsub
//==============================================================================
//==============================================================================
sub DbgMsg(byval msg$ as string)
print msg$
endsub
//==============================================================================
//==============================================================================
sub DbgVal(val)
print val
endsub
//==============================================================================
//==============================================================================
sub InitTempSensor()
//Poll sensor on a timer
TimerStart(0,TEMPERATURE_POLL_MS,1)
//Temperature sensor is AIN4 which is on the S3 pin
rc = GpioSetFunc(4,1,2) //Remove the pull resistor
AssertResCode(rc,"GpioSetFunc")
rc = GpioSetFunc(4,3,0) //Set as analog in
AssertResCode(rc,"GpioSetFunc")
endsub
//==============================================================================
// Bandgap = 1200mv
// Max Adc = 1024
// Input scaled by 2/3
//
// Hence mV = (((adc/MaxADC) * Bandgap)*3)/2
// refactoring for integer maths gives
// mv = (adc * Bandgap * 3)/(MaxADC*2)
// mv = (adc * 1200 * 3)/(1024 * 2)
// mv = (adc * 600 * 3)/1024
// mv = (adc * 150 * 3)/256
// mv = (adc * 75 * 3)/128
// mv = (adc * 225)/128
//==============================================================================
function Adc2Mv(adc)
adc = (adc * 225)/128
endfunc adc
//==============================================================================
// ((1864-mV)*16)/1889 * 10 in 10th of mV
//==============================================================================
function Mv2Temperature(mv)
mv = ((1864-mV) * 160)/189
endfunc mv
//******************************************************************************
// Handler definitions
//******************************************************************************
//==============================================================================
// This handler is called when TIMER 0 expires
//==============================================================================
function HandlerTimer0() as integer
dim mV,tmp
mv = Adc2Mv(GpioRead(4))
//DbgMsg("\nAdc mV=") : DbgVal(mv)
tmp = Mv2Temperature(mv)
DbgMsg("\nTemperature=") : DbgVal(tmp)
endfunc 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
//------------------------------------------------------------------------------
// Enable synchronous event handlers
//------------------------------------------------------------------------------
OnEvent EVTMR0 call HandlerTimer0
//------------------------------------------------------------------------------
// Initialise and then wait for events
//------------------------------------------------------------------------------
DbgMsg( "\nPoll Temperature")
InitTempSensor()
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// A Script can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
DbgMsg( "\nWait for event")
WaitEvent
DbgMsg( "\nPost WAITEVENT" )