要求通过矩阵键盘显示6位学号,但目前只可以单位显示,没法显示6位,希望大佬给一个思路
/**************************************************/
#include <reg52.h> //包含52头文件
#define uint unsigned int //宏定义uint代替unsigned int
#define uchar unsigned char //宏定义uchar代替unsigned char
#define out P0
sbit lcdrs=P2^4;
sbit lcdrw=P2^5;
sbit lcden=P2^6;
void check_busy(void);
void write_cmd(char cmd);
void write_data(uchar dat);
void write_str(uchar *str);
void lcd_init();
void keyscan();
uchar key,num=100,flag;
uint cnt=0;
uchar dat[]="0123456789ABCDEF";
void delay (uint xms) //毫秒函数定义
{
uint i,j;
for(i=0;i<xms;i++)
for(j=0;j<120;j++);
}
void main() //主函数
{ lcd_init();
write_cmd(0x80+0x40);
while(1)
{
keyscan();
if(num<16)
{
write_data(dat[key]);
}
}
}
/**********************检查忙标志函数************************/
void check_busy(void)
{
uchar dt;
out=0xff;
do
{
lcden=0;
lcdrs=0;
lcdrw=1;
lcden=1;
dt=out;
}while(dt&0x80);
lcden=0;
}
/**************************1602显示****************************/
void write_cmd(char cmd) //写指令函数
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=0;
lcdrw=0;//先将RW拉低
out=cmd;
lcden=1;
lcden=0;
}
void write_data(uchar dat) //写数据函数
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=1;
lcdrw=0;//先将RW拉低
out=dat;
lcden=1;
lcden=0;
}
void write_str(uchar *str)//写字符串函数
{
while(*str!='\0')
{
write_data(*str++);
delay(5);
}
}
/***************************初始化******************************/
void lcd_init()//初始化1602
{
write_cmd(0x38);//显示模式设置
write_cmd(0x0c);//显示开关,光标没有闪烁
write_cmd(0x06);//显示光标移动设置
write_cmd(0x01);//清除屏幕
delay(1);
}
void keyscan()
{
uchar temp,num;
P1=0xff; //先向P1写1
P1=0xfe; //选中按键第一行
temp=P1; //把P1给中间变量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:key=0; break;
case 0xde:key=1; break;
case 0xbe:key=2; break;
case 0x7e:key=3; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;//选中按键第二行
temp=P1; //把P1给中间变量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:key=4; break;
case 0xdd:key=5; break;
case 0xbd:key=6; break;
case 0x7d:key=7; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;//选中按键第三行
temp=P1; //把P1给中间变量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:key=8; break;
case 0xdb:key=9; break;
case 0xbb:key=10; break;
case 0x7b:key=11; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;//选中按键第四行
temp=P1; //把P1给中间变量
temp=temp&0xf0;//低四位清零
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:key=12; break;
case 0xd7:key=13; break;
case 0xb7:key=14; break;
case 0x77:key=15; break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
while((num<50)&&(temp!=0xf0))
{delay(10);
num++;
}
num=0;
}
|