标题: 我做51单片机,矩阵键盘输入,想输入4位数,并在数码管上显示出来,我的程序哪里不... [打印本页]
作者: lx983625241 时间: 2014-9-2 18:15
标题: 我做51单片机,矩阵键盘输入,想输入4位数,并在数码管上显示出来,我的程序哪里不...
键盘扫描函数,显示函数都没问题,都测试过了,就主函数那一块,一直调试不出来,要实现的功能就是由键盘输入,比如说输入2345,就在键盘上分别按2345键,然后由数码管显示出来
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
uchar temp,num,num1,bitnum=0,i,flag;
uint value,temp1=1;
uchar keybuffer[4];
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void display(uint num)
{
uchar ge,shi,bai,qian;
ge=num%10;
shi=num%100/10;
bai=num%1000/100;
qian=num/1000;
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[ge];
dula=0;
delay(5);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[shi];
dula=0;
delay(5);
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[bai];
dula=0;
delay(5);
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[qian];
dula=0;
delay(5);
}
uchar keyscan()
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:num=1;flag=1;
break;
case 0xde:num=2;flag=1;
break;
case 0xbe:num=3;flag=1;
break;
case 0x7e:num=4;flag=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:num=5;flag=1;
break;
case 0xdd:num=6;flag=1;
break;
case 0xbd:num=7;flag=1;
break;
case 0x7d:num=8;flag=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:num=9;flag=1;
break;
case 0xdb:num=10;flag=1;
break;
case 0xbb:num=11;flag=1;
break;
case 0x7b:num=12;flag=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:num=13;flag=1;
break;
case 0xd7:num=14;flag=1;
break;
case 0xb7:num=15;flag=1;
break;
case 0x77:num=16;flag=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
return num;
}
void main()
{
while(1)
{
while(num1<10&bitnum<4)
{
num1=keyscan();
keybuffer[bitnum]=num1;
if(flag==1)
{
bitnum++;
flag=0;
}
}
for(i=0;i<bitnum;i++)
{
value=keybuffer[bitnum-i]*temp1+value;
temp1=temp1*10;
}
display(value);
}
}
作者: admin 时间: 2014-9-2 18:29
你现在这个程序按键以后,显示屏上面有反应吗
作者: 2842687945 时间: 2014-9-3 11:01
本帖最后由 2842687945 于 2014-9-3 11:08 编辑
#include "reg52.h"
typedef unsigned int uint16;
typedef unsigned char uint8;
void main()
{
uint8 d;
P1 = 0xff;//初始化P1口
while(1)
{
P1 = 0xfe;//第一行键,1111 1110
//第一行键的键码的判断
d = P1;
switch(d)
{
case 0xee:P0 = 0x3f;break;
case 0xde:P0 = 0x06;break;
case 0xbe:P0 = 0x5b;break;
case 0x7e:P0 = 0x4f;break;
}
P1 = 0xfd;//第二行键,1111 1101
//第二行键的键码的判断
d = P1;
switch(d)
{
case 0xed:P0 = 0x66;break;
case 0xdd:P0 = 0x6d;break;
case 0xbd:P0 = 0x7d;break;
case 0x7d:P0 = 0x07;break;
}
P1 = 0xfb;//第三行键,1111 1011
//第三行键的键码的判断
d = P1;
switch(d)
{
case 0xeb:P0 = 0x7f;break;
case 0xdb:P0 = 0x6f;break;
case 0xbb:P0 = 0x77;break;
case 0x7b:P0 = 0x7c;break;
}
P1 = 0xf7;//第四行键,1111 0111
//第四行键的键码的判断
d = P1;
switch(d)
{
case 0xe7:P0 = 0x39;break;
case 0xd7:P0 = 0x5e;break;
case 0xb7:P0 = 0x79;break;
case 0x77:P0 = 0x71;break;
}
}
}
作者: 2842687945 时间: 2014-9-3 11:09
这个程序是我写的,我试过了
作者: hesongchuan 时间: 2014-9-3 16:09
他要实现的是连续按键4次,然后出现千位数。
作者: hesongchuan 时间: 2014-9-3 16:50
我感觉你那个 flag 没起作用, 另你在初始化下 num1=0 flag=0, 另外如果你那按键大于9以上的话,会直接调出while大循环,
作者: lx983625241 时间: 2014-9-3 21:04
那你说我具体应该怎么做呢
作者: lx983625241 时间: 2014-9-3 21:05
那你说我具体应该怎么做呢
作者: hesongchuan 时间: 2014-9-5 18:02
本帖最后由 hesongchuan 于 2014-9-7 13:57 编辑
这几天忙,很简答 啊,swith case ,语句 case 0x7e:num=4,flag=1; num 和flag用逗号不要分号,
把 num <=10的语句去掉, 另外while(num1<10&bitnum<5) 全局变量bitnum初值等于1。
下面程序可以实现连续按键四次,出现一个千位数,然后在按第十个键 ,数码管关闭,实验下吧。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^1;
sbit dula=P2^2;
sbit sb=P3^5;//管段数码管键 第十个键
uchar temp,num,num1,bitnum=1,i,flag;
uint value,temp1=1;
void display(uint z);
uchar keyscan();
uchar keybuffer[4];
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void display(uint z)
{
uchar ge,shi,bai,qian;
ge=z%10;
shi=z/10%10;
bai=z/100%10;
qian=z/1000%10;
wela=1;
P1=0xf7;
wela=0;
dula=1;
P1=table[ge];
dula=0;
delay(5);
wela=1;
P1=0xfb;
wela=0;
dula=1;
P1=table[shi];
dula=0;
delay(5);
wela=1;
P1=0xfd;
wela=0;
dula=1;
P1=table[bai];
dula=0;
delay(5);
wela=1;
P1=0xfe;
wela=0;
dula=1;
P1=table[qian];
dula=0;
delay(5);
}
uchar keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:num=1,flag=1;
break;
case 0xde:num=2,flag=1;
break;
case 0xbe:num=3,flag=1;
break;
case 0x7e:num=4,flag=1;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:num=5,flag=1;
break;
case 0xdd:num=6,flag=1;
break;
case 0xbd:num=7,flag=1;
break;
case 0x7d:num=8,flag=1;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:num=9,flag=1;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return num;
}
void main()
{
while(1)
{
while(bitnum<5)
{
num1=keyscan();
keybuffer[bitnum]=num1;
if(flag==1)
{
bitnum++;
flag=0;
}
}
value=0;
for(i=4;i>0;i--)
{
value=keybuffer[bitnum-i]*temp1+value;
temp1=temp1*10;
}
temp1=1;
bitnum=0;
sb=1;
while(sb==1) //未按管段键,一直执行显示程序。
{
display(value);
}
wela=1;
P1=0xff;
wela=0;
}
}
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |