/*-----------------------------------------------
论坛:www.doflye
编写:shifang
日期:2009.5
修改:无
内容:第一行显示固定密码,第二行显示输入密码,密码输入完毕后会有正确或者错误提示,
为了演示方便,提示密码,输入密码部分也没有用星号代替。可以自行更换成星号
------------------------------------------------*/
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#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 P0
#define KeyPort P3
#define _Nop() _nop_() //定义空指令
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
//sbit s4=P1^3;
uchar count,s1num;
char shi,fen,miao;
uchar code table[]=" 2010-7-30 MON";
uchar code table1[]=" 00:00:00";
sbit sda=P2^1;
sbit scl=P2^0;
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
sbit spk=P2^7;
unsigned char code password[8]={1,2,3,4,5,6,7,8};//可以更改此密码做多组测试
void start(void);
void stop(void);
void ack(void);
void noack(void);
void iicwr_byte(uchar dat);
uchar iicre_byte(void);
void delay (void);
void init(void);
void delay1(void);
void write_byte(uchar add,uchar dat);
uchar read_byte(uchar add);
void main1(uchar shuju);
/*------------------------------------------------
------------------------------------------------*/
unsigned char KeyScan(void); //键盘扫描函数,使用行列反转扫描法
/
*------------------------------------------------
按键值处理函数,返回扫键值
------------------------------------------------*/
unsigned char KeyPro(void);
bit LCD_Check_Busy(void) ;
void LCD_Write_Com(unsigned char com) ;
void LCD_Write_Data(unsigned char Data);
void LCD_Clear(void) ;
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) ;
void delay2(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void init1()//初始化液晶时期初始化
{
ucha
r num;
EN=0;
//miao=54;
//fen=59;
//shi=23;
LCD_Write_Com(0x38);//显示设置
LCD_Write_Com(0x0c);//不显示光标
LCD_Write_Com(0x06);//指针+1
LCD_Write_Com(0x01);//刷新
delay2(1);
LCD_Write_Com(0x80);
for(num=0;num<15;num++)//装数据
{
LCD_Write_Data(table[num]);
delay2(2);
}
LCD_Write_Com(0x80+0x40);//换行
for(num=0;num<12;num++)
{
LCD_Write_Data(table1[num]);
delay2(2);
}
TMOD=0x01;//timer1
TH0=(65536-50000)/256;//12M晶振为1ms
TL0=(65536-50000)%256;
EA=1;//open tatal interrupt
ET0=1;//open zi interrupt
TR0=1;//open timer
}
void write_sfm(uchar add,uchar date)//时间子程序
{
uchar shi,ge;
shi=date/10;
ge=date%10;
LCD_Write_Com(0x80+0x40+add);
LCD_Write_Data(0x30+shi);
LCD_Write_Data(0x30+ge);
}
void keyscan1() //按键子程序
愿你三冬暖歌曲原唱{ //扫描
if(s1==0)
{
delay2(5); //去抖
if(s1==0)
{
s1num++;
while(!s1); //松手检测
if(s1num==1)
{
TR0=0;//TIME STOP
LCD_Write_Com(0x80+0x40+10);//指针到miao
LCD_Write_Com(0x0f);//显示光标
}
if(s1num==2)
{
LCD_Write_Com(0x80+0x40+7);
}
if(s1num==3)
{
LCD_Write_Com(0x80+0x40+4);
}
if(s1num==4)
{
s1num=0;
LCD_Write_Com(0x0c);//去光标
TR0=1;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay2(5);
if(s2==0)
{
while(!s2); //check 松手
if(s1num==1) //sure at miao
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
LCD_Write_Com(0x80+0x40+10);
}
if(s1num==2) //at fen
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
LCD_Write_Com(0x80+0x40+7);
}
if(s1num==3) //at shi
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
LCD_Write_Com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay2(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
LCD_Write_Com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
LCD_Write_Com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
LCD_Write_Com(0x80+0x40+4);
}
}
}
}
}
void timer0() interrupt 1 //定时器中断子函数
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)//20=1s
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,mia
o);
}
}/*以上为时间程序*/
unsigned char KeyScan(void) //键盘扫描函数,使用行列反转扫描法
{
非主流网名超拽unsigned char cord_h,cord_l;//行列值中间变量
KeyPort=0x0f; //行线输出全为0
cord_h=KeyPort&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
DelayMs(10); //去抖
if((KeyPort&0x0f)!=0x0f)
{
cord_h=KeyPort&0x0f; //读入列线值
KeyPort=cord_h|0xf0; //输出当前列线值
cord_l=KeyPort&0xf0; //读入行线值
while((KeyPort&0xf0)!=0xf0);//等待松开并输出
return(cord_h+cord_l);//键盘最后组合码值
}
}return(0xff); //返回该值
}
/*------------------------------------------------
按键值处理函数,返回扫键值
------------------------------------------------*/
unsigned char KeyPro(void)
{
switch(KeyScan())
{
case 0x7e:return 0;break;//0 按下相应的键显示相对应的码值
case 0x7d:return 1;break;//1
case 0x7b:return 2;break;//2
case 0x77:return 3;break;//3
case 0xbe:return 4;break;//4
case 0xbd:return 5;break;//5
case 0xbb:return 6;break;//6
case 0xb7:return 7;break;//7
case 0xde:return 8;break;//8
case 0xdd:return 9;break;//9
case 0xdb:return 10;break;//a
case 0xd7:return 11;break;//b
case 0xee:return 12;break;//c
case 0xed:return 13;break;//d
case 0xeb:return 14;break;//e
case 0xe7:return 15;break;//f
default:return 0xff;break;
}
}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
return 0;
}
/*------------------------------------------------
写入命令函数
-
-----------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //忙则等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/
*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙则等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
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); /*显示模式设置*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
void beepon() /*蜂鸣器声音*/
{ int i;
for(i=0;i<300;i++)
{
DelayUs2x(50);
spk=!spk;
}
}
/*------------------------------------------------
主程序
------------------------------------------------*/
main()
{
unsigned char num,i,j,k,l,a=0,p=0x31,c=0x31;
unsigned char temp[8];
bit Flag;
LCD_Init(); //初始化液晶屏
DelayMs(10); //延时用于稳定,可以去掉
LCD_Clear(); //清屏
LCD_Write_String(0,0," Your Password: "); //写入第一行信息,主循环中不再更改此信息,所以在while之前写入
LCD_Write_String(0,1,"Input Password!"); //写入第二行信息,提示输入密码
while (1) //主循环
{
num=KeyPro(); //扫描键盘
if(num!=0xff) //如果扫描是按键有效值则进行处理
{
if(i==0) //输入是第一个字符的时候需要把改行清空,方便观看密码
LCD_Write_String(0,1," ");//清除该行
if(i<8) //密码是8位,大于8位时不再输入按键值
{
temp[i]=num;
LCD_Write_Char(i,1,num+'0');
}
i++; //输入数值累加
beepon();
if(i==9)//8位后的按键不输入数值,相当于确认按键(任意按键即可)
{
i=0; //计数器复位
Flag=1;//先把比较位置1
for(j=0;j<8;j++)//循环比较8个数值,如果有一个不等 则最终Flag值为0
Flag=Flag&&(temp[j]==password[j]);//比较输入值和已有密码
if(Flag)//如果比较全部相同,标志位置1
{
for(k=0;k<100;k++)
{
LCD_Write_String(0,0,"Congratulation!");//密码正确显示的信息
LCD_Write_String(0,1,"Right code!");//密码正确显示的信息
DelayMs(10);
}
for(k=0;k<10;k++)
{
LCD_Write_String(0,1,"Welcome To The New World!");
DelayMs(1);
}
for(k=0;k<100;k++)
{LCD_Write_Com(0x18);
DelayMs(1000);
}
init1();
while(1)
{
//初始化
keyscan1();
}
}
else
佟欣个人资料{
封门诡影LCD_Write_String(0,1,"Wrong Retry!")
;//密码错误,提示重新输入
}
}
}
}}
///////////////////////////////////
/*void main1(uchar shuju)
{
write_byte(7,shuju);
shuju=read_byte(7);
while(1);
}*/
void delay1(void)
{
uint a=30000;
while(a--);
}
小麦的价格void delay (void)
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void start(void)
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop(void)
{
宋仲基爸爸发声sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void ack(void)
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<200))i++;
scl=0;
delay();
}
void noack(void)
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void init(void)
{
sda=1;
scl=1;
}
void iicwr_byte(uchar dat)
{
uchar i;
scl=0;
for(i=0;i<8;i++)
{
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat=dat<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
}
uchar iicre_byte(void)
{
uchar i;
uchar dat;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
dat=dat<<1;
if(sda)
{
dat++;//
}
scl=0;
delay();
}
return dat;
}
void write_byte(uchar add,uchar dat)
{
init();
start();
iicwr_byte(0xa0);
ack();
iicwr_byte(add);
ack();
iicwr_byte(dat);
ack();
stop();
}
uchar read_byte(uchar add)
{
uchar a;
init();
start();
iicwr_byte(0xa0);
ack();
iicwr_byte(add);
ack();
start();
iicwr_byte(0xa1);
ack();
a=iicre_byte();
noack();
stop();
return a;
}
发布评论