-
Notifications
You must be signed in to change notification settings - Fork 3
/
tsl230.spin
125 lines (108 loc) · 12.7 KB
/
tsl230.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
''*******************************************
''* TSL230 Light to Frequency Driver v1.0 *
''* Author: Paul Baker *
''* Copyright (c) 2007 Parallax, Inc. *
''* See end of file for terms of use. *
''*******************************************
{{********************************************************
Taos TSL230 light to frequency sensor v1.0 driver
with manual and auto scaling capabilities
┌──────────┐
ctrlpinbase ──│1 o 8│──┳──┐
│ │ │
ctrlpinbase+1 ──│2 7│──┘
│ [] │
┌──│3 6│──── inpin
│ │ │
┣──│4 5│──┘ 3.3V
└──────────┘
*********************************************************}}
CON
_clkmode = xtal1 + pll16x
_XinFREQ = 5_000_000
ctrmode = $28000000
VAR
long freq, cog
long scale, cbase, sps, auto
PUB Stop
'' Stop driver - frees a cog
if cog
cogstop(cog~ - 1)
{{********************************************************
Start method to initialize driver, arguments:
inpin - pin number output of tsl230 is connected
ctrlpinbase - pin number connected to S0
S1 connected to ctrlpinbase + 1
samplefreq - number of measurements per second
higher values reduce significant digits
autoscale - boolean indicates if autoscaling is used
with autoscale on, output range is between
0 and ~160,000,000
with autoscale off, output range is between
0 and ~1,600,000
*********************************************************}}
PUB Start(inpin, ctrlpinbase, samplefreq, autoscale): okay
scale := %11 'set inital scale to maximum
cbase := ctrlpinbase 'copy parameters
sps := samplefreq
auto := autoscale
dira := %11 << cbase 'set control pins to output
outa := %11 << cbase 'set scale
ctra_ := ctrmode + inpin 'compute counter mode
cntadd := 80_000_000 / samplefreq 'compute wait period
cog := okay := cognew(@entry, @freq) 'start driver
PUB GetSample : val
val := freq * sps * lookup(scale: 100, 10, 1) 'compute scaled frequency
if auto 'autoscaling code
if val > 1_000_000 'if output exceeds 1 MHz, decrease gain
scale := --scale #> 1
elseif val < 10_000 'if output less than 10 kHz, increase gain
scale := ++scale <# 3
outa := scale << cbase
{{********************************************************
SetScale manually sets the gain, works in both autoscale
and manual modes, though autoscale will readjust the
scale if set too low or high
*********************************************************}}
PUB SetScale(range)
scale := 1 #> range <# 3 'limit argument range to 1,2 or 3
outa := scale << cbase 'set scale
DAT
{{********************************************************
Assembly driver for tsl230
*********************************************************}}
org
entry mov ctra, ctra_ 'setup counter to count positive edges
mov frqa, #1 'increment for each edge seen
mov cnt_, cnt 'initialize waitperiod
add cnt_, cntadd
:loop waitcnt cnt_, cntadd 'wait for next sampling period
mov new, phsa 'record new count
mov temp, new 'make second copy
sub new, old 'compute cycles since last
mov old, temp 'record a new old count
wrlong new, par 'write number of cycles since last period to hub memory
jmp #:loop
ctra_ long 0
cntadd long 0
cnt_ res 1
new res 1
old res 1
temp res 1
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}