CODE FOR MASTER
/*
This program is to demonstrate the concept of SPI communication in which the master-slave configuration is setup by microcontroller-microcontroller interface.
The code below(spi_config_master) is the configuration settings of the master.
Also the the transmit protocol(spi_master_tx) is defined to send data to the slave.
The connections are straight forward i.e.
SS of master-->SS of slave
MOSI of master-->MOSI of slave
MISO of master-->MISO of slave
clk from master-->clk of slave
*/
/*
All include ../files
*/
#include<p89v51rd2.h>
#include<LCD.h>
#include<DELAY.h>
#define ss_pin P1_4 //SS(chip select) pin of the masetr
//function to setup the master configuration for the SPI communication
void spi_config_master()
{
ss_pin=0; //make the SS(chip select) pin logic low because the SS pin of the slave is active low
SPCR=0X50; //SPI enable bit=1; MSTR bit=1 (to configure it as master) and frequency for SPI comm. = (F)osc/4
SPSR=0X00; //disable the SPI interrupt
SPDAT=0X00; //initialize the SPI data register to 0X00
}
//function for the master to transmit data to slave
void spi_master_tx(unsigned char data_given_to_slave)
{
unsigned char temp; //temporary variable to store the data to be given to slave
temp=data_given_to_slave; //store the data to be passed to the slave
SPDAT=temp; //pass the value in the data register of the SPI comm i.e.SPDAT
while(!(SPSR&0X80)); //wait while the transmission is complete ( the SPIF bit in the SPSR register goes high when the transmission is complete)
SPSR=0X00; //clear the SPIF bit
}
//main function
void main()
{
//initialize the port values
P0=0XFF;
P1=0xFF;
P3=0XFF;
P2=0XFF;
spi_config_master(); //setup the master configuration
//infinite loop to continuously send/transmit data to slave
while(1)
{
spi_master_tx('8'); //send the character(here the number 8 is transmitted)
DELAY(5); //random delay so that the processes on the slave side are completed and hence the slave data buffer (SPDAT of slave) is not filled with the garbage values
} //end of while loop
} //end of main() function
CODE FOR SLAVE
/*
This program is to demonstrate the concept of SPI communication in which the master-slave configuration is setup by microcontroller-microcontroller interface.
The code below(spi_config_slave) is the configuration settings of the slave.
The connections are straight forward i.e.
SS of master-->SS of slave
MOSI of master-->MOSI of slave
MISO of master-->MISO of slave
clk from master-->clk of slave
*/
/*
All include ../files
*/
#include<p89v51rd2.h>
#include<LCD.h>
#include<DELAY.h>
unsigned char data_from_master;
//function to initialize the UART for communication with the PC
void uart_init()
{
TMOD=0x20; //configure Timer/Counter1 in mode 2 (functioning as timer) for setting the correct baud rate required for communication with the PC
TH1=0XFD; //setup 9600bps for the (F)osc=11.0592MHz
TCON=0x40; //start timer1 by setting TR1 flag as 1 (this can also be done by writing TR=1)
SCON=0X50; //set the serial IO the receive in normal mode
RI=0; //receive interrupt flag is cleared
TI=0; //transmit interrupt flag is cleared
}
//function to receive the message(character) sent from the PC
unsigned char receive_data()
{
unsigned char received_data; //variable to store the received data
received_data=SBUF; //store the received data into the variable
return received_data; //return the data(variable value) to the point of call
}
//function to transmit the message(character) to the PC
void transmit_data(unsigned char tx_data)
{
SBUF=tx_data; //load the data to be transmitted into the SBUF register so that it transmits
while(TI==0); //wait while the data is being transmitted
}
//function to setup the slave configuration for the SPI communication
void spi_config_slave()
{
SPCR=0X40; //SPI enable bit=1; MSTR bit=0 (to configure it as slave) and frequency for SPI comm. = (F)osc/4
SPSR=0X00; //disable the SPI interrupt
SPDAT=0X00; //initialize the SPI data register to 0X00
}
//main function
void main()
{
//initialize the port values
P1=0xFF;
P3=0XFF;
P2=0XFF;
uart_init(); //initialize the UART configuration
spi_config_slave(); //setup the master configuration
//infinite loop to continuously read/receive data from master
while(1)
{
while(!(SPSR&0X80)); //wait while the transmission is complete ( the SPIF bit in the SPSR register goes high when the transmission is complete)
data_from_master=SPDAT; //store the data received/read from the master into the variable
transmit_data(data_from_master); //transmit the data receive d to the UART channel to be read on the PC terminal
SPSR=0X00; //clear the SPIF bit
} //end of the while loop
} //end of the main() function