#include<at89x52.h> #include<intrins.h> //include"iic.c" #define uint unsigned int #define uchar unsigned char sbit SDA=P3^1; sbit SCL=P3^0; uchar code shu1[]={0xfd,0x61,0xbb,0xeb,0x67,0xcf,0xdf,0xe1,0xff,0xef,0xa7,0xf5,0x97,0x03}; uchar data dingshi[]={0x00,0x03,0x00,0x02}; uchar data xian[]={0x00,0x00,0x00,0x00}; uchar k[]={0x00}; uchar a1=0x00,a3=0; bit a2; uchar idata rd[3]={0x01,0xa0,0xa1}; uchar idata wr[3]={0x01,0xa0,0xa1}; uchar temp[5]={0x00,0x00,0x00,0x00,0x00}; uchar n=0; void delay(uint x); void sta(); void stop(); void mack(); void mnack(); void cack(); void wrbyt(uchar shu); void wrbyt0(); void wrbyt1(); uchar rdbyt(); void wrnbyt(uchar slaw,uchar number,uchar ff[]); void rdnbyt(uchar number,uchar slar,uchar qq[]); void wrnbyta(uchar slaw,uchar gg[],uchar n);
void xianshi(); void miaojian(); uchar panjian(); void fenjia(); void jianchuli();
uchar panjian() { P3_2=1; P3_0=1; if(P3_2==0) { n=n+1; return 1; } else { P3_2=1; P3_0=0; if(P3_2==0) { P3_0=1; return 2; } } }
void fenjia() { uchar a8[]={0x00,0x00}; if(n==1) { a8[0]=dingshi[0]; a8[1]=dingshi[1]; } else if(n==2) { a8[0]=dingshi[2]; a8[1]=dingshi[3]; } if(a8[1]<9) { a8[1]=a8[1]+1; } else { if(a8[0]<9) { a8[0]=a8[0]+1; a8[1]=0x00; } else { a8[1]=0x00; a8[0]=0x00; } } if(n==1) { dingshi[0]=a8[0]; dingshi[1]=a8[1]; } else if(n==2) { dingshi[2]=a8[0]; dingshi[3]=a8[1]; } }
void jianchuli() { uchar jk; n=1; while(n<3) { if(n==1) { xian[0]=0x0a; xian[1]=0x0b; xian[2]=dingshi[0]; xian[3]=dingshi[1]; } else if(n==2) { xian[0]=0x0a; xian[1]=0x0b; xian[2]=dingshi[2]; xian[3]=dingshi[3]; } for(jk=0;jk<100;jk++) { xianshi(); } jk=panjian(); if(jk==1) { n=n+1; } else if(jk==2) { fenjia(); } } }
void sta() { SDA=1; SCL=1; while(SCL==0) { ; } _nop_(); SDA=0; _nop_(); _nop_(); SCL=0; }
void stop() { SDA=0; SCL=1; while(SCL==0) { ; } _nop_(); SDA=1; _nop_(); _nop_(); SCL=0; }
void mack() { SDA=0; SCL=1; _nop_(); _nop_(); SCL=0; SDA=1; }
void mnack() { SDA=1; SCL=1; _nop_(); _nop_(); SCL=0; SDA=0; }
void cack() { SDA=1; SCL=1; F0=0; if(SDA==0) { SCL=0; _nop_(); _nop_(); } else { F0=1; SCL=0; _nop_(); _nop_(); } }
void wrbyt(uchar shu) { uchar i; if((shu&0x80)>0) { SDA=1; } else { SDA=0; } SCL=1; while(SCL==0) {;} _nop_(); SCL=0; SDA=0; shu=_crol_(shu,1); for(i=1;1<8;i++) {if((shu&0x80)>0) {wrbyt1();} else {wrbyt0();} shu=_crol_(shu,1); } } void int00() interrupt 0 { uchar m,k; uchar xian1[]={0x00,0x00,0x00,0x00}; TR0=0; IE=0x00; xianshi(); n=0; P3_0=1; P3_2=1; m=panjian(); if(m==1) { for(k=0;k<4;k++) { xian1[k]=xian[k]; } jianchuli(); temp[0]=0x01; temp[1]=dingshi[0]; temp[2]=dingshi[1]; temp[3]=dingshi[2]; temp[4]=dingshi[3]; wrnbyt(0xa0,5,temp); delay(300); temp[0]=0x10; temp[1]=0x01; wrnbyt(0xa0,2,temp); } for(k=0;k<4;k++) { xian[k]=xian1[k]; }
P3_0=1; P3_2=1; IE=0x83; TR0=1; }
void wrbyt0() { SDA=0; SCL=1; _nop_(); _nop_(); SCL=0; }
void wrbyt1() { SDA=1; SCL=1; _nop_(); _nop_(); SCL=0; SDA=0; }
uchar rdbyt() { uchar nn=0xff,mm=0x80,uu=0x7f; uchar j; uint q=0; for(j=0;j<8;j++) { SDA=1; SCL=1; if(SDA==0) {nn=(nn&uu); nn=_crol_(nn,1); SCL=0; } else { nn=(nn|mm); nn=_crol_(nn,1); SCL=0; } } return(nn); }
void wrnbyta(uchar slaw,uchar gg[],uchar n) { do{ sta(); wrbyt(slaw); cack(); }while(F0==1); wrbyt(gg[n]); cack(); }
void wrnbyt(uchar slaw,uchar number,uchar ff[]) { uchar idata k; do{ sta(); wrbyt(slaw); cack(); }while(F0==1); for(k=0;k<number;k++) { wrbyt(ff[k]); cack(); while(F0==1) { wrnbyta(slaw,ff,k); } } stop(); }
void rdnbyt(number,slar,qq) uchar number,slar,qq[]; { uchar idata data0,l; do{ sta(); wrbyt(slar); cack(); }while(F0==1); for(l=0;l<number;l++) { data0=rdbyt(); qq[l]=data0; if(l<(number-1)) {mack();} } mnack(); stop(); }
void delay(uint x) { uchar j; while(x--) { for(j=0;j<150;j++) {;} } }
void main() { SP=0x60; TCON=0x01; IP=0x01; TMOD=0x01; TH0=0x3c; TL0=0xb0; IE=0x83;
wrnbyt(0xa0,1,0x10); // 读0x10地址一字节 rdnbyt(1,0xa1,k);
if(k[0]!=1) { temp[0]=0x01; temp[1]=dingshi[0]; temp[2]=dingshi[1]; temp[3]=dingshi[2]; temp[4]=dingshi[3];
wrnbyt(0xa0,5,temp); //写temp数组(5字节)
delay(300); }
wrnbyt(0xa0,1,0x01); rdnbyt(4,0xa1,dingshi); //读0x01开始4字节
xian[0]=dingshi[0]; xian[1]=dingshi[1]; xian[2]=0x00; xian[3]=0x00; a2=0; TR0=1;
while(1) { TR0=1; P3_2=1; xianshi(); P3_7=a2; } }
void xianshi() { uchar i=0x00; for(i=0;i<4;i++) { P1=0x01; P3_3=1; P3_4=1; P3_5=1; P1_0=1; P1=shu1[xian]; switch(i) { case 0: { P1_0=0; } break; case 1: { P3_5=0; } break; case 2: { P3_4=0; } break; case 3: { P3_3=0; } break; } //delay(10); } }
void timer00() interrupt 1 { TR0=0; TL0=0xb0; TH0=0x3c; a1=a1+1; if(a1==0x0a) { a1=0x00; P3_0=!P3_0; miaojian(); } IE=0x83; TR0=1; }
void miaojian() { if(xian[3]>0) { xian[3]=xian[3]-1; } else { if(xian[2]>0) { xian[3]=0x09; xian[2]=xian[2]-1; } else { if(xian[1]>0) { xian[3]=0x09; xian[2]=0x05; xian[1]=xian[1]-1; } else { if(xian[0]>0) { xian[3]=0x09; xian[2]=0x05; xian[1]=0x09; xian[0]=xian[0]-1; } else { a2=!a2; P3_7=a2; xian[3]=0x00; xian[2]=0x00; if(a2==0) { xian[1]=dingshi[1]; xian[0]=dingshi[0]; } else { xian[0]=dingshi[2]; xian[1]=dingshi[3]; } } } } } }
|