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