Löst man das ganze mit bitpacket und absolute, kann man es sehr elegant und übersichtlich lösen.
Das ganze Gewurschtelt mit if , or und and not entfällt.
Infos zu der Bitfolge
Code: Alles auswählen
// bit 0-11 Analog Value
// bit 15 0 = DAC_A 1 = DAC_B
// bit 14 Vref input buffer control 0 = unbuffered(default) 1 = buffered
// bit 13 Output Gain selection 0 = 2x 1 = 1x
// bit 12 Output shutdown control bit 0 = Shutdown the device 1 = Active mode operation
Code: Alles auswählen
procedure TMCP4922.sendValue(Value: UInt16);
begin
if fDAC then begin
Value := Value or (UInt16(1) shl 15);
end else begin
Value := Value and not (UInt16(1) shl 15);
end;
Value := Value and not (UInt16(1) shl 14);
if fGain then begin
Value := Value or (UInt16(1) shl 13);
end else begin
Value := Value and not (UInt16(1) shl 13);
end;
Value := Value or (UInt16(1) shl 12);
SPI_Write(@Value, 2);
end;
Code: Alles auswählen
procedure TMCP4922.sendValue(Value: UInt16);
var
Data: bitpacked array[0..15] of boolean absolute Value;
begin
Data[12] := True;
Data[13] := fGain;
Data[14] := False;
Data[15] := fDAC;
SPI_Write(@Data, 2);
end;
