* * *

Author Topic: New in pascal need help (raspberry pi SPI)  (Read 2018 times)


  • New member
  • *
  • Posts: 12
New in pascal need help (raspberry pi SPI)
« on: December 12, 2016, 11:35:42 am »
I am currently working with the library libwiringpi which include some very useful functions. One of them is for the spi protocol of raspberry pi. This library has as prototype the function wiringPiSPIDataRW(channel:longint; data: Pointer; len:longint):longint;cdecl;external;
Could please inform me how i could pass an array on the data section.
Currently i try to make an array as buffer with 5 elements integer. I try many pointer example without success. The problem is i always take as result our of memory , or garbage from random memory position ( i think because i am not in medium or advance in pascal).

The following code in C it works (i do not know if that helps).
unsigned char buffer[3];
buffer[0] |= 0b11000000| ((adc_channel)<<4);
printf("value to send is %d \n",buffer[0]);
wiringPiSPIDataRW(spi_channel, buffer, 1);
delay(2000);//wait to make the convertion
wiringPiSPIDataRW(spi_channel, buffer, 3);
printf ("results = %f \n",final);
printf("return value is %s \n",buffer);

I am attaching the source code form my pascal program and the outputs form the C and the pascal program. I upload the attachment in following link
Any new ideas perhaps?


  • Full Member
  • ***
  • Posts: 105
Re: New in pascal need help (raspberry pi SPI)
« Reply #1 on: December 12, 2016, 01:29:39 pm »
Hi !

I'm not an expert but, perhaps, you should try this :

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.     i:integer;                       //  because your  "adc_value" is only an index to browse "buffer" and not really an adc value,
  4.                                      //  this is clearer to call it "i" or whatever do you want  (in my opinion)
  5.     buffer:Array [0..2] of byte;     // [0..3] mean 4 byte length. You need only 3 bytes here
  6.     Res: longint;                    // this 2 variable replace your "result:double"
  7.     Value: double;
  8. begin
  9.  memo1.clear;
  10.  digitalWrite(CS,LOW);
  11.  for i:=0 to 2 do                     // take care: only from 0 to 2 because we have now buffer[0..2]
  12.   buffer[i]:=0; //clean up array
  13. // prepare the code
  14.  if channel_0.Checked                 // this is simpliest
  15.    then buffer[0] := $D0
  16.    else buffer[0] := $E0;
  17.  digitalWrite(CS,LOW);
  18. //spi_test;
  19.  Res := wiringPiSPIDataRW(spi_channel,@buffer[0],1);    // <- in Pascal language, "Result" is used to define the result of a function,
  20.                                                         //    so, better avoid using this as a variable
  21.  delay(200);//wait for the convertion and setup time
  22.  Res:=wiringPiSPIDataRW(spi_channel,@buffer[0],3);
  23.  if Res=-1
  24.    then showMessage('Error in send and receive data');
  25.  Value:= (buffer[1] shl 7) or buffer[2];
  26.  digitalWrite(CS,HIGH);//stop convertions
  27.  Value:=Value*3.3;//Vref=3.3 from rpi
  28.  Value:=Value/4096;//2^10  adc output=(4096 *vin)/Vcc  vcc=vref=3.3
  29. //check
  30.  for i:=0 to 3 do
  31.    memo1.lines.add('Buffer value '+intToStr(i)+' is '+IntToStr(Buffer[i]));
  32.  memo1.lines.add('result = '+FloatToStr(Value));
  33. end;

I can't compile it because there isn't dll in your project.

I hope this will help you.
fpc 3.0.4 Lazarus 1.8.0  -  AMD A8-6600K  -  Windows 10  64bits
bad english... sorry :)


  • Full Member
  • ***
  • Posts: 133
Re: New in pascal need help (raspberry pi SPI)
« Reply #2 on: December 12, 2016, 01:30:53 pm »
did you bother to check your previous question to see if it was answered or not??
*Thaddy Mode*  >:D

you asked the same question here http://forum.lazarus-ide.org/index.php?topic=35083 and some answers were provided.
did you test if those answers worked for you?
if no, then please continue the question in the previous topic.
« Last Edit: December 12, 2016, 04:02:54 pm by Xor-el »



Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus