|
|
View previous topic :: View next topic |
Author |
Message |
PicNewboy Guest
|
Help with SPI code |
Posted: Fri Apr 08, 2005 5:02 am |
|
|
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
|
|
Posted: Fri Apr 08, 2005 5:26 am |
|
|
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 |
|
|
|
|
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
|