初级会员
- 积分
- 53
- 金钱
- 53
- 注册时间
- 2015-3-22
- 在线时间
- 0 小时
|
5金钱
两单片机通信,STC15F2k60s2作为主机,STC80C52RC作为从机,操作主机,从机数据传输不对应,求解
主机程序
#include <stc15.h> //15系列单片机头文件
#define uchar unsigned char
#define uint unsigned int
sbit buzzer=P4^1;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--) //i=xms即延时约xms毫秒
for(j=110;j>0;j--);
}
/************蜂鸣器*****************/
void di()
{
buzzer=0;
delayms(60);
buzzer=1;
}
void send(uchar key_num)
{
// di();
SBUF=key_num;
while(!TI);
TI=0;
}
void matrixkeyscan()
{
uchar temp,key;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:
key=1;
break;
case 0xde:
key=2;
break;
case 0xbe:
key=3;
break;
case 0x7e:
key=4;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
// di();
send(key);
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:
key=5;
break;
case 0xdd:
key=6;
break;
case 0xbd:
key=7;
break;
case 0x7d:
key=8;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
send(key);
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:
key=9;
break;
case 0xdb:
key=10;
break;
case 0xbb:
key=11;
break;
case 0x7b:
key=12;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
send(key);
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:
key=13;
break;
case 0xd7:
key=14;
break;
case 0xb7:
key=15;
break;
case 0x77:
key=16;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
send(key);
}
}
}
void main()
{
TI=0;
TMOD=0x20;//设置定时器1为工作方式2
SCON=0x50;
AUXR=0x00;
TH1=0xfd; //波特率设置为9600
TL1=0xfd;
TR1=1;
EA=1;
ES=1;
while(1)
{
matrixkeyscan();//不停调用键盘扫描程序
}
}
从机程序:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit buzzer=P2^3;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--) //i=xms即延时约xms毫秒
for(j=110;j>0;j--);
}
/************蜂鸣器*****************/
void di()
{
buzzer=0;
delayms(60);
buzzer=1;
}
void display(uchar num)
{
P0=table[num];
dula=1;
dula=0;
// di();
}
void main()
{
TMOD=0x20;
SCON=0x50;
PCON=0x80;
// AUXR=0x00;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
P0=0xc0;
wela=1;
wela=0;
P0=table[10];
// di();
while(1);
}
void serial() interrupt 4
{
uchar a;
// di();
RI=0;
a=SBUF;
display(a);
}
|
|