- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- extern uchar keyscan(); //扫描按键
- extern void write_c(uchar command); //lcd写命令
- extern void write_d(uchar date); //lcd写数据
- extern void Lcd_init(); //lcd初始化
- extern void setposition(uchar x,uchar y) ;
- extern void writebyte(uchar x,uchar y,uchar dat);
- extern void writenbyte(uchar x,uchar y,uchar *p);
- extern void WriteSet(uchar add, uchar dat); //数据写入24c08的具体地址
- extern unsigned char ReadSet(uchar set_addr); //从24c08的具体地址读出数据
- sbit beep=P2^0; //蜂鸣器
- sbit led=P3^0;
- bit takmo,right,set,input,flag;
- uchar i,j,use,temp,m,money,b,c;
- uchar code wel1[]={" welcome to the"};
- uchar code wel2[]={"ATM system"};
- uchar code new[]={"input new keys:"};
- uchar code yu_e[]={" yu e is: "};
- uchar code note1[]={"Input your key"};
- uchar code note2[]={"key:"};
- uchar code note3[]={"set:"};
- uchar code note4[]={"your key is "};
- uchar code note5[]={" what can I "};
- uchar code note6[]={" do for you "};
- uchar code note7[]={" check the key "};
- uchar code note8[]={"and try it again"};
- uchar code note9[]={"thanks use"};
- uchar code note10[]={" goodbye"};
- uchar code note11[]={"how much do you"};
- uchar code note12[]={"need:"};
- uchar code note13[]={" not enough"};
- uchar code note14[]={" processing "};
- uchar code note15[]={" please wait... "};
- uchar code note16[]={" your money is"};
- uchar code note17[]={"remember new key"};
- uchar code right0[]={" right!"};
- uchar code error0[]={" wrong!"};
- uchar code setname[]={" set ok!"};
- uchar code setname1[]={" set error!"};
- /******************************************************************
- 延时函数
- **********************************************************************/
-
- void delay(uint x)
- {
- uint y;
- for(;x!=0;x--)
- for(y=110;y!=0;y--);
- }
- void delay0(uchar x)
- { uchar i;
- while(x--)
- {for (i = 0; i<13; i++);}
- }
- /******************************************************************
- 声音函数
- **********************************************************************/
- void beepon()
- { uchar i;
- for (i=0;i<180;i++)
- {delay0(5);
- beep=!beep;
- }
- beep=1;
- delay(20);
- }
- /*******************************************************************
- 灯闪,代表操作成功
- ********************************************************************/
- void light()
- { uchar n;
- for(n=0;n<=2;n++)
- {
- led=0;
- delay(1000);
- led=1;
- delay(1000);
- }
- }
- /******************************************************************
- 显示余额函数
- **********************************************************************/
- showmoney()
- { if(money>use)
- {
- money-=use;
- use=0;
- write_c(0x01);
- writenbyte(0,0,note14);
- writenbyte(1,0,note15);
- delay(5000);
- write_c(0x01);
- writenbyte(0,0,yu_e);
- setposition(1,0);
- write_d(money+0x30);
- write_d(0+0x30);
- write_d(0+0x30);
- WriteSet(6,money);
- light();
- }
- else
- {
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,0,note16);
- writenbyte(1,0,note13);
- }
- }
- /*********************************************************************
- 比较函数:用来确定密码正误
- *********************************************************************/
- bit check(uchar p[],uchar q[])
- { uchar n,k=0;
- for(n=0;n<6;n++)
- { if(p[n]==q[n])
- k++;}
- if(k==6)
- return 1;
- else return 0;
- }
- /*******************************************************************
- 初始化函数
- ******************************************************************/
- void init()
- {
- takmo=0;right=0;set=0;input=1;flag=0;
- Lcd_init();
- setposition(0,0);
- for(i=0;wel1!='\0';i++) //开机界面
- {
- write_d(wel1);
- beepon();
- }
- setposition(1,3);
- for(i=0;wel2!='\0';i++)
- {write_d(wel2);
- beepon();
- }
- delay(2000);
- write_c(0x01);
- writenbyte(0,1,note1); //提示输入密码
- writenbyte(1,0,note2); //显示key
- write_c(0x0f); //光标闪烁
- setposition(1,4); //第二行第五个位置开始键入密码
- i=0;
- }
- /*********************************************************************
- 主函数:确定不同按键组合功能
- ***********************************************************************/
- void main()
- {
- uchar key_buff[6]; //输入缓冲
- uchar new_key[6] ; //密码设置缓冲区
- uchar original_key[6]; //密码读出缓冲区
- WriteSet(6,9);
- init();
- while(1)
- { for(b=0;b<6;b++) //24c08里面取出正确的密码
- {
- original_key=ReadSet(b);
- delay(5);
- }
- temp=keyscan(); //扫描键盘
- if(temp>=0&&temp<=9)
- { beepon();
- if(input)
- {
- key_buff[i++]=temp;
- write_d('*');
- }
- else if(set)
- {
- new_key[i++]=temp;
- write_d('*');
- }
- else if(takmo)
- {
- i++;
- write_d(temp+0x30);
- use=10*use+temp;
- }
- else{;}
- }
- else if(temp==10) //取卡
- {
- beepon();
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,3,note9);
- writenbyte(1,3,note10);
- delay(3000);
- init();
- }
-
- else if(temp==11) //取钱
- {
- if(right)
- {
- beepon();
- takmo=1;
- write_c(0x01);
- write_c(0x0f);
- writenbyte(0,0,note11);
- writenbyte(1,0,note12);
- }
- else{beepon();}
- }
- else if(temp==12) //设置新密码
- {
- if(right)
- {beepon();
- set=1;
- write_c(0x01);
- write_c(0x0f);
- writenbyte(0,0,new);
- setposition(1,0);
- }
- else{beepon();}
- }
- else if(temp==13)
- {
- if(right)
- {
- beepon();
- write_c(0x01);
- money=ReadSet(6);
- showmoney(); //显示余额
- }
- else{beepon();}
- }
- else if(temp==14) //确认
- {
- beepon();
- if(input)
- {
- if(i!=6)
- { i=0;
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,2,note4);
- writenbyte(1,4,error0);
- delay(3000);
- write_c(0x01);
- writenbyte(0,0,note7);
- writenbyte(1,0,note8);
- delay(3000);
- init();
- ;
- }
- else
- { flag=check(key_buff,original_key);
- if(flag==1)
- {
- i=0;
- right=1;
- input=0;
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,3,note4);
- writenbyte(1,4,right0);
- delay(3000);
- write_c(0x01);
- writenbyte(0,1,note5);
- writenbyte(1,0,note6);
- }
- else
- {
- i=0;
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,2,note4);
- writenbyte(1,4,error0);
- delay(3000);
- write_c(0x01);
- writenbyte(0,0,note7);
- writenbyte(1,0,note8);
- delay(3000);
- init();
- ;
- }
- }
- }
-
- else if(set)
- { if(i==6)
- {
- set=0;
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,0,setname);
- writenbyte(1,0,note17);
- for(c=0;c<6;c++)
- {
- WriteSet(c,new_key[c]);
- delay(100);
- }
- c=0;
- }
- else
- {
- set=0;
- i=0;
- write_c(0x01);
- write_c(0x0c);
- writenbyte(0,0,setname1);
- }
- }
- else if(takmo)
- {
- money=ReadSet(6);
- takmo=0;
- showmoney();
- }
- else
- {beepon();}
- }
- else if(temp==15) //清除
- {
- beepon();
- if(input&&i>=1)
- {
- writebyte(1,4+(--i),' ');
- setposition(1,4+i);
- }
- else if(set&&i>=1)
- {
- writebyte(1,0+(--i),' ');
- setposition(1,0+i);
- }
- else if(takmo&&i>=1)
- {
- writebyte(1,5+(--i),' ');
- setposition(1,5+i);
- }
- else
- {;}
- }
- else {;}
- }
- }
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- void delayms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*********************************************************************
- * 函 数 名: keyscan
- * 功能描述: 键盘扫描返回值
- ***********************************************************************/
- uchar keyscan()
- { uchar num=16,temp;
- P1=0xfe;
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P1;
- switch(temp)
- {
- case 0xee:num=0;
- break;
- case 0xde:num=1;
- break;
- case 0xbe:num=2;
- break;
- case 0x7e:num=3;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P1;
- temp=temp&0xf0;
- }
- }
- }
- P1=0xfd;
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P1;
- switch(temp)
- {
- case 0xed:num=4;
- break;
- case 0xdd:num=5;
- break;
- case 0xbd:num=6;
- break;
- case 0x7d:num=7;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P1;
- temp=temp&0xf0;
- }
- }
- }
- P1=0xfb;
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P1;
- switch(temp)
- {
- case 0xeb:num=8;
- break;
- case 0xdb:num=9;
- break;
- case 0xbb:num=10;
- break;
- case 0x7b:num=11;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P1;
- temp=temp&0xf0;
- }
- }
- }
- P1=0xf7;
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P1;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P1;
- switch(temp)
- {
- case 0xe7:num=12;
- break;
- case 0xd7:num=13;
- break;
- case 0xb7:num=14;
- break;
- case 0x77:num=15;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P1;
- temp=temp&0xf0;
- }
- }
- }
- return num;
- }
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit RS=P2^5;
- sbit RW=P2^6;
- sbit EN=P2^7;
- void delaymms()
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- for(j=0;j<33;j++)
- ;
- }
- void delays(uint n)
- {
- uint i;
- for(i=0;i<n;i++)
- delaymms();
- }
- void write_c( uchar x )
- {
- RS=0;
- RW=0;
- EN=0;
- P0=x;
- delays(5);
- EN=1;
- delays(5);
- EN=0;
- }
- void write_d( uchar x )
- {
- RS=1;
- RW=0;
- EN=0;
- P0=x;
- delays(5);
- EN=1;
- delays(5);
- EN=0;
- }
- void setposition(uchar x,uchar y)
- {
- if(x==0)
- write_c( 0x80+y );
- else
- write_c( 0xc0+y );
- }
- void writebyte(uchar x,uchar y,uchar dat)
- {
- setposition( x,y);
- write_d( dat );
- }
- void writenbyte(uchar x,uchar y,uchar *p)
- {
- setposition( x,y);
- while(*p)
- {
- write_d( *p++ );
- }
- }
- void Lcd_init(void)
- {
- write_c( 0x01 );
- write_c( 0x38 );
- write_c( 0x0c );
- write_c( 0x06 );
- }
- #include <reg52.h> // 包含51单片机寄存器定义的头文件
- #include <intrins.h> //包含_nop_()函数定义的头文件
- #define OP_READ 0xa1 // 器件地址以及读取操作,0xa1即为1010 0001B
- #define OP_WRITE 0xa0 // 器件地址以及写入操作,0xa1即为1010 0000B
- sbit SCL=P3^5; //将串行时钟总线SCL位定义在为P3.4引脚
- sbit SDA=P3^6; //将串行数据总线SDA位定义在为P3.5引脚
- /*****************************************************
- 函数功能:延时1ms
- ***************************************************/
- void delay1ms()
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- for(j=0;j<33;j++)
- ;
- }
- /*****************************************************
- 函数功能:延时若干毫秒
- 入口参数:n
- ***************************************************/
- void delaynms(unsigned char n)
- {
- unsigned char i;
- for(i=0;i<n;i++)
- delay1ms();
- }
- /***************************************************
- 函数功能:开始数据传送
- ***************************************************/
- void start()
- // 开始位
- {
- SDA = 1; //SDA初始化为高电平“1”
- SCL = 1; //开始数据传送时,要求SCL为高电平“1”
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- SDA = 0; //SDA的下降沿被认为是开始信号
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- SCL = 0; //SCL为低电平时,SDA上数据才允许变化(即允许以后的数据传递)
- }
- /***************************************************
- 函数功能:结束数据传送
- ***************************************************/
- void stop()// 停止位
- {
- SDA = 0; //SDA初始化为低电平“0” _n
- SCL = 1; //结束数据传送时,要求SCL为高电平“1”
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- SDA = 1; //SDA的上升沿被认为是结束信号
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- SDA=0;
- SCL=0;
- }
- /***************************************************
- 函数功能:检测应答位
- ***************************************************/
- bit Ask() //检测应答
- {
- bit ack_bit; //储存应答位
- SDA = 1; // 发送设备(主机)应在时钟脉冲的高电平期间(SCL=1)释放SDA线,
- //以让SDA线转由接收设备(AT24Cxx)控制
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- SCL = 1; //根据上述规定,SCL应为高电平
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
- ack_bit = SDA; //接受设备(AT24Cxx)向SDA送低电平,表示已经接收到一个字节
- //若送高电平,表示没有接收到,传送异常 结束发送
- SCL = 0; //SCL为低电平时,SDA上数据才允许变化(即允许以后的数据传递)
- return ack_bit; // 返回AT24Cxx应答位
- }
- /***************************************************
- 函数功能:从AT24Cxx读取数据
- 出口参数:x
- ***************************************************/
- unsigned char ReadData()
- // 从AT24Cxx移入数据到MCU
- {
- unsigned char i;
- unsigned char x; //储存从AT24Cxx中读出的数据
- for(i = 0; i < 8; i++)
- {
- SCL = 1; //SCL置为高电平
- x<<=1; //将x中的各二进位向左移一位
- x|=(unsigned char)SDA; //将SDA上的数据通过按位“或“运算存入x中
- SCL = 0; //在SCL的下降沿读出数据
- }
- return(x); //将读取的数据返回
- }
- /***************************************************
- 函数功能:向AT24Cxx的当前地址写入数据
- 入口参数:y (储存待写入的数据)
- ***************************************************/
- //在调用此数据写入函数前需首先调用开始函数start(),所以SCL=0
- void WriteCurrent(unsigned char y)
- {
- unsigned char i;
- for(i = 0; i < 8; i++) // 循环移入8个位
- {
- SDA = (bit)(y&0x80); //通过按位“与”运算将最高位数据送到S
- //因为传送时高位在前,低位在后
- _nop_(); //等待一个机器周期
- SCL = 1; //在SCL的上升沿将数据写入AT24Cxx
- _nop_(); //等待一个机器周期
- _nop_(); //等待一个机器周期
-
- SCL = 0; //将SCL重新置为低电平,以在SCL线形成传送数据所需的8个脉冲
- y <<= 1; //将y中的各二进位向左移一位
-
- }
- }
- /***************************************************
- 函数功能:向AT24Cxx中的指定地址写入数据
- 入口参数:add (储存指定的地址);dat(储存待写入的数据)
- ***************************************************/
- void WriteSet(unsigned char add, unsigned char dat)
- // 在指定地址addr处写入数据WriteCurrent
- {
- start(); //开始数据传递
- WriteCurrent(OP_WRITE); //选择要操作的AT24Cxx芯片,并告知要对 其写入数据
- Ask();
- WriteCurrent(add); //写入指定地址
- Ask();
- WriteCurrent(dat); //向当前地址(上面指定的地址)写入数据
- Ask();
- stop(); //停止数据传递
- delaynms(5); //1个字节的写入周期为1ms, 最好延时1ms以上
- }
- /***************************************************
- 函数功能:从AT24Cxx中的当前地址读取数据
- 出口参数:x (储存读出的数据)
- ***************************************************/
- unsigned char ReadCurrent()
- {
- unsigned char x;
- start(); //开始数据传递
- WriteCurrent(OP_READ); //选择要操作的AT24Cxx芯片,并告知要读其数据
- Ask();
- x=ReadData(); //将读取的数据存入x
- stop(); //停止数据传递
- return x; //返回读取的数据
- }
- /***************************************************
- 函数功能:从AT24Cxx中的指定地址读取数据
- 入口参数:set_addr
- 出口参数:x
- ***************************************************/
- unsigned char ReadSet(unsigned char set_addr)
- // 在指定地址读取
- {
- start(); //开始数据传递
- WriteCurrent(OP_WRITE); //选择要操作的AT24Cxx芯片,并告知要对其写入数据
- Ask();
- WriteCurrent(set_addr); //写入指定地址
- Ask();
- return(ReadCurrent()); //从指定地址读出数据并返回
- }
复制代码
|