Beginners SPI questions

General discussion on mikroPascal.
Post Reply
Author
Message
Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Beginners SPI questions

#1 Post by Dany » 03 Feb 2009 16:17

Hi, the explanation in the compiler's library help is rather basic regarding what exactly the SPI read and write functions do.

I understand (from other sources) that SPI does simultaneously sends and receives a byte. Together with what I find in mE's help I can assume the following:
Lib help wrote:function Spi_Read(buffer : byte) : byte;
- Returns the received data.
- Provides clock by sending buffer and receives data at the end of period.

Code: Select all

take := Spi_Read(buffer);
Writes "buffer" to the SPI device and receives at the same time "take" from the SPI device.
Lib help wrote:procedure Spi_Write(data : byte) : byte;
Returns Nothing.
Writes byte data to SSPBUF, and immediately starts the transmission.
Writes "buffer" to the SPI device and ignores data received.

My actual question is: are above assumptions about mE's SPI library correct?

I do ask this question because I am a complete novice regarding SPI usage, and I try to understand the usage of the library functions before attempting using them... :oops:
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#2 Post by yo2lio » 03 Feb 2009 17:11

The SPI is very very simple ...

Take a look at my spi routine , I use this for read and write ...

Code: Select all

function Spi_Read_(dat_in : byte) : byte;
begin
  SSPBUF := dat_in;
  asm
    nop
    movf SSPSTAT,w
    btfss WREG,0
    bra	$-2
  end;
  result := SSPBUF;
end;
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#3 Post by Dany » 03 Feb 2009 21:31

Thanks Florin. Do you suggest I use your routine in stead of mE's?

As I assumed (and your routine suggests) the read and write take indeed place at the same time. "dat_in" is sent to the SPI slave and the what is coming from the SPI slave is put in the function result. All is clear now. Thanks! :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 03 Feb 2009 21:46

Dany wrote:Do you suggest I use your routine in stead of mE's?
No, you can use any routine, I show you how this it's made ...
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#5 Post by Dany » 04 Feb 2009 10:32

Thanks Florin! :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroPascal General”