AVR Embedded Tutorial - GPIO-Interrupt: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # GPIO interrupt | |
| 2 | 2 | ||
| 3 | 3 | ## Introduction | |
| 4 | 4 | ||
| 5 | 5 | How to control the GPIO and UART can be found here: | |
| 6 | 6 | ||
| 7 | 7 | - [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO]] | |
| 8 | 8 | - [[AVR Embedded Tutorial - UART/de|UART]] *UARTInit* and *UARTSendString(...* | |
| 9 | 9 | ||
| 10 | 10 | ## External interrupt | |
| 11 | 11 | ||
| 12 | 12 | Interrupt pin assignment for the ATmega328: | |
| 13 | 13 | ||
| 14 | 14 | - INT0: PD2 | |
| 15 | 15 | - INT1: PD3 | |
| 16 | 16 | ||
| 17 | 17 | ### Interrupt routine | |
| 18 | 18 | ||
| 19 | 19 | This example shows how this works with INT0. You use **EICRA** to specify which interrupt is triggered. See table below. You can also activate INT0 and INT1 at the same time. | |
| 20 | 20 | ||
| 21 | 21 | ``` pascal | |
| 22 | 22 | procedure Int0_Interrupt; public name 'INT0_ISR'; interrupt; | |
| 23 | 23 | begin | |
| 24 | 24 | UARTSendString('INT0'); // Identify interrupt | |
| 25 | 25 | end; | |
| 26 | 26 | ``` | |
| 27 | 27 | ||
| 28 | 28 | ### Activate interrupt | |
| 29 | 29 | ||
| 30 | 30 | The parameters are described in the table below. | |
| 31 | 31 | ||
| 32 | 32 | ``` pascal | |
| 33 | 33 | begin | |
| 34 | 34 | PORTD := %00001100; // PullUp Pin 2 + 3 | |
| 35 | 35 | ||
| 36 | 36 | // Initialize UART | |
| 37 | 37 | UARTInit ; | |
| 38 | 38 | ||
| 39 | 39 | // Activate INT0 | |
| 40 | 40 | EICRA := %11; // INT0 is triggered when the pin goes HIGH. | |
| 41 | 41 | EIMSK := %01; // activate INT0. | |
| 42 | 42 | ||
| 43 | 43 | // Activate interrupt | |
| 44 | 44 | asm | |
| 45 | 45 | Sei | |
| 46 | 46 | end; | |
| 47 | 47 | ||
| 48 | 48 | // Main loop | |
| 49 | 49 | repeat | |
| 50 | 50 | until 1 = 2; | |
| 51 | 51 | end. | |
| 52 | 52 | ``` | |
| 53 | 53 | ||
| 54 | 54 | ### Register for the Atmega328p | |
| 55 | 55 | ||
| 56 | 56 | The following registers are responsible for the activation of the ports/pins: | |
| 57 | 57 | ||
| 58 | 58 | - **EICRA** Function of the interrupt. | |
| 59 | 59 | - **EIMSK** Activate INT0 and/or INT1. | |
| 60 | 60 | ||
| 61 | 61 | Table for **EICRA** which shows what kind of action on the pin triggers an interrupt. | |
| 62 | 62 | ||
| 63 | - | ||
| 64 | 63 | <thead> | |
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 64 | + | ||
| 72 | 65 | </thead> | |
| 73 | 66 | <tbody> | |
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 67 | + | ||
| 119 | 68 | </tbody> | |
| 120 | - | ||
| 121 | 69 | ||
| 122 | 70 | ### INT1 interrupt | |
| 123 | 71 | ||
| 124 | 72 | The INT1 interrupt must be set as follows: | |
| 125 | 73 | ||
| 126 | 74 | ``` pascal | |
| 127 | 75 | EICRA := %1100; // INT1 is triggered when the pin goes HIGH. | |
| 128 | 76 | EIMSK := %10; // activate INT1. | |
| 129 | 77 | ``` | |
| 130 | 78 | ||
| 131 | 79 | For the INT1 interrupt you have to use the ISR procedure for the INT1 (**INT1_ISR**). | |
| 132 | 80 | ||
| 133 | 81 | ``` pascal | |
| 134 | 82 | procedure Int1_Interrupt; public name 'INT1_ISR'; interrupt; | |
| 135 | 83 | begin | |
| 136 | 84 | // do something | |
| 137 | 85 | end; | |
| 138 | 86 | ``` | |
| 139 | 87 | ||
| 140 | 88 | ## Pin change interrupt | |
| 141 | 89 | ||
| 142 | 90 | This example is for an Arduino Nano (Atmega328p). | |
| 143 | 91 | ||
| 144 | 92 | ### Terminal issue | |
| 145 | 93 | ||
| 146 | 94 | Output the pin status on a terminal. | |
| 147 | 95 | ||
| 148 | 96 | ``` pascal | |
| 149 | 97 | procedure WritePort(p : byte); | |
| 150 | 98 | begin | |
| 151 | 99 | UARTSendString ('PB' + char(p + 48) + ':'); | |
| 152 | 100 | ||
| 153 | 101 | if PinB and (1 shl p) = (1 shl p) then | |
| 154 | 102 | begin | |
| 155 | 103 | UARTSendString('HIGH'); | |
| 156 | 104 | end | |
| 157 | 105 | else | |
| 158 | 106 | begin | |
| 159 | 107 | UARTSendString('LOW'); | |
| 160 | 108 | end; | |
| 161 | 109 | end; | |
| 162 | 110 | ``` | |
| 163 | 111 | ||
| 164 | 112 | ### Interrupt routine | |
| 165 | 113 | ||
| 166 | 114 | The interrupt fires when a pin changes. | |
| 167 | 115 | ||
| 168 | 116 | ``` pascal | |
| 169 | 117 | procedure PC_Int0_Interrupt; public name 'PCINT0_ISR'; interrupt; | |
| 170 | 118 | var | |
| 171 | 119 | i : byte; | |
| 172 | 120 | begin | |
| 173 | 121 | UARTSendString(#27 '[0; 0H'); // Home the cursor | |
| 174 | 122 | ||
| 175 | 123 | for i := 0 to 3 do | |
| 176 | 124 | begin | |
| 177 | 125 | WritePort(i); | |
| 178 | 126 | end; | |
| 179 | 127 | end; | |
| 180 | 128 | ``` | |
| 181 | 129 | ||
| 182 | 130 | ### Declare pin change GPIO | |
| 183 | 131 | ||
| 184 | 132 | Activate pins PB0, PB1, PB2 and BP3 for interrupts. | |
| 185 | 133 | ||
| 186 | 134 | ``` pascal | |
| 187 | 135 | const | |
| 188 | 136 | inPortsB = %00001111; // PCINT0 + PCINT1 + PCINT2 + PCINT3 | |
| 189 | 137 | ``` | |
| 190 | 138 | ||
| 191 | 139 | ### Activate pin change interrupt | |
| 192 | 140 | ||
| 193 | 141 | The parameters are described in the table below. | |
| 194 | 142 | ||
| 195 | 143 | ``` pascal | |
| 196 | 144 | procedure PC_Int0_Interrupt; public name 'PCINT0_ISR'; interrupt; | |
| 197 | 145 | begin | |
| 198 | 146 | DDRB := 0; | |
| 199 | 147 | PORTB := inPortsB; // PullUp | |
| 200 | 148 | ||
| 201 | 149 | // Initialize UART | |
| 202 | 150 | UARTInit; | |
| 203 | 151 | ||
| 204 | 152 | // Activate PCINT0_ISR | |
| 205 | 153 | PCICR := %001; // Only on PORTB | |
| 206 | 154 | PCMSK0 := inPortsB; | |
| 207 | 155 | ||
| 208 | 156 | // Activate interrupt | |
| 209 | 157 | asm | |
| 210 | 158 | Sei | |
| 211 | 159 | end; | |
| 212 | 160 | ||
| 213 | 161 | // Main loop | |
| 214 | 162 | repeat | |
| 215 | 163 | // do something | |
| 216 | 164 | until 1 = 2; | |
| 217 | 165 | end. | |
| 218 | 166 | ``` | |
| 219 | 167 | ||
| 220 | 168 | ### Registers for the Atmega328p | |
| 221 | 169 | ||
| 222 | 170 | The following registers are responsible for the activation of the ports/pins: | |
| 223 | 171 | ||
| 224 | 172 | - **PCICR** Ports that trigger an interrupt. | |
| 225 | 173 | - **PCMSKx** The individual pins. | |
| 226 | 174 | ||
| 227 | 175 | Table for PCICR: | |
| 228 | 176 | ||
| 229 | 177 | | Bit | 2 | 1 | 0 | | |
| 230 | 178 | |-------|-------|-------|-------| | |
| 231 | 179 | | PCICR | PORTD | PORTC | PORTB | | |
| 232 | 180 | ||
| 233 | 181 | Table for the pins of PCMSKx : | |
| 234 | 182 | ||
| 235 | 183 | | Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| 236 | 184 | |--------|---------|---------|---------|---------|---------|---------|---------|--------| | |
| 237 | 185 | | PCMSK0 | PCINT7 | PCINT6 | PCINT5 | PCINT4 | PCINT3 | PCINT2 | PCINT1 | PCINT0 | | |
| 238 | 186 | | PCMSK1 | | PCINT14 | PCINT13 | PCINT12 | PCINT11 | PCINT10 | PCINT9 | PCINT8 | | |
| 239 | 187 | | PCMSK2 | PCINT23 | PCINT22 | PCINT21 | PCINT20 | PCINT19 | PCINT18 | PCINT17 | PCINT6 | | |
| 240 | 188 | ||
| 241 | 189 | ### Example for PORT B and PORT C | |
| 242 | 190 | ||
| 243 | 191 | ``` pascal | |
| 244 | 192 | // Activate PCINT0_ISR and PCINT1_ISR | |
| 245 | 193 | PCICR := %011; // PORT B and PORT C | |
| 246 | 194 | PCMSK0 := %11000000; // Pin 6 + 7 of PORT B | |
| 247 | 195 | PCMSK1 := %00000011; // Pin 0 + 1 of PORT C | |
| 248 | 196 | ``` | |
| 249 | 197 | ||
| 250 | 198 | A second ISR procedure is added for the PCINT1_ISR. If you would like to use PORT D, then a procedure for the PCINT2_ISR would need to be added. | |
| 251 | 199 | ||
| 252 | 200 | ``` pascal | |
| 253 | 201 | procedure PC_Int1_Interrupt; public name 'PCINT1_ISR'; interrupt; | |
| 254 | 202 | begin | |
| 255 | 203 | // do something | |
| 256 | 204 | end; | |
| 257 | 205 | ``` | |
| 258 | 206 | ||
| 259 | 207 | # See also | |
| 260 | 208 | ||
| 261 | 209 | - [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview | |
| 262 | 210 | ||
| 263 | 211 | --- | |
| 264 | 212 | *From the Free Pascal wiki, page "AVR Embedded Tutorial - GPIO-Interrupt", as it stood on 2020-01-24 after a change by Trev, 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).* | |
| 265 | 213 | Category: AVR | |
| 266 | 214 | Category: Arduino | |
| 267 | 215 | Category: Embedded | |
| 268 | 216 | Category: Tutorials |
