|
|
View previous topic :: View next topic |
Author |
Message |
Pascal
Joined: 30 Jun 2017 Posts: 1
|
SPI Slave - sending data to master |
Posted: Fri Jun 30, 2017 7:44 am |
|
|
I want to communicate with a FTDI (master) to a Pic18f252 (Slave) via SPI. I'm using MPlab IDE v8.92 with ccs compiler.
I want to write data from the master to an array of the slave and then read the data again.
The master sends 3 Bytes --> chipselect low--> 1. write instruction, 2. address ,3. -->data chipselect high
Then the master sends another 3 Bytes -->chipselect low--> 1.read instruction, 2.address, dummy Byte (to get the data of the slave(array)).-->data chipselect high
The problem is that the slave only sends data if the master do another chipselect before the dummy Byte. But i want to send all 3 Bytes in one chipselect.
Here the code of the Slave:
Code: |
int8 memory[80], data, instr, instr_tmp, address_spi; int8 cnt = 0 ;
int8 speicher;
void write_data(void)
{
//while(!spi_data_is_in());
data = spi_read();
if(address_spi >= 0 || address_spi <= 80)
{
memory[address_spi] = data;
}
}
#INT_SSP
void spi_isr(void)
{
cnt=cnt+1;
if(cnt==1)
{
instr = spi_read();
}
if(cnt==2)
{
address_spi = spi_read();
if(instr==0x18)
{
SSPBUF = memory[address_spi];
}
if(cnt==3)
{
cnt=0;
if(instr==0x0A)
{
write_data();
cnt=0;
}
}
}
void main()
{
//Spi
setup_spi(SPI_SLAVE |SPI_MODE_0 | spi_ss_disabled);
set_tris_c(0xDF); // SPI --> SDO(RC5) Output, 0b1101 1111
clear_interrupt(INT_SSP);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
SSPIF = false;
while(true)
{
}
}
|
if i debug, a register says that the sspbuf register colide while writing the array data (memory) to it in the interrupt. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Fri Jun 30, 2017 1:22 pm |
|
|
Whats the timing between the address byte, and reading?.
Problem is that you will only get into the interrupt about 30 instruction times after the address byte is sent. The code needs to get here, have time to read the byte and load the reply (at least another dozen instructions), before the master can start clocking back the data. You'll get a collision, if the master has already started clocking out the dummy byte, before you try to load the buffer....
So there needs to be a little pause (say 50 instruction times on the PIC), between sending the address, and sending the dummy. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Jun 30, 2017 4:05 pm |
|
|
hmm. I was curious so I looked in the manual. Though I've never used it, CCS has a function called
spi_xfer_in()
that may actually do what you want, receive 3 bytes of data.
The info in the manual is kinda sketchy, but I'd try it....
Jay |
|
|
|
|
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
|