![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Elysion
Joined: 03 Aug 2010 Posts: 26
|
Sending Data Problem at SPI Slave mode |
Posted: Mon Feb 21, 2011 6:12 am |
|
|
I wrote these codes shown below but I couldn't send any data at SPI Slave mode. (My compiler version is 4.114) (Also I looked at this link: http://www.ccsinfo.com/forum/viewtopic.php?t=39145&highlight=spi+slave
Master Side
Code: |
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
void main()
{
int a;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
while(1)
{
a=spi_read();
if(a==58)
{
output_high(PIN_C3);
delay_ms(1000);
output_low(PIN_C3);
delay_ms(1000);
}
}
} |
Slave
Code: |
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
int a;
#int_ssp
void ssp_isr(void)
{
a=58;
spi_write(a);
delay_us(100);
}
void main()
{
setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_L_TO_H);
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
}
}
|
Could you propose anything? (I tested these codes with real hardware.)
Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Mon Feb 21, 2011 6:29 am |
|
|
Two separate problems.
First, is that you are potentially not sending anything till _after_ the master has completed it's transaction.
With SPI, the master sends a byte to the slave, and gets back garbage (what is left in the slaves transmit buffer from last time). It then has to clock another byte to the slave, to get back what the slave loaded. INT_SSP, only occurs when the slave has received the first byte....
Then second, is that the master has to _clock_ each transaction. Currently you are not.
Code: |
//Slave
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
int a;
#int_ssp
void ssp_isr(void)
{
int dummy;
dummy=spi_read(); //get the byte the master has sent - you may want it
a=58;
spi_write(a);
}
void main()
{
setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_L_TO_H);
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
}
}
//Master
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
void main()
{
int a;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
while(1)
{
spi_write(0); //send a byte to trigger the SSP interrupt.
delay_us(40); //Wait for the slave to load the response
a=spi_read(0); //read the reply _clocking it_. Hence the '0'
if(a==58)
{
output_high(PIN_C3);
delay_ms(1000);
output_low(PIN_C3);
delay_ms(1000);
}
}
}
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Elysion
Joined: 03 Aug 2010 Posts: 26
|
|
Posted: Tue Feb 22, 2011 8:55 am |
|
|
Ok Thanks for your consideration. It is worked. But when I added RS232 module to these codes, it didn't work.
My slave side is same but I added 2 lines to master side:
Code: |
#include <18F14K50.H>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B7,rcv=PIN_B5)
void main()
{
int a;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
while(1)
{
spi_write(0); //send a byte to trigger the SSP interrupt.
delay_us(40); //Wait for the slave to load the response
a=spi_read(); //read the reply _clocking it_. Hence the '0'
if(a==58)
{
printf("It is worked");
output_high(PIN_C3);
delay_ms(1000);
output_low(PIN_C3);
delay_ms(1000);
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 22, 2011 1:00 pm |
|
|
Quote: | while(1)
{
spi_write(0); //send a byte to trigger the SSP interrupt.
delay_us(40); //Wait for the slave to load the response
a=spi_read(); //read the reply _clocking it_. Hence the '0'
if(a==58)
|
You took out the 0 parameter on the spi_read() function (but you left
in the comment). Why did you do that ? Now the Master won't make the
SCLK signal. Without the SCLK, it won't read the byte from the slave.
It won't work. |
|
![](templates/subSilver/images/spacer.gif) |
Elysion
Joined: 03 Aug 2010 Posts: 26
|
|
Posted: Thu Feb 24, 2011 2:18 am |
|
|
Ttelmah codes didn't work and I changed this line "a=spi_read(0); " and It is worked. But I couldn't solve RS232 problem,yet |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Thu Feb 24, 2011 4:01 am |
|
|
Had you taken the time delay out of the slave code. It won't work if that is there.
What you are doing at present, is the read, without '0', returns the 'garbage' value that was clocked when you sent the command to the master. Because the code loops, you will on the second loop, get back the value from the first command. No good if the value is changing, but will 'appear' to work, with the code just returning one value.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|