diff --git a/proj/hex/host_c.hex b/proj/hex/host_c.hex index e2a6a78..42c862e 100644 --- a/proj/hex/host_c.hex +++ b/proj/hex/host_c.hex @@ -1,51 +1,83 @@ -:030000000204A057 -:0C04A000787FE4F6D8FD7581510204E776 -:1005E8002020202020202020003120746F20323A43 -:1005F800003220746F20313A0031323324006162B6 -:100608006364005A59580000070B0D0E70B0D0E013 -:10074600759850E4F587758920AF1C8F8B8F8DD2F5 -:0B0756008E759A50851C9C758E102239 -:1003890078217C007D007BFF7A0579F17E007F086A -:100399001202D378297C007D007BFF7A0579F97EEA -:1003A900007F081202D378317C007D007BFF7A063A -:1003B90079017E007F0F1202D3E4F54B1207461232 -:1003C90007C07F801207E67B007A007921E4FD12DD -:1003D90005B87FC01207E67B007A007929E4FD128F -:1003E90005B87FE87E031207ABAF4B054BEF75F0FD -:1003F9000384E5F075F005A42431F9E4FAFBF5501E -:10040900C2007D051206EA7B007A00794075507FAB -:10041900D2007DFF1206188F4A74402FF8E4F67F48 -:10042900801207E67BFF7A0579E8E4FD1205B87AC0 -:100439000079211205B41205B07FE87E031207ABDB -:10044900AF4B054BEF75F00384E5F075F005A42477 -:1004590031F9E4FAFBF550D2007D051206EA7B007A -:100469007A00794075507FC2007DFF1206188F4AC5 -:1004790074402FF8E4F67FC01207E67BFF7A05790E -:10048900E8E4FD1205B87B007A0079297D90120510 -:07049900B81205B00203EBED -:060648008B148A158916CF -:10064E0030020775507FD200800BAB14AA15A91685 -:10065E0075507FC2007DFF120618AD07AB14AA15A8 -:05066E00A91602069E22 -:1007AB00EF1FAA0670011E4A600A7D7BAC051DEC8B -:0407BB0060EE80F874 -:0107BF002217 -:10052C0078087C007D007BFF7A0679107E007F04C2 -:10053C001202D3780C7C007D007BFF7A0679147E46 -:10054C00007F041202D375900FE590640F604FA9E1 -:10055C00907F057E001207ABE9659070417590F0B5 -:10056C00E59064F06038AB907F057E001207ABEB32 -:10057C006590702A7F0374082FF8E6B50102A9076D -:10058C00740C2FF8E6B50306EF25E025E0FB1FC33E -:10059C00EF6480948050DF75900FE94BFF227590CB -:0305AC000F7FFFBF -:0105AF002229 -:0805B0007A0079407B007D9088 -:1005B8008B4C8A4D894E8C4F8D50AB4CAA4DA94E11 -:1005C8001202F9601A054EE54E7002054D14F91233 -:1005D80002F9FF1207F1AF50AE4F1207AB80DB22D2 -:1007C0007F381207E67F0E1207E67F061207E67FE4 -:0407D000010207E635 +:0300000002052CCA +:0C052C00787FE4F6D8FD75815402057359 +:08097600070B0D0E70B0D0E07C +:04096E00C104C105FA +:10089F00759850E4F587758920AF158F8B8F8DD2A2 +:0B08AF008E759A5085159C758E1022E6 +:060838007B007A00793C10 +:06083E008B408A41894253 +:1008440075500AC2027D147C001206D9AD07AB4074 +:07085400AA41A94202079727 +:0D095600C2037FF61209061208BAC204227D +:1007C200200424C2037FF012090612083892042082 +:1007D2000415120956C2037FF01209061208389254 +:0707E2000420040312095674 +:0107E90022ED +:0F069B00754800754900754A00AB42AA43A9444F +:0606AA008B458A468947DA +:1006B000755004D2027D647C001206D9AD07E5496D +:1006C000454A4548600AAB48AA49A94AED120312B7 +:0906D000AB45AA46A947020797B1 +:10091C00439A08D2037FF61209067F047E00120860 +:04092C00BEC2052220 +:08073D007B007A00793DAF3C1E +:100745008F418B428A438944D2037FF512090612F1 +:10075500069B920520050F12091CD203AF41120911 +:070765000612069B9205221B +:02063C008F3CF1 +:10063E00439A0830050312091CD203AF3C12090677 +:10064E00754800754900754A007B007A00793D12A5 +:10065E0006AA920520050312073D300526E53DB496 +:10066E000108E53EB53C03D38001C3920520051376 +:10067E0012073DE53DB40108E53EB53C03D38001CC +:0C068E00C3920520050302091C539AF7D3 +:01069A00223D +:0A03AB008F328D338B348A3589368A +:1003B500120956C203AF321209061208BAAF321239 +:1003C500063C3005797538118533397B007A00791B +:1003D500387D0212087E8F377B007A007938C20299 +:1003E500AD371208EE1208BA7B007A007938D202CE +:1003F500AD371208EE754800754900754A37AB34BC +:10040500AA35A9361206AA920140217B007A007905 +:1004150038D202AD371208EE754800754900754AA5 +:1004250037AB34AA35A9361206AA920112091CE582 +:10043500376028AB34AA35A936C202FD8017E4F52A +:1004450038FB7A0079387D0112087EAD077B007A8A +:10045500007938C2021208EE1208BA20010EAB3438 +:10046500AA35A936900001E5331203241207C2A26A +:010475000185 +:010476002263 +:1004770012089FC2037FFA1209061208BA20040362 +:100487001207C23004F77521037B007A0079217DBA +:100497000112087EAD078D317B007A007921C202F7 +:1004A7001208EE7B007A00792112083E920050690B +:1004B700E521606524FC70387B007A007921AD2343 +:1004C700AF221203AB40137521047523FF7B007A1B +:1004D7000079217D0312087E8F313004137B007A67 +:1004E700007921C202AD311208EE7F0A7E00802416 +:1004F7003004267521037B007A0079217D011208DB +:100507007EAD078D317B007A007921C2021208EE99 +:100517007FE87E031208BE808A200003020484124B +:050527000956020484E6 +:0408BA007F0A7E0033 +:1008BE00EF4E6011EF1F70011E7D0BED60F27CF8A4 +:0508CE00DCFE1D80F6B8 +:0108D3002202 +:1005B80078087C007D007BFF7A0979767E007F04CD +:1005C8001202D3780C7C007D007BFF7A09797A7E51 +:1005D800007F041202D375900FE590640F604FAB53 +:1005E800907F057E001208BEEB659070417590F013 +:1005F800E59064F06038AA907F057E001208BEEA94 +:100608006590702A7F0374082FF8E6B50302AB07DC +:10061800740C2FF8E6B50206EF25E025E0FA1FC3B3 +:10062800EF6480948050DF75900FEB4AFF2275903D +:030638000F7FFF32 +:01063B00229C +:10070D008B108A1189128C138D14AB10AA11A9129A +:10071D001202F9601A0512E5127002051114F91290 +:10072D0002F9FF12097EAF14AE131208BE80DB2250 +:100930007F381209637F0E1209637F061209637FF5 +:040940000102096344 :1000030000102030405060708191A1B1C1D1E1F165 :1000130012023222524272629383B3A3D3C3F3E335 :100023002434041464744454A5B58595E5F5C5D505 @@ -78,51 +110,53 @@ :1001D3007D5C3F1EF9D8BB9A75543716F1D0B392A4 :1001E3002E0F6C4DAA8BE8C926076445A283E0C194 :1001F3001F3E5D7C9BBAD9F81736557493B2D1F084 -:10067300E4F51AF51BED601E1202F9651AFF9000EE -:100683000393651BF51AEF90010393F51B7401297E -:0A069300F9E43AFADDE2AE1AAF1BFB -:01069D00223A -:1006C9008B178A188919AC05120673AB17AA18A9D2 -:1006D900198C82758300EE8FF012035CEC2402FF03 -:0106E90022EE -:10069E008D138B108A11891215131513AD131206B3 -:1006AE0073AB10AA11A9128513827583001203244D -:0B06BE00B50706EEB5F002D322C32200 -:0307F800011CFDE4 -:100761007F017580FFC2B4C2B5D2B6D2B4AE80EEFD -:0907710030E7040FEF70EEC2B492 -:01077A00225C -:0B07E600EF6007C200F5511207D4229B -:0707F100D2008F510207D472 -:1007D400120761C2B6A20092B5855180D2B400C29C -:0207E400B4223D -:1007950030010B8F9BE59A30E1FB539A02228F992A -:0607A5003099FDC299220B -:1006EA00ED601B1202F9FFA2009201120795AF50AA -:0E06FA00EF6002DFFE740129F9E43AFADDE553 -:0107080022CE -:10077B00309802800CEF60101F7E7CEE60F7309893 -:09078B0004C298D3221E80F3C3BE -:010794002242 -:10070900E59A30E002800EEF60131F7E7CEE60F701 -:0E071900E59A30E005539A01D3221E80F0C30A -:0107270022AF -:1007280030010BAF051207095012E59B8009AF0590 -:0E07380012077B5007E599120312D322C32249 -:060618008B4C8A4D894E57 -:10061E00E4FCA2009201120728501C7401254EF52D -:10062E004EE4354DF54D0CAB4CFAA94EA20092019D -:09063E00AD5012072840E4AF049E -:010647002290 -:1004AC00020389E493A3F8E493A34003F68001F2DA -:1004BC0008DFF48029E493A3F85407240CC8C33351 -:1004CC00C4540F4420C8834004F456800146F6DF20 -:1004DC00E4800B01020408102040809007F8E47EB1 -:1004EC00019360BCA3FF543F30E509541FFEE49315 -:1004FC00A360010ECF54C025E060A840B8E493A3DC -:10050C00FAE493A3F8E493A3C8C582C8CAC583CA06 -:10051C00F0A3C8C582C8CAC583CADFE9DEE780BEBE -:0107FB0000FD +:10076C00E4F54FF550ED601E1202F9654FFF900055 +:10077C0003936550F54FEF90010393F550740129E5 +:0A078C00F9E43AFADDE2AE4FAF5097 +:010796002240 +:10087E008B3C8A3D893EAC0512076CAB3CAA3DA968 +:10088E003E8C82758300EE8FF012037EEC2402FF05 +:01089E002237 +:100797008D4E8B4B8A4C894D154E154EAD4E12071B +:1007A7006CAB4BAA4CA94D854E827583001203464C +:0B07B700B50706EEB5F002D322C32206 +:030972000115FD6F +:1008D4007F017580FFC2B4C2B5D2B6D2B4AE80EE89 +:0908E40030E7040FEF70EEC2B41E +:0108ED0022E8 +:0B096300EF6007C206F51612094422DF +:07097E00D2068F16020944A6 +:100944001208D4C2B6A20692B5851680D2B400C2EB +:02095400B422CB +:1009060030030B8F9BE59A30E1FB539AFD228F99BA +:060916003099FDC2992298 +:1008EE00ED60141202F9FFA20292031209067401BE +:0808FE0029F9E43AFADDEC22CD +:10085B003098028013EF4E6018EF1F70011E7D0B56 +:10086B00ED60F27C7C309804C298D322DCF71D80BB +:02087B00EFC3C9 +:01087D002258 +:1007EA00E59A30E0028015EF4E601BEF1F70011E84 +:1007FA007D19ED60F27C2BE59A30E005539AFED321 +:07080A0022DCF41D80ECC3A9 +:0108110022C4 +:100812008C538D5430030DAF54AE531207EA50146B +:10082200E59B800BAF54AE5312085B5007E599125B +:060832000312D322C322D1 +:0606D9008B4B8A4C894D99 +:1006DF00E4F552A2029203120812501F7401254D25 +:1006EF00F54DE4354CF54C0552AB4BFAA94DA20232 +:0D06FF009203AD51AC5012081240E1AF5211 +:01070C0022CA +:10053800020477E493A3F8E493A34003F68001F25E +:1005480008DFF48029E493A3F85407240CC8C333C4 +:10055800C4540F4420C8834004F456800146F6DF93 +:10056800E4800B010204081020408090096EE47EAC +:10057800019360BCA3FF543F30E509541FFEE49388 +:10058800A360010ECF54C025E060A840B8E493A34F +:10059800FAE493A3F8E493A3C8C582C8CAC583CA7A +:1005A800F0A3C8C582C8CAC583CADFE9DEE780BE32 +:010975000081 :10020300E709F608DFFA8046E709F208DFFA803EDD :1002130088828C83E709F0A3DFFA8032E309F608CA :10022300DFFA8078E309F208DFFA807088828C8332 @@ -143,11 +177,14 @@ :09030900E32289828A83E4932235 :10031200BB010689828A83F0225002F722BBFE01CA :02032200F322C4 -:10032400BB0110E58229F582E5833AF583E0F5F017 -:10033400A3E0225009E92582F886F008E622BBFEF4 -:100344000AE92582F8E2F5F008E222E5832AF5833A -:08035400E993F5F0A3E99322FF -:10035C00F8BB0111E58229F582E5833AF583E8F0D3 -:10036C00E5F0A3F0225009E92582C8F608A6F02290 -:0D037C00BBFE09E92582C8F2E5F008F22277 +:10032400F8BB010DE58229F582E5833AF583E8F00F +:10033400225006E92582C8F622BBFE05E92582C8BB +:02034400F222A3 +:10034600BB0110E58229F582E5833AF583E0F5F0F5 +:10035600A3E0225009E92582F886F008E622BBFED2 +:100366000AE92582F8E2F5F008E222E5832AF58318 +:08037600E993F5F0A3E99322DD +:10037E00F8BB0111E58229F582E5833AF583E8F0B1 +:10038E00E5F0A3F0225009E92582C8F608A6F0226E +:0D039E00BBFE09E92582C8F2E5F008F22255 :00000001FF diff --git a/proj/include/serialport_2.h b/proj/include/serialport_2.h index e4d7381..e658ea1 100644 --- a/proj/include/serialport_2.h +++ b/proj/include/serialport_2.h @@ -2,34 +2,19 @@ #define SERIAL_PORT_2_H #ifdef USE_QUERY_TRANSMIT -extern void SP_QTransmitByte(unsigned char byte, bit second); - -extern void SP_QTransmitData( - unsigned char* buf, unsigned char n, unsigned char cutT, bit second -); +// extern void SP_Set_QTWait(void (*waitFn)(unsigned int)); -#define SP1_QTransmitByte(byte) SP_QTransmitByte(byte, 0) -#define SP2_QTransmitByte(byte) SP_QTransmitByte(byte, 1) -#define SP1_QTransmitData(buf, n, cutT) SP_QTransmitData(buf, n, 0, 0) -#define SP2_QTransmitData(buf, n, cutT) SP_QTransmitData(buf, n, 0, 1) +extern void SP_QTransmitByte(unsigned char byte, bit second); +extern void SP_QTransmitData(unsigned char* buf, unsigned char n, bit second); #endif #ifdef USE_QUERY_RECEIVE -extern bit SP_QReceiveByte( - unsigned char* buf, unsigned char t, bit second -); +extern bit SP_QReceiveByte(unsigned char* buf, unsigned int t, bit second); extern unsigned char SP_QReceiveData( - unsigned char* buf, unsigned char waitT, unsigned char cutT, bit second + unsigned char* buf, unsigned int waitT, unsigned char cutT, bit second ); - -#define SP1_QReceiveByte(buf, t) SP_QReceiveByte(buf, t, 0) -#define SP2_QReceiveByte(buf, t) SP_QReceiveByte(buf, t, 1) -#define SP1_QReceiveData(buf, n, cutT) SP_QReceiveData(buf, n, cutT, 0) -#define SP2_QReceiveData(buf, n, cutT) SP_QReceiveData(buf, n, cutT, 1) - #endif - #endif // SERIAL_PORT_2_H \ No newline at end of file diff --git a/proj/keil_host_c/__config__.h b/proj/keil_host_c/__config__.h index 8f8d6a7..b962ecc 100644 --- a/proj/keil_host_c/__config__.h +++ b/proj/keil_host_c/__config__.h @@ -43,8 +43,8 @@ #define S2RI 0x01 #define S2_CHECK(X) (S2CON & X) -#define S2_SET_L(X) (S2CON &= X) #define S2_SET_H(X) (S2CON |= X) +#define S2_SET_L(X) (S2CON &= ~X) // ------- define for lcd1602 ---------- diff --git a/proj/keil_host_c/host.uvproj b/proj/keil_host_c/host.uvproj index bc49201..1737ff9 100644 --- a/proj/keil_host_c/host.uvproj +++ b/proj/keil_host_c/host.uvproj @@ -233,7 +233,7 @@ 0 0x0 - 0x10000 + 0xffff 0 diff --git a/proj/keil_host_c/main.c b/proj/keil_host_c/main.c index 692d691..d51efe1 100644 --- a/proj/keil_host_c/main.c +++ b/proj/keil_host_c/main.c @@ -11,19 +11,20 @@ * - 除了 CLOSE 和 广播 外, 主机有问必有答; */ -#define WAIT_TIME 255 -#define CUT_TIME 127 +#define WITH_PC 0 +#define WITH_SLAVE 1 -#define SP1_RECEIVE(buf_p) SP1_QReceiveData(buf_p, WAIT_TIME, CUT_TIME) +#define R_PC_WAIT_TIME 20 +#define R_PC_CUT_TIME 10 -#define SP2_RECEIVE(buf_p) SP2_QReceiveData(buf_p, WAIT_TIME, CUT_TIME) +#define R_SLAVE_WAIT_TIME 100 +#define R_SLAVE_CUT_TIME 4 unsigned char KeyValue(void); void Delay1ms(unsigned int t); void LCD1602_Action(void); void LCD1602_ShowString(unsigned char* s, unsigned char t); - void init(void) { extern unsigned char baudRateT; @@ -38,50 +39,10 @@ void init(void) S2CON = 0x50; // 串口2 方式1 10位异步收发 S2REN=1允许接收 BRT = baudRateT; // 独立波特率发生器初值 AUXR = 0x10; // BRTR=1 独立波特率发生器开始计数 -} - -// test -void main(void) -{ - unsigned char str1[] = "1 to 2:"; - unsigned char str2[] = "2 to 1:"; - unsigned char send[][5] = {"123$", "abcd", "ZYX"}; - unsigned char buf[10]; - unsigned char len, c = 0; - init(); - LCD1602_Action(); - - LCD1602_WriteCmd(Move_Cursor_Row1_Col(0)); - LCD1602_ShowString(str1, 0); - LCD1602_WriteCmd(Move_Cursor_Row2_Col(0)); - LCD1602_ShowString(str2, 0); - - while (1) - { - Delay1ms(1000); - SP1_QTransmitData(send[c++%3], 5, 1); - len = SP2_RECEIVE(buf); - buf[len] = 0; - LCD1602_WriteCmd(Move_Cursor_Row1_Col(0)); - LCD1602_ShowString(" ", 0); - LCD1602_ShowString(str1, 400); - LCD1602_ShowString(buf, 400); - - Delay1ms(1000); - SP2_QTransmitData(send[c++%3], 5, 1); - len = SP1_RECEIVE(buf); - buf[len] = 0; - LCD1602_WriteCmd(Move_Cursor_Row2_Col(0)); - LCD1602_ShowString(" ", 0); - LCD1602_ShowString(str2, 400); - LCD1602_ShowString(buf, 400); - } -} -bit receiveCheck(unsigned char* buf, bit second) -{ // 接收数据并检验真假 - unsigned char length = second ? SP2_RECEIVE(buf) : SP1_RECEIVE(buf); - return CRC16_CHECK_XMODEM(buf, length); + // EA = 1; //开总中断 + // ES = 1; //开串口1中断 + // IE2 = 0x01; //开串口2中断 ES2=1 } /* ========================================================================== */ @@ -93,7 +54,197 @@ bit receiveCheck(unsigned char* buf, bit second) * - 如果错误, 则重来并重新判断地址内容 * - 如果都正确则成功建立信道, 否则失败 */ -// bit channel = 0; +bit pcChannel = 0; +bit slaveChannel = 0; + +bit receiveCheckWithPC(unsigned char* buf) +{ // 接收数据并检验真假 + unsigned char length = + SP_QReceiveData(buf, R_PC_WAIT_TIME, R_PC_CUT_TIME, WITH_PC); + return CRC16_CHECK_XMODEM(buf, length); +} + +void closePCChannel() +{ + SP_QTransmitByte(_CLOSE_ADDR_, WITH_PC); + Delay1ms(R_PC_CUT_TIME); + pcChannel = 0; +} + +void createPCChannel() +{ + unsigned char buf[4]; + if (pcChannel) + return; + SP_QTransmitByte(_PC_ADDR_, WITH_PC); + pcChannel = receiveCheckWithPC(buf); + if (!pcChannel) + { + closePCChannel(); + SP_QTransmitByte(_PC_ADDR_, WITH_PC); + pcChannel = receiveCheckWithPC(buf); + if (!pcChannel) + closePCChannel(); + } +} + +bit receiveCheckWithSlave(unsigned char* buf, unsigned char* len) +{ // 接收数据并检验真假 + unsigned char length = + SP_QReceiveData(buf, R_SLAVE_WAIT_TIME, R_SLAVE_CUT_TIME, WITH_SLAVE); + if (len) + *len = length; + return CRC16_CHECK_XMODEM(buf, length); +} + +void closeSlaveChannel() +{ + S2_SET_H(S2TB8); + SP_QTransmitByte(_CLOSE_ADDR_, WITH_SLAVE); + Delay1ms(R_SLAVE_CUT_TIME); + slaveChannel = 0; +} + +void resetSlaveChannel(unsigned char addr, unsigned char* buf) +{ + SP_QTransmitByte(_REN_ADDR_, WITH_SLAVE); + slaveChannel = receiveCheckWithSlave(buf, 0); + if (!slaveChannel) + { + closeSlaveChannel(); + SP_QTransmitByte(addr, WITH_SLAVE); + slaveChannel = receiveCheckWithSlave(buf, 0); + } +} + +void createSlaveChannel(unsigned char addr) +{ + unsigned char buf[4]; + S2_SET_H(S2TB8); + if (slaveChannel) + closeSlaveChannel(); + SP_QTransmitByte(addr, WITH_SLAVE); + slaveChannel = receiveCheckWithSlave(buf, 0); + + if (!slaveChannel) + resetSlaveChannel(addr, buf); + if (slaveChannel) + { + slaveChannel = (buf[0] == _ACK_WORD_) && (buf[1] == addr); + if (!slaveChannel) + { + resetSlaveChannel(addr, buf); + slaveChannel = (buf[0] == _ACK_WORD_) && (buf[1] == addr); + } + } + if (!slaveChannel) + closeSlaveChannel(); + else + S2_SET_L(S2TB8); +} + +bit updateSlaveState(unsigned char addr, unsigned char word, unsigned char* buf) +{ + bit flg; + unsigned char len; + unsigned char rBuf[4]; + closePCChannel(); + SP_QTransmitByte(addr, WITH_PC); + Delay1ms(R_PC_CUT_TIME); + createSlaveChannel(addr); + if (slaveChannel) + { + rBuf[0] = _US_WORD_; + rBuf[1] = word; + len = CRC16_ADD_XMODEM(rBuf, 2); + SP_QTransmitData(rBuf, len, WITH_PC); + Delay1ms(R_PC_CUT_TIME); + SP_QTransmitData(rBuf, len, WITH_SLAVE); + if (!(flg = receiveCheckWithSlave(buf, &len))) + { + SP_QTransmitData(rBuf, len, WITH_SLAVE); + flg = receiveCheckWithSlave(buf, &len); + } + closeSlaveChannel(); + if (len) + { + SP_QTransmitData(buf, len, WITH_PC); + Delay1ms(R_PC_CUT_TIME); + } + } + else + { + rBuf[0] = _NULL_WORD_; + SP_QTransmitData(rBuf, CRC16_ADD_XMODEM(rBuf, 1), WITH_PC); + Delay1ms(R_PC_CUT_TIME); + } + if (!flg) + buf[1] = word; + createPCChannel(); + return flg; +} + +void main(void) +{ + bit flg; + unsigned char buf[16]; + unsigned char len; + // LCD1602_Action(); + init(); + + // SP_Set_QTWait(Delay1ms); + + SP_QTransmitByte(_ACTION_, WITH_PC); + Delay1ms(R_PC_CUT_TIME); + + while (1) + { + if (!pcChannel) + createPCChannel(); + if (pcChannel) + { + buf[0] = _ASK_WORD_; + SP_QTransmitData(buf, len = CRC16_ADD_XMODEM(buf, 1), WITH_PC); + while ((flg = receiveCheckWithPC(buf)) && buf[0] != _NULL_WORD_) + { + switch (buf[0]) + { + case _SSU_WORD_: + // SP_QTransmitData(buf, 5, 0, WITH_PC); + // continue; + if (!updateSlaveState(buf[1], buf[2], buf)) + { + buf[0] = _SSU_WORD_; + buf[2] = 0xff; + len = CRC16_ADD_XMODEM(buf, 3); + } + if (pcChannel) + { + SP_QTransmitData(buf, len, WITH_PC); + Delay1ms(R_PC_CUT_TIME); + continue; + } + break; + case _ACK_WORD_: + + break; + case _REN_WORD_: + + break; + default: + break; + } + if (!pcChannel) + break; + buf[0] = _ASK_WORD_; + SP_QTransmitData(buf, len = CRC16_ADD_XMODEM(buf, 1), WITH_PC); + Delay1ms(1000); + } + if (flg) + closePCChannel(); + } + } +} // void closeChannel() // { @@ -143,7 +294,7 @@ bit receiveCheck(unsigned char* buf, bit second) // TB8 = !channel; // } -/* ========================================================================== */ +// /* ========================================================================== */ // bit updateSlaveState(unsigned char addr, unsigned char word, unsigned char* buf) // { @@ -239,16 +390,17 @@ bit receiveCheck(unsigned char* buf, bit second) // } // } -void Delay1ms(unsigned int t) // 12MHz -{ // 软件延迟 参数用uchar 比uint 精准 - unsigned char i; - while (t--) // mov dec mov jnz orl jz : 9us +void Delay1ms(unsigned int t) // 11.0592 约1ms +{ + unsigned char i, j; + while (t) { - i = 123; // mov : 1us - while (i--) // mov dec mov jz : 6us * 124 - ; // jmp : 2us * 123 - } // 每当低位为 0 会多1us处理高位(dec) 忽略 -} // (9+1+8*124-2=1000)us * t + ((t/256)+10+6)us 约 t ms + t--; + for (i = 11; i; --i) + for (j = 248; j; --j) + ; + } +} unsigned char KeyValue(void) { @@ -333,3 +485,116 @@ void LCD1602_Action(void) LCD1602_WriteCmd(Clear_Screen); // 命令1 清屏 #endif } // LCD1602 Action + +/* //测试双串口通信程序 +#define WAIT_TIME 50 +#define CUT_TIME 40 + +#define S1_RECEIVE(buf_p) SP1_QReceiveData(buf_p, WAIT_TIME, CUT_TIME) + +#define S2_RECEIVE(buf_p) SP2_QReceiveData(buf_p, WAIT_TIME, CUT_TIME) + +unsigned char KeyValue(void); +void Delay1ms(unsigned int t); +void LCD1602_Action(void); +void LCD1602_ShowString(unsigned char* s, unsigned char t); + +void init(void) +{ + extern unsigned char baudRateT; + // 串口1初始化 + SCON = 0x50; // 设置串口控制寄存器SCON=0101 0000 + PCON = 0x00; // 设定电源控制寄存器PCON,这里表示波特率不加倍 + TMOD = 0x20; // 定时器T1的工作方式2 + TH1 = TL1 = baudRateT; // 波特率 9600bps + TR1 = 1; // 启动定时器 + + // 串口2初始化 + S2CON = 0x50; // 串口2 方式1 10位异步收发 S2REN=1允许接收 + BRT = baudRateT; // 独立波特率发生器初值 + AUXR = 0x10; // BRTR=1 独立波特率发生器开始计数 + + EA = 1; //开总中断 + ES = 1; //开串口1中断 + IE2 = 0x01; //开串口2中断 ES2=1 +} + +unsigned char num[2] = {0, 0}; +unsigned char str1[] = "1 to 2:"; +unsigned char str2[] = "2 to 1:"; +unsigned char send[][5] = {"123$", "abcd", "ZYX5"}; +unsigned char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +unsigned char len, c = 0; +// test +void main(void) +{ + + init(); + LCD1602_Action(); + + LCD1602_WriteCmd(Move_Cursor_Row1_Col(0)); + LCD1602_ShowString(str1, 4000); + LCD1602_WriteCmd(Move_Cursor_Row2_Col(0)); + LCD1602_ShowString(str2, 4000); + + while (1) + { + // 1 -> 2 + Delay1ms(6000); + LCD1602_WriteCmd(Clear_Screen); + len = 0; + SP1_QTransmitData(send[c++ % 3], 5, 10); + // len = S2_RECEIVE(buf); + SP1_QTransmitData(buf, len, 10); + buf[len] = 0; + num[0] = len + '0'; + + // LCD1602_WriteCmd(Move_Cursor_Row1_Col(0)); + // LCD1602_ShowString(" ", 1); + LCD1602_WriteCmd(Move_Cursor_Row1_Col(0)); + LCD1602_ShowString(str1, 4000); + if (*buf == 0) + LCD1602_ShowString("N", 4000); + else + LCD1602_ShowString(buf, 4000); + + // 2 -> 1 + Delay1ms(6000); + len = 0; + SP2_QTransmitData(send[c++ % 3], 5, 10); + // len = S1_RECEIVE(buf); + SP1_QTransmitData(buf, len, 10); + buf[len] = 0; + num[0] = len + '0'; + // LCD1602_WriteCmd(Move_Cursor_Row2_Col(0)); + // LCD1602_ShowString(" ", 1); + LCD1602_WriteCmd(Move_Cursor_Row2_Col(0)); + LCD1602_ShowString(str2, 4000); + if (*buf == 0) + LCD1602_ShowString("N", 4000); + else + LCD1602_ShowString(buf, 4000); + } +} + +// 串行口1中断处理函数 +void UART_1Interrupt(void) interrupt 4 +{ + if(RI==1) + { + RI=0; + buf[len] = SBUF; + len++; + } +} +// 串行口2中断处理函数 +void UART_2Interrupt(void) interrupt 8 +{ + if(S2_CHECK(S2RI)) + { + S2_SET_L(S2RI); + buf[len] = S2BUF; + len++; + } +} +*/ diff --git a/proj/src/serialport_2.c b/proj/src/serialport_2.c index c13c3f3..b961150 100644 --- a/proj/src/serialport_2.c +++ b/proj/src/serialport_2.c @@ -4,8 +4,14 @@ #define uint unsigned int // ============================================================================ - #ifdef USE_QUERY_TRANSMIT +// void (*SP_QTWait)(uint) = 0; + +// void SP_Set_QTWait(void (*waitFn)(uint)) +// { +// SP_QTWait = waitFn; +// } + void SP_QTransmitByte(uchar byte, bit second) { if (second) @@ -24,26 +30,33 @@ void SP_QTransmitByte(uchar byte, bit second) } } -void SP_QTransmitData(uchar* buf, uchar n, uchar cutT, bit second) +void SP_QTransmitData(uchar* buf, uchar n, bit second) { - uchar t; if (n) do { - SP_QTransmitByte(*buf, second); - t = cutT; - if (t) - while (--t) - ; + SP_QTransmitByte(*buf, second); buf++; } while (--n); } + +// void SP_QTransmitData(uchar* buf, uchar n, uint cutT, bit second) +// { +// if (n) +// do +// { +// SP_QTransmitByte(*buf, second); +// // if (SP_QTWait) +// // SP_QTWait(cutT); +// buf++; +// } while (--n); +// } #endif #ifdef USE_QUERY_RECEIVE -bit SP1_QRWait(uchar t) // 12MHz (t*1000 + 11)us above 1ms +bit SP1_QRWait(uint t) // 11.0592 约1ms { - uchar i; + unsigned char i, j; if (RI) { RI = 0; @@ -52,19 +65,20 @@ bit SP1_QRWait(uchar t) // 12MHz (t*1000 + 11)us above 1ms while (t) { t--; - for (i = 124; i; --i) - if (RI) - { - RI = 0; - return 1; - } + for (i = 11; i; --i) + for (j = 124; j; --j) + if (RI) + { + RI = 0; + return 1; + } } return 0; } -bit SP2_QRWait(uchar t) // 12MHz (t*1000 + 11)us above 1ms +bit SP2_QRWait(uint t) // 11.0592 约1ms { - uchar i; + unsigned char i, j; if (S2_CHECK(S2RI)) { S2_SET_L(S2RI); @@ -73,17 +87,18 @@ bit SP2_QRWait(uchar t) // 12MHz (t*1000 + 11)us above 1ms while (t) { t--; - for (i = 124; i; --i) - if (S2_CHECK(S2RI)) - { - S2_SET_L(S2RI); - return 1; - } + for (i = 25; i; --i) + for (j = 43; j; --j) + if (S2_CHECK(S2RI)) + { + S2_SET_L(S2RI); + return 1; + } } return 0; } -bit SP_QReceiveByte(uchar* buf, uchar t, bit second) +bit SP_QReceiveByte(uchar* buf, uint t, bit second) { if (second) { @@ -101,7 +116,7 @@ bit SP_QReceiveByte(uchar* buf, uchar t, bit second) return 0; } -uchar SP_QReceiveData(uchar* buf, uchar waitT, uchar cutT, bit second) +uchar SP_QReceiveData(uchar* buf, uint waitT, uint cutT, bit second) { uchar count = 0; if (SP_QReceiveByte(buf, waitT, second)) diff --git a/qt/Release.7z b/qt/Release.7z index bfcb0b1..788942f 100644 Binary files a/qt/Release.7z and b/qt/Release.7z differ diff --git a/qt/power_carrier/CMakeLists.txt b/qt/power_carrier/CMakeLists.txt index a857317..c6eb61c 100644 --- a/qt/power_carrier/CMakeLists.txt +++ b/qt/power_carrier/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) -project(power_carrier VERSION 0.2.0 LANGUAGES CXX) +project(power_carrier VERSION 0.2.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) diff --git a/qt/power_carrier/include/object/serialportworker.h b/qt/power_carrier/include/object/serialportworker.h index ebf1499..fb4056c 100644 --- a/qt/power_carrier/include/object/serialportworker.h +++ b/qt/power_carrier/include/object/serialportworker.h @@ -17,6 +17,7 @@ class SerialPortWorker : public QObject const QString ¤tPortName() const; + bool isChannel() const; public slots: void setAll(int br, int db, int sb, int p); diff --git a/qt/power_carrier/src/object/serialportworker.cpp b/qt/power_carrier/src/object/serialportworker.cpp index abfd7d9..381fb73 100644 --- a/qt/power_carrier/src/object/serialportworker.cpp +++ b/qt/power_carrier/src/object/serialportworker.cpp @@ -28,6 +28,11 @@ const QString &SerialPortWorker::currentPortName() const return portName; } +bool SerialPortWorker::isChannel() const +{ + return channelFlg; +} + void SerialPortWorker::setAll(int br, int db, int sb, int p) { @@ -170,7 +175,8 @@ void SerialPortWorker::receiveHostOrder() buf.clear(); buf.append(_ACK_WORD_); buf.append(_PC_ADDR_); - this->transmit(buf); + CRC16::ADD_XMODEM(buf); + m_sp->write(buf); break; case _CLOSE_ADDR_: channelFlg = false; diff --git a/qt/power_carrier/src/widget/serialportwidget.cpp b/qt/power_carrier/src/widget/serialportwidget.cpp index bb0c08b..28e0875 100644 --- a/qt/power_carrier/src/widget/serialportwidget.cpp +++ b/qt/power_carrier/src/widget/serialportwidget.cpp @@ -251,7 +251,9 @@ void SerialPortWidget::connectSPWorker() } if (!ui->log2LookPushButton->isChecked()) { - ui->log2TextEdit->setTextColor(QColor(0, 85, 0)); + ui->log2TextEdit->setTextColor( + spWorker->isChannel() ? QColor(0, 85, 0) : + QColor(Qt::darkMagenta)); ui->log2TextEdit->append(QString("[%1]:RX <- %2").arg( QTime::currentTime().toString("HH:mm:ss:zzz"), bytes.toHex(' ').trimmed().toUpper()