吾品公交 動態接龍密碼原始碼開原
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
typedef uchar BYTE;
typedef uint WORD;
#include "intrins.h"
#define ulong unsigned long
sbit SDA=P1^0;
sbit SCL=P1^1;
#define DEV_ADDR 0x81
sbit yybuzy = P1 ^ 3;
sbit yyxp_data = P1^ 4;
sbit yyxp_rest = P1 ^ 5;
//long int data_a,data_b;
long int data_c,fn;
sbit KEY1 = P3^0; //開鎖
sbit KEY2 = P3^1;
sbit KEY3 = P3^2; //開鎖
sbit KEY4 = P3^3;
sbit KEY5 = P3^6; //開鎖
sbit KEY6 = P3^7;
unsigned char Ru[6];
unsigned char yan[3];
unsigned int ReadVal16Bit(void);
unsigned char Jielong[35];
unsigned char Lin[10];
unsigned char N=0;
//unsigned char code adminpassword[6]={1,3,1,4,2,0};
unsigned char Dao=1;
sfr IAP_DATA = 0xC2; //IAP資料暫存器
sfr IAP_ADDRH = 0xC3; //IAP地址暫存器高位元組
sfr IAP_ADDRL = 0xC4; //IAP地址暫存器低位元組
sfr IAP_CMD = 0xC5; //IAP命令暫存器
sfr IAP_TRIG = 0xC6; //IAP命令觸發暫存器
sfr IAP_CONTR = 0xC7; //IAP控制暫存器
#define CMD_IDLE 0 //空閒模式
#define CMD_READ 1 //IAP位元組讀命令
#define CMD_PROGRAM 2 //IAP位元組程式設計命令
#define CMD_ERASE 3 //IAP扇區擦除命令
#define ENABLE_IAP 0x82 //if SYSCLK<20MHz
#define IAP_ADDRESS 0x0400//測試地址
//#define IAP_ADDRESS_A 0x800//4 6 8 A00 C00 E00 1000 1200 1800 1A00
void mDelay(uint t) //延時
{
uchar i;
while(t--)
{
for(i=0;i<125;i++)
{;}
}
}
void Nop(void) //空操作
{
// _nop_(); //僅作延時用一條語句大約1us
// _nop_();
// _nop_();
// _nop_();
}
/***************************延時函式*************************/
void Delay(BYTE n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
/*----------------------------
關閉IAP
----------------------------*/
void IapIdle()
{
IAP_CONTR = 0; //關閉IAP功能
IAP_CMD = 0; //清除命令暫存器
IAP_TRIG = 0; //清除觸發暫存器
IAP_ADDRH = 0x80; //將地址設定到非IAP區域
IAP_ADDRL = 0;
}
/*----------------------------
從ISP/IAP/EEPROM區域讀取一位元組
----------------------------*/
BYTE IapReadByte(WORD addr)
{
BYTE dat; //資料緩衝區
IAP_CONTR = ENABLE_IAP; //使能IAP
IAP_CMD = CMD_READ; //設定IAP命令
IAP_ADDRL = addr; //設定IAP低地址
IAP_ADDRH = addr >> 8; //設定IAP高地址
IAP_TRIG = 0x5a; //寫觸發命令(0x5a)
IAP_TRIG = 0xa5; //寫觸發命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
dat = IAP_DATA; //讀ISP/IAP/EEPROM資料
IapIdle(); //關閉IAP功能
return dat; //返回
}
/*----------------------------
寫一位元組資料到ISP/IAP/EEPROM區域
----------------------------*/
void IapProgramByte(WORD addr, BYTE dat)
{
IAP_CONTR = ENABLE_IAP; //使能IAP
IAP_CMD = CMD_PROGRAM; //設定IAP命令
IAP_ADDRL = addr; //設定IAP低地址
IAP_ADDRH = addr >> 8; //設定IAP高地址
IAP_DATA = dat; //寫ISP/IAP/EEPROM資料
IAP_TRIG = 0x5a; //寫觸發命令(0x5a)
IAP_TRIG = 0xa5; //寫觸發命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
IapIdle();
}
/*----------------------------
扇區擦除
----------------------------*/
void IapEraseSector(WORD addr)
{
IAP_CONTR = ENABLE_IAP; //使能IAP
IAP_CMD = CMD_ERASE; //設定IAP命令
IAP_ADDRL = addr; //設定IAP低地址
IAP_ADDRH = addr >> 8; //設定IAP高地址
IAP_TRIG = 0x5a; //寫觸發命令(0x5a)
IAP_TRIG = 0xa5; //寫觸發命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
IapIdle();
}
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 yin6(uchar f[6])
{ uint i;
for(i=0;i<6;i++) { if(f[i]==0){speak(11); while(yybuzy==0);}else{
speak(f[i]+1); while(yybuzy==0); }}
}void yin10(uchar f[10])
{ uint i;
for(i=0;i<10;i++) { speak(f[i]+1); while(yybuzy==0);}
}
unsigned int ReadVal16Bit(void)
{
unsigned char bitnum,temp,addr;
unsigned int key2byte;
bit bit_temp;
addr=DEV_ADDR; //晶片的器件地址,需根據不同晶片改成對應值。
// key2byte=0xffff; //用來儲存讀到的資料。預設全1.(9通道、12通道的資料為16bit)
key2byte=26;
SDA=0;
EA=0; //為演示方便,關閉其它中斷。實際使用中需要合理安排相應的中斷優先順序。
//P_FAILED = 0; //清除錯誤指示。
for(temp=0;temp<2;temp++){} //延遲
for(bitnum=0;bitnum<8;bitnum++) //傳送器件地址
{
temp=addr&0x80; //取地址的最高位
SCL=0; //SCL=0時SDA方可改變
if(temp==0x80) //根據temp的最高位值決定SDA傳送1還是0
SDA=1;
else
SDA=0;
addr=addr<<1; //地址移位,下一次迴圈依然傳送的是地址最高位
// for(temp=0;temp<1;temp++){} //delay ,根據實際決定是否需要這些延遲
SCL=1; //SCL 時序,參考I2C協議。
// for(temp=0;temp<2;temp++){} //delay
}
SDA=1; //傳送完地址後釋放SDA匯流排,等待晶片的ACK迴應。
//read ack
SCL=0; //SCL 時序,
for(temp=0;temp<2;temp++){} //delay
SCL=1; //SCL拉高,讀入SDA資料。
for(temp=0;temp<10;temp++){} //delay
bit_temp=SDA;
if(bit_temp)
{
// P_FAILED=1; //晶片沒有ACK,出錯。
}
//read key
for(bitnum=0;bitnum<16;bitnum++)
{
SCL=0;
for(temp=0;temp<10;temp++){} //delay
SCL=1;
// for(temp=0;temp<1;temp++){} //delay
bit_temp=SDA;
if(bit_temp)
{
key2byte=key2byte<<1; //移位並在最低位儲存讀到的資料1
key2byte=key2byte|0x0001;
}
else
{
key2byte=key2byte<<1; //移位並在最低位儲存讀到的資料0
}
}
SCL=0;
for(temp=0;temp<10;temp++){}
SDA=1; //SDA=1,準備傳送NACK訊號
for(temp=0;temp<10;temp++){}
SCL=1;
for(temp=0;temp<10;temp++){} //傳送NACK訊號
SCL=0;
for(temp=0;temp<10;temp++){} //
SDA=0;
for(temp=0;temp<10;temp++){} //
SCL=1;
for(temp=0;temp<10;temp++){} //delay
SDA=1; //傳送STOP訊號
EA=1;
// return(key2byte);
switch(key2byte)
{
case 0x7fff:return(1);break;
case 0xbfff:return(2);break;
case 0xdfff:return(3);break;
case 0xefff:return(4);break;
case 0xf7ff:return(5);break;
case 0xfbff:return(6);break;
case 0xfdff:return(7);break;
case 0xfeff:return(8);break;
case 0xff7f:return(9);break;
case 0xffbf:return(10);break;
case 0xffdf:return(11);break;
case 0xffef:return(12);break;
default:return(22);break;
}
}
/***************************一機一密改變區域開始*************************/
unsigned char code a0[20]={8,2,4,4,1,4,6,2,8,6,5,1,1,2,2,5,6,6,1,4};
unsigned char code a1[20]={1,1,3,2,4,6,8,2,1,1,3,2,1,1,2,4,1,9,3,2};
unsigned char code a2[20]={5,5,7,6,2,5,8,7,6,4,6,7,6,5,2,5,6,7,3,9};
unsigned char code a3[20]={8,7,8,4,3,3,5,9,4,1,3,6,8,5,7,7,1,6,5,2};
unsigned char code a4[20]={7,6,3,9,1,4,5,4,4,8,2,7,3,7,1,6,5,8,8,5};
unsigned char code a5[20]={3,7,3,4,1,3,6,2,1,7,4,4,2,2,1,2,2,2,5,9};
unsigned char code a6[20]={7,4,2,6,1,7,1,3,6,6,3,3,4,6,8,3,3,5,8,5};
unsigned char code a7[20]={7,4,4,4,3,4,1,7,8,9,2,5,8,3,9,8,3,4,2,5};
unsigned char code a8[20]={2,5,1,1,1,3,4,2,2,8,3,7,2,8,7,4,2,6,6,1};
unsigned char code a9[20]={2,8,3,4,5,5,3,3,2,1,4,7,2,3,2,6,3,9,3,5};
int dingzhi=11;
void kaiji()
{
speak(2); while(yybuzy==0);
speak(1); while(yybuzy==0);
speak(3); while(yybuzy==0);
speak(6); while(yybuzy==0);}
void yanz(x)
{
//千萬種方式可以通過驗證碼鑑別獲取密碼.安全考慮,這裡省略.
}
void suan()
{ uint h1,h2,h3,x1,x2,x3;
uint t,i,xu,han;
for(t=0;t<50;t++){Jielong[t]=IapReadByte(0x0400+t);}
if(Dao==1) {if(Jielong[6]!=2){x1=a1[1];x2=a2[1];x3=a3[1];xu=1;}else{x1=Jielong[0]; x2=Jielong[1];x3=Jielong[2];xu=Jielong[3]*100+Jielong[4]*10+Jielong[5];}}
if(Dao==2) {if(Jielong[13]!=2) {x1=a1[2];x2=a2[2];x3=a3[2];xu=1;}else{
x1=Jielong[7]; x2=Jielong[8];x3=Jielong[9];xu=Jielong[10]*100+Jielong[11]*10+Jielong[12];}}
if(Dao==3) { if(Jielong[20]!=2) { x1=a1[3];x2=a2[3];x3=a3[3];xu=1; } else{ x1=Jielong[14];x2=Jielong[15];x3=Jielong[16];xu=Jielong[17]*100+Jielong[18]*10+Jielong[19];}}
if(Dao==4) { if(Jielong[27]!=2){x1=a1[4];x2=a2[4];x3=a3[4];xu=1; }else{x1=Jielong[21];x2=Jielong[22];x3=Jielong[23];xu=Jielong[24]*100+Jielong[25]*10+Jielong[26];}}
if(Dao==5) { if(Jielong[34]!=2) {x1=a1[5];x2=a2[5];x3=a3[5];xu=1; } else{x1=Jielong[28]; x2=Jielong[29];x3=Jielong[30];xu=Jielong[31]*100+Jielong[32]*10+Jielong[33];} }
if(Dao==6) { if(Jielong[41]!=2) {x1=a1[6];x2=a2[6];x3=a3[6];xu=1; } else{x1=Jielong[35];x2=Jielong[36];x3=Jielong[37];xu=Jielong[38]*100+Jielong[39]*10+Jielong[40];} }
if(Dao==7) { if(Jielong[48]!=2) {x1=a1[7];x2=a2[7];x3=a3[7];xu=1; } else{x1=Jielong[42];x2=Jielong[43];x3=Jielong[44];xu=Jielong[45]*100+Jielong[46]*10+Jielong[47];} }
han=(xu+x1)%10;
if(han<2) {
data_c=a8[x2]*132307+xu*1+a4[x3]*168+a7[x1]*2+a5[x2]*665+a2[x2]*524+a6[x3]*364+a3[x2]*257+a8[x3]*231+a5[x1]*275+a6[x2]*9+a1[x2]*37+a8[x2]*83+a6[x3]*139+a2[x1]*87;}
if((han>1)&&(han<4)) {
data_c=a3[x2]*132320+xu*1+a6[x2]*758+a3[x2]*41+a4[x2]*116+a3[x1]*210+a7[x2]*130+a2[x1]*266+a5[x3]*280+a2[x3]*149+a7[x3]*159+a1[x1]*452+a3[x1]*57+a8[x3]*557+a5[x2]*75;}
if((han>3)&&(han<6)) {
data_c=a2[x2]*132306+xu*1+a5[x2]*335+a3[x2]*198+a3[x3]*599+a4[x2]*596+a5[x3]*674+a3[x3]*307+a4[x3]*122+a6[x2]*72+a1[x1]*150+a1[x1]*193+a7[x1]*148+a8[x2]*99+a1[x1]*73;}
if((han>5)&&(han<8)) {
data_c=a8[x3]*132320+xu*1+a8[x2]*105+a5[x2]*238+a3[x2]*642+a1[x2]*485+a4[x3]*498+a3[x3]*232+a3[x2]*129+a3[x3]*99+a5[x2]*40+a2[x2]*40+a1[x2]*179+a4[x1]*46+a1[x2]*50;}
if((han>7)&&(han<10)) {
data_c=a7[x1]*132321+xu*1+a8[x2]*410+a6[x2]*333+a8[x2]*808+a5[x3]*526+a7[x1]*406+a7[x2]*130+a7[x2]*230+a7[x2]*57+a7[x2]*50+a7[x2]*532+a4[x2]*128+a1[x2]*301+a2[x2]*26;}
/***************************改變區域end *************************/
Lin[0]=data_c%1000000/100000;
Lin[1]=data_c%100000/10000;
Lin[2]=data_c%10000/1000;
Lin[3]=data_c%1000/100;
Lin[4]=data_c%100/10;
Lin[5]=data_c%10;
// yin6(Lin); yin6(Ru);
for(i=0;i<6;)
{
if(Lin[i]==Ru[i]) //將兩次輸入的新
i++;
else
//如果兩次的密碼不同
{ // N=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(13);while(yybuzy==0);
for(t=0;t<6;t++)
//將輸入清除
{
Ru[t]=0;
} break;
} }
if(i==6)
{ KEY1=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;
//開鎖後將輸入位清零
}
xu=xu+1;
h1=data_c%10; if(h1==0){h1=1;}
h2=data_c%100/10; if(h2==0){h2=1;}
h3=data_c%1000/100; if(h3==0){h3=1;}
if((Dao==1)||(Dao==8)||(Dao==15)) {Jielong[6]=2;
Jielong[5]=xu%10;
Jielong[4]=xu%100/10;
Jielong[3]=xu%1000/100;
Jielong[2]=h1;
Jielong[1]=h2;
Jielong[0]=h3;
}
if((Dao==2)||(Dao==9)||(Dao==16)) { Jielong[13]=2;
Jielong[12]=xu%10;
Jielong[11]=xu%100/10;
Jielong[10]=xu%1000/100;
Jielong[9]=h1;
Jielong[8]=h2;
Jielong[7]=h3;
}
if((Dao==3)||(Dao==10)||(Dao==17)) { Jielong[20]=2;
Jielong[19]=xu%10;
Jielong[18]=xu%100/10;
Jielong[17]=xu%1000/100;
Jielong[16]=h1;
Jielong[15]=h2;
Jielong[14]=h3;
}
if((Dao==4)||(Dao==11)||(Dao==18)) { Jielong[27]=2;
Jielong[26]=xu%10;
Jielong[25]=xu%100/10;
Jielong[24]=xu%1000/100;
Jielong[23]=h1;
Jielong[22]=h2;
Jielong[21]=h3;
}
if((Dao==5)||(Dao==12)||(Dao==19)) { Jielong[34]=2;
Jielong[33]=xu%10;
Jielong[32]=xu%100/10;
Jielong[31]=xu%1000/100;
Jielong[30]=h1;
Jielong[29]=h2;
Jielong[28]=h3;
}
IapEraseSector(0x0400);
for(t=0;t<50;t++)
{
IapProgramByte(0x0400+t,Jielong[t]);
}
} else{ //N=0;
//speak(16);
for(t=0;t<6;t++)
//將輸入清除
{
Ru[t]=0;
}
}
}
void bao(x)
{ uint t5,t;
if((a1[19]==Ru[0])&&(a2[19]==Ru[1])&&(a3[19]==Ru[2])&&(a4[19]==Ru[3])&&(a5[19]==Ru
[4])){
for(t=0;t<50;t++)
{Jielong[t]=IapReadByte(0x0400+t);}
if(Dao==1){Jielong[3]=0;Jielong[4]=2;Jielong[5]=1;}
if(Dao==2){Jielong[10]=0;Jielong[11]=2;Jielong[12]=1;}
if(Dao==3){Jielong[17]=0;Jielong[18]=2;Jielong[19]=1;}
if(Dao==4){Jielong[24]=0;Jielong[25]=2;Jielong[26]=1;}
if(Dao==5) {Jielong[31]=0;Jielong[32]=2;Jielong[33]=1;}
IapEraseSector(0x0400);
for(t=0;t<50;t++)
{
IapProgramByte(0x0400+t,Jielong[t]);}
speak(19);while(yybuzy==0);
}// else{speak(15);}
if((a1[18]==Ru[0])&&(a2[18]==Ru[1])&&(a3[18]==Ru[2])&&(a4[18]==Ru[3])&&(a5[18]==Ru
[4])){
for(t=0;t<50;t++)
{Jielong[t]=IapReadByte(0x0400+t);}
if(Dao==1){Jielong[0]=0;Jielong[1]=2;Jielong[2]=1;}
if(Dao==2){Jielong[7]=0;Jielong[8]=2;Jielong[9]=1;}
if(Dao==3){Jielong[14]=0;Jielong[15]=2;Jielong[16]=1;}
if(Dao==4){Jielong[21]=0;Jielong[22]=2;Jielong[23]=1;}
if(Dao==5){Jielong[28]=0;Jielong[29]=2;Jielong[30]=1;}
IapEraseSector(0x0400);
for(t=0;t<50;t++)
{
IapProgramByte(0x0400+t,Jielong[t]);}
speak(19);while(yybuzy==0);
}// else{speak(15);}
if((0==Ru[0])&&(x==1)){
kaiji();
}
if((2==Ru[0])&&(x==1)){
N=2;
speak(19);while(yybuzy==0);
}
if((1==Ru[0])&&(x==1)){
N=1;
speak(19);while(yybuzy==0);
}
if((3==Ru[0])&&(x==1)){
}
if((3==Ru[0])&&(2==Ru[1])&&(1==Ru[2])){
}
}
void main()
{
unsigned int key=6;
uint n=0;
// speak(19);while(yybuzy==0);
// uchar key=0xff; //鍵值初始化
kaiji();
while(1)
{
key=ReadVal16Bit();
if(key<20)
{
if(key<20)
{
if(N==1)
{
if(key==0){speak(11); } else{
speak(key+1); } }else
{speak(key+1); 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