AVR Embedded Tutorial - GPIO-Interrupt/de: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: AVR Embedded Tutorial - GPIO-Interrupt/de
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # GPIO Interrupt | |
| 2 | + | ||
| 3 | + | ## Vorwort | |
| 4 | + | ||
| 5 | + | Wie man die GPIO und UART ansteuert, steht hier: | |
| 6 | + | ||
| 7 | + | - [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO]] | |
| 8 | + | - [[AVR Embedded Tutorial - UART/de|UART]] *UARTInit* und *UARTSendString(...* | |
| 9 | + | ||
| 10 | + | ## Externe Interrupt | |
| 11 | + | ||
| 12 | + | Interrupt Pin-Belegung beim ATmega328: | |
| 13 | + | ||
| 14 | + | - INT0: PD2 | |
| 15 | + | - INT1: PD3 | |
| 16 | + | ||
| 17 | + | ### Interrupt Routine | |
| 18 | + | ||
| 19 | + | Diese Beispiel zeigt wie dies beim INT0 geht. Wann der Interrupt ausgelöst wird, gibt man mit **EICRA** an. Siehe Tabelle weiter unten. Man kann auch **INT0** und **INT1** gleichzeitig aktivieren. | |
| 20 | + | ||
| 21 | + | ``` pascal | |
| 22 | + | procedure Int0_Interrupt; public Name 'INT0_ISR'; interrupt; | |
| 23 | + | begin | |
| 24 | + | UARTSendString('INT0'); // Identify interrupt | |
| 25 | + | end; | |
| 26 | + | ``` | |
| 27 | + | ||
| 28 | + | ### Interrupt aktivieren | |
| 29 | + | ||
| 30 | + | Die Parameter sind in der Tabelle weiter unten beschrieben. | |
| 31 | + | ||
| 32 | + | ``` pascal | |
| 33 | + | begin | |
| 34 | + | PORTD := %00001100; // PullUp Pin 2 + 3 | |
| 35 | + | ||
| 36 | + | // UART inizialisieren | |
| 37 | + | UARTInit; | |
| 38 | + | ||
| 39 | + | // INT0 aktivieren | |
| 40 | + | EICRA := %11; // INT0 wird ausgelöst, wen der Pin auf HIGH geht. | |
| 41 | + | EIMSK := %01; // INT0 aktivieren. | |
| 42 | + | ||
| 43 | + | // Interrupt aktivieren | |
| 44 | + | asm Sei end; | |
| 45 | + | ||
| 46 | + | // Hauptschleife | |
| 47 | + | repeat | |
| 48 | + | until 1 = 2; | |
| 49 | + | end. | |
| 50 | + | ``` | |
| 51 | + | ||
| 52 | + | ### Register für den Atmega328p | |
| 53 | + | ||
| 54 | + | Folgende Register sind für die Aktivierung der Ports / Pins zuständig: | |
| 55 | + | ||
| 56 | + | - **EICRA**: Funktion des Interrupt. | |
| 57 | + | - **EIMSK**: INT0 und/oder INT1 aktivieren. | |
| 58 | + | ||
| 59 | + | Tabelle für **EICRA**, sie zeigt, bei was für einer Aktion am Pin ein Interrupt ausgelöst wird. | |
| 60 | + | ||
| 61 | + | <table> | |
| 62 | + | <thead> | |
| 63 | + | <tr> | |
| 64 | + | <th></th> | |
| 65 | + | <th colspan="2">INT1</th> | |
| 66 | + | <th></th> | |
| 67 | + | <th colspan="2">INT0</th> | |
| 68 | + | <th></th> | |
| 69 | + | </tr> | |
| 70 | + | </thead> | |
| 71 | + | <tbody> | |
| 72 | + | <tr> | |
| 73 | + | <td>Bit</td> | |
| 74 | + | <td>3</td> | |
| 75 | + | <td>2</td> | |
| 76 | + | <td></td> | |
| 77 | + | <td>1</td> | |
| 78 | + | <td>0</td> | |
| 79 | + | <td></td> | |
| 80 | + | </tr> | |
| 81 | + | <tr> | |
| 82 | + | <td></td> | |
| 83 | + | <td>0</td> | |
| 84 | + | <td>0</td> | |
| 85 | + | <td></td> | |
| 86 | + | <td>0</td> | |
| 87 | + | <td>0</td> | |
| 88 | + | <td>Low Level am Pin</td> | |
| 89 | + | </tr> | |
| 90 | + | <tr> | |
| 91 | + | <td></td> | |
| 92 | + | <td>0</td> | |
| 93 | + | <td>1</td> | |
| 94 | + | <td></td> | |
| 95 | + | <td>0</td> | |
| 96 | + | <td>1</td> | |
| 97 | + | <td>Jede Änderung</td> | |
| 98 | + | </tr> | |
| 99 | + | <tr> | |
| 100 | + | <td></td> | |
| 101 | + | <td>1</td> | |
| 102 | + | <td>0</td> | |
| 103 | + | <td></td> | |
| 104 | + | <td>1</td> | |
| 105 | + | <td>0</td> | |
| 106 | + | <td>Pin auf LOW</td> | |
| 107 | + | </tr> | |
| 108 | + | <tr> | |
| 109 | + | <td></td> | |
| 110 | + | <td>1</td> | |
| 111 | + | <td>1</td> | |
| 112 | + | <td></td> | |
| 113 | + | <td>1</td> | |
| 114 | + | <td>1</td> | |
| 115 | + | <td>Pin auf HIGH</td> | |
| 116 | + | </tr> | |
| 117 | + | </tbody> | |
| 118 | + | </table> | |
| 119 | + | ||
| 120 | + | ### INT1 | |
| 121 | + | ||
| 122 | + | Der INT1 muss folgendermassen eingestellt werden. | |
| 123 | + | ||
| 124 | + | ``` pascal | |
| 125 | + | EICRA := %1100; // INT1 wird ausgelöst, wen der Pin auf HIGH geht. | |
| 126 | + | EIMSK := %10; // INT1 aktivieren. | |
| 127 | + | ``` | |
| 128 | + | ||
| 129 | + | Für den **INT1** muss man die ISR-Procedure für den **INT1** nehmen **(INT1_ISR)**. | |
| 130 | + | ||
| 131 | + | ``` pascal | |
| 132 | + | procedure Int1_Interrupt; public Name 'INT1_ISR'; interrupt; | |
| 133 | + | begin | |
| 134 | + | // mache Irgendwas | |
| 135 | + | end; | |
| 136 | + | ``` | |
| 137 | + | ||
| 138 | + | ## Pin Change Interrupt | |
| 139 | + | ||
| 140 | + | Dieses Beispiel ist für einen Arduino Nano (Atmega328p). | |
| 141 | + | ||
| 142 | + | ### Terminal Ausgabe | |
| 143 | + | ||
| 144 | + | Gibt den Pin-Status auf einem Terminal aus. | |
| 145 | + | ||
| 146 | + | ``` pascal | |
| 147 | + | procedure WritePort(p: byte); | |
| 148 | + | begin | |
| 149 | + | UARTSendString('PB' + char(p + 48) + ' : '); | |
| 150 | + | if PinB and (1 shl p) = (1 shl p) then begin | |
| 151 | + | UARTSendString('HIGH '); | |
| 152 | + | end else begin | |
| 153 | + | UARTSendString('LOW '); | |
| 154 | + | end; | |
| 155 | + | end; | |
| 156 | + | ``` | |
| 157 | + | ||
| 158 | + | ### Interrupt Routine | |
| 159 | + | ||
| 160 | + | Wird bei einer Änderung eines Pines ausgelöst. | |
| 161 | + | ||
| 162 | + | ``` pascal | |
| 163 | + | procedure PC_Int0_Interrupt; public Name 'PCINT0_ISR'; interrupt; | |
| 164 | + | var | |
| 165 | + | i: byte; | |
| 166 | + | begin | |
| 167 | + | UARTSendString(#27'[0;0H'); // Cursor home | |
| 168 | + | for i := 0 to 3 do begin | |
| 169 | + | WritePort(i); | |
| 170 | + | end; | |
| 171 | + | end; | |
| 172 | + | ``` | |
| 173 | + | ||
| 174 | + | ### Pin Change GPIO deklarieren | |
| 175 | + | ||
| 176 | + | PB0, PB1, PB2 und BP3 für Interrupt aktivieren. | |
| 177 | + | ||
| 178 | + | ``` pascal | |
| 179 | + | const | |
| 180 | + | inPortsB = %00001111; // PCINT0 + PCINT1 + PCINT2 + PCINT3 | |
| 181 | + | ``` | |
| 182 | + | ||
| 183 | + | ### Pin Change Interrupt aktivieren | |
| 184 | + | ||
| 185 | + | Die Parameter sind in der Tabelle weiter unten beschrieben. | |
| 186 | + | ||
| 187 | + | ``` pascal | |
| 188 | + | begin | |
| 189 | + | DDRB := 0; | |
| 190 | + | PORTB := inPortsB; // PullUp | |
| 191 | + | ||
| 192 | + | // UART inizialisieren | |
| 193 | + | UARTInit; | |
| 194 | + | ||
| 195 | + | // PCINT0_ISR aktivieren | |
| 196 | + | PCICR := %001; // Nur auf PORTB | |
| 197 | + | PCMSK0 := inPortsB; | |
| 198 | + | ||
| 199 | + | // Interrupt aktivieren | |
| 200 | + | asm Sei end; | |
| 201 | + | ||
| 202 | + | // Hauptschleife | |
| 203 | + | repeat | |
| 204 | + | // mache Irgendwas | |
| 205 | + | until 1 = 2; | |
| 206 | + | end. | |
| 207 | + | ``` | |
| 208 | + | ||
| 209 | + | ### Register für den Atmega328p | |
| 210 | + | ||
| 211 | + | Folgende Register sind für die Aktivierung der Ports / Pins zuständig: | |
| 212 | + | ||
| 213 | + | - **PCICR**: Ports, welche einem Interrupt auslösen. | |
| 214 | + | - **PCMSKx**: Die einzelnen Pins. | |
| 215 | + | ||
| 216 | + | Tabelle für **PCICR**: | |
| 217 | + | ||
| 218 | + | | Bit | 2 | 1 | 0 | | |
| 219 | + | |-------|-------|-------|-------| | |
| 220 | + | | PCICR | PORTD | PORTC | PORTB | | |
| 221 | + | ||
| 222 | + | Tabelle für die Pins von **PCMSKx**: | |
| 223 | + | ||
| 224 | + | | Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | |
| 225 | + | |--------|---------|---------|---------|---------|---------|---------|---------|--------| | |
| 226 | + | | PCMSK0 | PCINT7 | PCINT6 | PCINT5 | PCINT4 | PCINT3 | PCINT2 | PCINT1 | PCINT0 | | |
| 227 | + | | PCMSK1 | | PCINT14 | PCINT13 | PCINT12 | PCINT11 | PCINT10 | PCINT9 | PCINT8 | | |
| 228 | + | | PCMSK2 | PCINT23 | PCINT22 | PCINT21 | PCINT20 | PCINT19 | PCINT18 | PCINT17 | PCINT6 | | |
| 229 | + | ||
| 230 | + | ### Beispiel für PORTB und PORTC | |
| 231 | + | ||
| 232 | + | ``` pascal | |
| 233 | + | // PCINT0_ISR und PCINT1_ISR aktivieren | |
| 234 | + | PCICR := %011; // PORTB und PORTC | |
| 235 | + | PCMSK0 := %11000000 // Pin 6 + 7 von PORTB; | |
| 236 | + | PCMSK1 := %00000011 // Pin 0 + 1 von PORTC; | |
| 237 | + | ``` | |
| 238 | + | ||
| 239 | + | Für den **PCINT1_ISR** kommt noch eine 2. ISR-Procedure dazu. Wen man PORTD auch noch verwenden würde, dann würde noch eine Procedure mit **PCINT2_ISR** dazukommen. | |
| 240 | + | ||
| 241 | + | ``` pascal | |
| 242 | + | procedure PC_Int1_Interrupt; public Name 'PCINT1_ISR'; interrupt; | |
| 243 | + | begin | |
| 244 | + | // mache Irgendwas | |
| 245 | + | end; | |
| 246 | + | ``` | |
| 247 | + | ||
| 248 | + | # Siehe auch | |
| 249 | + | ||
| 250 | + | - Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]] | |
| 251 | + | ||
| 252 | + | Autor: Mathias | |
| 253 | + | ||
| 254 | + | --- | |
| 255 | + | *From the Free Pascal wiki, page "AVR Embedded Tutorial - GPIO-Interrupt/de", as it stood on 2020-01-23 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).* | |
| 256 | + | Category: AVR/de | |
| 257 | + | Category: Arduino/de | |
| 258 | + | Category: Deutsch | |
| 259 | + | Category: Embedded/de | |
| 260 | + | Category: Tutorials/de |
