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 communication

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 30, 2008 4:32 pm     Reply with quote

This chip has a really simple SPI interface. When you shift in the
control byte, you are shifting out the status byte at the same time.
So you can do it two different ways:

You can do it with two lines of spi code, like this:
Code:

int8 control_byte;
int8 diagnosis_data;

control_byte = 0x55;

spi_write(control_byte);
diagnosis_data = spi_read();


Or you can do it with one line, like this:
Code:

int8 control_byte;
int8 diagnosis_data;

control_byte = 0x55;

diagnosis_data = spi_read(control_byte);


These two methods produce the same ASM code in the .LST file.

Note that 0x55 is just a representative value. You need to choose
the real value.

-----------------
I just noticed something about this chip. It claims to be an SPI interface
but it sends the LSB (bit 0) first. But the hardware SPI module in the
PIC sends and receives the MSB (bit 7) first.

There are two possible solutions to this. You can switch to using
software SPI and send the data LSB first. Or, you can use hardware
SPI as shown in the code above, but then you need to call a bit reversal
routine to convert both the transmitted data and the received data.

There are several bit reversal routines shown in the Code library:
http://www.ccsinfo.com/forum/viewtopic.php?t=23364

The revised code would look like this:
Code:
spi_write(reverse_bits(control_byte));
data = reverse_bits(spi_read());

I have just used the function name of 'reverse_bits' as a generic name
for any of the routines shown in the Code Library.
rdesgagn



Joined: 30 Jul 2008
Posts: 3

View user's profile Send private message

PostPosted: Wed Jul 30, 2008 4:39 pm     Reply with quote

Thanks a lot PCM Programmer. Me too, I have noticed that the H-Bridge is sending the LSB first and I was reading it backwards so the data didn't make sense at all.

Thank you again for your help, everything is working like I want !!!
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