SPI PIC to PIC communication - Page 1
Forum section: electronics / beginners

author: nikoso40@yahoo.com
author_rank: Newbie
timestamp: October 06, 2022, 09:27:39 am
content: Hi allI 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 pic16f18446Code:#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 32MHzOSCTUNE = 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 pinSSP1CLKPPS = 0x16; //CLK1 on RC6SSP1DATPPS = 0x14; //SDI1 on RC4RC6PPS = 0x13; //RC6 set as output CLK1RC5PPS = 0x14; //RC5 as output pin SDO1//SPI setupSSP1STAT = 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 pic16f1829Code:#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 32MHzOSCCONbits.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 BufferSSP1STATbits.BF = 0; // Clear The Buffer-Filled Indicator BitPIR1bits.SSP1IF=0; // Clear The Interrupt Flag BitSSP1CON1bits.SSPOV = 0; // Clear The Overflow Indicator Bitreturn (Data);}}Thanks

author: freda
author_rank: Regular Contributor
timestamp: October 06, 2022, 09:47:47 am
content: make sure you explicitly disable the comparator modes, your not using them.

author: nikoso40@yahoo.com
author_rank: Newbie
timestamp: October 06, 2022, 09:49:32 am
content: 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

author: odessa
author_rank: Regular Contributor
timestamp: October 06, 2022, 11:07:01 am
content: 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.

author: MikeK
author_rank: Super Contributor
timestamp: October 06, 2022, 11:42:52 am
content: Your slave code disabled analog functions (ANSEL), but your master code does not.