101规约源代码C

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*==============================================================*//*filename:canP101.c*//*targetenv:canbus+P101*//*author:*//*lastmodified:2009.02.09*//*description:ZHXJ_F206PLAT*//*copyright:XJELECTRICDISTRIBUTIONDIVISION*//**//*version:1.1*//*note:*//*==============================================================*/#define_P101canAPP_GLOBAL_#includeconfig.h#includemath.h/*****************************************///变量定义uint8FG_flag,resetlink_flag;uint8rxDir,rxPrm,rxFcb,lastFcb,rxFcv,rxFunctionCode;uint8number,rxControlField;/*fcb,lastFcb,fcv*/uint8typeID,reason;/*qualifier,sq,number,test,pn,commonAdd*/uint8*rxBuf;uint8txBuf[261],txAppBuf[253],I2C_flash_buf[253];uint8txFunctionCode,txDir,txPrm,txFcb,txFcv,lastacd,linkState;uint16txLength,txAppLength;uint8FH_Select,FH_Add,YK_ERR;uint8re_recognize_flag,re_recognize_length;uint16rxLength,re_recognize_type;//重发变量定义uint8R_Send_Count,R_Send_Time,R_Send_Flag_ST,R_Send_Flag,R_Soe_Num;uint8R_Soe_Data[20][10];uint16R_varyxStat1,R_varyxStat2;//外部变量声明externuint8M12,clock_Flag;//externuint8Txflag0;externuint8rSelect,ykFlag,ykOverFlag,control_over1,control_over2,control_over3;externuint16dout;//externuint16LINK_ADD;//拨码链路地址externuint8cosFlag,soeFlag;externuint16yxDI;externuint8analogchanged;externuint16Analog[22];externuint16Old_Analog[22];externuint16config1[48];externuint16YXOfacu[24],oldYXOfacu[24];externuint16varyxStat1,varyxStat2;externuint16soeNumber,txsoenum,over20,soe[20][10],flashsoe[20][22];externuint16mSecond;externuint16miliSeconds,minute,hour,date,day,month,year;/*==============================================================*//***************************初始化***************************/voidinit_link_app(void){uint8i,j;FG_flag=0;txDir=1;txPrm=0;txFcb=0;txFcv=0;number=0;reason=0;resetlink_flag=5;rxControlField=0;txFunctionCode=0;txLength=0;txAppLength=0;FH_Select=0;FH_Add=0;YK_ERR=0;//重发变量初始化R_Send_Count=0;R_Send_Time=0;R_Send_Flag_ST=0;R_Send_Flag=0;R_varyxStat1=0;R_varyxStat2=0;R_Soe_Num=0;for(i=0;i20;i++){for(j=0;j10;j++)R_Soe_Data[i][j]=0;}for(i=0;i253;i++)//252?253{txAppBuf[i]=0;I2C_flash_buf[i]=0;}}/*************************链路层处理*************************//*接收*/uint8p101_rxLink(void){if(noError()){rxDir=rxControlField7&~(~01);//取得DIR位rxPrm=rxControlField6&~(~01);//取得PRM位rxFcb=rxControlField5&~(~01);//取得FCB位rxFcv=rxControlField4&~(~01);//取得FCV位rxFunctionCode=rxControlField&~(~04);//取得功能码if((rxDir==0)&&(rxPrm==1)&&(rxFcv==0))returnTRUE;//接收标志位判断else{//newFrame=1;lastFcb=rxFcb;returnTRUE;}}elsereturnFALSE;}/*endp101_rxLink()*//*************************//*接收校验*/uint8noError(void){uint8i;uint16station=0;if(rxBuf[0]==0x10)/*固定帧*/{/*frame=1;*/station=(rxBuf[3]8);station+=rxBuf[2];if((station!=config1[LINK_ADDR])&&(station!=0xffff))returnFALSE;elseif(rxBuf[4]!=check_sum())//检验校验和是否正确returnFALSE;elseif(rxBuf[5]!=0x16)//检验结束字符是否为0x16returnFALSE;else//校验OK,取得控制域{rxControlField=rxBuf[1];rxControlField&=0xff;returnTRUE;}}if(rxBuf[0]==0x68)/*可变帧*/{/*frame=2;*/station=(rxBuf[6]8);station+=rxBuf[5];if(rxBuf[0]!=rxBuf[3])//检验报文2个0x68是否一致returnFALSE;elseif(rxBuf[1]!=rxBuf[2])//检验报文2个报文长度是否一致returnFALSE;elseif((station!=config1[LINK_ADDR])&&(station!=0xffff))returnFALSE;else{i=rxBuf[1];if(rxBuf[i+4]!=check_sum())//检验校验和是否正确returnFALSE;elseif(rxBuf[i+5]!=0x16)returnFALSE;else{rxControlField=rxBuf[4];rxControlField&=0xff;returnTRUE;}}}returnFALSE;}/*endnoError()*//*************************/uint8check_sum(void){uint8i;uint8sum=0;if(rxBuf[0]==0x10)//取固定帧校验和{for(i=0;i3;i++)sum+=rxBuf[i+1];}elseif(rxBuf[0]==0x68)//取可变帧校验和{for(i=0;irxBuf[1];i++)sum+=rxBuf[i+4];}returnsum;}/***************************************************************************************************************************函数原型:voidThread_CAN_TX(uint16length,uint8*can_send_txbuf)**参数说明:length发送码总长度can_send_txbuf发送码所在缓冲区首址**返回值:**说明:将数据发送到CAN*************************************************************************************************************************/voidThread_CAN_TX(uint16length,uint8*can_send_txbuf){uint16i=0,j,k,DLC,m=0,n;UINT32status=0;stcTxBUFCTbuf;uint8can_frame_txbuf[7]={0x01,0x00,0x00,0x00,0x00,0x28,0x00};//can_frame_txbuf[5]=AI_ID;can_frame_txbuf[5]=40;k=length;j=(uint16)((k/8)+1);for(n=0;nj;n++){if(k=4){DLC=k;CTbuf.TxFrameInfo.Bits.PRIO_BIT=can_frame_txbuf[0];//CAN通道号CTbuf.TxFrameInfo.Bits.FF_BIT=(can_frame_txbuf[1]&0x80)7;//帧格式CTbuf.TxFrameInfo.Bits.RTR_BIT=(can_frame_txbuf[1]&0x40)6;//帧类型CTbuf.TxFrameInfo.Bits.DLC_BIT=DLC;//数据长度for(i=0;i4;i++){*((uint8*)&CTbuf.TxCANID.Word+i)=can_frame_txbuf[5-i];}memcpy((uint8*)&CTbuf.CANTDA,&can_send_txbuf[m],DLC);status=CANSendData(can_frame_txbuf[0],can_frame_txbuf[6],&CTbuf);for(i=0;i2000;i++);//两帧之间加延时否则dsp板接收数据会丢失}if((4k)&&(k=8)){DLC=k;CTbuf.TxFrameInfo.Bits.PRIO_BIT=can_frame_txbuf[0];//CAN通道号CTbuf.TxFrameInfo.Bits.FF_BIT=(can_frame_txbuf[1]&0x80)7;//帧格式CTbuf.TxFrameInfo.Bits.RTR_BIT=(can_frame_txbuf[1]&0x40)6;//帧类型CTbuf.TxFrameInfo.Bits.DLC_BIT=DLC;//数据长度for(i=0;i4;i++){*((uint8*)&CTbuf.TxCANID.Word+i)=can

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功