Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - SPI MCP4922/de: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# 12Bit DAC MCP4922 über SPI ansteuern.
22
33## Vorwort
44
55Dieser Code ist für einen **Atmega328/Arduino** mit **16MHz** und **MCP4922**.
66
77Die folgenden Funktionen, sind in folgenden Tutorials beschrieben:
88
99- [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO - Aus / Ein-gabe]] - Wie mache ich einen GPIO-Zugriff am AVR.
1010- [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle.
1111
1212## Beschreibung
1313
1414Dieses Beispiel zeigt, wie man den DAC MCP4922 über SPI ansteuert.
1515
1616## Funktionen für die Beschreibung von 74HC595 Schieberegister
1717
1818### Konstanten
1919
2020Pins welche an **PORTB** für **SPI** benötigt werden.
2121
2222``` pascal
2323type
2424 TSPIGPIO = bitpacked record
2525 p0, p1, SlaveSelect, MOSI, MISO, Clock, p6, p7: boolean;
2626 end;
2727
2828var
2929 SPI_PORT: TSPIGPIO absolute PORTB;
3030 SPI_DDR: TSPIGPIO absolute DDRB;
3131```
3232
3333### SPI Funktionen
3434
3535#### SPI initialisieren
3636
3737``` pascal
3838procedure SPIInit;
3939begin
4040 // SPI-Port auf Ausgabe
4141 SPI_DDR.MOSI := True;
4242 SPI_DDR.Clock := True;
4343 SPI_DDR.SlaveSelect := True;
4444
4545 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);
4646 SPSR := 1 shl SPI2X; // SCK x 2 auf 1 MHZ
4747end;
4848```
4949
5050#### SPI schreiben
5151
5252``` pascal
5353procedure SPIWrite(p: PByte; len: byte);
5454var
5555 i: byte;
5656begin
5757 SPI_Port.SlaveSelect := False;
5858 for i := len - 1 downto 0 do begin
5959 SPDR := p[i];
6060 while (SPSR and (1 shl SPIF)) = 0 do begin
6161 end;
6262 end;
6363 SPI_Port.SlaveSelect := True;
6464end;
6565```
6666
6767### MCP4922 Funktionen
6868
6969Bit-Belegung des MCP4922:
7070
71-<thead>
72-
73-</thead>
74-<tbody>
75-
76-<strong>1</strong> = Write to DACB</td>
77-
78-<strong>1</strong> = Buffered</td>
79-
80-<strong>1</strong> = 1x (VOUT = VREF * D / 4096)</td>
81-
82-<strong>1</strong> = Active mode operation. VOUT is available.</td>
83-
84-</tbody>
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. | |
8578
8679#### Ausgabe an DAC
8780
8881``` pascal
8982procedure MCP4922sendValue(Value: UInt16; Channel: Byte);
9083type
9184 TADC = bitpacked record
9285 Value: 0..4095; // Bit 0-11
9386 ShutDown, // Bit 12
9487 Gain, // Bit 13
9588 VRef, // Bit 14
9689 DAC: 0..1; // Bit 15
9790 end;
9891
9992begin
10093 with TADC(Value) do begin
10194 ShutDown := 1;
10295 Gain := 1;
10396 VRef := 0;
10497 Dac := Channel;
10598 end;
10699
107100 SPIWrite(@Value, 2);
108101end;
109102```
110103
111104## Beispiel
112105
113106Dieses Beispiel zeigt die Ausgabe von 2 Analogwerten aus dem DAC. 2 LEDs am Analog blinken abwechslungsweise, wobei der Übergang sehr sanft ist.
114107
115108### Konstanten und Variablen
116109
117110Eine Zähl-Variable die auf 4095 hochzählt.
118111
119112``` pascal
120113var
121114 z: Int16;
122115```
123116
124117### Hauptprogramm
125118
126119Abwechselnd einen Wert in DAC 0& 1 schrieben.
127120
128121``` pascal
129122begin
130123 // SPI initialisieren.
131124 SPIInit;
132125
133126 repeat
134127 for z := 0 to 4095 do begin
135128 MCP4922sendValue(4095 - z, 0);
136129 MCP4922sendValue(z, 1);
137130 end;
138131 until 1 = 2;
139132end.
140133```
141134
142135# Siehe auch
143136
144137- Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]]
145138- [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino.
146139
147140Autor: Mathias
148141
149142---
150143*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).*
151144Category: AVR/de
152145Category: Arduino/de
153146Category: Deutsch
154147Category: Embedded/de
155148Category: Tutorials/de

Back to history