|
想用兩個51單片機進行串口通信通信,從機掃描按鍵后,將鍵值發送給主機,主機得到鍵值后對應的led發光,用Proteus仿真,但是兩個單片機好像不能通信,請問哪里出問題了,晶振是11.0592的,波特率也一樣。問題是:主機燈沒亮,我覺得是兩個單片機沒有連上,求大神幫幫忙
從機程序:
void usart_Init(void)//串口中斷初始化
{
TMOD=0X20;//定時器1方式2
TH1=0xF3; //波特率4800
TL1=0xF3;
TR1=1;//打開定時器
SM0=0;//設置串口工作方式
SM1=1;
EA=1;//打開總中斷
ES=1;//打開串口中斷
}
void delayms(u8 xms)
{
u8 i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void Send_Data(u8 Key_val)
{
SBUF=Key_val;
while(!TI);
TI=0;
EA=0;
}
void Keyscan(void)
{
u8 Val;
if(k1==0)
{
delayms(10);
if(k1==0)
{
usart_Init();
Val=1;
}
while(!k1);
Send_Data(Val);
}
if(k2==0)
{
delayms(10);
if(k2==0)
{
usart_Init();
Val=2;
}
while(!k2);
Send_Data(Val);
}
if(k3==0)
{
delayms(10);
if(k3==0)
{
usart_Init();
Val=3;
}
while(!k3);
Send_Data(Val);
}
if(k1==4)
{
delayms(10);
if(k4==0)
{
usart_Init();
Val=4;
}
while(!k4);
Send_Data(Val);
}
if(k5==0)
{
delayms(10);
if(k5==0)
{
usart_Init();
Val=5;
}
while(!k5);
Send_Data(Val);
}
if(k6==0)
{
delayms(10);
if(k6==0)
{
usart_Init();
Val=6;
}
while(!k6);
Send_Data(Val);
}
if(k7==0)
{
delayms(10);
if(k7==0)
{
usart_Init();
Val=7;
}
while(!k7);
Send_Data(Val);
}
if(k8==0)
{
delayms(10);
if(k8==0)
{
usart_Init();
Val=8;
}
while(!k8);
Send_Data(Val);
}
}
void main()
{
while(1)
{
Keyscan();
}
}
-------------------------------------------------------------------------------------------------------------
主機程序:
void uart_init() //串口初始化函數
{
TMOD=0x20; //設置計數器工作方式2
TH1=0xF3; /波特率是4800
TL1=0xF3;
TR1=1; //打開計數器
SCON=0x50; //設置為工作方式1
ES=1; //打開接收中斷
EA=1; //打開總中斷
}
/*---------------------main函數-------------------------*/
void main()
{
uart_init();
while(1)
{
}
}
void Usart() interrupt 4
{
RI=0;
Val=SBUF;
switch(Val)
{
case '0':led=0;break;
}
}
|
-
1.gif
(115.8 KB, 下載次數: 69)
下載附件
2020-3-29 14:32 上傳
仿真圖
|