85e3e09a580e354dc32e5ba709bd2fe0f7d45946
Chip-dat/74xx-dat/74HC165-dat/74HC165-dat.md
... | ... | @@ -0,0 +1,14 @@ |
1 | + |
|
2 | +# 74HC165-dat |
|
3 | + |
|
4 | +## demo code |
|
5 | + |
|
6 | +- [[74HC165-rpi-1.py]] |
|
7 | +- copy from https://forums.raspberrypi.com/viewtopic.php?t=254053 |
|
8 | +- [[rpi-python-dat]] |
|
9 | + |
|
10 | +## ref |
|
11 | + |
|
12 | +- [[74xx-dat]] |
|
13 | + |
|
14 | +- DS - |
|
... | ... | \ No newline at end of file |
Chip-dat/74xx-dat/74HC165-dat/74HC165-rpi-1.py
... | ... | @@ -0,0 +1,228 @@ |
1 | + |
|
2 | +#!/usr/bin/env python |
|
3 | + |
|
4 | +import time |
|
5 | +import threading |
|
6 | +import pigpio |
|
7 | + |
|
8 | +MAIN_SPI = 0 |
|
9 | +AUX_SPI = 1 |
|
10 | + |
|
11 | +class PISO(threading.Thread): |
|
12 | + |
|
13 | + """ |
|
14 | + A class to read multiple inputs from one or more |
|
15 | + SN74HC165 PISO (Parallel In Serial Out) shift |
|
16 | + registers. |
|
17 | + |
|
18 | + Either the main SPI or auxiliary SPI peripheral |
|
19 | + is used to clock the data off the chip. SPI is |
|
20 | + used for performance reasons. |
|
21 | + |
|
22 | + Connect a GPIO (referred to as SH_LD) to pin 1 of |
|
23 | + the first chip. |
|
24 | + |
|
25 | + Connect SPI SCLK to pin 2 of the first chip. SCLK |
|
26 | + will be GPIO 11 if the main SPI is being used and |
|
27 | + GPIO 21 if the auxiliary SPI is being used. |
|
28 | + |
|
29 | + Connect SPI MISO to pin 9 of the last chip. MISO |
|
30 | + will be GPIO 9 if the main SPI is being used and |
|
31 | + GPIO 19 if the auxiliary SPI is being used. |
|
32 | + |
|
33 | + First chip |
|
34 | + |
|
35 | + Pi GPIO ------> SH/LD 1 o 16 Vcc ------ 3V3 |
|
36 | + Pi SPI clock -> CLK 2 15 CLK INH -- Ground |
|
37 | + E 3 14 D |
|
38 | + F 4 13 C |
|
39 | + G 5 12 B |
|
40 | + H 6 11 A |
|
41 | + Don't connect /Qh 7 10 SER ------ Ground |
|
42 | + Ground -------- GND 8 9 Qh ------> next SER |
|
43 | + |
|
44 | + |
|
45 | + Middle chips |
|
46 | + |
|
47 | + prior SH/LD --> SH/LD 1 o 16 Vcc ------ 3V3 |
|
48 | + prior CLK ----> CLK 2 15 CLK INH -- Ground |
|
49 | + E 3 14 D |
|
50 | + F 4 13 C |
|
51 | + G 5 12 B |
|
52 | + H 6 11 A |
|
53 | + Don't connect /Qh 7 10 SER <----- prior Qh |
|
54 | + Ground -------- GND 8 9 Qh ------> next SER |
|
55 | + |
|
56 | + |
|
57 | + Last chip |
|
58 | + |
|
59 | + prior SH/LD --> SH/LD 1 o 16 Vcc ------ 3V3 |
|
60 | + prior CLK ----> CLK 2 15 CLK INH -- Ground |
|
61 | + E 3 14 D |
|
62 | + F 4 13 C |
|
63 | + G 5 12 B |
|
64 | + H 6 11 A |
|
65 | + Don't connect /Qh 7 10 SER <----- prior Qh |
|
66 | + Ground -------- GND 8 9 Qh ------> Pi SPI MISO |
|
67 | + """ |
|
68 | + SPI_FLAGS_AUX = 256 # use auxiliary SPI device |
|
69 | + SPI_FLAGS_NO_CE0 = 32 # don't use CE0 |
|
70 | + |
|
71 | + def __init__(self, pi, SH_LD, SPI_device=MAIN_SPI, |
|
72 | + chips=1, reads_per_second=100, callback=None): |
|
73 | + """ |
|
74 | + Instantiate with the connection to the Pi. |
|
75 | + |
|
76 | + SL_LD is the GPIO connected to the shift/load pin |
|
77 | + of the shift register. |
|
78 | + |
|
79 | + SPI_device is either MAIN_SPI (default) or AUX_SPI. |
|
80 | + |
|
81 | + chips is the number of SN74HC165 being used (defaults |
|
82 | + to 1). |
|
83 | + |
|
84 | + reads_per_second is the number of readings to |
|
85 | + be made per second (defaults to 100). |
|
86 | + |
|
87 | + If a callback is specified it will be called once |
|
88 | + for each pin level change. The callback receives |
|
89 | + the pin, the new level, and the time of reading. |
|
90 | + """ |
|
91 | + |
|
92 | + threading.Thread.__init__(self) |
|
93 | + |
|
94 | + self._lock = threading.Lock() |
|
95 | + |
|
96 | + self.set_reads_per_second(reads_per_second) |
|
97 | + |
|
98 | + self._pi = pi |
|
99 | + |
|
100 | + assert 0 <= SH_LD <= 53 |
|
101 | + self._SH_LD = SH_LD |
|
102 | + |
|
103 | + assert 0 <= SPI_device <= 1 |
|
104 | + flags = self.SPI_FLAGS_NO_CE0 |
|
105 | + if SPI_device == AUX_SPI: |
|
106 | + flags |= self.SPI_FLAGS_AUX |
|
107 | + self._h = pi.spi_open(0, 5000000, flags) |
|
108 | + |
|
109 | + assert 1 <= chips |
|
110 | + self._chips = chips |
|
111 | + |
|
112 | + self._callback = callback |
|
113 | + |
|
114 | + self._last_data = [0]*chips |
|
115 | + |
|
116 | + self._exiting = False |
|
117 | + |
|
118 | + self.daemon = True |
|
119 | + |
|
120 | + self.start() |
|
121 | + |
|
122 | + def read(self): |
|
123 | + """ |
|
124 | + Reads the shift registers and returns the |
|
125 | + readings as a byte array (one byte per |
|
126 | + chip). |
|
127 | + |
|
128 | + In addition if a callback is registered it |
|
129 | + will be called for each pin level change. |
|
130 | + """ |
|
131 | + data = None |
|
132 | + with self._lock: |
|
133 | + if self._exiting: |
|
134 | + return data |
|
135 | + self._pi.gpio_trigger(self._SH_LD, 1, 0) |
|
136 | + read_time = time.time() |
|
137 | + count, data = self._pi.spi_read(self._h, self._chips) |
|
138 | + if data != self._last_data: |
|
139 | + if self._callback is not None: |
|
140 | + # Emit callbacks for changed levels. |
|
141 | + for i in range(self._chips): |
|
142 | + if data[i] != self._last_data[i]: |
|
143 | + for j in range(8): |
|
144 | + if ((data[i] & (1<<j)) != |
|
145 | + (self._last_data[i] & (1<<j))): |
|
146 | + self._callback((i*8)+j, |
|
147 | + (data[i]>>j)&1, |
|
148 | + read_time) |
|
149 | + self._last_data = data |
|
150 | + return data |
|
151 | + |
|
152 | + def set_callback(self, callback): |
|
153 | + """ |
|
154 | + Sets the callback function. The callback will |
|
155 | + be called for each pin level change. |
|
156 | + |
|
157 | + The callback receives three parameters: |
|
158 | + the pin |
|
159 | + the new level |
|
160 | + the time of the reading |
|
161 | + |
|
162 | + There are 8 pins per chip. The last chip has |
|
163 | + pins numbered 0 to 7, the next to last chip has |
|
164 | + pins numbered 8 to 15 etc. |
|
165 | + |
|
166 | + The callback is cleared by setting it to None. |
|
167 | + """ |
|
168 | + with self._lock: |
|
169 | + self._callback = callback |
|
170 | + |
|
171 | + def set_reads_per_second(self, reads_per_second): |
|
172 | + """ |
|
173 | + Sets the number of chip reads per second. |
|
174 | + It must be between 1 and 5000 reads per |
|
175 | + second. |
|
176 | + """ |
|
177 | + with self._lock: |
|
178 | + assert 1 <= reads_per_second <= 5000 |
|
179 | + self._interval = 1.0 / reads_per_second |
|
180 | + |
|
181 | + def cancel(self): |
|
182 | + """ |
|
183 | + Cancels chip readings and releases resources. |
|
184 | + """ |
|
185 | + with self._lock: |
|
186 | + self._exiting = True |
|
187 | + self._pi.spi_close(self._h) |
|
188 | + |
|
189 | + def run(self): |
|
190 | + self._next_time = time.time() |
|
191 | + while not self._exiting: |
|
192 | + self.read() |
|
193 | + self._next_time += self._interval |
|
194 | + delay = self._next_time - time.time() |
|
195 | + if delay > 0.0: |
|
196 | + time.sleep(delay) |
|
197 | + |
|
198 | +if __name__ == "__main__": |
|
199 | + |
|
200 | + import time |
|
201 | + import SN74HC165 |
|
202 | + import pigpio |
|
203 | + |
|
204 | + def cbf(pin, level, tick): |
|
205 | + print(pin, level, tick) |
|
206 | + |
|
207 | + pi = pigpio.pi() |
|
208 | + if not pi.connected: |
|
209 | + exit() |
|
210 | + |
|
211 | + run_for = 30 |
|
212 | + |
|
213 | + sr = SN74HC165.PISO( |
|
214 | + pi, SH_LD=16, |
|
215 | + SPI_device=SN74HC165.AUX_SPI, chips=7, |
|
216 | + reads_per_second=200, callback=cbf) |
|
217 | + |
|
218 | + time.sleep(run_for) |
|
219 | + |
|
220 | + # read all registers |
|
221 | + r = sr.read() |
|
222 | + # and print each value |
|
223 | + for i in range(len(r)): |
|
224 | + print(r[i]) |
|
225 | + |
|
226 | + sr.cancel() |
|
227 | + |
|
228 | + pi.stop() |
|
... | ... | \ No newline at end of file |
Chip-dat/74xx-dat/74HC595-dat/74HC595-dat.md
... | ... | @@ -43,6 +43,8 @@ optional |
43 | 43 | |
44 | 44 | ## ref |
45 | 45 | |
46 | +- [[74xx-dat]] |
|
47 | + |
|
46 | 48 | - [[RPI-dat]] - [[rpi-python-dat]] |
47 | 49 | |
48 | 50 | - [[74HC595]] |
... | ... | \ No newline at end of file |
Chip-dat/74xx-dat/74hc541-dat/74hc541-dat.md
... | ... | @@ -0,0 +1,3 @@ |
1 | + |
|
2 | +# 74hc541-dat |
|
3 | + |
Chip-dat/74xx-dat/74hct245-dat/74hct245-dat.md
... | ... | @@ -0,0 +1,6 @@ |
1 | + |
|
2 | +# 74hct245-dat |
|
3 | + |
|
4 | +## ref |
|
5 | + |
|
6 | +- [[74xx-dat]] |
|
... | ... | \ No newline at end of file |
Chip-dat/74xx-dat/74xx-Buffer-dat/74hc541-dat/74hc541-dat.md
... | ... | @@ -1,3 +0,0 @@ |
1 | - |
|
2 | -# 74hc541-dat |
|
3 | - |
Chip-dat/74xx-dat/74xx-Buffer-dat/74hct245-dat/74hct245-dat.md
... | ... | @@ -1,3 +0,0 @@ |
1 | - |
|
2 | -# 74hct245-dat |
|
3 | - |
Tech-dat/SBC-dat/RPI-dat/RPI-dat.md
... | ... | @@ -34,7 +34,9 @@ |
34 | 34 | |
35 | 35 | ## demo code |
36 | 36 | |
37 | -https://github.com/Edragon/RPI |
|
37 | +- [[rpi-python-dat]] - [[rpi-c-dat]] |
|
38 | + |
|
39 | +- https://github.com/Edragon/RPI |
|
38 | 40 | |
39 | 41 | can use commands nohup to run script in background for long term. |
40 | 42 |
Tech-dat/SBC-dat/RPI-dat/RPI-python-dat/RPI-python-dat.md
... | ... | @@ -1,19 +0,0 @@ |
1 | - |
|
2 | -# RPI-python-dat |
|
3 | - |
|
4 | -apt-get install pip |
|
5 | - |
|
6 | - Note, selecting 'python3-pip' instead of 'pip' |
|
7 | - |
|
8 | - |
|
9 | - |
|
10 | -## setup env |
|
11 | - |
|
12 | - python3 -m venv .venv |
|
13 | - source .venv/bin/activate |
|
14 | - |
|
15 | - pip install RPi.GPIO |
|
16 | - |
|
17 | - |
|
18 | - |
|
19 | -- [[python-dat]] |
|
... | ... | \ No newline at end of file |
Tech-dat/SBC-dat/RPI-dat/rpi-c-dat/rpi-c-dat.md
... | ... | @@ -1,23 +0,0 @@ |
1 | - |
|
2 | -# rpi-c-dat |
|
3 | - |
|
4 | - chmod +x 2_5449615149826598388 |
|
5 | - |
|
6 | - (.venv) root@raspberrypi:/home/pi# ./2_5449615149826598388 |
|
7 | - |
|
8 | - ./2_5449615149826598388: error while loading shared libraries: libwiringPi.so: cannot open shared object file: No such file or directory |
|
9 | - |
|
10 | - |
|
11 | -## WiringPi |
|
12 | - |
|
13 | -https://github.com/WiringPi/WiringPi |
|
14 | -https://github.com/WiringPi/WiringPi |
|
15 | - |
|
16 | -Unzip/use the portable prebuilt verison: |
|
17 | - |
|
18 | - # unzip the archive |
|
19 | - tar -xfv wiringpi_3.0.tar.gz |
|
20 | - Install the debian package: |
|
21 | - |
|
22 | - # install a dpkg |
|
23 | - sudo apt install ./wiringpi-3.0-1.deb |
code-dat/RPI-python-dat/RPI-python-dat.md
... | ... | @@ -0,0 +1,34 @@ |
1 | + |
|
2 | +# RPI-python-dat |
|
3 | + |
|
4 | +apt-get install pip |
|
5 | + |
|
6 | + Note, selecting 'python3-pip' instead of 'pip' |
|
7 | + |
|
8 | + |
|
9 | + |
|
10 | +## setup env |
|
11 | + |
|
12 | + python3 -m venv .venv |
|
13 | + source .venv/bin/activate |
|
14 | + |
|
15 | + pip install RPi.GPIO |
|
16 | + |
|
17 | + |
|
18 | +## rpi library |
|
19 | + |
|
20 | +- pigpio |
|
21 | +- pi74HC595 |
|
22 | +- wiringpi |
|
23 | + |
|
24 | +- build-in library: time, threading, |
|
25 | + |
|
26 | + |
|
27 | +## hardware driver |
|
28 | + |
|
29 | +- [[74HC595-dat]] - [[74HC165-dat]] |
|
30 | + |
|
31 | + |
|
32 | +## ref |
|
33 | + |
|
34 | +- [[python-dat]] |
|
... | ... | \ No newline at end of file |
code-dat/rpi-c-dat/rpi-c-dat.md
... | ... | @@ -0,0 +1,23 @@ |
1 | + |
|
2 | +# rpi-c-dat |
|
3 | + |
|
4 | + chmod +x 2_5449615149826598388 |
|
5 | + |
|
6 | + (.venv) root@raspberrypi:/home/pi# ./2_5449615149826598388 |
|
7 | + |
|
8 | + ./2_5449615149826598388: error while loading shared libraries: libwiringPi.so: cannot open shared object file: No such file or directory |
|
9 | + |
|
10 | + |
|
11 | +## WiringPi |
|
12 | + |
|
13 | +https://github.com/WiringPi/WiringPi |
|
14 | +https://github.com/WiringPi/WiringPi |
|
15 | + |
|
16 | +Unzip/use the portable prebuilt verison: |
|
17 | + |
|
18 | + # unzip the archive |
|
19 | + tar -xfv wiringpi_3.0.tar.gz |
|
20 | + Install the debian package: |
|
21 | + |
|
22 | + # install a dpkg |
|
23 | + sudo apt install ./wiringpi-3.0-1.deb |