AVR Embedded Tutorial - SPI MCP4922/de: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # 12Bit DAC MCP4922 über SPI ansteuern. | |
| 2 | 2 | ||
| 3 | 3 | ## Vorwort | |
| 4 | 4 | ||
| 5 | 5 | Dieser Code ist für einen **Atmega328/Arduino** mit **16MHz** und **MCP4922**. | |
| 6 | 6 | ||
| 7 | 7 | Die folgenden Funktionen, sind in folgenden Tutorials beschrieben: | |
| 8 | 8 | ||
| 9 | 9 | - [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO - Aus / Ein-gabe]] - Wie mache ich einen GPIO-Zugriff am AVR. | |
| 10 | 10 | - [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle. | |
| 11 | 11 | ||
| 12 | 12 | ## Beschreibung | |
| 13 | 13 | ||
| 14 | 14 | Dieses Beispiel zeigt, wie man den DAC MCP4922 über SPI ansteuert. | |
| 15 | 15 | ||
| 16 | 16 | ## Funktionen für die Beschreibung von 74HC595 Schieberegister | |
| 17 | 17 | ||
| 18 | 18 | ### Konstanten | |
| 19 | 19 | ||
| 20 | 20 | Pins welche an **PORTB** für **SPI** benötigt werden. | |
| 21 | 21 | ||
| 22 | 22 | ``` pascal | |
| 23 | 23 | type | |
| 24 | 24 | TSPIGPIO = bitpacked record | |
| 25 | 25 | p0, p1, SlaveSelect, MOSI, MISO, Clock, p6, p7: boolean; | |
| 26 | 26 | end; | |
| 27 | 27 | ||
| 28 | 28 | var | |
| 29 | 29 | SPI_PORT: TSPIGPIO absolute PORTB; | |
| 30 | 30 | SPI_DDR: TSPIGPIO absolute DDRB; | |
| 31 | 31 | ``` | |
| 32 | 32 | ||
| 33 | 33 | ### SPI Funktionen | |
| 34 | 34 | ||
| 35 | 35 | #### SPI initialisieren | |
| 36 | 36 | ||
| 37 | 37 | ``` pascal | |
| 38 | 38 | procedure SPIInit; | |
| 39 | 39 | begin | |
| 40 | 40 | // SPI-Port auf Ausgabe | |
| 41 | 41 | SPI_DDR.MOSI := True; | |
| 42 | 42 | SPI_DDR.Clock := True; | |
| 43 | 43 | SPI_DDR.SlaveSelect := True; | |
| 44 | 44 | ||
| 45 | 45 | SPCR := (1 shl SPE) or (0 shl SPIE) or (0 shl DORD) or (1 shl MSTR) or (0 shl CPOL) or (0 shl CPHA) or (%01 shl SPR); | |
| 46 | 46 | SPSR := 1 shl SPI2X; // SCK x 2 auf 1 MHZ | |
| 47 | 47 | end; | |
| 48 | 48 | ``` | |
| 49 | 49 | ||
| 50 | 50 | #### SPI schreiben | |
| 51 | 51 | ||
| 52 | 52 | ``` pascal | |
| 53 | 53 | procedure SPIWrite(p: PByte; len: byte); | |
| 54 | 54 | var | |
| 55 | 55 | i: byte; | |
| 56 | 56 | begin | |
| 57 | 57 | SPI_Port.SlaveSelect := False; | |
| 58 | 58 | for i := len - 1 downto 0 do begin | |
| 59 | 59 | SPDR := p[i]; | |
| 60 | 60 | while (SPSR and (1 shl SPIF)) = 0 do begin | |
| 61 | 61 | end; | |
| 62 | 62 | end; | |
| 63 | 63 | SPI_Port.SlaveSelect := True; | |
| 64 | 64 | end; | |
| 65 | 65 | ``` | |
| 66 | 66 | ||
| 67 | 67 | ### MCP4922 Funktionen | |
| 68 | 68 | ||
| 69 | 69 | Bit-Belegung des MCP4922: | |
| 70 | 70 | ||
| 71 | - | ||
| 72 | 71 | <thead> | |
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 72 | + | ||
| 78 | 73 | </thead> | |
| 79 | 74 | <tbody> | |
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 75 | + | ||
| 84 | 76 | <strong>1</strong> = Write to DACB</td> | |
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 77 | + | ||
| 90 | 78 | <strong>1</strong> = Buffered</td> | |
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 79 | + | ||
| 96 | 80 | <strong>1</strong> = 1x (VOUT = VREF * D / 4096)</td> | |
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 81 | + | ||
| 102 | 82 | <strong>1</strong> = Active mode operation. VOUT is available.</td> | |
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 83 | + | ||
| 109 | 84 | </tbody> | |
| 110 | - | ||
| 111 | 85 | ||
| 112 | 86 | #### Ausgabe an DAC | |
| 113 | 87 | ||
| 114 | 88 | ``` pascal | |
| 115 | 89 | procedure MCP4922sendValue(Value: UInt16; Channel: Byte); | |
| 116 | 90 | type | |
| 117 | 91 | TADC = bitpacked record | |
| 118 | 92 | Value: 0..4095; // Bit 0-11 | |
| 119 | 93 | ShutDown, // Bit 12 | |
| 120 | 94 | Gain, // Bit 13 | |
| 121 | 95 | VRef, // Bit 14 | |
| 122 | 96 | DAC: 0..1; // Bit 15 | |
| 123 | 97 | end; | |
| 124 | 98 | ||
| 125 | 99 | begin | |
| 126 | 100 | with TADC(Value) do begin | |
| 127 | 101 | ShutDown := 1; | |
| 128 | 102 | Gain := 1; | |
| 129 | 103 | VRef := 0; | |
| 130 | 104 | Dac := Channel; | |
| 131 | 105 | end; | |
| 132 | 106 | ||
| 133 | 107 | SPIWrite(@Value, 2); | |
| 134 | 108 | end; | |
| 135 | 109 | ``` | |
| 136 | 110 | ||
| 137 | 111 | ## Beispiel | |
| 138 | 112 | ||
| 139 | 113 | Dieses Beispiel zeigt die Ausgabe von 2 Analogwerten aus dem DAC. 2 LEDs am Analog blinken abwechslungsweise, wobei der Übergang sehr sanft ist. | |
| 140 | 114 | ||
| 141 | 115 | ### Konstanten und Variablen | |
| 142 | 116 | ||
| 143 | 117 | Eine Zähl-Variable die auf 4095 hochzählt. | |
| 144 | 118 | ||
| 145 | 119 | ``` pascal | |
| 146 | 120 | var | |
| 147 | 121 | z: Int16; | |
| 148 | 122 | ``` | |
| 149 | 123 | ||
| 150 | 124 | ### Hauptprogramm | |
| 151 | 125 | ||
| 152 | 126 | Abwechselnd einen Wert in DAC 0& 1 schrieben. | |
| 153 | 127 | ||
| 154 | 128 | ``` pascal | |
| 155 | 129 | begin | |
| 156 | 130 | // SPI initialisieren. | |
| 157 | 131 | SPIInit; | |
| 158 | 132 | ||
| 159 | 133 | repeat | |
| 160 | 134 | for z := 0 to 4095 do begin | |
| 161 | 135 | MCP4922sendValue(4095 - z, 0); | |
| 162 | 136 | MCP4922sendValue(z, 1); | |
| 163 | 137 | end; | |
| 164 | 138 | until 1 = 2; | |
| 165 | 139 | end. | |
| 166 | 140 | ``` | |
| 167 | 141 | ||
| 168 | 142 | # Siehe auch | |
| 169 | 143 | ||
| 170 | 144 | - Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]] | |
| 171 | 145 | - [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino. | |
| 172 | 146 | ||
| 173 | 147 | Autor: Mathias | |
| 174 | 148 | ||
| 175 | 149 | --- | |
| 176 | 150 | *From the Free Pascal wiki, page "AVR Embedded Tutorial - SPI MCP4922/de", as it stood on 2020-01-26 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).* | |
| 177 | 151 | Category: AVR/de | |
| 178 | 152 | Category: Arduino/de | |
| 179 | 153 | Category: Deutsch | |
| 180 | 154 | Category: Embedded/de | |
| 181 | 155 | Category: Tutorials/de |
