SPI Master / SPI Slave via PIC

Program Download:

Main C File
SPI_Master.c
SPI_Master.hex

SPI_Slave.c
SPI_Slave.hex

The Software
           This tutorial has two sets of firmware, one for the SPI master and one for the SPI slave. Below, we will take a quick look at both sets of firmware to get an idea for how to configure SPI and how data is transmit.
           First we'll look at the SPI master's firmware, and then the slave. The source code and compilex hex code is available for download in the box to the right.   =>   =>

SPI Master Code

------------« Begin Code »------------
void main(void){
unsigned x = 0xFF;
unsigned mask = 0x80;
TRISA = 0x00;
TRISC = 0x00;
PORTA = 0x03;
OpenSPI(SPI_FOSC_16, MODE_10, SMPMID);
    while(1){
        PORTAbits.RA0 = 0; //Slave Select
        putcSPI(x);
        PORTAbits.RA0 = 1; //Slave Select
        Delay10KTCYx(75);
        if(x == 0)
            x = 0xFF;
        else
            x--;
    }
}
------------« End Code »------------

           The SPI master firmware above, does 3 main things. First, it is constantly decrementing a count value stored in X. Second, it sends the slave a select signal through PORTA, RA0. Third, it sends the slave the 8 bits of data stored in X.

SPI Slave Code

------------« Begin Code »------------
void main(void){
unsigned char x;
ADCON1 = 0b00000110; //PORTA All Digital
TRISA = 0xFF;
TRISB = 0x00;
PORTA = 0x00;
PORTB = 0x00;
TRISCbits.TRISC3 = 1; //SCLK
TRISCbits.TRISC4 = 1; //MOSI
OpenSPI(SLV_SSON, MODE_10, SMPMID);
    while(1){
        while (!DataRdySPI());
        x = getcSPI();//ReadSPI();
        PORTB = (x>>1);
        Delay10KTCYx(5);
    }
}
------------« End Code »------------

           The SPI slave has two main chores. Firstly, it receives the 8 bits of SPI data and secondly, shifts that data to the right once (since there's only 7 LEDs) and displays it on PORTB (where the LEDs are). This process continues over and over again until infinity.



;