TMS320F28335的CAN通信例程
时间: 2023-09-11 13:11:32 浏览: 357
以下是TMS320F28335的CAN通信例程:
```c
#include "F2837xD_device.h"
#include "F2837xD_CpuTimers.h"
#include "F2837xD_Examples.h"
#include "F2837xD_Gpio.h"
#include "F2837xD_Ipc_drivers.h"
#include "F2837xD_Ipc.h"
#include "F2837xD_PieCtrl.h"
#include "F2837xD_PieVect.h"
#include "F2837xD_SysCtrl.h"
#include "F2837xD_can.h"
//定义CAN消息对象
struct ECAN_REGS ECanaShadow;
void InitECana(void){
//使能ECAN模块
EALLOW;
CpuSysRegs.PCLKCR10.bit.ECAN_A = 1;
EDIS;
//配置GPIO管脚为CAN引脚
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO30 = 0;
GpioCtrlRegs.GPAQSEL2.bit.GPIO30 = 3;
GpioCtrlRegs.GPAMUX2.bit.GPIO30 = 1;
GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;
GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;
GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 1;
EDIS;
//复位ECAN模块
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.RESET = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while (ECanaRegs.CANES.bit.CCE != 1) {}
ECanaShadow.CANES.all = ECanaRegs.CANES.all;
ECanaShadow.CANES.bit.CAN_ESP = 1;
ECanaRegs.CANES.all = ECanaShadow.CANES.all;
while (ECanaRegs.CANES.bit.CAN_ESP != 1) {}
//配置ECAN模块
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.SCB = 1;
ECanaShadow.CANMC.bit.DBO = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
ECanaShadow.CANBTC.all = 0;
ECanaShadow.CANBTC.bit.TSEG1REG = 11;
ECanaShadow.CANBTC.bit.TSEG2REG = 2;
ECanaShadow.CANBTC.bit.BRPREG = 4;
ECanaRegs.CANBTC.all = ECanaShadow.CANBTC.all;
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while(ECanaRegs.CANES.bit.CCE != 0) {}
//配置ECAN接收器
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while(ECanaRegs.CANES.bit.CCE != 0) {}
ECanaShadow.CANMD.all = ECanaRegs.CANMD.all;
ECanaShadow.CANMD.bit.RMD = 0;
ECanaRegs.CANMD.all = ECanaShadow.CANMD.all;
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 0;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while(ECanaRegs.CANES.bit.CCE != 0) {}
}
void ConfigECanaMessage(uint32_t MsgID, uint32_t MsgMask, uint32_t MsgFlags){
ECanaRegs.CANME.all = 0;
ECanaShadow.CANME.all = ECanaRegs.CANME.all;
ECanaShadow.CANME.bit.ME0 = 1;
ECanaRegs.CANME.all = ECanaShadow.CANME.all;
ECanaShadow.CANMAM.all = 0x1FFFFFFF;
ECanaRegs.CANMAM.all = ECanaShadow.CANMAM.all;
ECanaRegs.CANMIM.all = 0;
ECanaShadow.CANMIM.all = ECanaRegs.CANMIM.all;
ECanaShadow.CANMIM.bit.MIM0 = 1;
ECanaRegs.CANMIM.all = ECanaShadow.CANMIM.all;
ECanaShadow.CANMSGCTRL.all = 0;
ECanaRegs.CANMSGCTRL.all = ECanaShadow.CANMSGCTRL.all;
ECanaShadow.CANMSGID.all = MsgID;
ECanaRegs.CANMSGID.all = ECanaShadow.CANMSGID.all;
ECanaShadow.CANMSGID.bit.AAM = 1;
ECanaShadow.CANMSGID.bit.AME = 0;
ECanaRegs.CANMSGID.all = ECanaShadow.CANMSGID.all;
ECanaShadow.CANMSGID.all = MsgMask;
ECanaRegs.CANMSK.all = ECanaShadow.CANMSGID.all;
ECanaShadow.CANMSGCTRL.all = MsgFlags;
ECanaRegs.CANMSGCTRL.all = ECanaShadow.CANMSGCTRL.all;
}
void SendECanaMessage(void){
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while(ECanaRegs.CANES.bit.CCE != 0) {}
ECanaRegs.CANTRS.all = 0x0001;
EALLOW;
PieVectTable.ECAN0INTA = &ECana_isr;
EDIS;
PieCtrlRegs.PIEIER9.bit.INTx5 = 1;
IER |= M_INT9;
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 0;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
while(ECanaRegs.CANES.bit.CCE != 0) {}
}
interrupt void ECana_isr(void){
uint32_t ulStatus;
ulStatus = ECanaRegs.CANGIF0.all;
ECanaRegs.CANGIF0.all = ulStatus;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}
```
以上是一个简单的CAN通信例程,其中包括了CAN模块的初始化、CAN消息对象的配置和发送CAN消息等。请注意,这只是一个示例代码,具体实现还需要根据实际情况进行调整。
阅读全文