AVR Embedded Tutorial - Shiftregister: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # Shift Registers | |
| 2 | 2 | ||
| 3 | 3 | The 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. | |
| 4 | 4 | ||
| 5 | 5 | > **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. | |
| 6 | 6 | ||
| 7 | 7 | If you want to swap **MSB** to **LSB**, you simply have to swap **downto** to **to**. | |
| 8 | 8 | ||
| 9 | 9 | # Port access | |
| 10 | 10 | ||
| 11 | 11 | For how the shift register commands access the ports, see here: [[AVR Embedded Tutorial - Simple GPIO on and off output|GPIO output]]. | |
| 12 | 12 | ||
| 13 | 13 | Do not forget to set the outputs as outputs (**DDRx**). | |
| 14 | 14 | ||
| 15 | 15 | # Port constants | |
| 16 | 16 | ||
| 17 | 17 | The 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. | |
| 18 | 18 | ||
| 19 | - | ||
| 20 | 19 | <thead> | |
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 20 | + | ||
| 27 | 21 | </thead> | |
| 28 | 22 | <tbody> | |
| 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 | - | ||
| 23 | + | ||
| 74 | 24 | </tbody> | |
| 75 | - | ||
| 76 | 25 | ||
| 77 | 26 | A variable is declared for the port, this has the advantage that you do not have to change all ports in the shift register functions. | |
| 78 | 27 | ||
| 79 | 28 | ``` pascal | |
| 80 | 29 | var | |
| 81 | 30 | shiftPORT : byte absolute PORTA; | |
| 82 | 31 | shiftDDR : byte absolute DDRA; | |
| 83 | 32 | shiftPIN : byte absolute PINA; | |
| 84 | 33 | const | |
| 85 | 34 | dataOutPin = 0; | |
| 86 | 35 | dataInPin = 1; | |
| 87 | 36 | latchPin = 2; | |
| 88 | 37 | clockPin = 3; | |
| 89 | 38 | ``` | |
| 90 | 39 | ||
| 91 | 40 | # Shift register write 74HC595 | |
| 92 | 41 | ||
| 93 | 42 | ``` pascal | |
| 94 | 43 | procedure shiftOut595(val : byte); | |
| 95 | 44 | var | |
| 96 | 45 | i : byte; | |
| 97 | 46 | begin | |
| 98 | 47 | for i : = 7 downto 0 do // MSB FIRST | |
| 99 | 48 | begin | |
| 100 | 49 | if(val and (1 shl i)) <> 0 then | |
| 101 | 50 | begin | |
| 102 | 51 | shiftPORT := shiftPORT or (1 shl dataOutPin); | |
| 103 | 52 | end | |
| 104 | 53 | else | |
| 105 | 54 | begin | |
| 106 | 55 | shiftPORT := shiftPORT and not (1 shl dataOutPin); | |
| 107 | 56 | end; | |
| 108 | 57 | ||
| 109 | 58 | shiftPORT := shiftPORT or (1 shl clockPin); | |
| 110 | 59 | shiftPORT := shiftPORT and not (1 shl clockPin); | |
| 111 | 60 | end; | |
| 112 | 61 | end; | |
| 113 | 62 | ``` | |
| 114 | 63 | ||
| 115 | 64 | # Shift register read 74HC165 | |
| 116 | 65 | ||
| 117 | 66 | ``` pascal | |
| 118 | 67 | function ShiftIn165 : byte; | |
| 119 | 68 | var | |
| 120 | 69 | i : byte; | |
| 121 | 70 | begin | |
| 122 | 71 | Result := 0; | |
| 123 | 72 | ||
| 124 | 73 | for i := 0 to 7 do // LSB FIRST | |
| 125 | 74 | begin | |
| 126 | 75 | Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin); | |
| 127 | 76 | shiftPORT := shiftPORT or (1 shl clockPin); | |
| 128 | 77 | shiftPORT := shiftPORT and not (1 shl clockPin); | |
| 129 | 78 | end; | |
| 130 | 79 | end ; | |
| 131 | 80 | ``` | |
| 132 | 81 | ||
| 133 | 82 | # Arduino shift register | |
| 134 | 83 | ||
| 135 | 84 | This version corresponds to Arduino's **shiftIn**. | |
| 136 | 85 | ||
| 137 | 86 | ``` pascal | |
| 138 | 87 | function ShiftIn : byte; | |
| 139 | 88 | var | |
| 140 | 89 | i : byte; | |
| 141 | 90 | begin | |
| 142 | 91 | Result := 0; | |
| 143 | 92 | ||
| 144 | 93 | for i := 0 to 7 do // LSB FIRST | |
| 145 | 94 | begin | |
| 146 | 95 | shiftPORT := shiftPORT or (1 shl clockPin); | |
| 147 | 96 | Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin); | |
| 148 | 97 | shiftPORT := shiftPORT and not (1 shl clockPin); | |
| 149 | 98 | end; | |
| 150 | 99 | end; | |
| 151 | 100 | ``` | |
| 152 | 101 | ||
| 153 | 102 | # Shift register examples | |
| 154 | 103 | ||
| 155 | 104 | ## Read from a shift register | |
| 156 | 105 | ||
| 157 | 106 | Before the shift register can be read out, the port status must be written to the latch register, this is done via the LatchPin. | |
| 158 | 107 | ||
| 159 | 108 | ### Example for a 74HC165 | |
| 160 | 109 | ||
| 161 | 110 | ``` pascal | |
| 162 | 111 | var | |
| 163 | 112 | b : byte; | |
| 164 | 113 | begin | |
| 165 | 114 | ||
| 166 | 115 | // Latch data | |
| 167 | 116 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 168 | 117 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 169 | 118 | ||
| 170 | 119 | // Read shift register | |
| 171 | 120 | b := ShiftIn74HC165; | |
| 172 | 121 | ``` | |
| 173 | 122 | ||
| 174 | 123 | ### Example for cascaded 74HC165 | |
| 175 | 124 | ||
| 176 | 125 | ``` pascal | |
| 177 | 126 | var | |
| 178 | 127 | b0, bn : byte; | |
| 179 | 128 | begin | |
| 180 | 129 | ||
| 181 | 130 | // Latch data | |
| 182 | 131 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 183 | 132 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 184 | 133 | ||
| 185 | 134 | // Read shift register | |
| 186 | 135 | b0 := ShiftIn74HC165; | |
| 187 | 136 | ||
| 188 | 137 | // ... | |
| 189 | 138 | ||
| 190 | 139 | bn := ShiftIn74HC165; | |
| 191 | 140 | ``` | |
| 192 | 141 | ||
| 193 | 142 | ## Write to a shift register | |
| 194 | 143 | ||
| 195 | 144 | If the data is written into the shift register, you have to add the latch clock that the current outputs become active. | |
| 196 | 145 | ||
| 197 | 146 | ### Example for a 74HC595 | |
| 198 | 147 | ||
| 199 | 148 | ``` pascal | |
| 200 | 149 | var | |
| 201 | 150 | b : byte; | |
| 202 | 151 | begin | |
| 203 | 152 | b := 123; | |
| 204 | 153 | ||
| 205 | 154 | // Write shift register | |
| 206 | 155 | shiftOut595(b); | |
| 207 | 156 | ||
| 208 | 157 | // Latch data | |
| 209 | 158 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 210 | 159 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 211 | 160 | ``` | |
| 212 | 161 | ||
| 213 | 162 | ### Example for cascaded 74HC595 | |
| 214 | 163 | ||
| 215 | 164 | ``` pascal | |
| 216 | 165 | var | |
| 217 | 166 | b0, bn : byte; | |
| 218 | 167 | begin | |
| 219 | 168 | b0 := 123; | |
| 220 | 169 | ||
| 221 | 170 | // ... | |
| 222 | 171 | ||
| 223 | 172 | bn := 789; | |
| 224 | 173 | ||
| 225 | 174 | // Write shift register | |
| 226 | 175 | shiftOut595(b0); | |
| 227 | 176 | ||
| 228 | 177 | // ... | |
| 229 | 178 | ||
| 230 | 179 | shiftOut595(bn); | |
| 231 | 180 | ||
| 232 | 181 | // Latch data | |
| 233 | 182 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 234 | 183 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 235 | 184 | ``` | |
| 236 | 185 | ||
| 237 | 186 | # See also | |
| 238 | 187 | ||
| 239 | 188 | - [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview | |
| 240 | 189 | - [[AVR Embedded Tutorial - SPI Shiftregister|SPI shift register]] - control shift register 74HC595 via SPI (very fast). | |
| 241 | 190 | ||
| 242 | 191 | --- | |
| 243 | 192 | *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).* | |
| 244 | 193 | Category: AVR | |
| 245 | 194 | Category: Arduino | |
| 246 | 195 | Category: Embedded | |
| 247 | 196 | Category: Tutorials |
