View previous topic :: View next topic |
Author |
Message |
electronx.x.2
Joined: 25 Nov 2011 Posts: 17 Location: Pakistan
|
Spi_read Problem |
Posted: Fri Dec 09, 2011 3:53 am |
|
|
Do spi_read() performs both function, reading and writing ??
I'm confused plz help. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Fri Dec 09, 2011 4:16 am |
|
|
Depends how you use it.
For a _master_ device:
val=SPI_READ();
Will _just_ read the value in the receive register, already received from the last write.
val=SPI_READ(val_to_send);
Will clock out the 'val_to_send', and return the value clocked in at the same time. Equivalent to:
SPI_WRITE(val_to_send);
val=SPI_READ();
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
electronx.x.2
Joined: 25 Nov 2011 Posts: 17 Location: Pakistan
|
|
Posted: Fri Dec 09, 2011 4:22 am |
|
|
thnx, I'm 89% clear, but I want to send 16 bit data and want to receive 16 bit. I'm little bit confused here.
I can't understand the clock and timing, b/w sending and receiving. :( help |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Fri Dec 09, 2011 5:03 am |
|
|
The SPI hardware, only sends 8bits at a time. This though makes no difference at all. You just send two 8bit values, to give a 16bit transfer. The whole 'point' about SPI, is that the master generates the clock. So if you send 16bits as two 8bit transfers all that happens is that the clock between the two bytes is slightly longer.
Code: |
int16 val_to_write;
int16 val_returned;
int8 firstb, secondb;
val_to_write = what_you_want_to_send;
firstb=spi_read(make8(val_to_write,1)); //SPI normally MSB first
secondb=spi_read(make8(val_to_write,0));
val_returned=make16(firstb,secondb);
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
electronx.x.2
Joined: 25 Nov 2011 Posts: 17 Location: Pakistan
|
|
Posted: Fri Dec 16, 2011 1:31 am |
|
|
Ok, I got it.
But what if I want to use only spi_write() for sending purpose and spi_read() for reading purpose.
What will be the role of spi_data_is_in() ? Please guide.
![Exclamation](images/smiles/icon_exclaim.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Fri Dec 16, 2011 2:42 am |
|
|
On the master, no point at all.
spi_read, will not return anything, until a byte is clocked 'out' using spi_write, or by sending the byte with the read.
On a _slave_ device, the separate functions are important.
Here you can 'write' a byte, and it'll sit in the output register, waiting for the _master_ to read it. When the master read it, a byte will be sent back at the same time, and 'spi_data_is_in' will go true, so you can then use an spi_read to get the byte the master sent, and spi_write to load the next byte to send to the master.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
electronx.x.2
Joined: 25 Nov 2011 Posts: 17 Location: Pakistan
|
|
Posted: Sun Dec 18, 2011 10:00 pm |
|
|
What if we use #int_ssp, this gonna help us or not ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Mon Dec 19, 2011 2:34 am |
|
|
Exactly the same, except the hardware does it for you.
On a slave device, INT_SSP, will trigger at the same point that the software function 'spi_data_is_in' would go true. Saying that a byte has been received from the master, and can be read.
I'd always use interrupts (avoids having to have routines polling all the time, and potentially missing bytes if stuff is sent at reasonable speed). However the basic functionality is the same with either approach.
Best Wsihes |
|
![](templates/subSilver/images/spacer.gif) |
|