基于c51的led电子锁仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- #include<stdio.h>
- unsigned char password[6]={0,1,2,3,4,5,};//初始密码
- sbit LED1=P1^3;
- sbit LED2=P1^4;
- sbit LED3=P1^5;
- sbit Beep=P0^5;
- sbit RS = P1^0;
- sbit RW = P1^1;
- sbit EN = P1^2;
- #define CHECK_BUSY
- #define RS_CLR RS=0
- #define RS_SET RS=1
- #define RW_CLR RW=0
- #define RW_SET RW=1
- #define EN_CLR EN=0
- #define EN_SET EN=1
- #define DataPort P3
- void LCD_Write_Com(unsigned char com) ;
- void LCD_Write_Data(unsigned char Data) ;
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) ;
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) ;
- void LCD_Init(void) ;
- /*------------------------------------------------
- uS延时函数,含有输入参数 unsigned char t,无返回值
- ------------------------------------------------*/
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- //大致延时1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- unsigned char KeyScan(void)
- {
- unsigned n,key,key_r,key_c;
- n=0;
- P2=0x70;
- key_c=P2;
- key_c=key_c & 0x70;
- if(key_c!=0x70)
- {
- DelayMs(30);
- if(key_c!=0x70)
- {
- key_c=key_c | 0x07;
- P2=key_c;
- key_r=P2;
- key_r=key_r & 0x07;
- key_c=key_c & 0x70;
- key=key_c+key_r;
- key=key & 0x77;
- P2=0x70; //等待本次按键结束
- while(P2!=0x70);
- return key;
- }
- }
- return 0x70;
- }
- /*------------------------------------------------
- 按键值处理函数,返回扫键值
- ------------------------------------------------*/
- unsigned char KeyPro(void)
- {
- int n=9;
- switch(KeyScan())
- {
- {
- case 0x66: n=0;break;
- case 0X56: n=1;break;
- case 0X36: n=2;break;
- case 0X65: n=3;break;
- case 0X55: n=4;break;
- case 0X35: n=5;break;
- case 0X63: n=6;break;
- case 0X53: n=7;break;
- case 0X33: n=8;break;
- default : break;
- }
- DelayMs(20);
- }
- return n;
- }
- /*------------------------------------------------
- 写入命令函数
- ------------------------------------------------*/
- void LCD_Write_Com(unsigned char com)
- {
- DelayMs(5);
- RS_CLR;
- RW_CLR;
- EN_SET;
- DataPort= com;
- ;
- EN_CLR;
- }
- /*------------------------------------------------
- 写入数据函数
- ------------------------------------------------*/
- void LCD_Write_Data(unsigned char Data)
- {
- DelayMs(5);
- RS_SET;
- RW_CLR;
- EN_SET;
- DataPort= Data;
- ;
- EN_CLR;
- }
- /*------------------------------------------------
- 写入字符串函数
- ------------------------------------------------*/
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- while (*s)
- {
- LCD_Write_Char(x,y,*s);
- s ++; x++;
- }
- }
- /*------------------------------------------------
- 写入字符函数
- ------------------------------------------------*/
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- LCD_Write_Data( Data);
- }
- /*------------------------------------------------
- 初始化函数
- ------------------------------------------------*/
- void LCD_Init(void)
- {
- LCD_Write_Com(0x38);
- LCD_Write_Com(0x08); /*显示关闭*/
- LCD_Write_Com(0x01); /*显示清屏*/
- LCD_Write_Com(0x06); /*显示光标移动设置*/
- LCD_Write_Com(0x0C); /*显示开及光标设置*/
- }
- main()
- {
- unsigned char num,j;
- int i=0,a=0,value=9;
- unsigned char temp[6];
- bit Flag;
- LCD_Init(); //初始化液晶屏
- DelayMs(10); //延时用于稳定,可以去掉
- LCD_Write_String(0,0," chose mode "); //写入第一行信息,主循环中不再更改此信息,所以在while之前写入
- LCD_Write_String(0,1," input 6or7or8 "); //写入第二行信息
- while (1) //主循环
- {
-
- num=KeyPro(); //扫描键盘
- if(num!=9) //如果扫描是按键有效值则进行处理
- {
- if(num==8)
- {
- LCD_Write_String(0,0,"password: ");
- LCD_Write_String(0,1," ");
- LED1=0;
- LED2=1;
- LED3=1;
- }
- if(num==7)
- {
- i--;
- LCD_Write_Char(i,1,' ');
- }
- if(num==6)
- {
- LCD_Write_String(0,0,"Set password!");
- LCD_Write_String(0,1," ");
- LED1=1;
- LED2=0;
- LED3=1;
- for(j=0;j<6;j++)
- {
- while(value==9)
- {
- value = KeyPro();
- }
- password[j]= value ;
- value=9;
-
- LCD_Write_Char(j,1, password[j]+'0');
- }
- LCD_Write_String(0,0,"Set sucessd!");
- LCD_Write_String(0,1," ");
- }
- if(num<6) //密码是6位,大于6位时不再输入按键值
- {
- temp[i]=num;
- LCD_Write_Char(i,1,num+'0');
- i++; //输入数值累加
- }
- if(i==6)//6位后的按键不输入数值,相当于确认按键(任意按键即可)
- {
- i=0; //计数器复位
- Flag=1;//先把比较位置1
- for(j=0;j<6;j++)//循环比较6个数值,如果有一个不等 则最终Flag值为0
- Flag=Flag&&(temp[j]==password[j]);//比较输入值和已有密码
- if(Flag)//如果比较全部相同,标志位置1
- {
- LCD_Write_String(0,1,"Right Open!");//密码正确显示的信息
- }
- else
- {
- LCD_Write_String(0,1,"Wrong Retry!");//密码错误,提示重新输入
- DelayMs(255);
- DelayMs(255);
- DelayMs(255);
- DelayMs(255);
- LCD_Write_String(0,1," ");
- a++;
- if(a>=3)
- {
- LCD_Write_String(0,0,"not allow input");
- LCD_Write_String(0,1,"Wrong Waring!");
- LED1=1;LED2=1;LED3=0;
- Beep=0;
- while(1);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
led电子锁.rar
(19.42 KB, 下载次数: 114)
|