Вы находитесь на странице: 1из 3

/* program for the spi master***////////////

#include<reg51.h>
sbit spiclk = P2^0;
sbit master_out = P2^1;
sbit master_in=P2^2;
sbit slave_select =P2^3;
U8 str='a',recbyte;
void sendspimsg(unsigned char);
typedef unsigned char U8;

void main()
{
lcd_init();
message(0x80,"Spi Master");
delay(100);
while(1)
{
sendspimsg(str);
delay(3);
}
}
void sendspimsg(U8 pMsg)
{
U8 currentByte = pMsg;
U8 bitcount;
slave_select=0;
delay(2);
for(bitcount=0;bitcount<8;bitcount++)
{
spiclk = 1;
master_out = currentByte & 0x80;
delay(2);
currentByte <<= 1;
recbyte=master_in;
spiclk = 0;
delay(1);
spiclk = 1;
master_out = 1;
slave_select=1;
}
/////////////////////*********************************************//////////////
/////////////
/*************program for the spi slave******************/////////////////

#include<reg51.h>
sbit spiclk = P2^0;
sbit master_out = P2^1;
sbit master_in=P2^2;
sbit slave_select =P2^3;
void recspimsg(unsigned char);
typedef unsigned char U8;
void main()
{
lcd_init();
message(0x80,"Spi Slave");
while(1)
{
init(0xc0);
recspimsg(str);
}
}

void recspimsg(U8 pMsg)


{
U8 c=0,mask=0x80;
while(1)
{
init(0xc0);
if(slave_select==0)
{
U8 currentByte = pMsg;
if(spiclk==1)
{
if(master_out==1)
c|=mask; //store data
//write_lcd(c+0x30);
mask/=2; //shift mask
master_in = currentByte & 0x80;
currentByte <<= 1;
while (1)
{
bitcount++;
/* if(bitcount==8)
{
bitcount=0;
init(0xc0);
write_lcd(c);
}*/
if(spiclk==0)
break;
}
}
}
if(bitcount==8)
{
bitcount=0;
init(0xc0);
write_lcd(c+0x30);
}
}
}

Вам также может понравиться