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 Slave - sending data to master

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



Joined: 30 Jun 2017
Posts: 1

View user's profile Send private message

SPI Slave - sending data to master
PostPosted: Fri Jun 30, 2017 7:44 am     Reply with quote

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: 19541

View user's profile Send private message

PostPosted: Fri Jun 30, 2017 1:22 pm     Reply with quote

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: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jun 30, 2017 4:05 pm     Reply with quote

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
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