AVR Embedded Tutorial - SPI-Slave/de: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Contents | |
| 2 | + | ||
| 3 | + | * 1 SPI als Slave | |
| 4 | + | * 1.1 Vorwort | |
| 5 | + | * 1.2 Beispiel Atmega328 / Arduino Uno/Nano | |
| 6 | + | * 1.2.1 Units | |
| 7 | + | * 1.2.2 Konstanten und Variablen | |
| 8 | + | * 1.2.3 Zeichen von SPI empfangen | |
| 9 | + | * 1.2.4 Hauptprogramm | |
| 10 | + | * 1.3 Sendecode des ATtiny2313 | |
| 11 | + | * 2 Siehe auch | |
| 12 | + | ||
| 1 | 13 | # SPI als Slave | |
| 2 | 14 | ||
| 3 | 15 | ## Vorwort | |
| 4 | 16 | ||
| 5 | - | ||
| 17 | + | SPI kann man auch als Slave verwenden. | |
| 6 | 18 | ||
| 7 | - | ||
| 19 | + | Das SPI auf eine Eingabe reagieren kann, löst man dies am besten Interruptgesteuert. | |
| 8 | 20 | ||
| 9 | - | ||
| 21 | + | Die folgenden Funktionen, sind in folgenden Tutorials beschrieben: | |
| 10 | 22 | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 23 | + | * [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO - Aus / Ein-gabe]] \- Wie mache ich einen GPIO-Zugriff am AVR. | |
| 24 | + | * [[AVR Embedded Tutorial - UART/de|UART]] \- Serielle Ein und Ausgabe über UART (COM-Port). | |
| 25 | + | * [[AVR Embedded Tutorial - SPI/de|SPI]] \- Nutzung der Hardware-SPI-Schnittstelle. | |
| 14 | 26 | ||
| 15 | 27 | ## Beispiel Atmega328 / Arduino Uno/Nano | |
| 16 | 28 | ||
| 17 | - | ||
| 29 | + | In diesem Beispiel wird ein Interrupt ausgelöst, sobald ein Zeichen am Eingang anliegt und anschliessend wird dieses in den Ringpuffer geschrieben. | |
| 18 | 30 | ||
| 19 | - | ||
| 31 | + | In der Hauptschleife wird geprüft, ob sich etwas im Ringbuffer befindet, wen ja, wird dies über UART ausgegeben. | |
| 20 | 32 | ||
| 21 | - | ||
| 33 | + | Was man hier aufpassen muss, das nicht zu viel über SPI reinkommt, da diese Schnittstelle um einige sschneller ist als UART. | |
| 22 | 34 | ||
| 23 | - | ||
| 35 | + | Als Master habe ich eine ATtiny2313 verwendet, welcher auf Tastendruck eine String über SPI ausgibt. | |
| 24 | 36 | ||
| 25 | - | ||
| 37 | + | Am Ende ist noch der Code für den ATTiny angehängt, welcher die Strings sendet. | |
| 26 | 38 | ||
| 27 | 39 | ### Units | |
| 28 | 40 | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 41 | + | Wird für SEI benötig. | |
| 42 | + | ``` | |
| 32 | 43 | uses | |
| 33 | - | ||
| 44 | + | intrinsics; | |
| 34 | 45 | ``` | |
| 35 | 46 | ||
| 36 | 47 | ### Konstanten und Variablen | |
| 37 | 48 | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 49 | + | Variablen für Ringpuffer. | |
| 50 | + | ``` | |
| 41 | 51 | const | |
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 52 | + | // Buffergrösse | |
| 53 | + | SPI_Buf_Len = 255; | |
| 54 | + | ||
| 55 | + | var | |
| 56 | + | // Pufferezeiger unf Puffer | |
| 57 | + | SPI_Buf_FirstOut: byte = 0; | |
| 58 | + | SPI_Buf_LastIn: byte = 0; | |
| 59 | + | SPI_Buf_Data: array[0..SPI_Buf_Len - 1] of byte; | |
| 50 | 60 | ``` | |
| 51 | 61 | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 62 | + | Pins welche an PORTB für SPI benötigt werden. | |
| 55 | 63 | ||
| 56 | - | ||
| 64 | + | Wird aber nur gebraucht, wen man etwas sendet. | |
| 65 | + | ``` | |
| 57 | 66 | type | |
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 67 | + | TSPI_GPIO = bitpacked record | |
| 68 | + | p0, p1, SlaveSelect, MOSI, MISO, Clock, p6, p7: boolean; | |
| 69 | + | end; | |
| 70 | + | ||
| 71 | + | var | |
| 72 | + | SPI_Port: TSPI_GPIO absolute PORTB; | |
| 73 | + | SPI_DDR: TSPI_GPIO absolute DDRB; | |
| 65 | 74 | ``` | |
| 66 | 75 | ||
| 67 | 76 | ### Zeichen von SPI empfangen | |
| 68 | 77 | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 78 | + | Sobald der Master am SS anklopft, wird der Interrupt ausgelöst und das kommende Zeichen in den Ringpuffer geschrieben. | |
| 79 | + | ``` | |
| 80 | + | procedure SPI_Int_Send; public Name 'SPI__STC_ISR'; interrupt; | |
| 81 | + | var | |
| 82 | + | tchr: byte; | |
| 83 | + | begin | |
| 84 | + | tchr := SPDR; | |
| 85 | + | if tchr <> 0 then begin | |
| 86 | + | SPI_Buf_Data[SPI_Buf_LastIn] := tchr; | |
| 87 | + | Inc(SPI_Buf_LastIn); | |
| 88 | + | if SPI_Buf_LastIn >= SPI_Buf_Len then begin | |
| 89 | + | SPI_Buf_LastIn := 0; | |
| 90 | + | end; | |
| 91 | + | end; | |
| 82 | 92 | end; | |
| 83 | - | ||
| 84 | - | ||
| 85 | 93 | ``` | |
| 86 | 94 | ||
| 87 | 95 | ### Hauptprogramm | |
| 88 | 96 | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 97 | + | Im Hauptprogramm wird einfach geprüft, ob etwas im Ringpuffer ist und sobald etwas dort ist, wird es über UART ausgegeben. | |
| 98 | + | ``` | |
| 92 | 99 | var | |
| 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 | - | ||
| 100 | + | ch: byte; | |
| 101 | + | ||
| 102 | + | begin | |
| 103 | + | // UART auf inizialisieren. | |
| 104 | + | UARTInit; | |
| 105 | + | ||
| 106 | + | // SPI als Slave, Interrupt gesteuert. | |
| 107 | + | SPCR := (1 shl SPE) or (1 shl SPIE); | |
| 108 | + | ||
| 109 | + | // Interrupt aktivieren. | |
| 110 | + | avr_sei; | |
| 111 | + | ||
| 112 | + | // SendePin | |
| 113 | + | SPI_DDR.MISO := True; // Nur nötig, wen der Slave auch sendet. | |
| 114 | + | ||
| 115 | + | repeat | |
| 116 | + | while SPI_Buf_LastIn <> SPI_Buf_FirstOut do begin // Ist etwas im Puffer ? | |
| 117 | + | ch := SPI_Buf_Data[SPI_Buf_FirstOut]; | |
| 118 | + | Inc(SPI_Buf_FirstOut); | |
| 119 | + | if SPI_Buf_FirstOut >= SPI_Buf_Len then begin | |
| 120 | + | SPI_Buf_FirstOut := 0; | |
| 121 | + | end; | |
| 122 | + | UARTSendChar(char(ch)); | |
| 123 | + | end; | |
| 124 | + | until 1 = 2; | |
| 125 | + | end. | |
| 119 | 126 | ``` | |
| 120 | 127 | ||
| 121 | 128 | ## Sendecode des ATtiny2313 | |
| 122 | 129 | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 130 | + | Dieser Code sendet einen String, sobald eine Taste gedrückt wird. | |
| 126 | 131 | ||
| 127 | - | ||
| 132 | + | Da der ATTiny2313 keinen echten SPI besitzt, wird die Ausgabe über die Universal-Schnittelle (USI) gemacht. | |
| 128 | 133 | ||
| 129 | - | ||
| 134 | + | Es sind 3 Taster vorhanden. | |
| 135 | + | ``` | |
| 130 | 136 | program Project1; | |
| 131 | - | ||
| 132 | - | ||
| 133 | - | ||
| 134 | - | ||
| 135 | - | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 137 | + | ||
| 138 | + | {$O-} | |
| 139 | + | type | |
| 140 | + | TSPI_GPIO = bitpacked record | |
| 141 | + | p0, p1, p2, p3, SlaveSelect, DataInt, DataOut, Clock: boolean; | |
| 142 | + | end; | |
| 143 | + | ||
| 144 | + | TButton_GPIO = bitpacked array[0..2] of boolean; | |
| 145 | + | ||
| 146 | + | var | |
| 147 | + | SPI_PORT: TSPI_GPIO absolute PORTB; | |
| 148 | + | SPI_DDR: TSPI_GPIO absolute DDRB; | |
| 149 | + | ||
| 150 | + | Button_PIN: TButton_GPIO absolute PIND; | |
| 151 | + | ||
| 152 | + | procedure delay; | |
| 153 | + | var | |
| 154 | + | i: UInt32; | |
| 155 | + | begin | |
| 156 | + | for i := 0 to 65000 do; | |
| 157 | + | end; | |
| 158 | + | ||
| 159 | + | procedure SPIWriteData(p: PByte; len: byte); | |
| 160 | + | var | |
| 161 | + | i: byte; | |
| 162 | + | begin | |
| 163 | + | SPI_PORT.SlaveSelect := False; | |
| 164 | + | for i := 0 to len - 1 do begin | |
| 165 | + | USIDR := p[i]; | |
| 166 | + | USISR := 1 shl USIOIF; | |
| 167 | + | ||
| 168 | + | repeat | |
| 169 | + | USICR := (%01 shl USIWM) or (%10 shl USICS) or (1 shl USICLK) or (1 shl USITC); | |
| 170 | + | until (USISR and (1 shl USIOIF)) <> 0; | |
| 171 | + | ||
| 172 | + | end; | |
| 173 | + | SPI_PORT.SlaveSelect := True; | |
| 174 | + | end; | |
| 175 | + | ||
| 176 | + | const | |
| 177 | + | Text0 = 'Lazarus ist Toll !' + #13#10; | |
| 178 | + | Text1 = 'Hello World !, Hello AVR' + #13#10; | |
| 179 | + | Text2 = 'SPI als Slave' + #13#10; | |
| 180 | + | ||
| 181 | + | begin | |
| 182 | + | SPI_DDR.DataOut := True; | |
| 183 | + | SPI_DDR.Clock := True; | |
| 184 | + | SPI_DDR.SlaveSelect := True; | |
| 185 | + | ||
| 162 | 186 | repeat | |
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | - | ||
| 168 | - | ||
| 169 | - | ||
| 170 | - | ||
| 171 | - | ||
| 172 | - | ||
| 173 | - | ||
| 174 | - | ||
| 175 | - | ||
| 176 | - | ||
| 177 | - | ||
| 178 | - | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 184 | - | ||
| 185 | - | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 189 | - | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 194 | - | ||
| 187 | + | if not Button_PIN[0] then begin | |
| 188 | + | SPIWriteData(@Text0[1], Length(Text0)); | |
| 189 | + | delay; | |
| 190 | + | end; | |
| 191 | + | if not Button_PIN[1] then begin | |
| 192 | + | SPIWriteData(@Text1[1], Length(Text1)); | |
| 193 | + | delay; | |
| 194 | + | end; | |
| 195 | + | if not Button_PIN[2] then begin | |
| 196 | + | SPIWriteData(@Text2[1], Length(Text2)); | |
| 197 | + | delay; | |
| 198 | + | end; | |
| 199 | + | until 1 = 2; | |
| 200 | + | end. | |
| 195 | 201 | ``` | |
| 196 | 202 | ||
| 197 | 203 | # Siehe auch | |
| 198 | 204 | ||
| 199 | - | ||
| 200 | - | ||
| 201 | - | ||
| 205 | + | * Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]] | |
| 206 | + | * [[AVR Embedded Tutorial - Shiftregister/de|Schieberegister]] \- Wie steuere ich Schieberegister an. | |
| 207 | + | * [[AVR Embedded Tutorial - SPI/de|SPI]] \- Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino. | |
| 202 | 208 | ||
| 203 | - | ||
| 209 | + | Autor: [Mathias](</User:Mathias> "User:Mathias") | |
| 204 | 210 | ||
| 205 | 211 | --- | |
| 206 | - | ||
| 212 | + | *From the Free Pascal wiki, page "AVR Embedded Tutorial - SPI-Slave/de", as the Internet Archive kept it on 2023-03-27. 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).* |
