28335的CAN总线学习笔记.pdf

所需积分/C币:49 2017-03-26 10:54:28 836KB PDF
28
收藏 收藏
举报

个人对TMS320F28335的CAN总线学习笔记
32.// all bits (including reserved bits) of MSGCTRL must be initialized to zero 33 34 ECanaMboxes. MBOX0. MSGCTRL all=0x00000000; 35 ECanaMboxes. MBoX1. MSgCtrl all= 0x00000000 36 ECanaMboxes, mboX2 MSgCtrl all x00000000 ECanaMboxes. MboX3. MSGCTRL all= 0x00000000 ECanaMboxes, mboX4. MSgCtrl all= 0x00000000 39 ECanaMboxes, mBoX5. MSgCtrl all= 0x00000000 ECanaMboxes. MBOX6. MSGCTRL all=0X00000000; ECanaMboxes. MBOX7 MSGCTRL all= 0x00000000: ECanaMboxes. MBOX8. MSGCTRL all =0x00000000; ECanaMboxes. MBOX9. MSGCTRL all=0x00000000; 44 ECanaMboxes mBoX10, MSGCtrL all= 0x00000000 ECanaMboxes, mBoX11. MSGCtrl all= 0x00000000 46 ECanaMboxes mBoX12 MSgCtrl all= 0x00000000 ECanaMboxes. MBOX13. MSGCTRL all=0X00000000; ECanaMboxes mBoX14. MSgCtrl all= 0x00000000 ECanaMboxes, MBoX15. MSGCtRL all=0x00000000 5日. ECanaMboxes, mboX16. MsgCtrl all= 0x00000000 ECanaMboxes. MBOX17. MSGCTRL all=0X00000000; 52 ECanaMboxes mBoX18. MSGCTRL all= 0x00000000 ECanaMboxes. MBoX19. MSGCtrL all= 0x00000000 54 ECanaMboxes mBoX20. MSGCtrl all= 0x00000000 55 ECanaMboxes MboX21. MSgCtrl all=0x00000000 56 ECanaMboxes, mBoX22. MSGCtrL all= 0x00000000 ECanaMboxes mboX23, MSgCtrl all= 0x00000000 58 ECanaMboxes mbox24, msgctrl all= 0x00000000 ECanaMboxes. MBOX25. MSGCTRL all=0x00000000; 69 ECanaMboxes. MBOX26. MSGCTRL all=0X00000000; 61。 ECanaMboxes. MBOX27 MSGCTRL all=0X00000000: 62 ECanaMboxes, mboX28. MSGCTRL all= 0X00000000 63 ECanaMboxes. MBOX29. MSGCTRL, all=0X00000000; ECanamboxes. mbox30, msgctrl all= 0x00000000 65 ECanaMboxes. MBOX31. MSGCTRL all=0x00000000; 66 67.//TAn, RMPn, GIFn bits are all zero upon reset and are cleared again 68.// as a matter of precaution ECanaRegs CANTAall= OXFFFFFFFF;/ Clear all TAn bits * 91234万 ECanaRegs. CANRMP, all=OXF FFFFFFF;/ Clear all RMPn bits */ ECanaRegs. CANGIF0 all=0XFFFFFFFF;/ Clear all interrupt flag bits */ ECanaRegs. CANGIF1all=OXFFFFFFFF: 76 8./*Configure bit timing parameters for eCANA* 79 ECanaShadow, CANMC all= ECanaRegs CANMC all; ECanashadow, caNMc, bit. CCR= 1 // Set CCR= 1 81 ECanaRegs. CANMC all= ECanaShadow. CANMC all: ECanaShadow, CANES. all= ECanaRegs CANESall do 86 ECana Shadow CANES. all= ECanaRegs CANES all 88 I while(ecanashadow CANES bit CCE =1); // Wait for cce bit to be set 89 99 ECanashadow, canbtc all= 0 91 #立f( CPU FRQ158HZ) 93.初始化通信波特率 94 95.具体的波特率计算 96.波特率=系统时钟/(2*( BRPREG+TSEG2REG+TSE61REG+3),同时需要确保TSEG1REG>TSEG2EEG 98 ECanaShadow, canbtc bit brpreg= 9 199 ECanashadow, CANBtC. bit. TSEG2REG 2 ECanashadow, CANBtC. bit. tseg1reg 10 182 fendi 193 ECanaShadow. CANBTC bit SAM= 1; 194 ECanaRegs. CANBTC all=ECana Shadow. CANBTC all; ECanaShadow. CANMC all= ECanaRegs CANMC all; 196 ECanashadow, CANMc bit ccr= a // Set Ccr= 0 ECanaRegs. CANMC all= ECanaShadow. CANMC all; 198. ECanaShadow CANES all= ECanaRegs CANES all; 199 do 118 111 ECanashadow CANES all= ECanaRegs CANEs all 12 I while(ecanashadoW CANESbit CCE=0); // Wait for cce bit to be cle ared 114./*Disable all Mailboxes */ 115 ECanaRegs CANME all =0 / Required before writing the MSGIDS 116 117 EDIS: 118 120 121 122.void InitECanGpio(void 24。 InitECanaGpio()j 125.#if (DSP28 ECANB) 126 InitECanbGpio() 127.#endif / if DSP28 ECANB 128 129 130.void InitECanaGpio(void) 32 EALLOW; 133 s Enable internal pull-up for the selected CAN pins * 34.// Pull-ups can be enabled or disabled by the user 135.//This will enable the pullups for the specified pins 36.//Comment out other unwanted lines 37 Gpioctrlregs. GPAPUD, bit. GPI018= 0 // Enable pull-up for GPIO18(CANRXA) 138 GpioctrlRegs, GPAPUD. bit. GPI019=0 / Enable pull-up for GPI019(CANTXA) 149 Set qualification for selected CAN pins to asynch only // Inputs are synchronized to SYSCLKoUT by default 142.// This will select asynch (no qualification) for the selected pins GpioctrIRegs. GPAQSEL2bit GPI018= 3:// Asynch qual for GPI018(CANRXA) Configure eCAN-A pins using GPIo regs 145.//This specifies which of the possible GPIo pins will be eCAN functional pins 146 GpioctriRegs. GPAMUX2 bit GPI018= 3;// Configure GPI018 for CANRXA operati on GpioctrIRegs. GPAMUX2. bit. GPI019=3 / Configure GPI019 for CANTXA operati 149 EDIS 151 #if (DSP28 ECANB void InitECanbGpio( void) 154. EALLOW; 156./ Enable internal pull-up for the selected CAN pins */ // Pull-ups can be enabled or disabled by the user 158.// This will enable the pullups for the specified pins 159.// Comment out other unwanted lines 169 GpioctrlRegs. GPAPUD bit GPIO12= 0; / Enable pull-up for GPIO12(CANTXB) GpioctrIRegs. GPAPUD bit GPI013= 0; / Enable pull-up for GPIO13(CANRXB 162./* Set qualification for selected CAN pins to asynch only */ // Inputs are synchronized to sysclkout by default 164.//This will select asynch (no qualification) for the selected pins // Comment out other unwanted lines 166 GpioctrlRegs. GPAQSEL1bit GPIO13= 3;// Asynch qual for GPI013(CANRXB) 167./* Configure eCAN-B pins using GPIo regs*/ GpioctrlRegs. GPAMUXl bit GPIO12= 2;// Configure GPIO12 for CANTXB operation 169 GpioctrIRegs. GPAMUX1bit GPI013=2; //Configure GPI013 for CANRXB operation 178 17 EDIS: 174.#endif// if DSP28_ECANB 具关于CAN总线中的收发问题: 1、发送 2.1配置邮箱用于传输 为了传输一个消息,必须执行以下步骤(本例中以邮箱1为例): 1.将 CANTRS寄存器中适当位清零: 清除 CANTRS1=0(向TRS写入0无效:相反设置TRR1并等待直到TRS1清除。)如果RTR位 被置位,TRS可以发送一个远程帧。一旦一个远程帧被发送,则邮箱的TRS位被CAN模块清除。因 此,同一个邮箱可被用来从其它节点中请求一个数据帧。 2.通过清除邮箱使能( CANME)寄存器的相应位来禁用邮箱。 清除 CANME filter1=0 3.加载邮箱的消息标识符(MSG|D)寄存器。对于正常的发送邮箱(MSG|D30=0和MsGD29=0),清除 AME(MsGD.30)和AAM(MsGD.29)位。在操作过程中,该寄存器通常不会被修改。只有在邮箱被 禁用时,才能把它修改。例如: (a)写入MSG|D(1)=0×15Ac0000 (b)消息控制字段寄存器( MSGCTRL.30)DLC字段写入数据的长度。RTR标志通常都会被清除 ( MSGCTRL4=0)。 (c)通过清除 CANMD寄存器的相应位来设置邮箱方向。 (d)清除 CANMD1=0 4.通过设置 CANME寄存器的相应位设置邮箱启用 设置 CANME1=1 这将邮箱1配置为传输模式 2.2传输一个消息 为了开始一个传输(在这个例子中,对于邮箱 1.将消息数据写入邮箱数据字段。 (a)由于在配置时,DBO(Mc.10)设置为零, MSGCTRL(1)设置为2,数据存储在 CANMDL(1)2个最 有效字节中。 (b)写入 CANMDL(1)=xX000h 2.在发送请求寄存器中设定相应的标志( CANTRS1=1)来启动的消息传输。现在CAN模块处理CAN消 息的完整传输。 3.等待,直到相应邮箱的发送确认标志被设置(TA.1=1)。成功传输后,CAN模块设置此标志。 4.传输成功或中止传输后,模块将TRS标志复位为0(TRS1=0) 5.为了下一个传输(从同一邮箱)发送确认必须清除。 (a)设置TA1=1 (b)等到TA读取为0 6.要在同一邮箱发送另一条消息,邮箱RAM中的数据必须更新。设置TRS.1标志来启动下一个传输。写 入邮箱RAM中的可以是半字(16位)或整字(32位),但模块总是从偶数边界返回32位。CPU必 须接受所有32位或其中的一部分。 具体描述参见上图,配置程序如下所示 /发送一桢数据 3.cAN总线发送数据结构体 4.typedef struct unsigned short int staId;//标准帧ID,值为⊙x990到x7FFF; unsigned long int ExtId;/)扩展帧ID,值为到x1FFFF unsigned char SAE_1939F1ag;//表示是否使用SAE]1939协议 SAE ID SAE J1939 ID: unsigned char IDE;/帧类型,可为: CAN ID STD(标准帧), CAN ID EXT( 扩展帧) unsigned char dLC;//数据长度,可为9到8; unsigned char№ Box num;/邮箱编号,可为θ到31; unsigned short int Tx timeout cnt; CAN MSG DATA CAN TX msg_ data;/*!<帧消息内容,共8字节 f CanTxMsg; void CAN Send Msg( CanTXMsg* can tx msg)//发送一帧数据 Uint16 time cnt Uint32 mbox enable temp=0x0000; 19 Uint32 mbox disable temp= 0x0000 11 Uint32 mbox dir temp=0x0000; mbox enable temp=1<<(can tx msg->MBox num); mbox disable temp=-(1<<(can tx msg->MBoX num)); 14 mbox dir temp =-(<<can tx msg->MBox num)); struct ecan REGS ECanashadow 16 volatile struct mBox * Mailbox Mailbox =&ECanaMboxes MBOX0+can tx msg->MBoX num; ECanaShadow CANME all= ECanaRegs CANME all; 29 ECanaShadow CANME all &=mbox disable temp; ECanaRegs CANME all ECana Shadow CanMe all if(can tx msg->IDE=- CAN_ ID STD) 23 24 Mailbox->MSGID. all= can tx msg->StdId; //standard identifier 26。 Mailbox- MSGID, bit. IDE= can tx msg->IDE; 28 else if(can tx msg->IDE=- CAN ID_ EXT) 39 if(can tx msg->SAE J1939 Flag ==0) 31 Mailbox->MSGID. all= can tx msg->ExtId; //extended identifier 55 Mailbox->MSGID bit. IDE= can tx msg->IDE; 35。 else 36 37 Mailbox->MSGID. all= can tx msg->SAE_ J1939 ID id; //extended identifie 38 Mailbox->MSGID bit. IDE= can tx msg->IDE 39 ECanaShadow. CANMD all= ECanaRegs. CANMD all; ECana shadow,CAND.a118= mbox dir temp;/设置邮箱上作方向,表示邮箱上作于发送,1 表示工作于接收 ECanaRegs. CANMD all=ECana Shadow. CANMD all ECanaShadow CANME all= ECanaregs CANMe all ECana shadow. CANME,a11|= mbox enable temp;//使能邮箱 46 ECanaRegs CANME all= ECanaShadow CANME all Mai1box-> MSGCTRL.bit,DLC= can tx msg->DLC;//数据长度 lailbox->MDL byte. BYTE0= can tx msg->CAN TX msg data msg Byte byte; Mailbox->MDL byte BYTE1= can tx msg->CAN TX msg data msg Byte bytel; 59. Mailbox->MDL byte. BYTE2- can tx msg->CAN TX msg data msg Byte. byte2; Mailbox->MDL byte. BYTE3- can tx msg>CAN TX msg data msg Byte byte; Mailbox->MDH byte BYTE4= can tx msg->CAN TX msg data msg Byte byte4; Mailbox->MDH, byte. BYTE5= can tx msg->CAN TX msg data msg Byte bytes; 54 Mailbox->MDH byte. BYTE6= can tx msg->CAN TX msg_ data msg Byte. byte6; 55 Mailbox->MDH, byte. BYTE7- can tx msg->CAN Tx msg_ data msg Byte. byte7; 56 ECanaShadow, cAntos. all= 0 ECanaShadow. CANTRSall= mbox enable temp // Set TRs for mailbox under test 58 ECanaRegs. CANTRS all= ECana Shadow. CANTRS all do 69 ECanaShadow CANTA all= ECanaRegs CANTA all time cnt++; 63 I While(((ECanaShadow CANTA all&mbox enable temp)==0)&&(time cnt<100)) Wait for TAs bit to be set 64 ECanashadow. CANTA. all= 0 ECanaShadow CANTA all= mbox enable temp / Clear TA5 66 ECanaRegs CANTA all= ECanaShadow CANTA all; 67 68. 接收相关配置:(数据接受通常会采用中断来接收数据具体配置程序如下,包括中断的相关配 置 1. void CAN RX Config(void)/接收配置函数,程序中相关备注 2. struct ECAN REGS ECanaShadow; 5 ECanaShadow CANME all= ECanaRegs, CANME. all; ECanashadow, canme bit me1=6;/不使能邮箱1 ECanashadow, CANME bit Me31=6;//不使能邮箱31 8 ECanaRegs CANME all= ECanaShadow CANME all; 9 以下代码是配置接受邮箱的相关代码 //邮箱1相关配置 ECanaMboxes.MBOX1. MSGCTRL. bit dlc=8;//配置数据长度,应该是没意义的; 12 ECanaMboxes.MBOX1. MSGID.a11=8x8799ADC;//设置接收消息的有效ID 13 ECanaMboxes, mBox1.MGID.bit.AME=1;/屏蔽使能位,如果需要使用屏蔽,必须将该位置1 14。 ECanaMboxes, MBOX1 MSGID bit. IDE= CAN ID EXT 16 LAMn[28:0] 这些位启用一个进入消息的任意标识符位的屏蔽。 1针对接受到的标识符的相应位,接受一个或1(无关) 19 8接收到的标识符位值必须与 MSGID寄存器的相应标识符位相匹配 29 21 22 ECanaLAMRegs, LAM1.all=0x000000F;// LAMI本地接受屏蔽标识符扩展位 25。 1可以接收标准和扩展帧。在扩展帧的情况下,标识符的所有29位被存储在邮箱 中,木地接受屏 蔽寄存器的所有29位被过滤器使用。在一个标准帧的情况卜, 只有标识符的头11个位(28至 18位) 26 和木地接受屏蔽被使用。 8存储在邮箱中的标识符扩展位决定了哪些消息应该被接收到 28 29 ECanaLAMRegs, LAM1bit LAMI= 1; //邮箱31相关配置 ECanaMboxes,MBOX31. MSGCTRL.bit,DLC=8;//配置数据长度,应该是没意义的 ECanaMboxes, mBoX31 MSGID.a11=0x0799ABC;//设置接收消息的有效ID ECanaMboxes,MBOX31 MSGID.bit,AME=1;//屏蔽使能位, 34 ECanaMboxes, MBoX31 mSGiD, bit. IDE CAN ID EXT 35 36 AM[28: 这些位启用个进入消息的任意标识符位的屏蔽。 38 1针对接受到的标识符的相应位,接受个6或1(无关) 8接收到的标识符位值必须与MGID寄存器的相应标识符位相匹配。 49 444 ECanaLAMRegs. LAMB1all=0x000000F; / LAMI本地接受屏蔽标识符扩展位 1可以接收标淮和扩展帧。在扩展帧的情况下,标识符的所有29位被存储在邮箱中,本 也接長屏蔽寄 存器的所有29位被过滤器使用。在一个标准帧的情况下,只有标识符的头11个位(2 8至18位) 和本地接受屏蔽被使用 θ存储在邮箱中的标识符扩展位决定了哪些消息应该被接收到 ECanaLAMRegs. LAM31 bit LAMI =1 ECanaRegs. CANRMP all OxFFFFFFFF ECanashadow. CANMD all ECanaRegs. CANMD all; ECanashadow, caned, bit mD1= 1 ECanashadow. CANMD, bit. MD31 =1: ECanaRegs. CANMD all ECanaShadow, canmD all ECanashadow. canme all ECanaRegs CANME all; 19 ECanashado, canme bit me1=1;//使能邮箱1 11 ECanashadow,CAME. bit me31=1;//使能邮箱1 12 ECanaRegs CANME all ECanaShadow. canme all 14. void CAN RX IT Concig(void)/邮箱中断相关配置 16 EALLOW 17。 ECanaRegs. CANMIM, bit. MIM1=1;/使能中断邮箱1的中断 18 ECanaRegs, CANMIL. bit. Mil1=1;//将中断1连接至中断1; 19 ECanaRegs. CANMIM, bit. MIM31=1;//使能中断邮箱31的中断 29 ECanaRegs. CANMIL, bit, MIL31=1;//将中断1连接至中断1; ECanaRegs. CANGIM.bit.IEN=1;//使能中断1 22 EDIS; 23. /只体的中断函数,在清除GMIF1标志位时,不能通过向GMIF1写1来清除,只能通过向RMPn来清除 该标志位 25 26.CAN总线接收数据的结构体 typedef struct unsigned short int staId;/标准帧ID,值为8x08到ex7FFF unsigned long int ExtId;//扩展帧工D,值为到×1FFF unsigned char SAE_1939F1ag;//表小是合使用SAE1939协议 SAE_ ID SAE了1939ID; unsigned char IDE;//帧类型,可为: CAN ID STD(标准帧), CAN ID EXI(扩展 帧) unsigned char DLC;/数据长度,可为到8 unsigned char MBox num;//发送所用邮箱编号 unsigned short int Rx timeout cnt; CAN MSG_ DATA CAN RX msg_data;/*!<帧消息内容,共8字节*/ f CanRXMsg; 28 29 interrupt void Cana isrl(void) 38. if(ECanaRegs. CANGIF1. bit. GMIF1 if(ECanaRegs CANRMP. bit. RMP1 == 1) 34 35./读取该位是知道当前哪一个邮箱收到数据, 36 can rx msg MBox num= ECanaRegs CANGIFl bit MIV1 can rx msg DLC= ECanaMboxes MBOX1 MSGCTRL, bit DLC; 38 can rx msg. IDE= ECanaMboxes MBOX1 MSGID bit. IDE: if(can rx msg. IDE== CAN_ID_ EXT) 49 41 can_rx msg. ExtId= ECanaMboxes MBOX1 MSGID.all&0X1FFFFFFF; 42 can rx msg. SAE J1939_ IDid= can rx msg. ExtId; 44 else if(can rx msg. IDE = CAN_ ID STD) 45 46 can rx msg. StdId= ECanaMboxes MBOX1 MSGID, bit. STDMSGID; 48 can rx msg CAN RX msg data msg Byte byte@= ECanaMboxes MBOX1 MDL byte BYTE can rx msg CAN_ RX msg_ data msg Byte byte1= ECanaMboxes MBOX1 MDL byte BYTE1 can rx msg CAN_RX msg data msg Byte. byte2- ECanaMboxes MBOX1 MDL byte BYTE2 can rx msg CAN RX msg data msg Byte. byte3- ECanaMboxes MBoX1 MDL byte BYTE3 can rx msg CAN_ RX msg data msg Byte byte4- ECanaMboxes MBOX1 MDH byte. BYTE4 can rx msg CAN RX msg_ data msg Byte bytes- ECanaMboxes MBOX1 MDH byte BYTE5 can rx msg CAN RX msg data msg Byte. byte6= ECanaMboxes MBOX1 MDH byte BYTE6 can rx msg CAN RX msg data msg Byte byte= ECanaMboxes MBOX1MDH byte BYTE7 56 ECanaRegs. CANRMP. bit. RMP1 =1: 58 else if(ECanaRegs CANRMPbit RMP31==1 69 can rx msg MBox num= ECanaRegs CANGIF1 bit MIV1: can rx msg DLC= ECanaMboxes MBOX31 MSGCTRL bit DLC, 62 can rx msg. IDE= ECanaMboxes MB0X31 MSGID bit. IDE; 63 if(can rx msg. IDE = CAN ID EXT) 64 65 can rx msg. ExtId=ECanaMboxes MBOX31 MSGID all&0X1FFFFFFF; 66 can rx msg. SAE J1939 IDid= can rx msg. ExtId;

...展开详情
试读 11P 28335的CAN总线学习笔记.pdf
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
haoerhei 不错,很有用!
2019-08-06
回复
qq_38100207 用起来还不错
2018-01-06
回复
  • GitHub

    绑定GitHub第三方账户获取
  • 分享宗师

    成功上传21个资源即可获取
关注 私信 TA的资源
上传资源赚积分or赚钱
    最新推荐
    28335的CAN总线学习笔记.pdf 49积分/C币 立即下载
    1/11
    28335的CAN总线学习笔记.pdf第1页
    28335的CAN总线学习笔记.pdf第2页
    28335的CAN总线学习笔记.pdf第3页

    试读结束, 可继续读1页

    49积分/C币 立即下载 >