CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Spi_read Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
electronx.x.2



Joined: 25 Nov 2011
Posts: 17
Location: Pakistan

View user's profile Send private message AIM Address MSN Messenger

Spi_read Problem
PostPosted: Fri Dec 09, 2011 3:53 am     Reply with quote

Do spi_read() performs both function, reading and writing ??
I'm confused plz help.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Fri Dec 09, 2011 4:16 am     Reply with quote

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
electronx.x.2



Joined: 25 Nov 2011
Posts: 17
Location: Pakistan

View user's profile Send private message AIM Address MSN Messenger

PostPosted: Fri Dec 09, 2011 4:22 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Fri Dec 09, 2011 5:03 am     Reply with quote

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
electronx.x.2



Joined: 25 Nov 2011
Posts: 17
Location: Pakistan

View user's profile Send private message AIM Address MSN Messenger

PostPosted: Fri Dec 16, 2011 1:31 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 2:42 am     Reply with quote

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
electronx.x.2



Joined: 25 Nov 2011
Posts: 17
Location: Pakistan

View user's profile Send private message AIM Address MSN Messenger

PostPosted: Sun Dec 18, 2011 10:00 pm     Reply with quote

What if we use #int_ssp, this gonna help us or not ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Mon Dec 19, 2011 2:34 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group