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

From Free Pascal wiki
Jump to navigationJump to search
Line 61: Line 61:
 
===MCP4922 Funktionen===
 
===MCP4922 Funktionen===
 
Bit-Belegung des MCP4922:
 
Bit-Belegung des MCP4922:
*bit 0-11 Analog Value
+
{| class="wikitable"
*bit 12 Output shutdown control bit  0 = Shutdown the device  1 = Active mode operation
+
|-
*bit 13 Output Gain selection        0 = 2x                    1 = 1x
+
!Bit  !! Beschreibung                !! Low                  !! High
*bit 14 Vref input buffer control    0 = unbuffered(default)   1 = buffered
+
|-
*bit 15                             0 = DAC_A                1 = DAC_B
+
| 0-11 || Analog Value                 || 0 - 4095              ||
 +
|-
 +
| 12   || Output shutdown control bit  || Shutdown the device  || Active mode operation
 +
|-
 +
| 13   || Output Gain selection        || 2x                    || 1x
 +
|-
 +
| 14   || Vref input buffer control    || unbuffered (default) || buffered
 +
|-
 +
| 15   || DAC                          || DAC_A                || DAC_B
 +
|}
  
  
Line 73: Line 82:
 
type
 
type
 
   TADC = bitpacked record
 
   TADC = bitpacked record
     Value: 0..2048;
+
     Value: 0..4095;
 
     ShutDown,
 
     ShutDown,
 
     Gain,
 
     Gain,

Revision as of 20:30, 14 August 2018

Template:Translate

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, DataOut, DataIn, 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.DataOut := 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 Beschreibung Low High
0-11 Analog Value 0 - 4095
12 Output shutdown control bit Shutdown the device Active mode operation
13 Output Gain selection 2x 1x
14 Vref input buffer control unbuffered (default) buffered
15 DAC DAC_A DAC_B


Ausgabe an DAC

procedure MCP4922sendValue(Value: UInt16; Channel: Byte);
type
  TADC = bitpacked record
    Value: 0..4095;
    ShutDown,
    Gain,
    VRef,
    DAC: 0..1;
  end;

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

  SPIWrite(@Value, 2);
end;

Beispiel

Diese Beispiel zeigt da Beschreiben von 2 kaskadierten Schieberegister.

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