Monday, 19 October 2015

PIC16F877A SPI SINGLE MASTER MULTI SLAVE





//************************************MASTER*********************************//
#include<pic.h>
#include<htc.h>
#define _PIC16F877A_H
#define sck TRISCbits.TRISC3
#define sdi TRISCbits.TRISC4
#define sdo TRISCbits.TRISC5
__CONFIG(PWRTE_ON & FOSC_HS & LVP_OFF & WDTE_OFF);

void delay(unsigned int i)
{
while(i--);
}

void send(unsigned char dat,unsigned char ss)
{
PORTD=ss;
SSPBUF=dat;          //data register
while(SSPIF==0);      //till the bit is not send
}

void main()
{
TRISD=0X00;
sck=0;                //clk pin is output pin          
sdo=0;                //data out for spi
SSPSTAT=0X00;         //in master mode this is zero
SSPCON=0X20;          //for enable serial port
    send(0x01,0x06);
delay(100);
send(0x02,0x05);
delay(100);
send(0x04,0x03);
delay(100);
while(1);
}
//**************************************************************//





//*****************************SLAVE****************************//
#include<pic.h>
#include<htc.h>
#define _PIC16F877A_H
#define sck TRISCbits.TRISC3
#define sdi TRISCbits.TRISC4
#define sdo TRISCbits.TRISC5
__CONFIG(PWRTE_ON & FOSC_HS & LVP_OFF & WDTE_OFF);




void main()
{
unsigned char x;
TRISD=0X00;
SSPSTAT=0X00;       //in slave mode this is zero
SSPCON=0X24;        //for enable  //SPI Slave mode, clock = SCK pin. SS pin control enabled.
while(1)
{
while(SSPIF==0);
x=SSPBUF;
PORTD=x;
BF=0;
}
}
//********************************************************************//

No comments:

Post a Comment