EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: nikoso40@yahoo.com on October 06, 2022, 09:27:39 am

Title: SPI PIC to PIC communication
Post by: nikoso40@yahoo.com on October 06, 2022, 09:27:39 am
Hi all
 I am trying first time SPI between PIC 16F18446 and 16F1829. The project is not running at all. I assume that the problem i have is on the pin configurations (PPS) on master PIC 16F18446. I am posting both codes. If someone can enlightens me will appreciated.

CODE FOR MASTER pic16f18446

Code: [Select]
#define _XTAL_FREQ 32000000
#define CS PORTCbits.RC0

/* i.e. uint8_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
void SPI_Init();
void SPI_Open();
void SPI_Close();
uint8_t SPI_SendData(uint8_t data);

void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
    OSCCON1 = 0x10; // HFINTIPLL is on (and 2xPLL)
    OSCCON3 = 0x00;
    OSCEN   = 0x00;
    OSCFRQ  = 0x05; // FOSC 16 MHz + 2xPLL TOTAL FOSC 32MHz
    OSCTUNE = 0x00;

    /* Initialize I/O and Peripherals for application */
    InitApp();
    SPI_Init();


    while(1)
    {
        SPI_Open();
        __delay_ms(10);
        SPI_SendData(0X01);
        __delay_ms(100);
        SPI_SendData(0X00);
        __delay_ms(100);
        SPI_Close();
        __delay_ms(200);
    }
}

void SPI_Init(){
    //Setup PPS pin
    SSP1CLKPPS = 0x16;   //CLK1 on RC6
    SSP1DATPPS = 0x14;   //SDI1 on RC4
    RC6PPS     = 0x13;   //RC6 set as output CLK1
    RC5PPS     = 0x14;   //RC5 as output pin SDO1
   
    //SPI setup
    SSP1STAT = 0x40;
    SSP1CON1 = 0x00;
    TRISCbits.TRISC6 = 0;
    SSP1CON1bits.SSPEN = 0;
}

void SPI_Open(){
    SSP1CON1bits.SSPEN = 1;
    CS = 0;
}

void SPI_Close(){
    SSP1CON1bits.SSPEN = 0;
}

uint8_t SPI_SendData(uint8_t data)
{
CS = 0;
__delay_ms(10);
    unsigned char dummy_data;
    SSP1BUF = data;     /* Copy data in SSBUF to transmit */
    while(!PIR3bits.SSP1IF); /* Wait for complete 1 byte transmitted */
    PIR3bits.SSP1IF=0;     /* Clear SSPIF flag */
    dummy_data=SSP1BUF;     /* Flush the data if 2 way communication */
    CS = 1;
    __delay_ms(10);
}


CODE FOR SLAVE pic16f1829

Code: [Select]
#define _XTAL_FREQ      4000000
#define LED PORTBbits.RB5

/* i.e. uint8_t <variable_name>; */
short x;

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
void SPI_Init_Slave();
unsigned char SPI_Read();

void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
    OSCCONbits.IRCF = 0b1110;        //FOSC @8MHz,4xPLL is on Final FOSC 32MHz
    OSCCONbits.SCS = 0x00;

    /* Initialize I/O and Peripherals for application */
    InitApp();
    SPI_Init_Slave(); /* Initialize SPI communication as a slave */


    while(1)
    {
        x = SPI_Read();
        if(x == 0x01) LED = 1;
        if(x == 0x00) LED = 0;
 
    }
}

void SPI_Init_Slave()
{
    /* PORT definition for SPI pins*/   
    TRISBbits.TRISB4 = 1; /* RB4 as input(SDI1) */
    TRISBbits.TRISB6 = 1; /* RB6 as output(SCK1) */
    TRISCbits.TRISC6 = 1; /* RC6 as a output(SS1) */
    TRISCbits.TRISC7 = 0; /* RC7 as output(SDO1) */
    TRISBbits.TRISB5 = 0;   /* RB5 as LED output */
 
    ANSELB = 0x00;
    ANSELC = 0x00;
    RB5 = 0;

    /* To initialize SPI Communication configure following Register*/
   
    SSP1STATbits.CKE = 0;
    SSP1STATbits.SMP = 0;
    SSP1CON1bits.CKP = 0;
    SSP1CON1bits.SSPEN = 1;
    SSP1CON1bits.SSPM = 0x04;
   
    PIE1bits.SSP1IE = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
}

unsigned char SPI_Read()
{   
    uint8_t Data;
    if(SSP1STATbits.BF) // Check If Any New Data Is Received
    {
      Data = SSP1BUF; // Read The Buffer
      SSP1STATbits.BF = 0; // Clear The Buffer-Filled Indicator Bit
      PIR1bits.SSP1IF=0; // Clear The Interrupt Flag Bit
      SSP1CON1bits.SSPOV = 0; // Clear The Overflow Indicator Bit
      return (Data);
    }
}

Thanks
Title: Re: SPI PIC to PIC communication
Post by: freda on October 06, 2022, 09:47:47 am
make sure you explicitly disable the comparator modes, your not using them.
Title: Re: SPI PIC to PIC communication
Post by: nikoso40@yahoo.com on October 06, 2022, 09:49:32 am
 I made a mistake on the posting codes and i have now corrected. So pic 16f18446 is the master and pic 16f1829 is the slave. Sorry about that.
regards
Title: Re: SPI PIC to PIC communication
Post by: odessa on October 06, 2022, 11:07:01 am
What do you mean not running at all ?

You are writing CS on your master and LED on your slave to the PORT register, try changing it to the LAT registers.
Title: Re: SPI PIC to PIC communication
Post by: MikeK on October 06, 2022, 11:42:52 am
Your slave code disabled analog functions (ANSEL), but your master code does not.