Re: [問題] URAT的溝通限制

看板ASM作者 (呆呆小狼)時間10年前 (2013/09/24 02:38), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《hogiking (***ˋ(  ̄▽ ̄)ˊ***)》之銘言: 撰寫原則先知們已經推過了不贅述,我的話則是採用Queued的方式。 直接給你一段專案裡面挖出來碼自己參詳吧。(這是485UART轉CANBUS的 產品(已經量產銷售)韌體程式片段) // union CAN_Protocol{ unsigned char b[14]; struct CAN_ID{ unsigned long ID; unsigned char DLC; unsigned char D[8]; unsigned char CTL; }i; }; // unsigned char ser_tx_buf[256]; unsigned char ser_tx_widx; unsigned char ser_tx_ridx; unsigned char mon; union CAN_Protocol Comm; // // //Queued RS-485/UART Transmit Services void tx485_svr(void){ if(TXSTA1bits.TRMT){ if(ser_tx_widx!=ser_tx_ridx){ mon=ser_tx_buf[ser_tx_ridx++];//Move to mon for diagnostic TxEN=1; //Enable RS-485 Transmit bit Write1USART(mon); //Write to UART }else{TxEN=0;}//Transmite final, return to recive } } // //Queued Version RS-485/UART Transmit Function void tx485(unsigned char v){ unsigned char idx; idx=ser_tx_widx+1; while(idx==ser_tx_ridx){tx485_svr();} //buffer over flow, wait to push ser_tx_buf[idx]=v; //push charecter ser_tx_widx=idx; tx485_svr(); //Check Send status } // void uartProcess(void){ unsigned char i,bf; while(PIR1bits.RC1IF){ bf=RCREG1; // got char switch(bf){ case 27: for(i=0;i<14;i++){Comm.b[i]=0;}//Clean Command Quie break; case 13: if(Comm.i.DLC>0){ canTxD(&Comm); } for(i=0;i<14;i++){Comm.b[i]=0;}//Clean Command Quie break; default: bf-=48;if(bf>9){bf-=7;if(bf>15){bf-=32;}} if(bf<16){ for(i=0;i<13;i++){ShiftHfx(Comm,i);}//shift bit bf&=0xf;Comm.b[12]&=0xf0; Comm.b[12]|=bf; } break; } } } -- ~~於是,銀河的歷史又被海賊抹黑了好幾百頁。 - A23203 - BM4GRM Harlock WolfLord <<上站通知>> -- 我來啦! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.230.64.69 補一段CAN端回送的程式碼對照TX的使用 union CAN_Protocol CanRxb; const char *hexTAB="0123456789ABCDEFuck\0"; void SendByte(unsigned char a){ tx485(hexTAB[a>>4]); tx485(hexTAB[a&0xf]); } // void canReport(void){ unsigned char i; tx485('#');for(i=0;i<13;i++){SendByte(CanRxb.b[i]);} SendByte(DID); tx485(13);tx485(10); for(i=0;i<13;i++){CanRxb.b[i]=0;} } ※ 編輯: WolfLord 來自: 125.230.64.69 (09/24 02:50)
文章代碼(AID): #1IG8goMf (ASM)
文章代碼(AID): #1IG8goMf (ASM)