基于51的电子密码锁的实验,但程序出错,求高手

2019-07-16 06:30发布

#include <reg51.h>
#include <intrins.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define Delay4us() {_nop_();_nop_(); _nop_();_nop_();}
sbit SCL =P3^2;
sbit SDA =P3^3;
//上次按键和当前按键序号,该矩阵中序号范围0-15,16表示无按键
uchar Pre_KeyNo = 16,KeyNo = 16;
uchar code title_Test[] ="Your Password..."; //标题字符串
uchar DSY_BUFFER[10] ="";  //显示缓冲
uchar UserPassword[10] =""; //用户输入的密码
void LCD_Init();  //液晶初始化
void Display_String(uchar *str,uchar LineNo); //液晶指定行显示字符串
void IIC_24C04_Init(); //IIC初始化
void Beep(); //蜂鸣器

QQ截图20120106155732.jpg
uchar RecString(uchar Slave,uchar Subaddr,uchar *Buffer,uchar N); //从IIC读取数据
uchar SendString(uchar Slave,uchar Subaddr,uchar *Buffer,uchar N); //向IIC写入数据
uchar Keys_Scan(); //键盘扫描,返回键盘号0-15
sbit LED_OPEN =P2^7;  //开锁亮灯
sbit BEEP=P3^7;
sbit LCD_RS= P2^0;
sbit LCD_RW= P2^1;
sbit LCD_EN= P2^2;


//起始位//
void Start()
{
SDA = 1;
SCL = 1;
Delay4us();
SDA = 0;
Delay4us();
SCL = 0;
}
//停止位//
void Stop()
{
SDA = 0;
SCL = 1;
Delay4us();
SDA = 1;
Delay4us();
SCL = 0;
}
//IIC总线初始化//
void IIC_24C04_Init()
{
SCL = 0; Stop();
}
//发送应答信号//
void ACK()
{
SDA = 0; SCL = 1; Delay4us(); SCL = 0; SDA = 1;
}
//发送非应答信号//
void NO_ACK()
{
SDA = 1; SCL = 1; Delay4us(); SCL = 0; SDA = 0;
}
//从芯片读取1字节//
uchar RecByte()
{
uchar i,rd;
rd = 0x00;
SDA = 1;
for(i = 0; i<8; i++)
{
SCL = 1; rd <<=1; rd|= SDA; Delay4us(); SCL = 0; Delay4us();
}
SCL = 0;
Delay4us();
return rd;
}
//向芯片发送1字节//
uchar SendByte(uchar wd)
{
uchar i;
bit ack0;
for(i = 0; i<8; i++)
{
SDA = (bit)(wd & 0x80 );
_nop_(); _nop_(); SCL = 1; Delay4us(); SCL = 1; wd <<= 1;
}
Delay4us();
SDA = 1;
SCL = 1;
Delay4us();
ack0 = !SDA;
SCL = 0;
Delay4us();
return ack0;
}
//发送多字节数据函数//
uchar SendString(uchar Slave,uchar Subaddr,uchar *Buffer,uchar N)
{
uchar i;
Start();
if(!SendByte(Slave)) return 0;
if(!SendByte(Subaddr)) return 0;
for(i = 0; i<N; i++)
{
if (!SendByte(Buffer)) return 0;
}
Stop();
return 1;
}
//接收多字节数据函数//
uchar RecString(uchar Slave,uchar Subaddr,uchar *Buffer,uchar N)
{
uchar i;
Start();
if(!SendByte(Slave)) return 0;
if(!SendByte(Subaddr)) return 0;
Start();
if(!SendByte(Slave + 1)) return 0;
for(i = 0; i<N-1; i++)
{
  Buffer = RecByte();
  ACK();
}
Buffer[N-1] = RecByte();
NO_ACK();
Stop();
return 1;
}
void DelayMS(uint x);
uchar Keys_Scan()
{
uchar Tmp,KeyNo = 0;
P1 = 0x0F;
DelayMS(1);
Tmp = P1^ 0x0F;
switch(Tmp)
{
case 1: KeyNo = 0;break;
case 2: KeyNo = 1;break;
case 4: KeyNo = 2;break;
case 8: KeyNo = 3;break;
default: KeyNo = 16;
}
P1 = 0xF0;
DelayMS(1);
Tmp = P1 >> 4^ 0x0F;
switch(Tmp)
{
case 1: KeyNo += 0;break;
case 2: KeyNo += 4;break;
case 4: KeyNo += 8;break;
case 8: KeyNo += 12;
}
return KeyNo;
}
         //延时//
void DelayMS(uint x)
{
uchar i;
while (x--) for (i=0;i<120;i++);
}
//蜂鸣器子程序//
void Beep()
{
uchar i;
for(i = 0;i<100;i++)
{
DelayMS(1);
BEEP= ~BEEP;
}
BEEP =0;
}
//清除密码//
void Clear_Password()
{
UserPassword[0]='';
DSY_BUFFER[0]='';
}
//主程序//
void main()
{
uchar i=0;
uchar IIC_Password[10];
uchar IS_Valid_User =0;
P0=P1=P2=0xFF;
TMOD=0x02;
TH0=175;
TL0=175;
TR0=1;
DelayMS(10);
LCD_Init();
IIC_24C04_Init();
Display_String(Title_Test,0x00);
RecString(0xa0, 0,IIC_Password,6);
IIC_Password[6]='';
while(1)
{
P1=0xF0;
if(P1 !=0xF0) KeyNo=Keys_Scan();
if(Pre_KeyNo !=KeyNo);
{
if (i<10)
{
switch (KeyNo)
{
case 0: case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8: case 9:
if(i==0)Display_String("     ",0x40);
UserPassword =KeyNo+ '0';
UserPassword[i+1] = '';
DSY_BUFFER ='*';
DSY_BUFFER[i+1] ='';
Display_String(DSY_BUFFER,0x40);
i++;
break;
case 10:
if (strcmp(UserPassword,IIC_Password) ==0)
{
LED_OPEN = 0;
Clear_Password();
Display_String("Unlock OK!   ",0X40);
IS_Valid_User = 1;
}
else
{
LED_OPEN = 1;
Clear_Password();
Display_String("ERROR!   ",0X40);
IS_Valid_User = 0;
}
i = 0;
break;
case 11:
LED_OPEN = 1;
Clear_Password();
Display_String(Title_Test,0x00);
Display_String("  ",0X40);
i = 0;
IS_Valid_User = 0;
break;
case 12:
if ( !IS_Valid_User ) Display_String("No rights !",0x40);
else
{
i = 0;
Display_String("New Password:   ",0x00);
Display_String("  ",0X40);
}
break;
case 13:
if ( !IS_Valid_User ) Display_String("No rights !",0x40);
else
{
SendString(0xa0, 0, UserPassword,6);
RecString(0xa0, 0,IIC_Password,6);
IIC_Password[6] ='';
i = 0;
Display_String(Title_Test,0x00);
Display_String("Password Saved!   ",0X40);
}
break;
case 14:
i = 0;
Clear_Password();
Display_String("  ",0X40);
}
}
Beep();
Pre_KeyNo = KeyNo;
}
DelayMS(100);
}
}
//LCD显示驱动函//

bit LCD_Busy_Check();
void LCD_Initialize();
void LCD_Set_POS(uchar);
void LCD_Write_Command(uchar);
void LCD_Write_Data(char);
//延时//
void DelayMS(uint ms)
{
uchar t;
while(ms--) for (t=0;t<120;t++);
}
//忙检查//
bit LCD_Busy_Check()
{
bit Result;
LCD_RS=0;LCD_RW=1;LCD_EN=1;DelayNOP();
Result =(bit)(P0&0x80);
LCD_EN=0;
return Result;
}
//向LCD写指令//
void void LCD_Write_Command(uchar cmd)
{
while (LCD_Busy_Check());
LCD_RS_=0;LCD_RW_=0;LCD_EN_=0;
_nop_(); _nop_();
P0=cmd; DelayNOP();
LCD_EN_=1;DelayNOP();
LCD_EN_=0;
}
//向LCD写数据//
void LCD_Write_Data(uchar str)
{
while (LCD_Busy_Check());
LCD_RS_=1;LCD_RW_=0;LCD_EN_=0;
_nop_(); _nop_();
P0=str; DelayNOP();
LCD_EN_=1;DelayNOP();
LCD_EN_=0;
}
//初始化LCD//
void LCD_Initialize()
{
DelayMS(5); LCD_Write_Command(0x38);
DelayMS(5); LCD_Write_Command(0xoC);
DelayMS(5); LCD_Write_Command(0x06);
DelayMS(5); LCD_Write_Command(0x01);
DelayMS(5);
}
//设置显示位置//
void LCD_Set_POS(uchar Position)
{
LCD_Write_Command(Position | 0x80);
}
//显示函数,在LCD指定行上显示字符串//
void Display_String(uchar * str, uchar LineNo)
{
uchar k;
LCD_Set_POS(LineNo);
for(k=0;k<16;k++)
LCD_Write_Data(str[k]);
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
16条回答
li.sunny
1楼-- · 2019-07-17 00:55
编译出错?把错误信息和错误代码贴出来吧
sky520147
2楼-- · 2019-07-17 03:25
这个思路那么长,看得懂的是一部分,看不懂的是大部分,独立能设计出这个程序的水平大概是到哪个层次?刚才复制了编译是出现几个错误亦不懂其意思“TEXT4.C(108): warning C214: 'Argument': conversion: pointer to non-pointer
TEXT4.C(124): error C214: illegal pointer conversion
TEXT4.C(289): error C237: '_DelayMS': function already has a body
Target not created
金持
3楼-- · 2019-07-17 08:21
楼主你这样大家看的眼花啊。注释呢。遇到的问题也说的不明白啊。哪里出错?
cdy_shenhua
4楼-- · 2019-07-17 09:13
我已经通过实验自己解决了,谢谢大家的帮忙,主要问题是键盘抖动问题,我已经通过松手检测实现了,谢谢大家!
叫我医生
5楼-- · 2019-07-17 13:24
 精彩回答 2  元偷偷看……
NIMADEa
6楼-- · 2019-07-17 16:26
??看不懂

一周热门 更多>