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