Monday, 19 October 2015

PIC16F877A SPI TRANSFER A STRING





//*********************************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 string(unsigned char *p)
{
while(*p!='\0')
{
delay(500);
SSPBUF=*p;
while(SSPIF==0);
p++;
}
}



void main()
{
TRISC=0x00;

SSPSTAT=0X00;         //in master mode this is zero
SSPCON=0X20;          //for enable serial port
delay(500);
string ("EMBEDDED LAB");
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 delay(unsigned int i)
{
while(i--);
}

void cmd(unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;


PORTD=h|0X40;
delay(20);
PORTD=h|0X00;

PORTD=l|0X40;
delay(20);
PORTD=l|0X00;
}


void dat (unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;

PORTD=h|0X50;
delay(20);
PORTD=h|0X10;

PORTD=l|0X50;
delay(20);
PORTD=l|0X10;
}


void lcd_init()
{
cmd(0x02);
delay(100);
cmd(0x28);
delay(100);
cmd(0x01);
delay(100);
cmd(0x80);
delay(100);
cmd(0x0e);
delay(100);
}


void string(unsigned char *p)
{
while(*p!='\0')
{
dat(*p);
p++;
}
}


void num(unsigned int p)
{
unsigned int k,b=1,w;
k=p;
while(k>9)
{
b=b*10;
k=k/10;
}
while(b>=1)
{
w=p/b;
p=p%b;
b=b/10;
dat(w+48);
}
}


void main()
{
TRISD=0X00;
unsigned int x;
lcd_init();
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;      //data is show on PORTD
dat(x);
//BF=0;              //buffer pin is output pin
SSPIF=0;
}
}
//***********************************************************************//

No comments:

Post a Comment