|
要求通過(guò)矩陣鍵盤顯示6位學(xué)號(hào),但目前只可以單位顯示,沒(méi)法顯示6位,希望大佬給一個(gè)思路
/**************************************************/
#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) //毫秒函數(shù)定義
{
uint i,j;
for(i=0;i<xms;i++)
for(j=0;j<120;j++);
}
void main() //主函數(shù)
{ lcd_init();
write_cmd(0x80+0x40);
while(1)
{
keyscan();
if(num<16)
{
write_data(dat[key]);
}
}
}
/**********************檢查忙標(biāo)志函數(shù)************************/
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) //寫指令函數(shù)
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=0;
lcdrw=0;//先將RW拉低
out=cmd;
lcden=1;
lcden=0;
}
void write_data(uchar dat) //寫數(shù)據(jù)函數(shù)
{
check_busy();
lcden=0;//再把EN拉低
lcdrs=1;
lcdrw=0;//先將RW拉低
out=dat;
lcden=1;
lcden=0;
}
void write_str(uchar *str)//寫字符串函數(shù)
{
while(*str!='\0')
{
write_data(*str++);
delay(5);
}
}
/***************************初始化******************************/
void lcd_init()//初始化1602
{
write_cmd(0x38);//顯示模式設(shè)置
write_cmd(0x0c);//顯示開(kāi)關(guān),光標(biāo)沒(méi)有閃爍
write_cmd(0x06);//顯示光標(biāo)移動(dòng)設(shè)置
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;
}
|
|