Re: [问题] URAT的沟通限制

楼主: WolfLord (呆呆小狼￾ ￾ N￾ ￾ )   2013-09-24 02:38:39
※ 引述《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;
}
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com