?? w4.c
字號:
//單片機P1 口接8 只發光二極管,P3.2~P3.5 接有K1~K4 共四個按鍵,使用串行
//口編程,1)由PC 機控制單片機的P1 口,將PC 機送出的數以二進制形式顯示在發光二極
//管上;2)按下K1 向主機發送數字0x55,按下K2 向主機發送數字0xAA,使顯示轉下一行。
#define uchar unsigned char
#include "string.h"
#include "reg51.h"
void SendData(uchar Dat)
{ uchar i=0;
SBUF=Dat;
while(1){ if(TI)
{ TI=0;
break;}}
}
void mDelay(unsigned int DelayTime)
{ unsigned char j=0;
for(;DelayTime>0;DelayTime--)
{ for(j=0;j<125;j++)
{;}
}
}
uchar Key()
{ uchar KValue;
P3|=0x3e; //中間4 位置高電平
if((KValue=P3|0xe3)!=0xff)
{ mDelay(10);
if((KValue=P3|0xe3)!=0xff)
{ for(;;)
if((P3|0xe3)==0xff)
return(KValue);
}
}
return(0);
}
void main()
{ uchar KeyValue;
P1=0xff; //關閉P1 口接的所有燈
TMOD=0x20; //確定定時器工作模式
TH1=0xFD;
TL0=0xFD; //定時初值
PCON=0x80; //SMOD=1
TR1=1; //開啟定時器1
SCON=0x40; //串口工作方式1
REN=1; //允許接收
for(;;)
{ if(KeyValue=Key())
{ if((KeyValue|0xfb)!=0xff) //K1 按下
SendData(0x55);
if((KeyValue|0xf7)!=0xff)
SendData(0xaa);
}
if(RI)
{ P1=SBUF;
RI=0;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -