二、初始化
#define configCPU_CLOCK_HZ (80000000UL) //80M
#define CAN_BUS_SPEED 250000 //250k
#define CAN1_FIFO_Numb 2 // 配置2块FIFO自动缓冲区
#define CAN_FIFO_LENG 32 // 每个FIFO配置32个报文
#define CAN_FRAM_SIZE 16 // 每个报文占有16字节
void CanInit(UINT canNo)
{
CAN_BIT_CONFIG canBitConfig;
CANEnableModule(CAN1,TRUE); //使能CAN1
CANSetOperatingMode(CAN1, CAN_CONFIGURATION); //配置模式
while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION); //配置模式确认
canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ; //相位缓冲段2
canBitConfig.phaseSeg1Tq = CAN_BIT_3TQ; //相位缓冲段1
canBitConfig.propagationSegTq = CAN_BIT_3TQ; //传播时间段
canBitConfig.phaseSeg2TimeSelect = TRUE; //阶段2 TRUE代码指定,FALSE模块自动设置
canBitConfig.sample3Time = TRUE; //bit被采样次数,TRUE 3次,FALSE 1次
canBitConfig.syncJumpWidth = CAN_BIT_2TQ; //同步段
CANSetSpeed(CAN1,&canBitConfig,configCPU_CLOCK_HZ,CAN_BUS_SPEED); //设置CAN总线波特率
//分配缓存区,2个FIFO,每个FIFO有32个报文,每个报文16个字节
CANAssignMemoryBuffer(CAN1,CAN1MessageFifoArea,(CAN1_FIFO_Numb *CAN_FIFO_LENG *CAN_FRAM_SIZE)); //分配缓存区 2*32*16
//FIFO0配置为发送帧,FIFO1配置为接收帧
CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, CAN_FIFO_LENG, CAN_TX_RTR_ENABLED, CAN_LOW_MEDIUM_PRIORITY); //FIFO0配置为发射远程帧,低中优先级
CANConfigureChannelForRx(CAN1, CAN_CHANNEL1, CAN_FIFO_LENG, CAN_RX_FULL_RECEIVE); //FIFO0配置为数据全接收(ID+Data)
//配置屏蔽器屏蔽所有位,FIFO0接收所有帧
CANConfigureFilter (CAN1, CAN_FILTER0, 0x1FFFFFFF, CAN_SID); //过滤器0配置
CANConfigureFilterMask (CAN1, CAN_FILTER_MASK0, 0x00000000, CAN_EID, CAN_FILTER_MASK_ANY_TYPE);//屏蔽器0配置为扩展帧/所有数据
CANLinkFilterToChannel (CAN1, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL1); //过滤器0、屏蔽器0、FIFO0匹配
CANEnableFilter (CAN1, CAN_FILTER0, TRUE); //使能过滤器0
CANEnableChannelEvent(CAN1, CAN_CHANNEL1, CAN_RX_CHANNEL_NOT_EMPTY, TRUE); //打开接收非空中断
CANEnableModuleEvent (CAN1, CAN_RX_EVENT, TRUE); //打开接收中断
INTSetVectorPriority(INT_CAN_1_VECTOR, INT_PRIORITY_LEVEL_3); //中断优先级 3
INTSetVectorSubPriority(INT_CAN_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); //子优先级 0
INTEnable(INT_CAN1, INT_ENABLED); //中断使能
CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION); //正常工作模式
while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION); //正常工作模式确认
}
|