pic16f877a spi master slave help needed

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
n_needs_help
Posts: 1
Joined: Wed Jul 02, 2014 8:02 pm

pic16f877a spi master slave help needed

Post by n_needs_help » Wed Jul 02, 2014 8:11 pm

I am having trouble with this code, I am not seeing anything on the slave, it appears to be looking up while waiting for the BF bit to set before transferring the data out of the SPI buffer. Any help would be appreciated

Code: Select all

//master code 
 #include <htc.h>
 #include <stdio.h>
#include "usart.h"
__CONFIG(0x3f3A);
//Defintions.h
#define SS RA5
#define SCK RC3
#define SDI RC4
#define SDO RC5
#define FALSE 0
#define TRUE 1
#define RTS RA0
#define CTS RA1

int i,j;
void main (void)
{
//initialisations and declaration of variables
unsigned char testCounter = 0, dummy;
	INTCON=0;	// purpose of disabling the interrupts.

	init_comms();
printf("\r this is a test");
//initialisations of port pins
for (i=1;i<250;i++);//1ms delay

PCFG3 = 0; // Configure PORTA digital I/O
PCFG2 = 1;
PCFG1 = 1;
TRISC3 = 0; // SCK=RC3 is the serial clock
TRISC4 = 1; // SDI=RC4 is serial data input
TRISC5 = 0; // SDO=RC5 is serial data output
TRISA0 = 0; // RA0 = RTS, an output
TRISA1 = 1; // RA1 = CTS, an input
TRISA5 = 0; // SS=RA5 is slave select output
INTCON = 0; // Disable

//initialisation of timer SPI

SMP = 0; // Input data sampled at middle data output time
CKE = 0; // Transmit occurs on idle to active clock state
CKP = 1; // Idle state for clock is a high level
SSPM3 = 0; //
SSPM2 = 0; //
SSPM1 = 0; //
SSPM0 = 0; // SPI Master mode, clock = FOSC/4
SSPEN = 1; // Enables serial port
SS = 1; // De-select Slave (pin 7)
SSPIF = 0;

//superloop program
RTS = 1; //Master signals "I'm
//ready!"
while(CTS == 0){} //Wait for Slave
//ready
while(1){
SS = 0; // Select Slave (Active low)
// Transmit data:
SSPBUF = testCounter;
//Wait for transmission complete:
while(BF==FALSE) {}
dummy = SSPBUF; // Makes BF=FALSE
dummy = SSPBUF; // Makes BF=FALSE
printf("\r p2 ");
SS = 1; // De-select Slave
testCounter++;
for (j=1;j<2000;j++){

for (i=1;i<250;i++);//1ms delay
}
}
}

//-------------------------------------------------
//slave code 
 #include <htc.h>
 #include <stdio.h>
#include "usart.h"
__CONFIG(0x3f3A);

#define SS RA5
#define SCK RC3
#define SDI RC4
#define SDO RC5
#define FALSE 0
#define TRUE 1
#define RTS RA0
#define CTS RA1
int i,expectedValue;
void main (void)
{
	INTCON=0;	// purpose of disabling the interrupts.

	init_comms();
//initialisations and declarations of variables
unsigned char testCounter = 0, data;
unsigned char errorCount = 0;

//initialisations of port pins
printf("\r this is a test");
for (i=1;i<250;i++);

PCFG3 = 0; // Configure PORTA digital I/O
PCFG2 = 1;
PCFG1 = 1;
TRISC3 = 1; // SCK = RC3 clock (inputforslave)
TRISC4 = 1; // SDI = RC4 serial data input
TRISC5 = 0; // SDO = RC5 serial data output
TRISA0 = 0; // RA0 = RTS, an output
TRISA1 = 1; // RA1 = CTS, an input
TRISA5 = 1; // SS = RA5 slave select input
// Extra for displaying a LED
TRISA2=0;
TRISA3=0;
INTCON = 0; // Disable all interrupts
SSPIF = 0;

//initialisation of timer SPI
SMP = 0; // Input data sampled at middle data output time
CKE = 0; // Transmit occurs on idle to active clock state
CKP = 1; // Idle state for clock is a high level
SSPM3 = 0; //
SSPM2 = 1; //
SSPM1 = 0; //
SSPM0 = 0; // SPI Slave mode, SS pin control enabled
SSPEN = 1; // Enables serial port
SSPIF = 0;
expectedValue = errorCount = 0; // For testing and debugging
RA2=0;RA3=0;
//superloop program
RTS = 1; //Slave signals "I'm
//ready!"
while(CTS == 0){} //Wait for Master ready

while(1){
// Wait for Master to select Slave:
while(SS == 1){}
// Wait for SSPBUF to be full
//printf("\r p2 ");
//(Signifies reception complete):
while(BF==FALSE){}
data = SSPBUF; // Get data byte
data = SSPBUF; // Get data byte
//printf("\r p3 ");
printf("\r P3 ");
RA2=1;
// Wait for Master to de-select Slave while(SS == 0){}
if(data != expectedValue)
{
errorCount++;
}

printf("\r d [%c] ",data);
expectedValue++;

}

}//end of main





waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: pic16f877a spi master slave help needed

Post by waltr » Fri Jul 04, 2014 2:45 pm

You really need to use an O'scope or a logic analyzer to first determine if the 'master' is sending the correct code, timing, etc.

If you have the PICKit2 then you can use the logic analyzer feature of the PK2 software if you slow the SPI clock way down. This is what I use to figure out how to get the PIC's SPI working.

Post Reply