nikhil patil
nikhil patil

Reputation: 142

PIC- SPI slave select not functioning

I am Using PIC24FJ128GB202 I want to communicate with a slave device which does not need to respond. So configuration is as follows enter image description here

I have written code as

    /* 
 * File:   main.c
 * Author: Nikhil
 *
 * Created on September 18, 2015, 2:59 PM
 */

#include <stdio.h>
#include <stdlib.h>
#define FCY 16000000UL
#include <xc.h>
#include <libpic30.h>
#include<GenericTypeDefs.h>
#include <p24FJ128GB202.h>

_CONFIG4(DSWDTPS_DSWDTPS5 & DSWDTOSC_LPRC & DSBOREN_OFF & DSWDTEN_OFF & DSSWEN_OFF & PLLDIV_PLL8X & I2C1SEL_DISABLE & IOL1WAY_OFF)
_CONFIG3(WPFP_WPFP63 & SOSCSEL_OFF & WDTWIN_PS75_0  & BOREN_OFF & PLLSS_PLL_FRC & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPSTARTMEM) //& PLLSS_PLL_FRC
_CONFIG2(POSCMD_NONE & WDTCLK_LPRC & OSCIOFCN_ON  & FCKSM_CSDCMD & FNOSC_FRCPLL  & ALTRB6_RETAIN & ALTCMPI_CxINC_RX & WDTCMX_WDTCLK & IESO_OFF)//& FNOSC_FRCPLL
_CONFIG1(WDTPS_PS1024 & FWPSA_PR128 & WINDIS_OFF & FWDTEN_OFF & ICS_PGx1 & LPCFG_ON & GCP_OFF & JTAGEN_OFF)



void __attribute__((__interrupt__,__auto_psv__)) _SPI1Interrupt(void);


void SPI_write()
{
PORTAbits.RA2 =0;
    SPI1BUFL = 0xAAAA;
   }
void SPI_delay()
{
    short i;
    for(i=0;i<8;i++)
    ;


}
void SPI_config(){
    SPI1CON1Lbits.SPIEN = 0 ;
    __builtin_write_OSCCONL(OSCCON & 0xbf);         // Unlock PPS
    RPOR1bits.RP2R = 7;                             //SPI SDO1
    RPOR1bits.RP3R = 8;                             //SPI SCK1
    SPI1STATLbits.SPIROV =0;                        //OVERflow Flag reset
    SPI1CON1Lbits.MSTEN = 1;                        //SPI Master
    SPI1CON1Lbits.MODE = 1;                         //16- bit Data transfer
    SPI1CON1Lbits.CKE = 0;
    SPI1CON1Lbits.CKP =0;   
    __builtin_write_OSCCONL(OSCCON | 0x40);      // Lock PPS
    SPI1CON1Lbits.SPIEN = 1 ;
}

static inline void
init_io(void)
{
        /* Digital Mode */
        ANSA = 0;
        ANSB = 0;

        /* Reset O/P */
        LATB = 0;
        LATA =0 ;

    /* O/D Off */
    ODCA = 0;
    ODCB = 0;

       /* Define Outpot port */
        TRISB = 0 ;
        TRISA =0 ;
}


int main (void)

{
    CLKDIVbits.CPDIV = 0x01;
    init_io();
    SPI_config();
    //////////////////////////////// SPI Interrupt ////////////////////////////////
    _SPI1IP = 1;
    _SPI1IF =  0;
    SPI1IMSKLbits.SRMTEN = 0x01;    
    _SPI1IE =1;


//////////////////////////////// main code //////////////////////////////////////////


  while(1)
    {
      SPI_write();
      __delay_ms(1);
    }
   return 0;

}





void __attribute__((__interrupt__, __auto_psv__)) _SPI1Interrupt(void)
{
    // Clear SPI1 1 interrupt flag
    _SPI1IF = 0;

     PORTAbits.RA2 =1;




}

I am receiving clock and data properly. But the port A (slave select) goes high while SPI transfer.

enter image description hereI have tried millions of combinations of delays

I have even tried SPIwrite as:

void SPI_write()
{
PORTAbits.RA2 =0;
short temp,temp1; 

    SPI1BUFL = 0xAAAA;
    __delay_ms(1);
    temp = SPI1BUFL;
    temp1 =SPI1BUFH;
    while(!SPI1STATLbits.SPIRBF);
   }

and also tried to enable disable pin before and after writing
Like

PORTAbits.RA2 =0;
SPI_write();
PORTAbits.RA2 =1;

Still same error

Please please please!!! help I am getting frustrated with this error

Upvotes: 1

Views: 1526

Answers (1)

Ken 99
Ken 99

Reputation: 19

I think I may have the same issue, but I was wanting the Slave Select to assert while the transfer is going on and then de-assert between transfers. However, like you I could not get it to de-assert. I finally figured out a way to have it de-assert between transfers. I poll the SS pin to see when it starts to de-assert. This means that that internal ssp state machine has moved to that "done" state, does not see a way to pipeline multiple transfers, and will now de-assert the SS pin.

Since I am always going to be signal work transfers and I will never do large transfers, I will be moving this while statement inside of the SPI1_Exchange16() call. The SPI1_Excahnge16 call is a modified version of the SPI1_Exchange() call, which does a 16 bit transfers instead of 8.

Below is my example code and I attached a trace showing the impact of the while statement on the SPI bus. You can see the first two transfers have SS de-asserting between transfers.Affect of waiting for SS1 to de-assert

SPI1_Exchange16( &txData16[0], &rxData16[0] );
while (SS1_GetValue()==0){};
SPI1_Exchange16( &txData16[1], &rxData16[1] );
while (SS1_GetValue()==0){};
SPI1_Exchange16( &txData16[1], &rxData16[1] );  
// NO WHILE STATEMENT
SPI1_Exchange16( &txData16[1], &rxData16[1] );
// NO WHILE STATEMENT
SPI1_Exchange16( &txData16[1], &rxData16[1] );
// NO WHILE STATEMENT

Upvotes: 2

Related Questions