输出相同不读读黑匣子中数据,输出不同读黑匣子中数据。
心片是89C2051和MAX202
/*晶振 14.7456 读EE-发送并发 密码生命期5S 波特率 可变 接受命令后2S回复9600 ee fm24c256 */ #include <intrins.h> #include <absacc.h> #include <C52sh.h> #define 1_pwr RD #define 1_txd T0 #define 1_rxd T1 #define sda INT0//0 #define scl INT1//1 #define COM_LENGTH 36 #define bt_384 TH1=0xfe
data uchar buf_i0; data uchar buf_i1; typedef union{ uchar buf_c[5]; unit buf_n[2]; }RG; RG data rg; #define buf_c0 rg.buf_c[0] #define buf_c1 rg.buf_c[1] #define buf_c2 rg.buf_c[2] #define buf_c3 rg.buf_c[3] #define buf_c4 rg.buf_c[4] #define buf_n0 rg.buf_n[0] #define buf_n1 rg.buf_n[1]
data uchar f_have_ee; data uchar com_time; data uchar com_count; data uchar dly_com; data uchar dly_baut; typedef struct{ union{ uint OFFSET; uchar 0FFARR[2]; }HL; uchar BLANK; uchar NUM; }EE; EE data ee;
//data uchar com_no; //data uchar com_sum; bdata uchar fg_tm; sbit tm_flash=fg_tm^0; sbit tm_ls_ee=fg_tm^1; sbit fg_err0=fg_tm^4; sbit fg_err1=fg_tm^5; sbit f_pword=fg_tm^6; sbit f_init=fg_tm^7; data uchar ti_buf[24]; data uchar ri_buf[35];
void send_fun(char *pr,uchar n);
void delay(void) /*延时*/ {_nop_();_nop_();_nop_();_nop_();_nop_();}
void bstart(void) /*开始在外部EEROM读数据*/ { scl=0;delay(); sda=1;delay(); scl=1;delay(); sda=0;delay(); scl=0; }
void bstop(void) /*停止望外部EEROM读数据*/ { scl=0;delay(); sda=0;delay(); scl=1;delay(); sda=1;delay(); scl=0; }
void wbyte(unsigned char ch) /*写8位数据到外部EEROM*/ {//rlmove buf_c1=ch; for(buf_c0=0;buf_c0<8;buf_c0++) { buf_c1=buf_c1<<1; scl=0;delay(); sda=CY; scl=1;delay(); } scl=0;delay(); scl=1;delay(); scl=0; }
void rbyte(void) /*在外部EEROM读一个字节*/ {//rrmove buf_c1=0; for(buf_c0=0;buf_c0<8;buf_c0++) { scl=0;delay(); scl=1;delay(); buf_c1=buf_c1<<1; if(sda) buf_c1++; } scl=0;delay(); sda=1;delay(); scl=1; //return(buf_c1) }
#define com_wt(i) wbyte(0xa0+i*2) #define E_OFFSET ee.HL.OFFSET #define E_OFF_H ee.HL.OFFARR[0] #define E_OFF_L ee.HL.OFFARR[1] #define E_BLANK ee.BLANK #define E_NUM ee.NUM void wr_data(uchar data *eedata) /*望外部EEROM写入一组数据*/ {//写字节 c0,c1,c2 P1=0; bstart(); com_wt(E_BLANK); wbyte(E_OFF_H);wbyte(E_OFF_L); for(buf_c2=0;buf_c2<E_NUM;buf_c2++){ wbyte(*(eedata+buf_c2)); if((E_OFFSET+buf_c2)>0x7FFF) { E_OFFSET=0;E_BLANK++; bstop(); bstart(); com_wt(E_BLANK); wbyte(0);wbyte(0); } } bstop(); if(E_BLANK){ for(buf_c2=0;buf_c2<250;buf_c2++) {_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();} } P1=255; }
#define com_rt(i) wbyte(0xa1+i*2) void rd_data(uchar data *eedata) /*在EEROM中读一组数据*/ {//读字节 c0,c1,c2 P1=0; bstart(); com_wt(E_BLANK); wbyte(E_OFF_H);wbyte(E_OFF_L); for(buf_c2=0;buf_c2<E_NUM;buf_c2++) { bstart(); com_rt(E_BLANK); rbyte(); *(eedata+buf_c2)=buf_c1; if((E_OFFSET+buf_c2)>0x7FFF){ E_OFFSET=0;E_BLANK++; bstop(); bstart(); com_wt(E_BLANK); wbyte(0);wbyte(0); } } bstop(); P1=255; }
//fg_err0 发送错误 //fg_err1 ri字符串中字头aa标志 //fg_err2 ri比较值 //fg_err3 //fg_err4 //fg_err5 //fg_err6 //fg_err7
void receive(void) interrupt 4 using 1 /*串行口中断程序*/ { TH0=0xfc; if(RI) { RI=0; 1_rxd=0; if(com_count){ ri_buf[com_count]=SBUF; com_count=(com_count+1)%COM_LENGTH; } else{ if(SBUF==0xaa) {com_time=200;fg_err1=1;} if(fg_err1&&(SBUF==0x75)) {com_count=1;} } } }
void ri_zero(void) /*数组清空*/ { for(buf_c0=0;buf_c0<COM_LENGTH;buf_c0++) ri_buf[buf_c0]=0; com_count=0; }
uchar comp_read(void) /*检验外部EEROM是否大于等于1小于等于3*/ { if(ri_buf[2]!=0x0) return 1; if(ri_buf[3]!=0x3) return 2; if(ri_buf[5]+1>f_have_ee) return 3; if((ri_buf[5]==0)&&(ri_buf[6]==0)&&(ri_buf[7]<0x20)) return 4; buf_c0=0xaa^0x75^0x20^0x00^0x03^0xff^ri_buf[5]^ri_buf[6]^ri_buf[7]; if(ri_buf[8]!=buf_c0) return 5; return 0; }
void send_fun(char *pr,uchar n) /*串行接收数据*/ { 1_txd=0; fg_err0=0; ES=0; for(buf_c0=0;buf_c0<n;buf_c0++) { T1=0; ACC=*(pr+buf_c0); TB8=~P; SBUF=ACC; for(buf_c1=0;buf_c1<200;buf_c1++) if(T1) break; if(!T1) fg_err0=1; } T1=0; ES=1; } code uchar rd_err1[]={0x55,0x7a,0xfa,0xff,0x69}; code uchar wr_err1[]={0x55,0x7a,0xfb,0xff,0x2b}; code uchar wr_err2[]={0x55,0x7a,0xf5,0xff,0x25}; code uchar wr_rgh[]={0x55,0x7a,0xe0,0x00,0x00,0xff,0x30};
void main(void) { P1=255;1_pwr=0; IP =0x10; /*优先级为定时器0,外部中断,串行口*/ TH1 =0xf8; /*9600 11.05926 smod=1 0xfa*/ TL1 =0xf8; /*9600*/ SCON =0xd0; /*方式3*/ PCON =0x80; TH0 =0xfb; /*-(4608/256);*/ TL0 =0x33; /*-(4608/256);晶振 11.05926*/ TCON =0x50; /*开时间T1 T0中断 无外部中断*/ TMOD =0x21; /*T1为方式2的自动重装;T0为方式1的定时方式*/ IE =0x82; /*串行口中断允许,ET1允许,INT1,ETO允许,INT0*/ ES=1;
_nop_();
while(1) { //rc_handler(); if(!dly_baut) bt_96; read_cord(); if(com_count>1) { switch(ri_buf[1]) { case 0x0a://查询 if(com_count>=6){//回复存在 if(ri_buf[5]==0x2a){ ti_buf[0]=0x55;ti_buf[1]=0x7a;ti_buf[2]=0x0a; ti_buf[3]=0x00;ti_buf[4]=0x03;ti_buf[5=0xff; ti_buf[6]=0xaa;ti_buf[7]=f_have_ee; E_BLANK=0;E_NUM=1;E_OFFSET=0x7ffc;rd_data(&ti_buf[8]); ti_buf[9]=/*0x55^0x7a^0x0a^0x00^0x03^0xff^0xaa*/0x73^ti_buf[7]^ti_buf[8]; send_fun(ti_buf,10); ri_zero(); } } break; case 0x20://主机读 //aa,75,20,0,3,no,bl,a1,a2,sm; if(com_count>=9){ if(comp_read()) send_fun(rd_err1,5); else{ //ti_put(); if(dly_baut==0){ bt_384; for(buf_c3=0;buf_c3<250;buf_c3++) for(buf_c4=0;buf_c4<40;buf_c4++) } dly_baut=20; ti_buf[0]=0x55;ti_buf[1]=0x7a;ti_buf[2]=0x20; ti_buf[3]=0x00;ti_buf[4]=0x10;ti_buf[5]=0xff; ti_buf[22]=0x55^0x7a^0x20^0x00^0x10^0xff; E_BLANK=ri_buf[5];E_OFFSET=ri_buf[6]*256+ri_buf[7];ee.NUM=16; rd_data(ti_buf+6); for(buf_c3=6;buf_c3<22;buf_c3++) ti_buf[22]^=ti_buf[buf_c3]; send_fun(ti_buf,23); } break; case 0xe0://写数据 //aa,75,e0,0 //#AAH,#75H,#E0H,(数据长度,2字节),#FFH(8位密码),(BANK,1字节),(EEP_ADRESS,2字节),(数据长度,1字节), (数据,。。。。。),#XXH //#AAH,#75H,#E0H,11,12,#FFH,m1,m2,m3,m4,m5,m6,m7,m8,b1,a1,a2,l1,d1_dn,xxh if(ri_buf[2]!=0){send_fun(wr_err2,5);ri_zero();ES=1;break;} if(com_count>=(ri_buf[3]+6)){ ES=0; if(dly_baut==0){ bt_384; for(buf_c3=0;buf_c3<250;buf_c3++) for(buf_c4=0;buf_c4<40;buf_c4++) } dly_baut=20; if((ri_buf[3]-ri_buf[16]!=12) {send_fun(wr_err1,5);ri_zero();ES=1;break;}//数据不符 for(buf_c4=0xaa^0x75,buf_c3=1;buf_c3<(ri_buf[3]+5);buf_c3++) buf_c4^=ri_buf[buf_c3]; if(buf_c4!=ri_buf[(ri_buf[3]+5)]){send_fun(wr_err1,5);ri_zero();ES=1;break;}//较错 if(ri_buf[13]==0) if((ri_buf[14]*256+ri_buf[15]>=0x0350)&&(ri_buf[14]*256+ri_buf[15]<=0x1b51)) dly_com=2; if(f_init&&(dly_com==0)){ E_BLANK=0;E_OFFSET=0x8;ee.NUM=8;rd_data(ti_buf); for(buf_c0=buf_c1=0;buf_c0<8;buf_c0++) { if(ri_buf[5+buf_c0]!=ti_buf[buf_c0]) {buf_c1=255;ES=1;break;} } if(buf_c1){send_fun(wr_err2,5);ri_zero();ES=1;break;} dly_com=50; } E_BLANK=ri_buf[13];E_OFFSET=ri_buf[14]*256+ri_buf[15];E_NUM=ri_buf[16];wr_data(ri_buf+17); sen_fun(wr_rgh,7); ri_zero(); ES=1; } break; dafault :break; } } //end } }