单片机源程序如下:
- #include<reg52.h>
- #include<string.h>
- #include<intrins.h>
- #define Delay4us(); {_nop_();_nop_();_nop_();_nop_();}
- #define uchar unsigned char
- #define uint unsigned int
- uchar Pre_KeyNo=16,KeyNo=16;
- uchar code Title_Text[]="Your Password...";
- char DSY_BUFFER[16]=" ";
- char UserPassword[16]=" ";
- sbit LCD_RS=P1^0;
- sbit LCD_RW=P1^1;
- sbit LCD_EN=P1^2;
- sbit LED_OPEN=P1^5;
- sbit SCL=P3^2;
- sbit SDA=P3^3;
- void Delaynms(uint x)
- {
- uchar i;
- while(x--)
- for(i=0;i<123;i++);
- }
- void Start()
- {
- SDA=1;
- SCL=1;
- Delay4us();
- SDA=0;
- Delay4us();
- SCL=0;
- }
- void Stop()
- {
- SDA=0;
- SCL=1;
- Delay4us();
- SDA=1;
- Delay4us();
- SCL=0;
- }
- void IIC_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;
- }
- 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;
- }
- uchar SendByte(uchar wd)
- {
- uchar i;
- bit ack0;
- for(i=0;i<8;i++)
- {
- SDA=(bit)(wd&0x80);
- _nop_();
- _nop_();
- SCL=1;
- Delay4us();
- SCL=0;
- 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[i]))
- 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[i]=RecByte();
- ACK();
- }
- Buffer[N-1]=RecByte();
- NO_ACK();
- Stop();
- return 1;
- }
- bit LCD_Busy_Check(void)
- {
- bit result;
- LCD_RS=0;
- LCD_RW=1;
- LCD_EN=1;
- Delay4us();
- result=(bit)(P0&0x80);
- LCD_EN=0;
- return result;
- }
- void Write_LCD_Command (unsigned char cmd)
- {
- while(LCD_Busy_Check()==1);
- LCD_RS=0;
- LCD_RW=0;
- LCD_EN=0;
- _nop_();
- _nop_();
- P0=cmd;
- Delay4us();
- LCD_EN=1;
- Delay4us();
- LCD_EN=0;
- }
- void Write_LCD_Data(uchar dat)
- {
- while(LCD_Busy_Check()==1);
- LCD_RS=1;
- LCD_RW=0;
- LCD_EN=0;
- P0=dat;
- Delay4us();
- LCD_EN=1;
- Delay4us();
- LCD_EN=0;
- }
- void Set_LCD_POS(uchar pos)
- {
- Write_LCD_Command(pos|0x80);
- }
- void LcdInitiate(void)
- {
- Delaynms(15);
- Write_LCD_Command(0x38);
- Delaynms(5);
- Write_LCD_Command(0x38);
- Delaynms(5);
- Write_LCD_Command(0x38);
- Delaynms(5);
- Write_LCD_Command(0x0c);
- Delaynms(5);
- Write_LCD_Command(0x06);
- Delaynms(5);
- Write_LCD_Command(0x01);
- Delaynms(5);
- }
- void Display_String(uchar *str,uchar LineNo)
- { uchar k;
- Set_LCD_POS(LineNo);
- for(k=0;k<16;k++)
- {
- if(str[k]=='\0')
- break;
- Write_LCD_Data(str[k]);
- Delaynms(1);
- }
- }
- uchar Keys_Scan()
- {
- uchar Tmp,KeyNo=0;
- P2=0x0F;
- Delaynms(1);
- Tmp=P2^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;
- }
- P2=0xF0;
- Delaynms(1);
- Tmp=P2>>4^0x0F;
- switch(Tmp)
- {
- case 1:KeyNo+=0;break;
- case 2:KeyNo+=4;break;
- case 4:KeyNo+=8;break;
- case 8:KeyNo+=12;break;
- }
- return KeyNo;
- }
- void Clear_Password()
- {
- uchar i;
- for(i=0;i<16;i++)
- {
- UserPassword[i]=' ';
- DSY_BUFFER[i]=' ';
- }
- }
- void main()
- {
- uchar i = 0;
- uchar IS_Valid_User=0;
- uchar temp[1];
- uchar Flag[1]=0x55;
- uchar IIC_Password[10];
- uchar Secrect_code[ ]="123456";
- P0=0xFF;
- P1=0xFF;
- P2=0xFF;
- Delaynms(10);
- LcdInitiate();
- IIC_Init();
- Display_String(Title_Text,0x00);
- RecString(0xa0, 0xff , temp, 1);
- if(temp[0]!=0x55)
- {
- SendString(0xa0,0,Secrect_code,6);
- SendString(0xa0,0xff,Flag,1);
- }
- RecString(0xa0,0,IIC_Password, 6);
- IIC_Password[6] = '\0';
- while(1)
- {
- P2 = 0xF0;
- if(P2!= 0xF0)
- KeyNo = Keys_Scan();
- switch ( KeyNo )
- {
- case 0: case 1: case 2: case 3: case 4:
- case 5: case 6: case 7: case 8: case 9:
- if ( i<= 5 )
- {
- if (i ==0)
- Display_String(" ",0x40);
- UserPassword[i]=KeyNo+'0';
- UserPassword[i+1]='\0';
- DSY_BUFFER[i]='*';
- DSY_BUFFER[i+1]='\0';
- Display_String(DSY_BUFFER,0x40);
- i++;
- }
- break;
- case 10:
- RecString(0xa0,0,IIC_Password,6);
- IIC_Password[6]='\0';
- if(strcmp(UserPassword,IIC_Password)==0)
- {
- LED_OPEN=1;
- Clear_Password();
- Display_String(" OK Unlocked! ",0x40);
- IS_Valid_User=1;
- }
- else
- {
- LED_OPEN=0;
- Clear_Password();
- Display_String(" OK! Unlocked! ",0x40);
- IS_Valid_User=0;
- }
- i=0;
- break;
- case 11:
- LED_OPEN =1;
- Clear_Password();
- Display_String(Title_Text,0x00);
- Display_String(" Locked OK! ",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]='\0';
- i=0;
- Display_String(Title_Text,0x00);
- Display_String("Password Saved! ",0x40);
- }
- break;
- case 14:
- i = 0;
- Clear_Password();
- Display_String(" ",0x40);
- }
- Delaynms(10);
- P2=0xF0;
- while(P2!=0xF0);
- while(P2==0xF0);
- }
- }
复制代码
|