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 Master Slave code

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



Joined: 05 Jul 2018
Posts: 18

View user's profile Send private message

Help with SPI Master Slave code
PostPosted: Wed Aug 15, 2018 4:24 pm     Reply with quote

I'm trying to send data from a PIC18 MCU[Master] to another PIC18[Slave] to turn ON/OFF 2 independent LEDs. So when the SW1 pin goes high at master, it sends 0x05 and when it goes low, it send 0x07. So depending on 5 or 7 being sent, LED1 or LED2 turns ON/OFF

I could see the appropriate bits being sent on the oscilloscope. But when the slave receives this data, none of the LEDs turn ON in any case. Where am I going wrong?

The following are my codes

Master
Code:

#include <18F87K22.h>
#use delay(crystal = 8000000)
#define LED1    PIN_J6
#define LED2    PIN_J7
#define SW1     PIN_J5
#define EN      PIN_H0
#FUSES INTRC_IO                  //Internal RC Osc,
#FUSES NOPROTECT                 //No code protecte   
#FUSES MCLR                      //Master Clear pin
#FUSES NOCPD                     //No EE protection
#FUSES NOBROWNOUT                //No brownout rese
#FUSES NOIESO                    //Internal Externa
#FUSES NOFCMEN                   //Fail-safe clock
#FUSES NOWDT                                                                                                                       
#FUSES NOPUT                    //No Power Up Timer
#FUSES NODEBUG                  //No Debug mode for ICD
#include <string.h>                               
#INCLUDE <stdlib.h>

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)                   
#define SPI_MODE_2  (SPI_H_TO_L)                   
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


void main ()
{
   setup_spi(SPI_MASTER |  SPI_MODE_0 | SPI_CLK_DIV_64);

  while(TRUE)
  {
   if (input(SW1))
   {
   delay_ms(10);
   output_low(EN);
     spi_write(0x05);
    output_high(EN);
   }

  else if (!input(SW1))
   {
       
        delay_ms(10);
        output_low(EN);
       spi_write(0x07);
       output_high(EN);
   }
  }
 






Slave
Code:


#include <18F87K22.h>
#use delay(crystal = 8000000)
#define LED1    PIN_J6
#define LED2    PIN_J7
#define SW1     PIN_J5
#define EN      PIN_H0
#FUSES INTRC_IO                  //Internal RC Osc,
#FUSES NOPROTECT                 //No code protecte   
#FUSES MCLR                      //Master Clear pin
#FUSES NOCPD                     //No EE protection
#FUSES NOBROWNOUT                //No brownout rese
#FUSES NOIESO                    //Internal Externa
#FUSES NOFCMEN                   //Fail-safe clock
#FUSES NOWDT                                                                                                                       
#FUSES NOPUT                    //No Power Up Timer
#FUSES NODEBUG                  //No Debug mode for ICD
#include <string.h>                               
#INCLUDE <stdlib.h>

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)                   
#define SPI_MODE_2  (SPI_H_TO_L)                   
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

int16 value = 0;

void main ()
{
   setup_spi(SPI_Slave |  SPI_MODE_0 | SPI_CLK_DIV_64);

  while(TRUE)
  {   
    if (spi_data_is_in())
    {
      value = spi_read();

      if(value == 7)
      {
         output_low(LED2);     
         output_high(LED1);
      }
      else if (value == 5)
      {
         output_low(LED1);
         output_high(LED2);
      }
   }
  }
}
   

 
Jeetdesai



Joined: 05 Jul 2018
Posts: 18

View user's profile Send private message

Solved
PostPosted: Wed Aug 15, 2018 5:04 pm     Reply with quote

Code:

setup_spi(SPI_Slave |  SPI_MODE_0 | SPI_CLK_DIV_64);


I removed the clock setting from the above command and its working fine.

So the updated command is
Code:

setup_spi(SPI_Slave |  SPI_MODE_0);


I kept referring other posts in the forums and finally experimented by removing the clock in slave mode.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Thu Aug 16, 2018 12:39 am     Reply with quote

Well done.
It's fairly obvious if you think about it (the master controls the clock, not the slave), but it is a surprisingly common error. Seen on both SPI, and I2C....
You should have found posts on the forum from me telling people to get rid of the clock setting on the slave.

On your chip, selecting the clock to /64 sets '10', in the low two bits of the control register. 'Slave', sets bit 2 in this register. The combination with 110 in this register is actually I2C slave mode with 7bit address. No wonder it didn't work!...

Smile
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Aug 16, 2018 6:29 am     Reply with quote

NICE ! It's refreshing to see a poster figure out how and why code doesn't work as expected !
All too often, many seek a solution('give me working code') from the forum instead of reading,testing, and thinking...'hmm, what is really happening here'?
In the good ol' dayze, you'd 'play computer' and with paper and pencil, write down the actual code(machine code) and change registers/variables as a line of code was executed. Yes, that took a long time but you got real familiar as to how the computer operated.

some days I miss them good ol' dayze...

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