forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.dac.duty.spin
135 lines (116 loc) · 4.47 KB
/
signal.dac.duty.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
{
--------------------------------------------
Filename: signal.dac.duty.spin
Author: Jesse Burt
Description: 2-channel DAC object using the duty mode of the counters as output
Started Feb 16, 2020
Updated Jul 21, 2023
See end of file for terms of use.
--------------------------------------------
NOTE: This object is based on the Parallax Simple-Library functionality
}
CON
{ default I/O configuration - can be overridden in parent object }
CH0 = 0
CH1 = 1
DAC_RES_BITS = 16
VAR
long _dac_stack[50]
long _ctra, _ctrb, _frqa, _frqb
byte _dac_res
byte _ch0, _ch1
byte _cog
PUB start(): status
' Start using default I/O settings
return startx(CH0, CH1, DAC_RES_BITS)
PUB startx(ch0_pin, ch1_pin, bits): status
' ch0_pin: DAC channel 0 I/O pin
' ch1_pin: DAC channel 1 I/O pin (optional; pass invalid pin to ignore)
' bits: DAC resolution (bits)
if (lookup(ch0_pin: 0..31)) ' validate ch0 pin
_ch0 := ch0_pin
outa[ch0_pin] := 0
dira[ch0_pin] := 1
if (lookup(ch1_pin: 0..31)) ' optional - ignore if outside of 0..31 range
_ch1 := ch1_pin
outa[ch1_pin] := 0
dira[ch1_pin] := 1
if (_cog := cognew(cog_dac_loop{}, @_dac_stack) + 1)
dac_res(bits) ' set resolution (default to 8 if invalid)
return _cog
return FALSE ' if we got here, something went wrong
PUB stop
' Stop the DAC cog
if (_cog)
cogstop(_cog-1)
_cog := 0
PUB output(channel, value)
' Output value to DAC
' Valid values:
' channel: 0, 1
' value: 0..(1 << Resolution)-1
' Voltage output will be approx: value * (3.3V / 2^Resolution)
' Example:
' OBJ
'
' dac : "signal.dac.duty"
'
' PUB example_method
'
' dac.startx(26, 27, 8)' ch0 = GPIO 26, ch1 = GPIO 27; set resolution to 8 bits
' dac.output(0, 0) ' Output 0V on channel 0
' dac.output(1, 127) ' Output 1.65V on channel 1
' dac.output(0, 255) ' Output 3.3V on channel 0
ifnot (channel) ' Channel 0
_frqa := (value << _dac_res)
else ' Channel 1
_frqb := (value << _dac_res)
PUB dac_res(bits)
' Set DAC resolution, in bits
' Valid values: 1..32
' Any other value sets a default resolution of 8 bits
case bits
1..32:
other:
bits := 8
_dac_res := 32-bits
#include "core.con.counters.spin"
PRI cog_dac_loop | pin
' Digital to Analog Converter
_ctra := (DUTY_SINGLEEND + _ch0) ' Set counters to single-ended duty-cycle mode
_ctrb := (DUTY_SINGLEEND + _ch1)
repeat
if (_ctra <> ctra)
if (ctra <> 0)
pin := (ctra & %111111)
dira &= (1 << pin) ^ $FFFFFFFF
ctra := _ctra
if (_ctra <> 0)
pin := (ctra & %111111)
dira |= (1 << pin)
if (_ctrb <> ctrb)
if (ctrb <> 0)
pin := (ctrb & %111111)
dira &= (1 << pin) ^ $FFFFFFFF
ctrb := _ctrb
if (ctrb <> 0)
pin := (ctrb & %111111)
dira |= (1 << pin)
frqa := _frqa
frqb := _frqb
DAT
{
Copyright 2022 Jesse Burt
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.
}