作者WolfLord (呆呆小狼)
看板ASM
标题Re: [问题] URAT的沟通限制
时间Tue Sep 24 02:38:39 2013
※ 引述《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)