Hi,
I am trying to get some communication going using SPI between two PIC16f877a's and am failing miserably. Any alternative code to solve this problem using 877a or suggestions would be greatly appreciated. The basic problem is the slave is just not receiving anything from the master!
I am trying to get some communication going using SPI between two PIC16f877a's and am failing miserably. Any alternative code to solve this problem using 877a or suggestions would be greatly appreciated. The basic problem is the slave is just not receiving anything from the master!
Rich (BB code):
//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
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
//printf("\r p3 ");
printf("\r d [%c] ",data);
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
Last edited by a moderator: