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 problem, please advise

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



Joined: 03 Sep 2008
Posts: 1

View user's profile Send private message

SPI problem, please advise
PostPosted: Wed Sep 03, 2008 8:25 am     Reply with quote

Hi, I am using 2 x 16F887 as Master and Slave. I have no problem transmitting data from Master to slave but I can't get data from Slave.

Master code:
I transfer data by:
Code:
output_low(SS_SPI);
spi_write(data);
output_high(SS_SPI);


I recieve data by:
Code:
output_low(SS_SPI);
spi_write(0x00);
slave_data = spi_read();
output_high(SS_SPI);


Am i correct?

Slave code:
I receive data by:
Code:
while(!spi_data_is_in());
{   
      spi_data_recieved = spi_read();
}


I transmit data by:
Code:
spi_write(0x00); //Put data need to send into the output buffer
while(!spi_data_is_in());
{   
      spi_data_recieved = spi_read();
}

Am I correct?

Please advise on this code format.
I will appreciate if any kind soul can pass me his working sample for my reference.

Thanks.
gip_mad



Joined: 23 Aug 2008
Posts: 24
Location: Italy

View user's profile Send private message Visit poster's website

PostPosted: Wed Sep 03, 2008 8:49 am     Reply with quote

There are a lot of working examples in the "examples" folder of your compiler...
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: SPI problem, please advise
PostPosted: Wed Sep 03, 2008 10:04 am     Reply with quote

The CCS library functions are really setup for the master mode only. For example, the function spi_write() probably does more than just load the SSPBUF. But in slave mode that's all you want to do. I suggest that for slave mode you write directly to the SSPBUF register instead of using the CCS library functions. See the 16F887 Microchip datasheet for details.

The code you have written shows that you understand the principles of master and slave mode operation just fine. The only difficulty is knowing exactly what each CCS library function really does. Personally I would not bother using library functions for this, except maybe for the initializations. For everything else, it is easier to talk directly to the registers.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
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