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

Porting SPI function to another compiler

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



Joined: 02 Jun 2010
Posts: 74

View user's profile Send private message

Porting SPI function to another compiler
PostPosted: Mon May 13, 2013 4:33 pm     Reply with quote

Hi, I am trying to migrate spi function to another compiler and trying to experiment with it
Code:
#if 0
void spi_write1(uint8_t wData){
   SSP1BUF = wData;
   while(!SSP1STAT & 0x01);
}
uint8_t spi_read1(uint8_t wData){
   uint8_t rData;
   SSP1BUF = wData;
   while(!SSP1STAT  & 0x01);
   rData = SSP1BUF;
   return rData;
}
#else
void spi_write1(uint8_t wData){
   spi_write(wData);
}   
uint8_t spi_read1(uint8_t wData){
   return spi_read(wData);
}
#endif


The first versin(#if 1 version) is the my version, and the second version is utilizing CCS-C's built in function.

Can anybody point out why my version is not working? CCS's built in version works, so I believe all the configurations are set right.

Thank you
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon May 13, 2013 5:06 pm     Reply with quote

Why not just stick with known working code ???

OK, easy way to figure out what you did wrong...create 2 program, identical except for the SPI stuff,compile and then printout the listings.
Go line by line and you'll soon see what you did wrong and what CCS did right.

you'll learn a lot better and remember more if you do the work.

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Tue May 14, 2013 12:37 am     Reply with quote

Far more efficient to declare the buffer full bit as a #bit, and then just test this directly. Single instruction.
You need to empty the buffer before loading it at the start of the routines. Otherwise if a byte has been received and not read, the transaction won't work.
So:
Code:

#bit BF=SSP1STAT.0

void spi_write1(uint8_t wData){
   int8 dummy;
   dummy=SSP1BUF;
   SSP1BUF = wData;
   while(!BF);
}

uint8_t spi_read1(uint8_t wData){
   uint8_t rData;
   rData=SSP1BUF;
   SSP1BUF = wData;
   while(!BF);
   return(SSP1BUF);
}

The extra read is probably your problem.

Best Wishes
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue May 14, 2013 1:47 am     Reply with quote

...and also your test may not be working as you expect:

Code:

   while(!SSP1STAT  & 0x01);


The ! gives the logical negation of SSP1STAT. If its 0 it gives 1 (or true), otherwise, i.e. for ALL other values of SSP1STAT, it returns 0 (or false). That then gets bitwise anded with 0x01, which is useless as it simply the 1 or 0 you've already got. This is not what you need. You need the and to be done first, THEN the logicial negation. To sort out this precedence ordering issue, you need brackets:

Code:

   while(!(SSP1STAT  & 0x01));


...even so it is confusing. Better, as Ttelmah suggests, to use #bit.

RF Developer
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