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 help... clocking issue maybe?

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







SPI help... clocking issue maybe?
PostPosted: Sat Nov 02, 2002 9:47 pm     Reply with quote

Trying to use SPI to communicate between two PIC's.

One set up as master, and provides clock.
Another set up as slave. All the printf statements are just used to see what's happening via RS232 link.

Below is a section of the code which is suppose to interract with one another... any suggestions?

Thanks...


//Master

//////////////////////////////////////////////////////////////
// requestPosition
//////////////////////////////////////////////////////////////
void requestPosition()
{
// ...spi stuff

byte low_byte, high_byte;

output_low(PIN_PIC_CS); //select slave
spi_write(REQUEST_POSITION1);
printf("Requesting position x from slave...\r\n");
output_high(PIN_PIC_CS); //release slave

delay_cycles(50); //give slave time to vector

output_low(PIN_PIC_CS); //select slave again
low_byte = spi_read();
printf("Position x is \%x\r\n", low_byte);
output_high(PIN_PIC_CS); //release slave


output_low(PIN_PIC_CS); //select slave
spi_write(REQUEST_POSITION2);
printf("Requesting position y from slave...\r\n");
output_high(PIN_PIC_CS); //release slave

delay_cycles(50);

output_low(PIN_PIC_CS); //select slave
high_byte = spi_read();
printf("Position y is \%x\r\n", high_byte);
output_high(PIN_PIC_CS); //release slave
}


// Slave

//////////////////////////////////////////////////////////////
// SPI Communication
//////////////////////////////////////////////////////////////

#int_ssp
ssp_isr()
{
int command;
int position_x;
int position_y;

SSPOV = FALSE; //clear possible overwrite

command = spi_read();

switch(command)
{
case '0': //request position_x
printf("Position x requested... \r\n");
position_x = 0x04;
spi_write(position_x);
printf("Position x sent to master... \r\n");
break;

case '1': //request position y
printf("Position y requested... \r\n");
position_y = 0x02;
spi_write(position_y);
printf("Position x sent to master... \r\n");
break;

SSPIF = FALSE; //clear follow-on interrupts

}
___________________________
This message was ported from CCS's old forum
Original Post ID: 8442
R.J.Hamlett
Guest







Re: SPI help... clocking issue maybe?
PostPosted: Sun Nov 03, 2002 8:16 am     Reply with quote

:=Trying to use SPI to communicate between two PIC's.
:=
:=One set up as master, and provides clock.
:=Another set up as slave. All the printf statements are just used to see what's happening via RS232 link.
:=
:=Below is a section of the code which is suppose to interract with one another... any suggestions?
:=
:=Thanks...
I have never played with useing the CS (haven't got enough pins for this!), but I suspect there may be a timing issue that I have outlined in the code...

:=
:=
:=//Master
:=
:=//////////////////////////////////////////////////////////////
:=// requestPosition
:=//////////////////////////////////////////////////////////////
:=void requestPosition()
:={
:= // ...spi stuff
:=
:= byte low_byte, high_byte;
:=
:= output_low(PIN_PIC_CS); //select slave
:= spi_write(REQUEST_POSITION1);
:= printf("Requesting position x from slave...\r\n");
:= output_high(PIN_PIC_CS); //release slave
Are you _sure_ the SPI has completed here?. I think you will find that the SPI write, only sends the character to the hardware, and returns immediately. Hence (depending in the SPI speed selected), the character won't actually be sent till several uSecs latter. Try clearing the read register before sending, and then you can poll the SPI, till there is a character to read. This will imply the character has transferred in the opposite direction, allowing you to drop the CS.

:= delay_cycles(50); //give slave time to vector
Again are you sure this is enough. Depends on the chip, but once the SPI interrupt occurs, on a 18F chip for example, there may be about 20 cycles before you even enter the handler code, and this is assuming the chip does not have interrupts disabled at any time. You have to add the length of the longest routine that disables interrupts, the time needed to save all the registers, and the length of your handler together to ensure that a response has been generated. I have to use 200 cycles delay to cover all this!...

:= output_low(PIN_PIC_CS); //select slave again
:= low_byte = spi_read();
:= printf("Position x is \%x\r\n", low_byte);
:= output_high(PIN_PIC_CS); //release slave
:=
:=
:= output_low(PIN_PIC_CS); //select slave
:= spi_write(REQUEST_POSITION2);
:= printf("Requesting position y from slave...\r\n");
:= output_high(PIN_PIC_CS); //release slave
:=
:= delay_cycles(50);
:=
:= output_low(PIN_PIC_CS); //select slave
:= high_byte = spi_read();
:= printf("Position y is \%x\r\n", high_byte);
:= output_high(PIN_PIC_CS); //release slave
:=}
:=
:=
:=// Slave
:=
:=//////////////////////////////////////////////////////////////
:=// SPI Communication
:=//////////////////////////////////////////////////////////////
:=
:=#int_ssp
:=ssp_isr()
:={
:= int command;
:= int position_x;
:= int position_y;
:=
:= SSPOV = FALSE; //clear possible overwrite
:=
:= command = spi_read();
:=
:= switch(command)
:= {
:= case '0': //request position_x
:= printf("Position x requested... \r\n");
:= position_x = 0x04;
:= spi_write(position_x);
:= printf("Position x sent to master... \r\n");
:= break;
:=
:= case '1': //request position y
:= printf("Position y requested... \r\n");
:= position_y = 0x02;
:= spi_write(position_y);
:= printf("Position x sent to master... \r\n");
:= break;
:=
:= SSPIF = FALSE; //clear follow-on interrupts
:=
:=}

Good Luck.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8448
Woody
Guest







Re: SPI help... clocking issue maybe?
PostPosted: Mon Nov 04, 2002 2:30 am     Reply with quote

In master mode: the function spi_write() causes a byte to be clocked out and the code generated will wait until transmission is complete by checking SSPSTAT bit 0; the function spi_read() only reads the contents of the SSPBUF register.

From your code a command is clocked out to the slave which upon reciept would then presumably write a value into it's SSPBUF.

In order to retrieve this value you need to clock out a dummy value from the master with the chip selected, before your call to spi_read() since the clock is only generated by the master device.

Best Regards

___________________________
This message was ported from CCS's old forum
Original Post ID: 8473
R.J.Hamlett
Guest







Re: SPI help... clocking issue maybe?
PostPosted: Mon Nov 04, 2002 3:58 am     Reply with quote

:=In master mode: the function spi_write() causes a byte to be clocked out and the code generated will wait until transmission is complete by checking SSPSTAT bit 0; the function spi_read() only reads the contents of the SSPBUF register.
:=
This depends on the compiler, but on at least two versions, this wait is not performed...
I have run into exactly this problem, and ended up taking the assembler code apart. It waited _before_ sending the byte, to verify that the register was empty, but did not wait for the character to send....

:=From your code a command is clocked out to the slave which upon reciept would then presumably write a value into it's SSPBUF.
:=
:=In order to retrieve this value you need to clock out a dummy value from the master with the chip selected, before your call to spi_read() since the clock is only generated by the master device.
:=
:=Best Regards
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 8476
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