AVR Embedded Tutorial - SPI MCP4922/de: what changed
Revision 2 to 3. 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 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 71 | + | | Bit | Bezeichnung | Beschreibung | | |
| 72 | + | |---|---|---| | |
| 73 | + | | 15 | **A/B:** DACA or DACB Selection bit | **0** = Write to DACA · **1** = Write to DACB | | |
| 74 | + | | 14 | **BUF:** Vref Input Buffer Controlbit | **0** = Unbuffered · **1** = Buffered | | |
| 75 | + | | 13 | **GA:**Output Gain Selection bit | **0** = 2x (VOUT = 2 * VREF * D / 4096) · **1** = 1x (VOUT = VREF * D / 4096) | | |
| 76 | + | | 12 | **SHDN:** Output Shutdown Control bit | **0** = 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) · **1** = Active mode operation. VOUT is available. | | |
| 77 | + | | 11:0 | **D11:D0:** DAC Input Data bits. Bit x is ignored. | | | |
| 85 | 78 | ||
| 86 | 79 | #### Ausgabe an DAC | |
| 87 | 80 | ||
| 88 | 81 | ``` pascal | |
| 89 | 82 | procedure MCP4922sendValue(Value: UInt16; Channel: Byte); | |
| 90 | 83 | type | |
| 91 | 84 | TADC = bitpacked record | |
| 92 | 85 | Value: 0..4095; // Bit 0-11 | |
| 93 | 86 | ShutDown, // Bit 12 | |
| 94 | 87 | Gain, // Bit 13 | |
| 95 | 88 | VRef, // Bit 14 | |
| 96 | 89 | DAC: 0..1; // Bit 15 | |
| 97 | 90 | end; | |
| 98 | 91 | ||
| 99 | 92 | begin | |
| 100 | 93 | with TADC(Value) do begin | |
| 101 | 94 | ShutDown := 1; | |
| 102 | 95 | Gain := 1; | |
| 103 | 96 | VRef := 0; | |
| 104 | 97 | Dac := Channel; | |
| 105 | 98 | end; | |
| 106 | 99 | ||
| 107 | 100 | SPIWrite(@Value, 2); | |
| 108 | 101 | end; | |
| 109 | 102 | ``` | |
| 110 | 103 | ||
| 111 | 104 | ## Beispiel | |
| 112 | 105 | ||
| 113 | 106 | Dieses Beispiel zeigt die Ausgabe von 2 Analogwerten aus dem DAC. 2 LEDs am Analog blinken abwechslungsweise, wobei der Übergang sehr sanft ist. | |
| 114 | 107 | ||
| 115 | 108 | ### Konstanten und Variablen | |
| 116 | 109 | ||
| 117 | 110 | Eine Zähl-Variable die auf 4095 hochzählt. | |
| 118 | 111 | ||
| 119 | 112 | ``` pascal | |
| 120 | 113 | var | |
| 121 | 114 | z: Int16; | |
| 122 | 115 | ``` | |
| 123 | 116 | ||
| 124 | 117 | ### Hauptprogramm | |
| 125 | 118 | ||
| 126 | 119 | Abwechselnd einen Wert in DAC 0& 1 schrieben. | |
| 127 | 120 | ||
| 128 | 121 | ``` pascal | |
| 129 | 122 | begin | |
| 130 | 123 | // SPI initialisieren. | |
| 131 | 124 | SPIInit; | |
| 132 | 125 | ||
| 133 | 126 | repeat | |
| 134 | 127 | for z := 0 to 4095 do begin | |
| 135 | 128 | MCP4922sendValue(4095 - z, 0); | |
| 136 | 129 | MCP4922sendValue(z, 1); | |
| 137 | 130 | end; | |
| 138 | 131 | until 1 = 2; | |
| 139 | 132 | end. | |
| 140 | 133 | ``` | |
| 141 | 134 | ||
| 142 | 135 | # Siehe auch | |
| 143 | 136 | ||
| 144 | 137 | - Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]] | |
| 145 | 138 | - [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino. | |
| 146 | 139 | ||
| 147 | 140 | Autor: Mathias | |
| 148 | 141 | ||
| 149 | 142 | --- | |
| 150 | 143 | *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).* | |
| 151 | 144 | Category: AVR/de | |
| 152 | 145 | Category: Arduino/de | |
| 153 | 146 | Category: Deutsch | |
| 154 | 147 | Category: Embedded/de | |
| 155 | 148 | Category: Tutorials/de |
