Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - SPI MCP4922/de: what changed

Revision 1 to 2. 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-<table>
7271<thead>
73-<tr>
74-<th>Bit</th>
75-<th>Bezeichnung</th>
76-<th>Beschreibung</th>
77-</tr>
72+
7873</thead>
7974<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 />
75+
8476<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 />
77+
9078<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 />
79+
9680<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 />
81+
10282<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>
83+
10984</tbody>
110-</table>
11185
11286#### Ausgabe an DAC
11387
11488``` pascal
11589procedure MCP4922sendValue(Value: UInt16; Channel: Byte);
11690type
11791 TADC = bitpacked record
11892 Value: 0..4095; // Bit 0-11
11993 ShutDown, // Bit 12
12094 Gain, // Bit 13
12195 VRef, // Bit 14
12296 DAC: 0..1; // Bit 15
12397 end;
12498
12599begin
126100 with TADC(Value) do begin
127101 ShutDown := 1;
128102 Gain := 1;
129103 VRef := 0;
130104 Dac := Channel;
131105 end;
132106
133107 SPIWrite(@Value, 2);
134108end;
135109```
136110
137111## Beispiel
138112
139113Dieses Beispiel zeigt die Ausgabe von 2 Analogwerten aus dem DAC. 2 LEDs am Analog blinken abwechslungsweise, wobei der Übergang sehr sanft ist.
140114
141115### Konstanten und Variablen
142116
143117Eine Zähl-Variable die auf 4095 hochzählt.
144118
145119``` pascal
146120var
147121 z: Int16;
148122```
149123
150124### Hauptprogramm
151125
152126Abwechselnd einen Wert in DAC 0& 1 schrieben.
153127
154128``` pascal
155129begin
156130 // SPI initialisieren.
157131 SPIInit;
158132
159133 repeat
160134 for z := 0 to 4095 do begin
161135 MCP4922sendValue(4095 - z, 0);
162136 MCP4922sendValue(z, 1);
163137 end;
164138 until 1 = 2;
165139end.
166140```
167141
168142# Siehe auch
169143
170144- Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]]
171145- [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino.
172146
173147Autor: Mathias
174148
175149---
176150*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).*
177151Category: AVR/de
178152Category: Arduino/de
179153Category: Deutsch
180154Category: Embedded/de
181155Category: Tutorials/de

Back to history