-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuttonsMultiplex.txt
218 lines (175 loc) · 4.52 KB
/
buttonsMultiplex.txt
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
\ *********************************************************************
\ buttons multiplex for ARDUINO NANO
\ Filename: buttonsMultiplex.txt
\ Date: 23 jul 2021
\ Updated: 29 jul 2021
\ File Version: 1.0
\ MCU: ARDUINO NANO
\ Copyright: Marc PETREMANN
\ Author: Marc PETREMANN
\ GNU General Public License
\ *********************************************************************
-defPin
marker -defPin
\ *** start part of pinsDefinitions.txt *****************
\ Use:
\ PORTD %10000000 defPIN: PD7 ( define portD pin #7)
: defPIN: ( PORTx mask --- <word> | <word> --- mask port)
create
c, c, \ compile pin mask and PORT
does>
dup c@ \ push pin mask
swap 1+ c@ \ push PORT
;
\ Turn a pin on, dont change others
: high ( pinmask portadr -- )
mset
;
\ Turn a pin off, dont change others
: low ( pinmask portadr -- )
mclr
;
\ Only for PORTx bits
\ address of DDRx is one less than address of PORTx
\ Set DDRx so its corresponding pin is output.
: output ( pinmask portadr -- )
1- high
;
\ Set DDRx so its corresponding pin is input.
: input ( pinmask portadr -- )
1- low
;
\ read the pins masked as input
: pin@ ( pinmask portaddr -- fl )
2- mtst \ select PINx register as input
if true
else false then
;
\ *** end part of pinsDefinitions.txt ******************
-74hc595
marker -74hc595
\ for Arduino NANO - port comm. with 74HC595
$2b constant PORTD
flash
\ SERial data
PORTD %00000100 defPIN: SER \ PD2 -> pin 14 on the 74HC595
\ storage-Register CLocK 1
PORTD %00001000 defPIN: RCLK1 \ PD3 -> pin 12 on the fist 74HC595
\ storage-Register CLocK 2
PORTD %00100000 defPIN: RCLK2 \ PD5 -> pin 12 on the second 74HC595
\ Shift-Register CLocK
PORTD %00010000 defPIN: SRCLK \ PD4 -> pin 11 on the 74HC595
ram
\ init ports affected to 74HC595
: init.74HC595 ( ---)
SER output
RCLK1 output
RCLK2 output
SRCLK output
SER low
RCLK1 high
RCLK2 high
SRCLK low
;
\ serialize 1 byto to 74HC595
1 r@ lshift \ calculate binary mask
over and \ bit 0 or not 0
if SER high \ datapin HIGH
else SER low \ datapin LOW
then
SRCLK high \ clockPin HIGH
SRCLK low \ clockPin LOW
next
drop
;
\ serialize 1 byte to buttons input
: toButtons ( c -- )
RCLK1 low \ latchPin LOW
csend
RCLK1 high \ latchPin HIGH
;
\ serialize 1 byte to LEDs
: toLeds ( c -- )
RCLK2 low \ latchPin LOW
csend
RCLK2 high \ latchPin HIGH
;
\ set all bits to high level on buttons input
: FFtoButtons ( -- )
$ff toButtons
;
-buttonsScan
marker -buttonsScan
\ input pin - buttons output
$25 constant PORTB
flash
PORTB %00000001 defPIN: BUTTONS \ PB0 (8)
ram
\ init. PORT B PB0
: init.buttons ( ---)
BUTTONS input
;
init.74HC595
init.buttons
variable LEDs \ store result of active buttons
\ toggle LED state, c value 1 or 2 or 4.. 128
: toggleLED ( c --- )
dup LEDs mtst \ test if bit = 1
if LEDs mclr \ clear bit
else LEDs mset \ set bit
then
;
\ set LED state. n between 0..7
: setLED ( n -- )
1 swap lshift \ convert n to 2 EXP n
toggleLED \ toggle LED/relay state
LEDs @ toLeds \ change LEDs/relay state
;
: buttonsScan ( -- )
8 for
1 r@ lshift toButtons
BUTTONS pin@ \ button activated?
if
r@ setLED
then
next
;
\ @TODO: test and debug following words
\ creation of pseudo register
\ : pseudoReg: ( --- name | --- nameAddr+2)
\ create
\ 0 c, 0 c, 0 c,
\ does>
\ 2+
\ ;
\ for later usage
defer buttons.action
\ set ON or OFF the selected switch
\ : button.detect ( n ---)
\ BUTTONS pin@ \ switch active ?
\ if
\ SWITCHES mset \ set switch ON
\ else
\ SWITCHES mclr \ set switch OFF
\ then
\ buttons.action
\ SWITCHES dup c@ swap 2- c!
\ ;
\ main loop
\ : buttons.loop
\ fl- \ Disable writes to flash and eeprom
\ begin
\ 1 \ start index
\ NB_REG_PINS for
\ dup csend
\ 10 ms
\ dup button.detect
\ SWITCHES c@ . cr
\ 2*
\ next
\ drop
\ key? until
\ fl+ \ Enable writes to flash and eeprom
\ ;
init.74HC595
init.buttons