|
- #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寫數(shù)據(jù)
- 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); //數(shù)據(jù)寫入24c08的具體地址
- extern unsigned char ReadSet(uchar set_addr); //從24c08的具體地址讀出數(shù)據(jù)
- 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!"};
- /******************************************************************
- 延時(shí)函數(shù)
- **********************************************************************/
-
- 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++);}
- }
- /******************************************************************
- 聲音函數(shù)
- **********************************************************************/
- 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);
- }
- }
- /******************************************************************
- 顯示余額函數(shù)
- **********************************************************************/
- 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);
- }
- }
- /*********************************************************************
- 比較函數(shù):用來(lái)確定密碼正誤
- *********************************************************************/
- 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;
- }
- /*******************************************************************
- 初始化函數(shù)
- ******************************************************************/
- void init()
- {
- takmo=0;right=0;set=0;input=1;flag=0;
- Lcd_init();
- setposition(0,0);
- for(i=0;wel1!='\0';i++) //開(kāi)機(jī)界面
- {
- 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); //光標(biāo)閃爍
- setposition(1,4); //第二行第五個(gè)位置開(kāi)始鍵入密碼
- i=0;
- }
- /*********************************************************************
- 主函數(shù):確定不同按鍵組合功能
- ***********************************************************************/
- void main()
- {
- uchar key_buff[6]; //輸入緩沖
- uchar new_key[6] ; //密碼設(shè)置緩沖區(qū)
- uchar original_key[6]; //密碼讀出緩沖區(qū)
- 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) //設(shè)置新密碼
- {
- 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) //確認(rèn)
- {
- 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--);
- }
- /*********************************************************************
- * 函 數(shù) 名: 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單片機(jī)寄存器定義的頭文件
- #include <intrins.h> //包含_nop_()函數(shù)定義的頭文件
- #define OP_READ 0xa1 // 器件地址以及讀取操作,0xa1即為1010 0001B
- #define OP_WRITE 0xa0 // 器件地址以及寫入操作,0xa1即為1010 0000B
- sbit SCL=P3^5; //將串行時(shí)鐘總線SCL位定義在為P3.4引腳
- sbit SDA=P3^6; //將串行數(shù)據(jù)總線SDA位定義在為P3.5引腳
- /*****************************************************
- 函數(shù)功能:延時(shí)1ms
- ***************************************************/
- void delay1ms()
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- for(j=0;j<33;j++)
- ;
- }
- /*****************************************************
- 函數(shù)功能:延時(shí)若干毫秒
- 入口參數(shù):n
- ***************************************************/
- void delaynms(unsigned char n)
- {
- unsigned char i;
- for(i=0;i<n;i++)
- delay1ms();
- }
- /***************************************************
- 函數(shù)功能:開(kāi)始數(shù)據(jù)傳送
- ***************************************************/
- void start()
- // 開(kāi)始位
- {
- SDA = 1; //SDA初始化為高電平“1”
- SCL = 1; //開(kāi)始數(shù)據(jù)傳送時(shí),要求SCL為高電平“1”
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- SDA = 0; //SDA的下降沿被認(rèn)為是開(kāi)始信號(hào)
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- SCL = 0; //SCL為低電平時(shí),SDA上數(shù)據(jù)才允許變化(即允許以后的數(shù)據(jù)傳遞)
- }
- /***************************************************
- 函數(shù)功能:結(jié)束數(shù)據(jù)傳送
- ***************************************************/
- void stop()// 停止位
- {
- SDA = 0; //SDA初始化為低電平“0” _n
- SCL = 1; //結(jié)束數(shù)據(jù)傳送時(shí),要求SCL為高電平“1”
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- SDA = 1; //SDA的上升沿被認(rèn)為是結(jié)束信號(hào)
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- SDA=0;
- SCL=0;
- }
- /***************************************************
- 函數(shù)功能:檢測(cè)應(yīng)答位
- ***************************************************/
- bit Ask() //檢測(cè)應(yīng)答
- {
- bit ack_bit; //儲(chǔ)存應(yīng)答位
- SDA = 1; // 發(fā)送設(shè)備(主機(jī))應(yīng)在時(shí)鐘脈沖的高電平期間(SCL=1)釋放SDA線,
- //以讓SDA線轉(zhuǎn)由接收設(shè)備(AT24Cxx)控制
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- SCL = 1; //根據(jù)上述規(guī)定,SCL應(yīng)為高電平
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
- ack_bit = SDA; //接受設(shè)備(AT24Cxx)向SDA送低電平,表示已經(jīng)接收到一個(gè)字節(jié)
- //若送高電平,表示沒(méi)有接收到,傳送異常 結(jié)束發(fā)送
- SCL = 0; //SCL為低電平時(shí),SDA上數(shù)據(jù)才允許變化(即允許以后的數(shù)據(jù)傳遞)
- return ack_bit; // 返回AT24Cxx應(yīng)答位
- }
- /***************************************************
- 函數(shù)功能:從AT24Cxx讀取數(shù)據(jù)
- 出口參數(shù):x
- ***************************************************/
- unsigned char ReadData()
- // 從AT24Cxx移入數(shù)據(jù)到MCU
- {
- unsigned char i;
- unsigned char x; //儲(chǔ)存從AT24Cxx中讀出的數(shù)據(jù)
- for(i = 0; i < 8; i++)
- {
- SCL = 1; //SCL置為高電平
- x<<=1; //將x中的各二進(jìn)位向左移一位
- x|=(unsigned char)SDA; //將SDA上的數(shù)據(jù)通過(guò)按位“或“運(yùn)算存入x中
- SCL = 0; //在SCL的下降沿讀出數(shù)據(jù)
- }
- return(x); //將讀取的數(shù)據(jù)返回
- }
- /***************************************************
- 函數(shù)功能:向AT24Cxx的當(dāng)前地址寫入數(shù)據(jù)
- 入口參數(shù):y (儲(chǔ)存待寫入的數(shù)據(jù))
- ***************************************************/
- //在調(diào)用此數(shù)據(jù)寫入函數(shù)前需首先調(diào)用開(kāi)始函數(shù)start(),所以SCL=0
- void WriteCurrent(unsigned char y)
- {
- unsigned char i;
- for(i = 0; i < 8; i++) // 循環(huán)移入8個(gè)位
- {
- SDA = (bit)(y&0x80); //通過(guò)按位“與”運(yùn)算將最高位數(shù)據(jù)送到S
- //因?yàn)閭魉蜁r(shí)高位在前,低位在后
- _nop_(); //等待一個(gè)機(jī)器周期
- SCL = 1; //在SCL的上升沿將數(shù)據(jù)寫入AT24Cxx
- _nop_(); //等待一個(gè)機(jī)器周期
- _nop_(); //等待一個(gè)機(jī)器周期
-
- SCL = 0; //將SCL重新置為低電平,以在SCL線形成傳送數(shù)據(jù)所需的8?jìng)(gè)脈沖
- y <<= 1; //將y中的各二進(jìn)位向左移一位
-
- }
- }
- /***************************************************
- 函數(shù)功能:向AT24Cxx中的指定地址寫入數(shù)據(jù)
- 入口參數(shù):add (儲(chǔ)存指定的地址);dat(儲(chǔ)存待寫入的數(shù)據(jù))
- ***************************************************/
- void WriteSet(unsigned char add, unsigned char dat)
- // 在指定地址addr處寫入數(shù)據(jù)WriteCurrent
- {
- start(); //開(kāi)始數(shù)據(jù)傳遞
- WriteCurrent(OP_WRITE); //選擇要操作的AT24Cxx芯片,并告知要對(duì) 其寫入數(shù)據(jù)
- Ask();
- WriteCurrent(add); //寫入指定地址
- Ask();
- WriteCurrent(dat); //向當(dāng)前地址(上面指定的地址)寫入數(shù)據(jù)
- Ask();
- stop(); //停止數(shù)據(jù)傳遞
- delaynms(5); //1個(gè)字節(jié)的寫入周期為1ms, 最好延時(shí)1ms以上
- }
- /***************************************************
- 函數(shù)功能:從AT24Cxx中的當(dāng)前地址讀取數(shù)據(jù)
- 出口參數(shù):x (儲(chǔ)存讀出的數(shù)據(jù))
- ***************************************************/
- unsigned char ReadCurrent()
- {
- unsigned char x;
- start(); //開(kāi)始數(shù)據(jù)傳遞
- WriteCurrent(OP_READ); //選擇要操作的AT24Cxx芯片,并告知要讀其數(shù)據(jù)
- Ask();
- x=ReadData(); //將讀取的數(shù)據(jù)存入x
- stop(); //停止數(shù)據(jù)傳遞
- return x; //返回讀取的數(shù)據(jù)
- }
- /***************************************************
- 函數(shù)功能:從AT24Cxx中的指定地址讀取數(shù)據(jù)
- 入口參數(shù):set_addr
- 出口參數(shù):x
- ***************************************************/
- unsigned char ReadSet(unsigned char set_addr)
- // 在指定地址讀取
- {
- start(); //開(kāi)始數(shù)據(jù)傳遞
- WriteCurrent(OP_WRITE); //選擇要操作的AT24Cxx芯片,并告知要對(duì)其寫入數(shù)據(jù)
- Ask();
- WriteCurrent(set_addr); //寫入指定地址
- Ask();
- return(ReadCurrent()); //從指定地址讀出數(shù)據(jù)并返回
- }
復(fù)制代碼
|
|