目的:单片机通过串口发送键盘输入的键值给PC机,PC机回发该键值给单片机,并输出到LED数码管显示
问题:经检验,矩阵键盘,及595程序均无问题,问题在串口程序,PC端能收到并显示正确的数据,但是数据回发失败,数据不能在数码管上显示,数码管上始终显示00
,求指导。
2、实验程序
#include<stc15.h>
#include<intrins.h>
#define KEY P0
#define uchar unsigned char
#define uint unsigned int
sbit HC595_SER=P4^0;
sbit HC595_RCLK=P5^4;
sbit HC595_SRCLK=P4^3;
uchar code SEG7[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
uchar code Scon_bit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar data Dis_buf[]={10,10,10,10,10,10,10,10};
uchar key=20;
uchar temp,count;
void GPIO()
{
P0M0=0;P0M1=0;
P3M0=0;P3M1=0;
P4M0=0;P4M1=0;
P5M0=0;P5M1=0;
}
/**********************************************±**************************************************
void delay1ms(uint t)
{
uint y;
for(;t>0;t--)
for(y=110;y>0;y--);
}
void Delay4us() //@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
i = 8;
while (--i);
}
/*******************************************595HC**********************************************************************/
void Send_595(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
x=x<<1;
HC595_SER=CY;
HC595_SRCLK=1;
HC595_SRCLK=0;
}
}
void display(void)
{
uchar i;
for(i=0;i<8;i++)
{
Send_595(Scon_bit);
Send_595(SEG7[Dis_buf]);
HC595_RCLK=1;
HC595_RCLK=0;
delay1ms(1);
}
}
*矩阵键盘****************************************************************/
void keyDown(void)
{
uint a=0;
KEY=0x0f;
if(KEY!=0x0f)
{
delay1ms(5);
if(KEY!=0x0f)
{
KEY=0x0f;
switch(KEY)
{
case(0x07): key=0;break;
case(0x0b): key=1;break;
case(0x0d): key=2;break;
case(0x0e): key=3;break;
}
KEY=0xf0;
Delay4us();
switch(KEY)
{
case(0x70): key=key;break;
case(0xb0): key=key+4;break;
case(0xd0): key=key+8;break;
case(0xe0): key=key+12;break;
}
while((a<500)&&(key!=0xf0))
{
delay1ms(5);
a++;
}
}
}
}
/******************************************************
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8???,?????
AUXR |= 0x40; //???1???Fosc,?1T
AUXR &= 0xFE; //??1?????1???????
TMOD &= 0x0F; //?????1?16???????
TL1 = 0xE0; //??????
TH1 = 0xFE; //??????
ET1 = 0; //?????1??
TR1 = 1; //?????1
}
void Serial_ISR() interrupt 4
{
RI=0;
count=SBUF;
}
void main()
{
GPIO();
UartInit();
ES=1;
EA=1;
while(1)
{
keyDown();
if(key!=20)
{
delay1ms(1000);
SBUF=key/10+0x30;
while(!TI);
TI=0;
Dis_buf[6]=count-0x30;
SBUF=key%10+0x30;
while(!TI);
TI=0;
Dis_buf[7]=count-0x30;
SBUF=0x20;
while(!TI);
TI=0;
key=20;
}
display();
}
} |