AVR Embedded Tutorial - Shiftregister: what changed
Revision 2 to 3. 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 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 19 | + | | IC | Attiny44 | | 74HC165 | | 74HC595 | | | |
| 20 | + | |---|---|---|---|---|---|---| | |
| 21 | + | | | Pin No. | Description | Pin No. | Description | Pin No. | Description | | |
| 22 | + | | dataOutPin | 13 | PA0 | | | 14 | DS | | |
| 23 | + | | dataInPin | 12 | PA1 | 10 | DS | | | | |
| 24 | + | | latchPin | 11 | PA2 | 1 | PL | 12 | ST_CP | | |
| 25 | + | | clockPin | 10 | PA3 | 2 | CP | 11 | SH_CP | | |
| 25 | 26 | ||
| 26 | 27 | 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. | |
| 27 | 28 | ||
| 28 | 29 | ``` pascal | |
| 29 | 30 | var | |
| 30 | 31 | shiftPORT : byte absolute PORTA; | |
| 31 | 32 | shiftDDR : byte absolute DDRA; | |
| 32 | 33 | shiftPIN : byte absolute PINA; | |
| 33 | 34 | const | |
| 34 | 35 | dataOutPin = 0; | |
| 35 | 36 | dataInPin = 1; | |
| 36 | 37 | latchPin = 2; | |
| 37 | 38 | clockPin = 3; | |
| 38 | 39 | ``` | |
| 39 | 40 | ||
| 40 | 41 | # Shift register write 74HC595 | |
| 41 | 42 | ||
| 42 | 43 | ``` pascal | |
| 43 | 44 | procedure shiftOut595(val : byte); | |
| 44 | 45 | var | |
| 45 | 46 | i : byte; | |
| 46 | 47 | begin | |
| 47 | 48 | for i : = 7 downto 0 do // MSB FIRST | |
| 48 | 49 | begin | |
| 49 | 50 | if(val and (1 shl i)) <> 0 then | |
| 50 | 51 | begin | |
| 51 | 52 | shiftPORT := shiftPORT or (1 shl dataOutPin); | |
| 52 | 53 | end | |
| 53 | 54 | else | |
| 54 | 55 | begin | |
| 55 | 56 | shiftPORT := shiftPORT and not (1 shl dataOutPin); | |
| 56 | 57 | end; | |
| 57 | 58 | ||
| 58 | 59 | shiftPORT := shiftPORT or (1 shl clockPin); | |
| 59 | 60 | shiftPORT := shiftPORT and not (1 shl clockPin); | |
| 60 | 61 | end; | |
| 61 | 62 | end; | |
| 62 | 63 | ``` | |
| 63 | 64 | ||
| 64 | 65 | # Shift register read 74HC165 | |
| 65 | 66 | ||
| 66 | 67 | ``` pascal | |
| 67 | 68 | function ShiftIn165 : byte; | |
| 68 | 69 | var | |
| 69 | 70 | i : byte; | |
| 70 | 71 | begin | |
| 71 | 72 | Result := 0; | |
| 72 | 73 | ||
| 73 | 74 | for i := 0 to 7 do // LSB FIRST | |
| 74 | 75 | begin | |
| 75 | 76 | Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin); | |
| 76 | 77 | shiftPORT := shiftPORT or (1 shl clockPin); | |
| 77 | 78 | shiftPORT := shiftPORT and not (1 shl clockPin); | |
| 78 | 79 | end; | |
| 79 | 80 | end ; | |
| 80 | 81 | ``` | |
| 81 | 82 | ||
| 82 | 83 | # Arduino shift register | |
| 83 | 84 | ||
| 84 | 85 | This version corresponds to Arduino's **shiftIn**. | |
| 85 | 86 | ||
| 86 | 87 | ``` pascal | |
| 87 | 88 | function ShiftIn : byte; | |
| 88 | 89 | var | |
| 89 | 90 | i : byte; | |
| 90 | 91 | begin | |
| 91 | 92 | Result := 0; | |
| 92 | 93 | ||
| 93 | 94 | for i := 0 to 7 do // LSB FIRST | |
| 94 | 95 | begin | |
| 95 | 96 | shiftPORT := shiftPORT or (1 shl clockPin); | |
| 96 | 97 | Result := (Result shl 1) or ((shiftPIN and (1 shl dataInPin)) shr dataInPin); | |
| 97 | 98 | shiftPORT := shiftPORT and not (1 shl clockPin); | |
| 98 | 99 | end; | |
| 99 | 100 | end; | |
| 100 | 101 | ``` | |
| 101 | 102 | ||
| 102 | 103 | # Shift register examples | |
| 103 | 104 | ||
| 104 | 105 | ## Read from a shift register | |
| 105 | 106 | ||
| 106 | 107 | Before the shift register can be read out, the port status must be written to the latch register, this is done via the LatchPin. | |
| 107 | 108 | ||
| 108 | 109 | ### Example for a 74HC165 | |
| 109 | 110 | ||
| 110 | 111 | ``` pascal | |
| 111 | 112 | var | |
| 112 | 113 | b : byte; | |
| 113 | 114 | begin | |
| 114 | 115 | ||
| 115 | 116 | // Latch data | |
| 116 | 117 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 117 | 118 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 118 | 119 | ||
| 119 | 120 | // Read shift register | |
| 120 | 121 | b := ShiftIn74HC165; | |
| 121 | 122 | ``` | |
| 122 | 123 | ||
| 123 | 124 | ### Example for cascaded 74HC165 | |
| 124 | 125 | ||
| 125 | 126 | ``` pascal | |
| 126 | 127 | var | |
| 127 | 128 | b0, bn : byte; | |
| 128 | 129 | begin | |
| 129 | 130 | ||
| 130 | 131 | // Latch data | |
| 131 | 132 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 132 | 133 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 133 | 134 | ||
| 134 | 135 | // Read shift register | |
| 135 | 136 | b0 := ShiftIn74HC165; | |
| 136 | 137 | ||
| 137 | 138 | // ... | |
| 138 | 139 | ||
| 139 | 140 | bn := ShiftIn74HC165; | |
| 140 | 141 | ``` | |
| 141 | 142 | ||
| 142 | 143 | ## Write to a shift register | |
| 143 | 144 | ||
| 144 | 145 | If the data is written into the shift register, you have to add the latch clock that the current outputs become active. | |
| 145 | 146 | ||
| 146 | 147 | ### Example for a 74HC595 | |
| 147 | 148 | ||
| 148 | 149 | ``` pascal | |
| 149 | 150 | var | |
| 150 | 151 | b : byte; | |
| 151 | 152 | begin | |
| 152 | 153 | b := 123; | |
| 153 | 154 | ||
| 154 | 155 | // Write shift register | |
| 155 | 156 | shiftOut595(b); | |
| 156 | 157 | ||
| 157 | 158 | // Latch data | |
| 158 | 159 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 159 | 160 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 160 | 161 | ``` | |
| 161 | 162 | ||
| 162 | 163 | ### Example for cascaded 74HC595 | |
| 163 | 164 | ||
| 164 | 165 | ``` pascal | |
| 165 | 166 | var | |
| 166 | 167 | b0, bn : byte; | |
| 167 | 168 | begin | |
| 168 | 169 | b0 := 123; | |
| 169 | 170 | ||
| 170 | 171 | // ... | |
| 171 | 172 | ||
| 172 | 173 | bn := 789; | |
| 173 | 174 | ||
| 174 | 175 | // Write shift register | |
| 175 | 176 | shiftOut595(b0); | |
| 176 | 177 | ||
| 177 | 178 | // ... | |
| 178 | 179 | ||
| 179 | 180 | shiftOut595(bn); | |
| 180 | 181 | ||
| 181 | 182 | // Latch data | |
| 182 | 183 | shiftPORT := shiftPORT and not (1 shl latchPin); | |
| 183 | 184 | shiftPORT := shiftPORT or (1 shl latchPin); | |
| 184 | 185 | ``` | |
| 185 | 186 | ||
| 186 | 187 | # See also | |
| 187 | 188 | ||
| 188 | 189 | - [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview | |
| 189 | 190 | - [[AVR Embedded Tutorial - SPI Shiftregister|SPI shift register]] - control shift register 74HC595 via SPI (very fast). | |
| 190 | 191 | ||
| 191 | 192 | --- | |
| 192 | 193 | *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).* | |
| 193 | 194 | Category: AVR | |
| 194 | 195 | Category: Arduino | |
| 195 | 196 | Category: Embedded | |
| 196 | 197 | Category: Tutorials |
