接龍動態密碼陣列外接版吾品公交
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit sda = P3^7;
sbit scl = P3^6;
sbit yybuzy = P2 ^ 3;
sbit yyxp_data = P2^ 4;
sbit yyxp_rest = P2 ^ 5;
#define DEV_ADDR 0x81
unsigned char ru[6];
unsigned char yan[3];
unsigned char Lin[10];
unsigned char N=1;
long int data_c=0,fn;
unsigned char Dao=1;
void usdelay()
{
_nop_();_nop_();
_nop_();_nop_();
_nop_();_nop_();
}
void
delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
} void yydalay(uint x) //簡單延
{
uint t;
while (x--)
{
for (t = 0; t < 125; t++)
;
}
}
void speak(uint z)
{
yyxp_rest = 1; // reset
yydalay(2);
yyxp_rest = 0;
yydalay(2);
while (z > 0)
{
yyxp_data = 1;
yydalay(1);
yyxp_data = 0;
yydalay(1);
z--;
}
}
void start() //起始訊號 當時鍾線為1,資料線有個下降沿
{
sda=1;
usdelay();
scl=1;
usdelay();
sda=0;
usdelay();
}
void stop()//終止訊號 當時鍾線為1,資料線有個上升沿
{
sda=0;
usdelay();
scl=1;
usdelay();
sda=1;
usdelay();
}
unsigned int keycheckdown(void)
{
short temp1,temp2,temp,a=0xff;
P1=0xf0; /* 輸入行值(或列值) */
/* 延時 */
temp1=P1; /* 讀列值(或行值) */
P1=0xff;
/* 延時 */
P1=0x0f; /* 輸入列值(或行值) */
/* 延時 */
temp2=P1; /* 讀行值(或列值) */
P1=0xff;
temp=(temp1&0xf0)|(temp2&0xf); /* 將兩次讀入資料組合 */
switch(temp) /* 通過讀入資料組合判斷按鍵位置 */
{
// case 0x77 :a=0x0d;break;// 按鍵/
// case 0x7b :a=0x0e; break;// 按鍵=
// case 0x7d :a=0; break;// 按鍵0
// case 0x7e :a=0x0f; break;// 按鍵CE
case 0xb7 :a=0x0c;break;// 按鍵*
case 0xbb :a=0x9;break; // 按鍵9
case 0xbd :a=0x8;break; // 按鍵8
case 0xbe :a=0x7;break; // 按鍵7
case 0xd7 :a=0;break;// 按鍵-
case 0xdb :a=0x6;break; // 按鍵6
case 0xdd :a=0x5;break; // 按鍵5
case 0xde :a=0x4;break; // 按鍵4
case 0xe7 :a=0x0e; break;// 按鍵+
case 0xeb :a=3;break; // 按鍵3
case 0xed :a=2;break; // 按鍵2
case 0xee :a=1;break; // 按鍵1
default :a=0xff;
}
return a;
}
void yin6(uchar f[6])
{ uint i;
// for(i=0;i<6;i++) { speak(f[i]+1); while(yybuzy==0);}
}
void ack() //應答訊號 由從機發出訊號為sda由1變為0
{
uchar i;
scl=1;
usdelay();
while((sda==1)
&&i<200)i++;
scl=0;
usdelay();
}
void init24c16()//24c16初始化
{
sda=1;
usdelay();
scl=1;
usdelay();
}
void
write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
scl=0;
usdelay();
sda=(bit)(dat&0x80);
usdelay();
scl=1;
usdelay();
dat<<=1;
}
scl=0;
usdelay();
}
uchar read_byte()
{
uchar i,k;
for(i=0;i<8;i++)
{
scl=1;
usdelay();
k=(k<<1)|sda;
scl=0;
usdelay();
}
return k;
}
void write_add(int add,uchar dat)
{ uchar
ye=0xa0;
if(add>255){ye=0xa2;}
if(add>510){ye=0xa4;}
if(add>765){ye=0xa6; }
if(add>1020){ye=0xa8;}
//if(add>1275){ye=0xaa; }
//if(add>1530){ye=0xac; }
//if(add>1785){ye=0xae; }
start();
write_byte(ye);
ack();
write_byte(add);
ack();
write_byte
(dat);
ack();
stop();
}
uchar read_add(int add)
{ uchar ye=0xa0;
uchar ye2=0xa1; uchar dat;
if(add>255){ye=0xa2; ye2=0xa3;}
if(add>510){ye=0xa4; ye2=0xa5;}
if(add>765){ye=0xa6; ye2=0xa7;}
if(add>1020){ye=0xa8; ye2=0xa9;}
if(add>1275){ye=0xaa; ye2=0xab;}
// if(add>1530){ye=0xac; ye2=0xad;}
// if(add>1785){ye=0xae; ye2=0xaf;}
start();
write_byte(ye);
ack();
write_byte(add);
ack();
start
();
write_byte(ye2);
ack();
dat=read_byte();
usdelay();
stop();
return dat;
}
void bao(x)
{ int t;}
void yanz(x)
{
//千萬種方式可以通過驗證碼鑑別獲取密碼.安全考慮,這裡省略.
}
void suan()
{
uint h2,h1,h3,x,x1,x2,i,t;
x1=read_add(10);x2=read_add(x1);
h1=read_add(0);h2=read_add(h1);
x=read_add(17)*100+read_add(18)*10+read_add(19);
for(i=0;i<6;i++){Lin[i]=read_add(20+x+x2+h2+i);}
for(i=0;i<6;){if(Lin[i]==ru[i]) i++;
else
{
speak(14);while(yybuzy==0);
if(Dao<10){ speak(Dao+1);while(yybuzy==0);}
else{ speak(2);while(yybuzy==0);
speak(Dao%10+1);while(yybuzy==0); }
speak(13);while(yybuzy==0);
for(t=0;t<6;t++)
{ru[t]=0; } break;
}
}
//yin6(Lin);
if(i==6)
{ // KEY=0;
speak(14);while(yybuzy==0);
if(Dao<10){ speak(Dao+1);while(yybuzy==0);}
else{ speak(2);while(yybuzy==0);
speak(Dao%10+1);while(yybuzy==0); }
speak(15);while(yybuzy==0);
for(t=0;t<6;t++) {ru[t]=0;} //開鎖後將輸入位清零
x=x+1;
if(x>145){
x1=x1+1;if(x1>6){x1=1;}write_add(10,x1);yydalay(50);
h1=h1+1;if(h1>6){h1=1;}write_add(0,h1);yydalay(50);
write_add(17,0);yydalay(50);
write_add(18,0);yydalay(50);
write_add(19,1);yydalay(50);
}else{
write_add(17,x%1000/100);yydalay(50);
write_add(18,x%100/10);yydalay(50);
write_add(19,x%10); yydalay(50);}
} else{ //N=0;
speak(16);
for(t=0;t<6;t++) { ru[t]=0;}
}
}
void main()
{
uchar key=0xff; //鍵值初始化
uint n=0;
init24c16();
// speak(16); while(yybuzy==0);
// kaiji();
while(1)
{
key=keycheckdown();
if(0xff!=key)
{
if(key<10)
{
if(N==1)
{
if(key==0){speak(11); } else{
speak(key+1); } }else
{speak(16); while(yybuzy==0);}
ru[n]=key;
if(n<5){n++;}else{n=0; suan();
}
}
else
{ switch(key) /*功能鍵選擇*/
{
// case 0xe:bao(n);n=0;break;
// case 0xc:yanz(n);n=0;break; /* * S=
/* = */
// case 0xf:n=0;m=0;x=0;bao();break; /* C*/
}
}
do{P1=0xf0;}while(P1!=0xf0); /*等待按鍵鬆開*/
}//(0xff!=key)
}//while
}//main