利用單片機(jī)實(shí)現(xiàn)的電磁密碼鎖
C代碼如下:
#include"reg52.h"
#include"intrins.h"
#include"stdlib.h"
#include"string.h"
#define uchar unsigned char
#define uint unsigned int
//define port
sbit suo=P2^0;
sbit lcd_e=P2^5;
sbit lcd_wr=P2^6;
sbit lcd_rs=P2^7;
uchar key,i=0;
uchar? putmima[5];
uchar code mima[]={"11111"};
uchar code dis2[]={"please input passwd"};
void delayxms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=114;j>0;j--);
}
//write instructions to LCD
//rs=l,rw=l,e=1,d0-d7=data
void write_com(uchar com)
{
_nop_();
lcd_rs=0;
lcd_wr=0;
lcd_e=1;
P0=com;
lcd_e=0;
delayxms(5);
}
void write_data(uchar data0)
{
_nop_();
lcd_e=1;
_nop_();
lcd_rs=1;
lcd_wr=0;
P0=data0;
lcd_e=0;
delayxms(100);
}
void init()
{
lcd_e=1;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void matrixkeyscan()
{
P3=0xfe;//first? line 11111110=P3
if(P3==0Xee)
{
delayxms(10);
if(P3==0Xee)
{
key='c';
while(!P3==0Xee);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xde)
{
delayxms(10);
if(P3==0Xde)
{
key='d';
while(!P3==0Xde);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xbe)
{ delayxms(10);
if(P3==0Xbe)
{
key='e';
while(!P3==0Xbe);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0X7e)
{
delayxms(10);
if(P3==0X7e)
{
key='f';
while(!P3==0X7e);
putmima[i]=key;
i++;
write_data('*');
}
}
P3=0xfd;// second line.
if(P3==0Xed)
{
delayxms(10);
if(P3==0Xed)
{
key='8';
while(!P3==0Xed);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xdd)
{
delayxms(10);
if(P3==0Xdd)
{
key='9';
while(!P3==0Xdd);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xbd)
{
delayxms(10);
if(P3==0Xbd)
{
key='a';
while(!P3==0Xbd);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0X7d)
{
delayxms(10);
if(P3==0X7d)
{
key='b';
while(!P3==0X7d);
putmima[i]=key;
i++;
write_data('*');
}
}
P3=0xfb;// third line.
if(P3==0Xeb)
{
delayxms(10);
if(P3==0Xeb)
{
key='4';
while(!P3==0Xed);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xdb)
{
delayxms(10);
if(P3==0Xdb)
{
key='5';
while(!P3==0Xdb);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xbb)
{
delayxms(10);
if(P3==0Xbb)
{
key='6';
while(!P3==0Xbb);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0X7b)
{
delayxms(10);
if(P3==0X7b)
{
key='7';
while(!P3==0X7b);
putmima[i]=key;
i++;
write_data('*');
}
}
? P3=0xf7;// fourth line.
if(P3==0Xe7)
{
delayxms(10);
if(P3==0Xe7)
{
key='0';
while(!P3==0Xe7);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xd7)
{
delayxms(10);
if(P3==0Xd7)
{
key='1';
while(!P3==0Xd7);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0Xb7)
{ delayxms(10);
if(P3==0Xb7)
{
key='2';
while(!P3==0Xb7);
putmima[i]=key;
i++;
write_data('*');
}
}
if(P3==0X77)
{
delayxms(10);
if(P3==0X77)
{
key='3';
while(!P3==0X77);
putmima[i]=key;
i++;
write_data('*');
}
}?
}
void main()
{ uchar num;
init();
write_com(0x80);
for(num=0;num<30;num++)
{
write_data(dis2[num]);
delayxms(1);
}
write_com(0x80+0x44);
while(i<5)
{
matrixkeyscan();
}
if(putmima[0]==mima[0])
{
if(putmima[1]==mima[1])
{
if(putmima[2]==mima[2])
{
if(putmima[3]==mima[3])
{
if(putmima[4]==mima[4])
{
suo=0;
}
}
}
}
}
while(1);
}
