Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SPI communication between two PICs

Status
Not open for further replies.

onlyroshan

Newbie level 1
Joined
Jun 26, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,319
Hi!
I am trying to transfer data from master to slave and from slave to master at the same time. I have tried to use SPI for the communication. I cannot go for USART or I2C because I have to use those pins for other configuration. So, I am left only with SPI. I have used MPLab and C18 compiler. I have posted the code below. In the program I have tried to send character 'B' from master to slave and character 'Z' from slave to master. When I run the program, I can see 'B' is sent from the master and received by the slave. But character 'Z' is lost somewhere. I guess, I am not good in configuration and communication algorithm. I have used PIC-18F4550. So, please help me out!! Thank you in advance!!

Master code
#include <htc.h>
#define _XTAL_FREQ 48000000

//config word(s)
#pragma config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2 //20MHz XTAL/ '5' -> 96MHz PLL/' 2'= 48MHz CPU + USB
#pragma config IESO = OFF, FOSC = HSPLL_HS, FCMEN = OFF //Internal Osc off, HS Xtal, failsafe clock monitoring on
#pragma config VREGEN = ON, BOR = OFF, BORV = 0, PWRT = ON //usb vreg on, BORDIS, PWRTEN
#pragma config WDTPS = 32768, WDT = OFF //WDTDIS
#pragma config CCP2MX = ON, PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON //CCP2:RC0, PortB Digital on reset, timer1 highpower, MCLREN
#pragma config DEBUG = OFF, STVREN = ON, XINST = OFF, LVP = OFF, ICPRT = OFF //debug mode off, stack error reset on, xtended instructions off, LVPDIS
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF //CODE PROTECT OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

#define SS RC5 //this pin when low selects the slave
char a;

void SPI_enable()
{
//SSPSTAT=0xC0;
SMP=1; //Input data sampled at the end of data o/p time
CKE=1; //Transmission at transition from active to ideal

//SSPCON1=0x20;
WCOL=0; //No collision
CKP=0; // Ideal state is low level
SSPEN=1; //Enable serial port
SSPM3=SSPM2=SSPM1=SSPM0=0; // Master Mode, Fosc/4 clock for data transfer
}

void SPI_read()
{
SS=0; //select slave
SSPIF=0;
SSPBUF='B';
while(!BF);
while(!SSPIF);
a=SSPBUF;
SSPIF=0;
SS=1;
}

int main()
{
//Port initialization
TRISA=0x00; //RA5 as output
TRISB=0b11111101; //making RB1 as output and other as input
TRISC=0x00; //making all pins as output

//Configuration of interrupt

SPI_enable(); // enables SPI module
SPI_read();
while(1)
{
SPI_read();
}
}

Slave Code
#include <htc.h>

#define _XTAL_FREQ 48000000

//config word(s)
#pragma config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2 //20MHz XTAL/ '5' -> 96MHz PLL/' 2'= 48MHz CPU + USB
#pragma config IESO = OFF, FOSC = HSPLL_HS, FCMEN = OFF //Internal Osc off, HS Xtal, failsafe clock monitoring on
#pragma config VREGEN = ON, BOR = OFF, BORV = 0, PWRT = ON //usb vreg on, BORDIS, PWRTEN
#pragma config WDTPS = 32768, WDT = OFF //WDTDIS
#pragma config CCP2MX = ON, PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON //CCP2:RC0, PortB Digital on reset, timer1 highpower, MCLREN
#pragma config DEBUG = OFF, STVREN = ON, XINST = OFF, LVP = OFF, ICPRT = OFF //debug mode off, stack error reset on, xtended instructions off, LVPDIS
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF //CODE PROTECT OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

char a;
void SPI_enable()
{
//SSPSTAT=0xC0;
SMP=0; //must be cleared for slave mode
CKE=1; //Transmission at transition from active to ideal


//SSPCON1=0x20;
WCOL=0; //No collision
CKP=0; // Ideal state is low level
SSPEN=1; //Slave mode
SSPM3=0;SSPM2=1;SSPM1=0;SSPM0=0; //SPI Slave mode, SCK clock, SS control bit enabled
}

void SPI_write()
{

//.......TRANSFER DATA.......
SSPIF=0;
SSPBUF='Z';
while(!BF); //Transfer complete
while(!SSPIF);
a=SSPBUF;
SSPIF=0;
}

void main()
{
TRISA=0xFF; //Port A as input
TRISB=0xFF; //Port B as input
TRISC=0x00; //Port C as output
SPI_enable(); //Enable SPI in slave mode
SPI_write();
while(1)
{
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top