sherryliu
Member level 1
Hi all
I have a project need control relay via COM Port send command control it.I have a confuse of the issue.Here is code:
if I want to control P2.0,send 0x02fe command,but I want receive a command as when sent 0x02fe,How to do it.thanks!
I have a project need control relay via COM Port send command control it.I have a confuse of the issue.Here is code:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include <reg51.h> unsigned char Buffer[5]={0}; unsigned char prefix=0x00; unsigned char suffix=0x00; void Delay(){ unsigned char i,j; for(i=0;i<255;i++) for(j=0;j<255;j++); } void URATinit( ) { TMOD=0x20; SCON=0x50; TH1=0xfd; TL1=0xfd; EA=1; ES=1; TR1=1; } void controlbit(unsigned int ch){ prefix=(ch>>8)&0xff; suffix=(char)ch; switch(prefix){ case 0x00: P0=suffix;break; case 0x01: P1=suffix;break; case 0x02: P2=suffix;break; case 0x03: P3=suffix;break; } } void send_char_com(unsigned char cha) { SBUF=cha; while(0==TI); TI=0; } void send_str_com(unsigned char *str,unsigned int strlen){ unsigned int k=0; for(k=0;k<strlen;k++){ send_char_com(*(str+k)); } } void get_str_com(){ unsigned int y=0; for(y=0;y<2;y++){ Buffer[y]=SBUF; while(0==RI); RI=0;} } void main() { URATinit( ); while(1){ get_str_com(); // send_str_com(Buffer,2); controlbit((unsigned int)Buffer); Delay(); } }
if I want to control P2.0,send 0x02fe command,but I want receive a command as when sent 0x02fe,How to do it.thanks!
Attachments
Last edited by a moderator: