unit uTesterGUI;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
ExtCtrls, PrintersDlgs, Types, fpspi;
type
{ TForm1 }
TForm1 = class(TForm)
........
private
function GetADValue(ADChan: string): LongWord;
public
end;
Type
....
implementation
{$R *.lfm}
{ TForm1 }
...
procedure TForm1.bCalibrateClick(Sender: TObject);
begin
lRaw.Text:= IntToStr(GetADValue('Ch0')); //AD result into LableEdit box
end;
...
function GetADValue(ADChan: string): LongWord;
var
spi: TSPILinuxDevice;
rbuf, wbuf, smallrbuf, highrbuf, completerbuf: LongWord;
begin
// SPI Bus 0
// 2nd ChipSelect
// The numbers are directly mapped to the file name /dev/spidevB.C
// with B = Bus and C = Chipselect
// see https://www.kernel.org/doc/Documentation/spi/spidev
spi := TSPILinuxDevice.Create(0, 1);// bus 0, cs 1
// set a SPI mode
spi.Mode := SPI_MODE_0;
spi.MaxFrequency:= 500;
spi.LSBFirst:= false;
try
begin
// data sent to device
{b'0000,0001,,0000,0000,,0000,0000,,0000,0000,,0000,0000'
$01000 A/D channel 0 & 1, Differential, ch0 = +, ch1 = -
b'0000,0001,,0010,0000,,0000,0000,,0000,0000,,0000,0000'
$01300 A/D channel 2 & 3, Differential, ch2 =+, ch3 =-}
if ADChan = 'Ch0' then wbuf := $01000 else wbuf:= $01200;
// handle result
smallrbuf:= rbuf shr 24; //shr = shift right
highrbuf := rbuf AND $f0000;
highrbuf := highrbuf shr 8;
completerbuf:= smallrbuf + highrbuf;
GetADValue:= CompleteRbuf;
sleep(100);
end;
finally
spi.Destroy;
end;
end;
end.