View previous topic :: View next topic |
Author |
Message |
hello188
Joined: 02 Jun 2010 Posts: 74
|
Porting SPI function to another compiler |
Posted: Mon May 13, 2013 4:33 pm |
|
|
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: 9215 Location: Greensville,Ontario
|
|
Posted: Mon May 13, 2013 5:06 pm |
|
|
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: 19477
|
|
Posted: Tue May 14, 2013 12:37 am |
|
|
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
|
|
Posted: Tue May 14, 2013 1:47 am |
|
|
...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 |
|
|
|