Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - Shiftregister: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Shift Registers
22
33The sample code is for an [ATtiny44](https://upload.chipdatas.com/root/pdf/Atmel-7701_Automotive-Microcontrollers-ATtiny24-44-84_Datasheet_eXTunT.pdf) and the shift registers used are the 74HC165 for reading and the 74HC595 for writing.
44
55> **Warning:** The read routine is **not** compatible with Arduino **shiftIn (...**! The Arduino read function is for other shift registers. The clock must be controlled differently.
66
77If you want to swap **MSB** to **LSB**, you simply have to swap **downto** to **to**.
88
99# Port access
1010
1111For how the shift register commands access the ports, see here: [[AVR Embedded Tutorial - Simple GPIO on and off output|GPIO output]].
1212
1313Do not forget to set the outputs as outputs (**DDRx**).
1414
1515# Port constants
1616
1717The pins used for the ATtiny44 and the shift register. With the AVR you can use any pins. I have declared the ports, except the data port, globally, so access from the shift registers is a little faster. If you declared them locally, everything would be more complicated because we use direct port access. Arduino does this differently, but it is about 10 times slower.
1818
19-<table>
2019<thead>
21-<tr>
22-<th>IC</th>
23-<th colspan="2">Attiny44</th>
24-<th colspan="2">74HC165</th>
25-<th colspan="2">74HC595</th>
26-</tr>
20+
2721</thead>
2822<tbody>
29-<tr>
30-<td></td>
31-<td>Pin No.</td>
32-<td>Description</td>
33-<td>Pin No.</td>
34-<td>Description</td>
35-<td>Pin No.</td>
36-<td>Description</td>
37-</tr>
38-<tr>
39-<td>dataOutPin</td>
40-<td>13</td>
41-<td>PA0</td>
42-<td></td>
43-<td></td>
44-<td>14</td>
45-<td>DS</td>
46-</tr>
47-<tr>
48-<td>dataInPin</td>
49-<td>12</td>
50-<td>PA1</td>
51-<td>10</td>
52-<td>DS</td>
53-<td></td>
54-<td></td>
55-</tr>
56-<tr>
57-<td>latchPin</td>
58-<td>11</td>
59-<td>PA2</td>
60-<td>1</td>
61-<td>PL</td>
62-<td>12</td>
63-<td>ST_CP</td>
64-</tr>
65-<tr>
66-<td>clockPin</td>
67-<td>10</td>
68-<td>PA3</td>
69-<td>2</td>
70-<td>CP</td>
71-<td>11</td>
72-<td>SH_CP</td>
73-</tr>
23+
7424</tbody>
75-</table>
7625
7726A variable is declared for the port, this has the advantage that you do not have to change all ports in the shift register functions.
7827
7928``` pascal
8029 var
8130 shiftPORT : byte absolute PORTA;
8231 shiftDDR : byte absolute DDRA;
8332 shiftPIN : byte absolute PINA;
8433 const
8534 dataOutPin = 0;
8635 dataInPin = 1;
8736 latchPin = 2;
8837 clockPin = 3;
8938```
9039
9140# Shift register write 74HC595
9241
9342``` pascal
9443procedure shiftOut595(val : byte);
9544 var
9645 i : byte;
9746begin
9847 for i : = 7 downto 0 do // MSB FIRST
9948 begin
10049 if(val and (1 shl i)) <> 0 then
10150 begin
10251 shiftPORT := shiftPORT or (1 shl dataOutPin);
10352 end
10453 else
10554 begin
10655 shiftPORT := shiftPORT and not (1 shl dataOutPin);
10756 end;
10857
10958 shiftPORT := shiftPORT or (1 shl clockPin);
11059 shiftPORT := shiftPORT and not (1 shl clockPin);
11160 end;
11261end;
11362```
11463
11564# Shift register read 74HC165
11665
11766``` pascal
11867function ShiftIn165 : byte;
11968 var
12069 i : byte;
12170 begin
12271 Result := 0;
12372
12473 for i := 0 to 7 do // LSB FIRST
12574 begin
12675 Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin);
12776 shiftPORT := shiftPORT or (1 shl clockPin);
12877 shiftPORT := shiftPORT and not (1 shl clockPin);
12978 end;
13079 end ;
13180```
13281
13382# Arduino shift register
13483
13584This version corresponds to Arduino's **shiftIn**.
13685
13786``` pascal
13887function ShiftIn : byte;
13988 var
14089 i : byte;
14190begin
14291 Result := 0;
14392
14493 for i := 0 to 7 do // LSB FIRST
14594 begin
14695 shiftPORT := shiftPORT or (1 shl clockPin);
14796 Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin);
14897 shiftPORT := shiftPORT and not (1 shl clockPin);
14998 end;
15099end;
151100```
152101
153102# Shift register examples
154103
155104## Read from a shift register
156105
157106Before the shift register can be read out, the port status must be written to the latch register, this is done via the LatchPin.
158107
159108### Example for a 74HC165
160109
161110``` pascal
162111 var
163112 b : byte;
164113 begin
165114
166115 // Latch data
167116 shiftPORT := shiftPORT and not (1 shl latchPin);
168117 shiftPORT := shiftPORT or (1 shl latchPin);
169118
170119 // Read shift register
171120 b := ShiftIn74HC165;
172121```
173122
174123### Example for cascaded 74HC165
175124
176125``` pascal
177126 var
178127 b0, bn : byte;
179128 begin
180129
181130 // Latch data
182131 shiftPORT := shiftPORT and not (1 shl latchPin);
183132 shiftPORT := shiftPORT or (1 shl latchPin);
184133
185134 // Read shift register
186135 b0 := ShiftIn74HC165;
187136
188137 // ...
189138
190139 bn := ShiftIn74HC165;
191140```
192141
193142## Write to a shift register
194143
195144If the data is written into the shift register, you have to add the latch clock that the current outputs become active.
196145
197146### Example for a 74HC595
198147
199148``` pascal
200149 var
201150 b : byte;
202151 begin
203152 b := 123;
204153
205154 // Write shift register
206155 shiftOut595(b);
207156
208157 // Latch data
209158 shiftPORT := shiftPORT and not (1 shl latchPin);
210159 shiftPORT := shiftPORT or (1 shl latchPin);
211160```
212161
213162### Example for cascaded 74HC595
214163
215164``` pascal
216165 var
217166 b0, bn : byte;
218167 begin
219168 b0 := 123;
220169
221170 // ...
222171
223172 bn := 789;
224173
225174 // Write shift register
226175 shiftOut595(b0);
227176
228177 // ...
229178
230179 shiftOut595(bn);
231180
232181 // Latch data
233182 shiftPORT := shiftPORT and not (1 shl latchPin);
234183 shiftPORT := shiftPORT or (1 shl latchPin);
235184```
236185
237186# See also
238187
239188- [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview
240189- [[AVR Embedded Tutorial - SPI Shiftregister|SPI shift register]] - control shift register 74HC595 via SPI (very fast).
241190
242191---
243192*From the Free Pascal wiki, page "AVR Embedded Tutorial - Shiftregister", as it stood on 2023-08-02 after a change by Zave2023, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
244193Category: AVR
245194Category: Arduino
246195Category: Embedded
247196Category: Tutorials

Back to history