Difference between revisions of "AVR Embedded Tutorial - SPI MCP4922/de"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed template loop; syntax highlighting; modded categories to German; added language category)
 
(13 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Translate}}
+
{{LanguageBar}}
 +
 
 
=12Bit DAC MCP4922 über SPI ansteuern.=
 
=12Bit DAC MCP4922 über SPI ansteuern.=
  
 
==Vorwort==
 
==Vorwort==
  
Dieser Code ist für einen '''Atmega328/Arduino''' mit '''16MHz''' und '''MCP4922'''.<br>
+
Dieser Code ist für einen '''Atmega328/Arduino''' mit '''16MHz''' und '''MCP4922'''.
  
 +
Die folgenden Funktionen, sind in folgenden Tutorials beschrieben:
  
Die folgenden Funktionen, sind in folgenden Tutorials beschrieben:
 
 
* [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO - Aus / Ein-gabe]] - Wie mache ich einen GPIO-Zugriff am AVR.
 
* [[AVR Embedded Tutorial - Simple GPIO on and off output/de|GPIO - Aus / Ein-gabe]] - Wie mache ich einen GPIO-Zugriff am AVR.
 
* [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle.
 
* [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle.
  
 
==Beschreibung==
 
==Beschreibung==
 +
 
Dieses Beispiel zeigt, wie man den DAC MCP4922 über SPI ansteuert.
 
Dieses Beispiel zeigt, wie man den DAC MCP4922 über SPI ansteuert.
  
 
== Funktionen für die Beschreibung von 74HC595 Schieberegister ==
 
== Funktionen für die Beschreibung von 74HC595 Schieberegister ==
 +
 
=== Konstanten ===
 
=== Konstanten ===
Pins welche an PORTB für SPI benötigt werden.
+
 
<syntaxhighlight>
+
Pins welche an '''PORTB''' für '''SPI''' benötigt werden.
 +
 
 +
<syntaxhighlight lang="pascal">
 
type
 
type
 
   TSPIGPIO = bitpacked record
 
   TSPIGPIO = bitpacked record
     p0, p1, SlaveSelect, DataOut, DataIn, Clock, p6, p7: boolean;
+
     p0, p1, SlaveSelect, MOSI, MISO, Clock, p6, p7: boolean;
 
   end;
 
   end;
  
Line 29: Line 34:
  
 
===SPI Funktionen===
 
===SPI Funktionen===
 +
 
====SPI initialisieren====
 
====SPI initialisieren====
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
procedure SPIInit;
 
procedure SPIInit;
 
begin
 
begin
 
   // SPI-Port auf Ausgabe
 
   // SPI-Port auf Ausgabe
   SPI_DDR.DataOut := True;
+
   SPI_DDR.MOSI        := True;
   SPI_DDR.Clock := True;
+
   SPI_DDR.Clock       := True;
 
   SPI_DDR.SlaveSelect := True;
 
   SPI_DDR.SlaveSelect := True;
  
   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));
+
   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);
   SPSR := (1 shl SPI2X);  // SCK x 2 auf 1 MHZ
+
   SPSR := 1 shl SPI2X;  // SCK x 2 auf 1 MHZ
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
====SPI schreiben====
 
====SPI schreiben====
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
procedure SPIWrite(p: PByte; len: byte);
 
procedure SPIWrite(p: PByte; len: byte);
 
var
 
var
Line 60: Line 68:
  
 
===MCP4922 Funktionen===
 
===MCP4922 Funktionen===
 +
 
Bit-Belegung des MCP4922:
 
Bit-Belegung des MCP4922:
*bit 0-11 Analog Value
 
*bit 12 Output shutdown control bit  0 = Shutdown the device  1 = Active mode operation
 
*bit 13 Output Gain selection        0 = 2x                    1 = 1x
 
*bit 14 Vref input buffer control    0 = unbuffered(default)  1 = buffered
 
*bit 15                              0 = DAC_A                1 = DAC_B
 
  
 +
{| class="wikitable"
 +
!Bit  !! Bezeichnung !! Beschreibung     
 +
|-
 +
| 15  ||''' A/B:''' DACA or DACB Selection bit                || '''0''' = Write to DACA<br>'''1''' = Write to DACB
 +
|-
 +
| 14  || '''BUF:''' Vref Input Buffer Controlbit              || '''0''' = Unbuffered<br>'''1''' = Buffered
 +
|-
 +
| 13  || '''GA:'''Output Gain Selection bit                  ||'''0''' = 2x (VOUT = 2 * VREF * D / 4096)<br>'''1''' = 1x (VOUT = VREF * D / 4096)
 +
|-
 +
| 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)<br>'''1''' = Active mode operation. VOUT is available.
 +
|-
 +
| 11:0 || '''D11:D0:''' DAC Input Data bits. Bit x is ignored. ||
 +
|}
  
 
====Ausgabe an DAC====
 
====Ausgabe an DAC====
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
procedure MCP4922sendValue(Value: UInt16; Channel: Byte);
 
procedure MCP4922sendValue(Value: UInt16; Channel: Byte);
 
type
 
type
 
   TADC = bitpacked record
 
   TADC = bitpacked record
     Value: 0..2048;
+
     Value: 0..4095; // Bit 0-11
     ShutDown,
+
     ShutDown,       // Bit 12
     Gain,
+
     Gain,           // Bit 13
     VRef,
+
     VRef,           // Bit 14
     Dac: 0..1;
+
     DAC: 0..1;     // Bit 15
 
   end;
 
   end;
  
Line 83: Line 101:
 
   with TADC(Value) do begin
 
   with TADC(Value) do begin
 
     ShutDown := 1;
 
     ShutDown := 1;
     Gain := 1;
+
     Gain     := 1;
     VRef := 0;
+
     VRef     := 0;
     Dac := Channel;
+
     Dac     := Channel;
 
   end;
 
   end;
  
Line 93: Line 111:
  
 
== Beispiel ==
 
== Beispiel ==
Diese Beispiel zeigt da Beschreiben von 2 kaskadierten Schieberegister.
+
 
 +
Dieses Beispiel zeigt die Ausgabe von 2 Analogwerten aus dem DAC.
 +
2 LEDs am Analog blinken abwechslungsweise, wobei der Übergang sehr sanft ist.
  
 
=== Konstanten und Variablen ===
 
=== Konstanten und Variablen ===
Eine 16Bit Variable die einfach hochzählt.
+
 
<syntaxhighlight>
+
Eine Zähl-Variable die auf 4095 hochzählt.
 +
 
 +
<syntaxhighlight lang="pascal">
 
var
 
var
 
   z: Int16;
 
   z: Int16;
Line 103: Line 125:
  
 
=== Hauptprogramm ===
 
=== Hauptprogramm ===
 +
 
Abwechselnd einen Wert in DAC 0& 1 schrieben.
 
Abwechselnd einen Wert in DAC 0& 1 schrieben.
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
begin
 
begin
 
   // SPI initialisieren.
 
   // SPI initialisieren.
Line 118: Line 142:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Siehe auch ==
+
= Siehe auch =
 +
 
 
* Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]]
 
* Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]]
 
* [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino.
 
* [[AVR Embedded Tutorial - SPI/de|SPI]] - Nutzung der Hardware-SPI-Schnittstelle bei einem ATmega328 / Arduino.
Line 124: Line 149:
 
Autor: [[User:Mathias|Mathias]]
 
Autor: [[User:Mathias|Mathias]]
  
[[Category:Embedded]]
+
[[Category:AVR/de]]
[[Category:AVR]]
+
[[Category:Arduino/de]]
[[Category:Arduino]]
+
[[Category:Embedded/de]]
 +
[[Category:Tutorials/de]]
 +
{{AutoCategory}}

Latest revision as of 05:21, 26 January 2020

Deutsch (de) English (en)

12Bit DAC MCP4922 über SPI ansteuern.

Vorwort

Dieser Code ist für einen Atmega328/Arduino mit 16MHz und MCP4922.

Die folgenden Funktionen, sind in folgenden Tutorials beschrieben:

Beschreibung

Dieses Beispiel zeigt, wie man den DAC MCP4922 über SPI ansteuert.

Funktionen für die Beschreibung von 74HC595 Schieberegister

Konstanten

Pins welche an PORTB für SPI benötigt werden.

type
  TSPIGPIO = bitpacked record
    p0, p1, SlaveSelect, MOSI, MISO, Clock, p6, p7: boolean;
  end;

var
  SPI_PORT: TSPIGPIO absolute PORTB;
  SPI_DDR:  TSPIGPIO absolute DDRB;

SPI Funktionen

SPI initialisieren

procedure SPIInit;
begin
  // SPI-Port auf Ausgabe
  SPI_DDR.MOSI        := True;
  SPI_DDR.Clock       := True;
  SPI_DDR.SlaveSelect := True;

  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);
  SPSR := 1 shl SPI2X;  // SCK x 2 auf 1 MHZ
end;

SPI schreiben

procedure SPIWrite(p: PByte; len: byte);
var
  i: byte;
begin
  SPI_Port.SlaveSelect := False;
  for i := len - 1 downto 0 do begin
    SPDR := p[i];
    while (SPSR and (1 shl SPIF)) = 0 do begin
    end;
  end;
  SPI_Port.SlaveSelect := True;
end;

MCP4922 Funktionen

Bit-Belegung des MCP4922:

Bit Bezeichnung Beschreibung
15 A/B: DACA or DACB Selection bit 0 = Write to DACA
1 = Write to DACB
14 BUF: Vref Input Buffer Controlbit 0 = Unbuffered
1 = Buffered
13 GA:Output Gain Selection bit 0 = 2x (VOUT = 2 * VREF * D / 4096)
1 = 1x (VOUT = VREF * D / 4096)
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.
11:0 D11:D0: DAC Input Data bits. Bit x is ignored.

Ausgabe an DAC

procedure MCP4922sendValue(Value: UInt16; Channel: Byte);
type
  TADC = bitpacked record
    Value: 0..4095; // Bit 0-11
    ShutDown,       // Bit 12
    Gain,           // Bit 13
    VRef,           // Bit 14
    DAC: 0..1;      // Bit 15 
  end;

begin
  with TADC(Value) do begin
    ShutDown := 1;
    Gain     := 1;
    VRef     := 0;
    Dac      := Channel;
  end;

  SPIWrite(@Value, 2);
end;

Beispiel

Dieses Beispiel zeigt die Ausgabe von 2 Analogwerten aus dem DAC. 2 LEDs am Analog blinken abwechslungsweise, wobei der Übergang sehr sanft ist.

Konstanten und Variablen

Eine Zähl-Variable die auf 4095 hochzählt.

var
  z: Int16;

Hauptprogramm

Abwechselnd einen Wert in DAC 0& 1 schrieben.

begin
  // SPI initialisieren.
  SPIInit; 

  repeat
    for z := 0 to 4095 do begin
      MCP4922sendValue(4095 - z, 0);
      MCP4922sendValue(z, 1);
    end;
  until 1 = 2;
end.

Siehe auch

Autor: Mathias