主机程序:
#include <STRING.H>
#include<REG52.H>
#define _MHZ_ 11
sbit
LED1=P0^0;
sbit
LED2=P0^1;
sbit
P0_2=P0^2;
sbit
P0_3=P0^3;
sbit
K1=P0^4;
sbit
K2=P0^5;
sbit
P0_6=P0^6;
sbit
P0_7=P0^7;
sbit
LED3=P1^0;
sbit
LED4=P1^1;
sbit
LED5=P1^2;
sbit
P1_3=P1^3;
sbit
P1_4=P1^4;
sbit
P1_5=P1^5;
sbit
K3=P1^6;
sbit
K4=P1^7;
sbit
P2_0=P2^0;
sbit
P2_1=P2^1;
sbit
P2_2=P2^2;
sbit
P2_3=P2^3;
sbit
P2_4=P2^4;
sbit
P2_5=P2^5;
sbit
P2_6=P2^6;
sbit
K5=P2^7;
int a=0;
unsigned char data1[6];
unsigned char data2[6];
void init_serial0();
void init_serial1();
void coll_data() ;
void send_data(unsigned char *data1);
void recv_data(unsigned char *data2);
void delay10ms(unsigned int count);
unsigned char ecc=0x00;
/*************** 延时程序*********** */
void delay10ms(unsigned int count)
{
unsigned int i;
unsigned char j;
for(i=0; i<count; i++)
for(j=0; j<200; j++);
//for(k=0; k<150; k++);
}
/******************采集数据**********************/
void coll_data()
{
data1[0]=0xC5;
data1[1]=0xFF;
data1[2]=0xF6;
data1[3]=0xC8;
/***************** ******************/
/*****************?1******************/
if(K1==0)
{
delay10ms(1);
if(K1==0)
{
data1[1]=data1[1]&0x7F;
}
}
if(K2==0)
{
delay10ms(1);
if(K2==0)
{
data1[1]=data1[1]&0xBF;
}
}
if(K3==0)
{
delay10ms(1);
if(K3==0)
{
data1[1]=data1[1]&0xDF;
}
}
if(K4==0)
{
delay10ms(1);
if(K4==0)
{
data1[1]=data1[1]&0xEF;
}
}
if(K5==0)
{
delay10ms(1);
if(K5==0)
{
data1[1]=data1[1]&0xF7;
}
}
data1[4]=0xC5+ data1[1]+ 0xF6+0xC8;//D£?é×??ú£?oí×??ˉò?3?è?×?μí8??
}
/****************** ·发送数据 **********************/
void send_data(unsigned char *data1)
{
int i;
/* ·¢?íêy?Y */
for(i=0; i<5; i++)
{
TI = 0;
SBUF = data1[i];
while(!TI);
TI = 0;
}
}
/****************串口初始化********************/
void init_serial1()
{
TMOD = 0x20;
TH1 = 250;
TL1 = 250;
TR1 = 1;
PCON = 0x80;
SCON = 0x50;
}
/* *****************?¨ê±?÷0 **********************/
void init_serial0()
{
TMOD = 0x10;
TH0 = 0xB8;
TL0 = 0x00;
TR0 = 1;
ET0=1;
EA=1;
}
/*************** 定时器0中断*********** */
void timer0_int () interrupt 1 using 1
{
TH0 = 0xB8;
TL0 = 0x00;
send_data(data1);
coll_data();
}
/* 接收数据*/
void recv_data(unsigned char *data2)
{
int i;
/* ?óê?êy?Y */
for(i=0; i<5; i++)
{
while(!RI);
data2[i] = SBUF;
RI = 0;
}
ecc=(data2[0])+(data2[1])+(data2[2])+(data2[3]);
data2[5]=0;
if((data2[0]==0xA5)&&(data2[4]==ecc))
{
if((data2[1]&0x80)==0x80)
LED1=1;
else
LED1=0;
if((data2[1]&0x40)==0x40)
LED2=1;
else
LED2=0;
if((data2[1]&0x20)==0x20)
LED3=1;
else
LED3=0;
if((data2[1]&0x10)==0x10)
LED4=1;
else
LED4=0;
if((data2[1]&0x08)==0x08)
LED5=1;
else
LED5=0;
}
}
/* **********************主程序******************** */
void main(void)
{
init_serial1(); //串口初始化
init_serial0(); //启动定时器0
TI=0;
RI=0;
IE=0x92;
while(1)
{
recv_data(data2); //接收程序
}
}
//////串口中断///////////
void Serial_INT() interrupt 4
{
int i;
/* ?óê?êy?Y */
for(i=0; i<5; i++)
{
RI = 0;
while(!RI);
data2[i] = SBUF;
RI = 0;
}
ecc=(data2[0])+(data2[1])+(data2[2])+(data2[3]);
data2[5]=0;
// ±íê?êy?Y?áê?
/* 如果接收到程序,使灯亮 */
//if((data2[0]==0xA5)&&(data2[4]==ecc))
{
if((data2[1]&0x80)==0x80)
LED1=1;
else
LED1=0;
if((data2[1]&0x40)==0x40)
LED2=1;
else
LED2=0;
if((data2[1]&0x20)==0x20)
LED3=1;
else
LED3=0;
if((data2[1]&0x10)==0x10)
LED4=1;
else
LED4=0;
if((data2[1]&0x08)==0x08)
LED5=1;
else
LED5=0;
}
} |