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

Help with SPI code

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







Help with SPI code
PostPosted: Fri Apr 08, 2005 5:02 am     Reply with quote

I have a problem with a SPI communication between two Pic18f4525
I try with a simple program,the master is the only pic that can use the serial 232 port,the slave make an operation(e.g a sum) and send back the result to master...How I make wrong?

Master Code:

#include <18f4525.h>
#device adc=10
#fuses H4,NOLVP,NOWDT,PUT,NOBROWNOUT
#use delay(clock=40000000)
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7)

void init_spi(void)
{
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
return;
}

void main(void)
{
int data=0;

init_spi();
printf("SPI test start.\r\n");
delay_ms(1500);

while (1) {
spi_write(data);
if (spi_data_is_in())
data=spi_read();
printf("M:data = %d\r\n", data);
}
}


Slave code:

void main(void)
{
int data=0;

init_spi();
while (1) {
if (spi_data_is_in())
data=spi_read();
spi_write(data+1);
}
}

Can anyone help me?Thank you.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 08, 2005 5:26 am     Reply with quote

The slave can never generate clock pulses, so the spi_write on the slave is depending on the master to generate the clock pulses, this can be achieved by either adding an extra spi_write in your master or by calling spi_read in the master with a value to be transmitted.

A nice example was posted a few days ago by Ttelmah: http://www.ccsinfo.com/forum/viewtopic.php?t=22416
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