12864密码锁

这个程序真的花了我好长的时间调试,终于写好了,大家可以看看我的密码锁,有点花哨呕!!

下面是密码锁简约版本:

main.c

#include<reg52.h>
#include"12864.h"
#include"keyscan.h"
#include"baojin.h"

uchar mima[5]={1,2,3,4,5};


main()
{
    uchar length,i,flag,inputtimes,flagkaimeng;//tempÊÇÃÜÂëµÄÁÙʱ±äÁ¿
    uchar itouch,flaggaimima;//ÓÃÀ´¼Ç¼ÕæÕýÊäÈëµÄÃÜÂëµÄ¸öÊý
    uchar mimatemp[5];
    init_12864();
    lcd_pos(0,2);
    write_string("welcom!!");
    delay1ms(2);
    lcd_pos(1,0);
    write_string("PSW:");
    write_cmd(0x0f);//´ò¿ª¹â±ê
    
    
    f3:
    while(1)
    {
            i=0;
            if(inputtimes>=1)//ÊäÈë´íÎóºóµÄ΢µ÷ÎÊÌâ
            {
                    lcd_pos(1,2);
                    write_string("          ");
                    lcd_pos(1,2);
                    
            }
            while(1)
            {
                f:    
                num=keyscan();
                    if(num!=0xff)
                    {
                            if(num==15)//¶¨ÒåΪȷÈÏ
                                break;
                            
                            if(num==16)//Õâ¸öËã·¨¿ÉÒÔʵÏֶಽÍ˸ñ
                            {
                                i--;//±íʾǰÃæÊäÈëµÄÊý¾ÝÎÞЧ
                            
                                lcd_pos(1,2);
                                write_string("                      ");
                                lcd_pos(1,2);
                                if(i==-1)//±ÜÃâ¶à´Î°´ÏÂÔì³Ébug
                                        i=0;
                                
                                for(itouch=0;itouch<i;itouch++)
                                        write_string("*");
                                
                                goto f;
                            }
                            
                            if(num==14)//±íʾҪÐÞ¸ÄÃÜÂë
                            {
                                    flaggaimima = 1;
                                    break;
                            }
                            
                            if(num!=16)//16¶¨ÒåΪ»Ø³µ£¬Ïû³ýÇ°ÃæÒ»¸öÊý¾Ý
                            {
                                mimatemp[i]=num;
                                i++;
                                length = i;
                                write_string("*");
                                //write_date(num+0x30);
                            }
                    }
                
            }
            
            
            if(flaggaimima==1)
                {
                flaggaimima = 0;
                while(1)
                {
                f1:    
                num=keyscan();
                    if(num!=0xff)
                    {
                            if(num==15)//¶¨ÒåΪȷÈϱíʾÐÞ¸ÄÃÜÂë½áÊø
                            {
                                    lcd_pos(1,2);
                                    write_string("             ");
                                    lcd_pos(1,2);
                                    break;
                            }
                            if(num==16)//Õâ¸öËã·¨¿ÉÒÔʵÏֶಽÍ˸ñ
                            {
                                i--;//±íʾǰÃæÊäÈëµÄÊý¾ÝÎÞЧ
                            
                                lcd_pos(1,2);
                                write_string("                      ");
                                lcd_pos(1,2);
                                if(i==-1)//±ÜÃâ¶à´Î°´ÏÂÔì³Ébug
                                        i=0;
                                
                                for(itouch=0;itouch<i;itouch++)
                                        write_string("*");
                                
                                goto f1;
                            }
                            if(num!=16)//16¶¨ÒåΪ»Ø³µ£¬Ïû³ýÇ°ÃæÒ»¸öÊý¾Ý
                            {
                            mima[i]=num;
                            i++;
                            length = i;
                            write_string("*");
                            //write_date(num+0x30);
                            }
                            
                    }
                
                }
                goto f3 ;//±íʾÖØпªÊ¼
                    
            }
            
            
            
            flag = 1;
            if(length == sizeof(mima))//±È½ÏÊäÈëµÄÃÜÂëÊÇ·ñÒ»Ñù
            {
            for(i=0;i<length;i++)
                {
                    flag=flag&&(mimatemp[i]==mima[i]);
                        //write_date(mimatemp[i]+0x30);
                }
            }
            else
            {
                        flag=0;
            }
            
            if(flag ==1)//Èç¹û±ê־λµÈÓÚ1£¬±íʾ¿ªËø³É¹¦
            {
                    flagkaimeng = 1;//±íʾ´ò¿ª²½½øµç»úµÄ±íʾλÆô¶¯
                    lcd_pos(2,1);
                    write_string("             ");
                    lcd_pos(2,2);
                    write_string("open!");
                    write_cmd(0x0c);
                    break;//ͬʱÌø³öÑ­»·
            }
            
            
            else //·ñÔòµÄ»°£¬±íʾ¿ªËøʧ°Ü
            {
                    lcd_pos(2,1);
                    write_string("error,again!");
                    
                
                    inputtimes++;
                    
                    
                    if(inputtimes==3)
                    {
                            lcd_pos(2,1);
                            write_string("error 3 times!");
                            write_cmd(0x0c);
                            feng();
                            
                    }
            }
    }
    

    if(flagkaimeng==1)//¿ªÃųɹ¦µÄ²Ù×÷
    {
                write_cmd(0x01);
                lcd_pos(1,2);
                write_string("³öÈëƽ°²£¡");
                lcd_pos(2,2);
                write_string("»¶Ó­»Ø¼Ò£¡");
             JDQ();
        
    }
    
    while(1);
    
                    
}
 
View Code

12864.c

#include"12864.h"
#include<reg52.h>

sbit lcdrs = P2^4;//Êý¾Ý»¹ÊÇÖ¸ÁîÑ¡Ôñ¶Ë¿Ú
sbit lcdwr = P2^5;//¶ÁдѡÔñ¶Ë¿Ú
sbit lcden = P2^6;//ʹÄܶ˿Ú
sbit PSB = P2^1;//´®²¢¿ÚÑ¡Ôñ¶Ë¿Ú

/*uchar code table1[]={"Welcome!!"};
uchar code table2[]={"Psw:"};
uchar code table3[] ={"Open"};
uchar code table4[] ={"Error try again!"};
uchar code table5[] ={"Error 3 times!!"};*/
unsigned char code pic1[] = { 0x01,
};

void delay1ms(uint x)//ÑÓʱº¯Êý
{
    uint i,j;
    for(i= x;i>0;i--)
        for(j=110;j>0;j--);
}

void write_cmd(uchar cmd)//дָÁÊý
{
    lcdrs = 0;
    lcdwr = 0;
    P0 = cmd;
    lcden = 1;
    delay1ms(5);//
    lcden = 0;
}

void write_date(uchar date)//дÊý¾Ýº¯Êý
{
    lcdrs = 1;
    lcdwr = 0;
    P0 = date;
    lcden = 1;
    delay1ms(5);//
    lcden = 0;
}

void write_string(uchar *str)//дÈë×Ö·û´®º¯Êý
{
    
    
    for(;*str!='\0';str++)
    {
            write_date(*str);
            delay1ms(2);
    }
    
}

void init_12864()
{
    PSB = 1;//²¢¿Ú·½Ê½´«ËÍÊý¾Ý
    write_cmd(0x30);//»ù±¾Ö¸Áî²Ù×÷
    write_cmd(0x0c);//¿ªÏÔʾ£¬¹Ø¹â±ê
    write_cmd(0x01);//Çå³ýlcdµÄÏÔʾÄÚÈÝ
}

void lcd_putpic(unsigned char code *img)//»­Í¼º¯Êý
{
    uint i,j;
    //»­Í¼Éϰ벿·Ö
    for(i = 0;i<32;i++)
    {
        write_cmd(0x80+i);//
        write_cmd(0x80);//
        for(j=0;j<16;j++)
        {
            write_date(*img);
            img++;
        }
    }
    //»­Ï²¿·ÖµÄÆÁÄ»   
    for(i = 0;i<32;i++)
    {
        write_cmd(0x80+i);//É趨ÁÐ
        write_cmd(0x88);//´ÓµÚÈýÐпªÊ¼»­Í¼
        for(j=0;j<16;j++)
        {
            write_date(*img);
            img++;
        }
    }
}

void setpic()//É趨»­Í¼¹¦Äܵĺ¯Êý
{
    write_cmd(0x36);//É趨»­Í¼Æ仭ͼ¹¦ÄÜ
    delay1ms(5);
}

void lcd_pos(uchar x,uchar y)//É趨ÏÔʾλÖõĺ¯Êý
{
    uchar pos;
    if(x==0)
        x=0x80;
    else 
        if(x==1)
            x=0x90;
        else 
            if(x==2)
                x=0x88;
            else 
                if(x==3)
                    x=0x98;
                pos=x+y;
                write_cmd(pos);
}
View Code

keyscan.c

#include<reg52.h>
#include "12864.h"

uchar num,num1,temp;
uchar keyscan()
{
    P3 = 0xfe;//1111 1110
        temp = P3;
        temp = temp&0xf0;//11110000
        while(temp!=0xf0)//??????
        {
            delay1ms(5);//????P3?
            temp = P3;
            temp = temp&0xf0;
            while(temp!=0xf0)
            {
                temp = P3;  
                switch(temp)
                {
                    case 0x7e:num = 1;
                        break;
                    case 0xbe:num = 2;//10
                        break;
                    case 0xde:num = 3;//11
                        break;
                    case 0xee:num = 4;//110
                        break;
                     
                }
                while(temp!=0xf0)//????
                {
                    temp = P3;
                    temp = temp&0xf0;
                }
                                return num;
            }
        }
 
         
        P3 = 0xfd;//1111 1101
        temp = P3;
        temp = temp&0xf0;//11110000
        while(temp!=0xf0)//??????
        {
            delay1ms(5);//????P3?
            temp = P3;
            temp = temp&0xf0;
            while(temp!=0xf0)
            {
                temp = P3;  
                switch(temp)
                {
                    case 0x7d:num = 5;//101
                        break;
                    case 0xbd:num = 6;//110
                        break;
                    case 0xdd:num = 7;//111
                        break;
                    case 0xed:num = 8;//1000
                        break;
                     
                }
                while(temp!=0xf0)//????
                {
                    temp = P3;
                    temp = temp&0xf0;
                }
                                return num;
            }
        }
 
 
 
        P3 = 0xfb;//1111 1011
        temp = P3;
        temp = temp&0xf0;//11110000
        while(temp!=0xf0)//??????
        {
            delay1ms(5);//????P3?
            temp = P3;
            temp = temp&0xf0;
            while(temp!=0xf0)
            {
                temp = P3;  
                switch(temp)
                {
                    case 0x7b:num = 9;//1001
                        break;
                    case 0xbb:num = 10;//10
                        break;
                    case 0xdb:num = 11;//11
                        break;
                    case 0xeb:num = 12;//12
                        break;
                                    
                     
                }
                while(temp!=0xf0)//????
                {
                    temp = P3;
                    temp = temp&0xf0;
                }
                                return num;
            }
        }
 
        P3 = 0xf7;//1111 0111
        temp = P3;
        temp = temp&0xf0;//11110000
        while(temp!=0xf0)//??????
        {
            delay1ms(5);//????P3?
            temp = P3;
            temp = temp&0xf0;
            while(temp!=0xf0)
            {
                temp = P3;  
                switch(temp)
                {
                    case 0x77:num = 13;//13
                        break;
                    case 0xb7:num = 14;//14
                        break;
                    case 0xd7:num = 15;//15
                        break;
                    case 0xe7:num = 16;//16
                        break;
                                    
                }
                while(temp!=0xf0)//????
                {
                    temp = P3;
                    temp = temp&0xf0;
                }
                                return num;
     
            }
        }
         
        return 0xff;
 
}
View Code

baojin.c

#include<reg52.h>
#include"baojin.h"
#include"12864.h"
sbit jdq = P1^0;
sbit led = P1^1;

sbit fen = P1^2;

void feng()
{
    while(1)
    {
        delay1ms(10); 
        fen = ~fen;
    }
}



void JDQ()
{
        led = 0;
        jdq = 1;//´ò¿ª¼ÌµçÆ÷
}

void step()
{
        unsigned int i;
        while(1)
        {
            P1 = 0x01;
            for(i=0;i<510;i++);
            P1 = 0x02;
            for(i=0;i<510;i++);
            P1 = 0x04;
            for(i=0;i<510;i++);
            P1 = 0x08;
            for(i=0;i<510;i++);
        }
}
View Code

12864.h

#ifndef __12864_H__
#define _12864_H__

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char



/*extern uchar code table1[] ;
extern uchar code table2[] ;
extern uchar code table3[] ;
extern uchar code table4[] ;
extern uchar code table5[] ;*/


extern unsigned char code pic1[];
void delay1ms(uint x);//ÑÓʱº¯Êý
void write_cmd(uchar cmd);//дָÁÊý
void write_date(uchar date);//дÊý¾Ýº¯Êý

void init_12864();
void lcd_putpic(unsigned char code *img);//»­Í¼º¯Êý
void setpic();//É趨»­Í¼¹¦Äܵĺ¯Êý
void lcd_pos(uchar x,uchar y);//É趨ÏÔʾλÖõĺ¯Êý
void write_string(uchar *str);
#endif
View Code

baojin.h

#ifndef __BAOJIN_H__
#define __BAOJIN_H__

void feng();
void step();
void JDQ();//¼ÌµçÆ÷¿ªÆô¿ª¹Ø

#endif
View Code

至于复杂的版本呢,大家可以在12864.c上进行添加
注意这里还有4个警告,是因为有几个画图的函数我没有调用,为的是让大家能够在本程序的基础上进行添加

 

豪华版本:http://download.csdn.net/detail/generoius/6404009

版权所有,转载请注明链接地址http://www.cnblogs.com/fengdashen/p/3357651.html

 

posted @ 2013-10-15 18:23  跨七海的风  阅读(687)  评论(0编辑  收藏  举报