#include #include #include #include #include #include #include "..\lib\7188.h" /* Demo14: Use 7188 COM2 link to 7053(16 D/I) x 7 as counter to count pulse. address 01-07 : 7053(1-7), connect to COM2(RS485) of 7188 9600,N,8,1 7188 COM4 link to PC com1(or com2) 5digitled : digit 1 will show channel no digit 2-5 will show the input value */ extern char hex_to_ascii[16]; int ascii_to_hex(char c); int SendCmdTo7000(int iPort, unsigned char *cCmd, int iChksum); int read_7000(unsigned char *cmd); int ReceiveResponseFrom7000(int iPort, unsigned char *cCmd, long lTimeout, int iChksum); int GetModualName(int addr,char *name); /* ------------------------------------------------------------------- */ char OutCmd[8][10]={ "$01L0", "$01L0", "$02L0", "$03L0", "$04L0", "$05L0", "$06L0", "$07L0", }; char ResetLatchCmd[8][10]={ "$01C", "$01C", "$02C", "$03C", "$04C", "$05C", "$06C", "$07C", }; int Name[8]; int Echo7000=0; unsigned long Counter[8][16]; unsigned OldData[8]; unsigned char recstr[20]; #define Com1Base 0x200 #define Mcr 0x04 /* modem control reg */ #define OutHigh 0x03 #define OutLow 0x00 void SendHigh(void) { outp(Com1Base+Mcr,OutHigh); } void SendLow(void) { outp(Com1Base+Mcr,OutLow); } void ResetAllCounter(void) { int i,j; for(i=0;i<8;i++) { for(j=0;j<16;j++) Counter[i][j]=0L; } } void EchoResult(char *cmd) { int addr=atoi(cmd); int offset=addr&0x0f; addr>>=4; addr&=7; printf("\ncount %s=%lu\n",cmd,Counter[addr][offset]); } void ShowAllResult(void) { int addr,offset; for(addr=1;addr<8;addr++){ printf("\n(%d)",addr); for(offset=0;offset<16;offset++) printf("[%d=%lu]",offset,Counter[addr][offset]); } } void main(int argc,char *argv[]) { int i,k; unsigned char c,cmd[80]; int ledmode=1; int quit=0,idx=0,reset,data; int channel=-1; unsigned long count,baud=9600L; unsigned long TimeTick; int EchoMode=0; if(argc>1) { baud=atol(argv[1]); if(baud<=3000) baud=9600; } InitLib(); /* driver initial */ InstallCom(2,baud,8,0,1); /* COM2, RS-485, half-duplex mode, INT */ /* --> need to control 485 direction */ /* --> initial 485 direction = Receive */ /* --> address 1 = 7060 */ /* COM4, RS-232, connect to PC's COM port for download */ Init5DigitLed(); /* initial & blank the 5-digit LED */ TimerOpen(); LedOn(); /* red-LED on */ ResetAllCounter(); for(i=1;i<=7;i++){ OldData[i]=0; if(GetModualName(i,cmd)==NoError){ Name[i]=atoi(cmd); Show5DigitLed(1,i); Show5DigitLed(2,cmd[0]-'0'); Show5DigitLed(3,cmd[1]-'0'); Show5DigitLed(4,cmd[2]-'0'); Show5DigitLed(5,cmd[3]-'0'); } else { Name[i]=0; Show5DigitLed(1,i); Show5DigitLed(2,17); Show5DigitLed(3,17); Show5DigitLed(4,17); Show5DigitLed(5,17); } } channel=8; for(i=1;i<=7;i++){ if(Name[i]==7053 || Name[i]==7041){ channel=i; Show5DigitLed(1,i); break; } } /* printf("\n'1'-'7': show channel 1-7\n'q' : quit" "\npress any key to start."); */ if(channel<8){ count=0; while(!quit) { TimeTick=TimerReadValue(); if(TimeTick>=500){ TimerResetValue(); if(ledmode){ ledmode=0; LedOff(); } else { ledmode=1; LedOn(); } } if (kbhit4()) { /* is user press PC's keyboard from COM4 */ c=getch(); /* if this key is Q or Q --> exit this program */ putchar(c); switch(c){ case 'e': case 'E': EchoMode=!EchoMode; break; case 'q': case 'Q': quit=1; break; case 'r': case 'R': ResetAllCounter(); break; case '\r': cmd[idx]=0; EchoResult(cmd); idx=0; break; case '\b': idx--; break; default: if(c>='0' && c<='9'){ cmd[idx++]=c; } break; } } else { for(channel=1;channel<2;channel++){ count++; reset=0; read_7000(OutCmd[channel]); data=0; for(i=1;i<=4;i++){ data<<=4; data+=ascii_to_hex(recstr[i]); } if(data){ int xordata; xordata=OldData[channel]^data; if(xordata){ /* printf("\ndata=%04X",data); */ for(i=0,reset=1;i<16;i++){ if((xordata&reset) && !(OldData[channel]&reset)){ Counter[channel][i]++; /* printf("counter[%d][%d]=%lu",channel,i,Counter[channel][i]); */ } reset<<=1; } } Echo7000=0; read_7000(ResetLatchCmd[channel]); Echo7000=EchoMode; } OldData[channel]=data; } } } ShowAllResult(); } else printf("\nCannot find 7053 or 7041 on address 0-3"); RestoreCom(2); TimerClose(); return; } /* ------------------------------------------------------------------- */ /* send cmd & receive result to 7000 module */ char *Binary[16]= { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111", }; int read_7000(unsigned char *cmd) { int val,data; static unsigned long count=0; count++; SendLow(); SendCmdTo7000(2, cmd, 0); SendHigh(); if(Echo7000) printf("\n[%lu]Send=%s, Receive=",count,cmd); val=ReceiveResponseFrom7000(2, recstr, 10000L, 0); SendLow(); if(Echo7000) { if (val==NoError) { printf("%s ",recstr); /* show the I/O value */ data=ascii_to_hex(recstr[1]); Show5DigitLed(2,data); printf("[%s",Binary[data]); data=ascii_to_hex(recstr[2]); Show5DigitLed(3,data); printf(" %s",Binary[data]); data=ascii_to_hex(recstr[3]); Show5DigitLed(4,data); printf(" %s",Binary[data]); data=ascii_to_hex(recstr[4]); Show5DigitLed(5,data); printf(" %s]",Binary[data]); } else printf("Errorno=%d(%s)",val,val==TimeOut?"TimeOut":""); } return val; } int GetModualName(int addr,char *name) { char cmd[10]; int val,i; cmd[0]='$'; cmd[1]=hex_to_ascii[(addr>>4)&0xf]; cmd[2]=hex_to_ascii[addr&0xf]; cmd[3]='M'; cmd[4]=0; SendCmdTo7000(2, cmd, 0); val=ReceiveResponseFrom7000(2, cmd, 500L, 0); if(val==NoError) { if(cmd[0]=='!'){ for(i=0;i<4;i++){ name[i]=cmd[3+i]; } name[i]=0; return NoError; } else return PortError; } return val; }