谢谢楼上的,但是好像定义了也还是有这样的错误。。。。:(
我把程序发上来,是一个简单的密码锁的程序,程序中有详尽的注释
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//I/O口定义
sfr p0=0x80;
sfr p1=0x90;
sfr p2=0xa0;
sfr p3=0xb0;
//状态口定义
sbit red=p3^3;
sbit green=p3^2;
sbit speaker=p3^6;
//所用的数组定义
uchar b[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//数字
uchar error[5] = {0x11,0x03,0x11,0x11,0x61}; //密码输入错误时的提示
uchar good[4] = {0x85,0x03,0x03,0x09}; //密码输入正确时的提示
uchar lock[8] = {0xfd,0xfd,0x93,0x63,0x03,0xe3,0xfd,0xfd}; //锁定
uchar mima[8] = {4,4,4,4,4,4,4,4};
uchar pw[8] = {10,10,10,10,10,10,10,10};
uchar c[8];
//全局变量
uchar n=0,error_time=0;
bit mima_right;
void keydo(uchar); //按楼上说的进行声明
void delay(uint pp) //误差 0us
{
uchar a,b,c;
while(pp--)
{
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);//1ms
}
}
void display()
{
uchar i;
for(i=0;i<n;i++) //为了让8个数码管轮流亮一遍过去
{
P0=c[i];
P2=i;
delay(2);
}
}
/*----键盘扫描-------------------------*/
uchar KeyScan()
{
uchar num=100,temp;
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=0;
break;
case 0xde:num=1;
break;
case 0xbe:num=2;
break;
case 0x7e:num=3;
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=4;
break;
case 0xdd:num=5;
break;
case 0xbd:num=6;
break;
case 0x7d:num=7;
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=8;
break;
case 0xdb:num=9;
break;
case 0xbb:num=10;
break;
case 0x7b:num=11;
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=12;
break;
case 0xd7:num=13;
break;
case 0xb7:num=14;
break;
case 0x77:num=15;
break;
}
while(temp!=0xf0)
{
temp=p1;
temp=temp&0xf0;
}
}
}
return num;
}
uint check() //比对密码,判断密码数否正确
{
uint flag;
uint i;
if(n==8) //密码位数为八位
{
for(i=0; i<8; i++)
{
if(mima[i] != pw[i])
{
flag = 0;
break;
}
else
{
flag = 1;
}
}
}
else
flag = 0;
return flag;
}
//对按键的响应
void keydo(uchar key)
{
uint i;
if(key < 10) //对编码小于10 的按键的响应
{
if(n<8)
{
pw[n] = key; //保存输入的密码
for(i=7; i>0; i--) //赋值给c用于显示
c[i]=c[i-1];
c[0] = b[key];
n++; //位选加1
}
}
else if(key == 11)// 按键编码为11,功能为确定键
{
if( check() )
{
for(i=0; i<1; i++)
{
speaker = 0;
green = 0;
delay(200);
speaker = 1;
delay(200);
}
}
else
{
for(i=0; i<3; i++)
{
speaker = 0;
red = 0;
delay(200);
speaker = 1;
red = 1;
delay(200);
}
}
for(i=0; i<8; i++)//置输入为乱码,为下一次输入做准备
pw[i] = 10;
}
}
void main()
{
uchar key;
green = 1; //初始化绿灯端口,不亮
red = 1; //初始化红灯端口,不亮
speaker = 1; //初始化蜂鸣器端口,不响
while(1)
{
key = KeyScan();
if(key != 100) //当有按键按下时,关闭所有指示灯和蜂鸣器
{ //该功能暂定,根据要求可以删除
green = 1;
red = 1;
speaker = 1;
}
keydo(key);
display();
}
} |