21ic问答首页 - AC7801X CAN ID设置后 有问题
AC7801X CAN ID设置后 有问题
瘫着2021-03-02
参考 官方例程改的 不知道是哪里没有设置对 过滤ID有问题 前面两个ID一直收不到 (使用的是回环模式)
CAN_MsgInfoType g_sendCANMsgInfo;
CAN_MsgInfoType g_recvCANMsgInfo;
CAN_FilterControlType canFilterControl01[]=
{
{0, ENABLE, CAN_ID_1, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl02[]=
{
{1, ENABLE, CAN_ID_2, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl03[]=
{
{2, ENABLE, CAN_ID_3, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl04[]=
{
{3, ENABLE, CAN_ID_4, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl05[]=
{
{4, ENABLE, CAN_ID_5, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl06[]=
{
{5, ENABLE, CAN_ID_6, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
u8 sendDataBuff[CAN_SEND_LENGTH] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
u8 recvDataBuff[CAN_RECIEVE_LENGTH] = {0};
u8 can_recieve_buffer[CAN_RECIEVE_LENGTH] = {0};
void CAN0_IRQnCallBack(void *device, uint32_t wpara, uint32_t lpara);
void CAN0_gpio_init(void)
{
CKGEN_Enable(CLK_CAN0, ENABLE);
GPIO_SetFunc(GPIOB, GPIO_PIN0, GPIO_FUN3);//CAN0_TX
GPIO_SetFunc(GPIOB, GPIO_PIN1, GPIO_FUN3);//CAN0_RX
// GPIO_SetFunc(GPIOC, GPIO_PIN8, GPIO_FUN0);//CAN0_STB
// GPIO_SetDir(GPIOC, GPIO_PIN8, GPIO_OUT);//ÉèÖÃCAN0ÊÕ·¢Æ÷¿ØÖÆΪIO¿ØÖÆ
//CAN0_TRASCVER_NML;//ʹÄÜCANÊÕ·¢Æ÷
// GPIO_SetPinLevel(GPIOC, GPIO_PIN8, GPIO_LEVEL_LOW);
}
void CAN_InitHardwr(void)
{
CAN_ConfigType canConfig;
CAN_BitrateConfigType canBandrateConfig;
//CAN_FilterControlType CAN_FilterControlTypedef;
CAN0_gpio_init();
/*
ÉèÖò¨ÌØÂÊΪ500K,²ÉÑùµãΪ81.25%.
tSeg1 = (S_SEG_1 + 2); tSeg2 = (S_SEG_2 + 1).
BandRate = (48M / (S_PRESC + 1) / ((S_SEG_1 + 2) + (S_SEG_2 + 1)))
SamplePoint = (tSeg1 / (tSeg1 + tSeg2)).
ÔÚÒѾ֪µÀ²¨ÌØÂÊÓë²ÉÑùµãµÄÇé¿öÏÂ,¾¹ý¼ÆËãºó:tSeg1 = 13,tSeg2 = 3.
*/
canBandrateConfig.PRESC = 11;//·ÖƵºóʱÖÓΪ8M.
canBandrateConfig.SEG_1 = 11;
canBandrateConfig.SEG_2 = 2;
canBandrateConfig.SJW = 2;//Âú×ãS_SJW <= tSeg2¼´¿É.
canConfig.interruptMask = CAN_IRQ_ALL_ENABLE_MSK; //´ò¿ªËùÓÐÖжÏ
canConfig.canMode = CAN_MODE_LOOPBACK_INTERNAL;//CAN_MODE_NORMAL;//
canConfig.clockSrc = CAN_CLKSRC_AHB;
canConfig.tsMode = CAN_TSMODE_FIFO;
canConfig.tsAmount = CAN_TRANSMIT_ONE;
canConfig.interruptEn = ENABLE;//ʹÄÜÖжÏ
canConfig.tpss = DISABLE;
canConfig.tsss = ENABLE;
canConfig.timeStampEn = DISABLE; /*!< Time stamp enable */
canConfig.timeStampClk = CAN_TIME_CLK_DIVIDER_8; /*!< Time stamp clock */
canConfig.timeStampPos = CAN_TIME_STAMP_SOF; /*!< Time stamp position */
canConfig.rom = CAN_RECV_OVER_WRITE;
canConfig.selfAckEn = DISABLE; /*!< Self-Acknowledge enable(when LBME=1) */
canConfig.fdModeEn = DISABLE; /*!< FD enable */
canConfig.fdIsoEn = DISABLE; /*!< FD ISO mode */
canConfig.tdcEnable = DISABLE;
canConfig.filterNum = 0;
//canConfig.filterList = &CAN_FilterControlTypedef;
canConfig.filterList = canFilterControl01;//¸³Öµ¹ýÂËÆ÷ÉèÖÃlist
canConfig.callback = CAN0_IRQnCallBack;
canConfig.normalBitrate = &canBandrateConfig;
g_sendCANMsgInfo.DATA = sendDataBuff;
g_recvCANMsgInfo.DATA = recvDataBuff;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 1;
canConfig.filterList = canFilterControl02;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 2;
canConfig.filterList = canFilterControl03;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 3;
canConfig.filterList = canFilterControl04;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 4;
canConfig.filterList = canFilterControl05;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 5;
canConfig.filterList = canFilterControl06;
CAN_Init(CAN0, &canConfig);
}
void CAN_send_data(u32 ID, u8 *buffer)
{
g_sendCANMsgInfo.ID = ID;
g_sendCANMsgInfo.IDE = 1;
g_sendCANMsgInfo.DLC = 8;
for (u8 a=0; a<g_sendCANMsgInfo.DLC; a++)
g_sendCANMsgInfo.DATA[a] = *(buffer+a);
CAN_TransmitMessage(CAN0, &g_sendCANMsgInfo, CAN_TRANSMIT_SECONDARY);//·¢ËÍÊý¾Ý
}
CAN_MsgInfoType g_sendCANMsgInfo;
CAN_MsgInfoType g_recvCANMsgInfo;
CAN_FilterControlType canFilterControl01[]=
{
{0, ENABLE, CAN_ID_1, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl02[]=
{
{1, ENABLE, CAN_ID_2, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl03[]=
{
{2, ENABLE, CAN_ID_3, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl04[]=
{
{3, ENABLE, CAN_ID_4, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl05[]=
{
{4, ENABLE, CAN_ID_5, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
CAN_FilterControlType canFilterControl06[]=
{
{5, ENABLE, CAN_ID_6, 0x00000000}, ///<Ö»½ÓÊÕ0x400 ID
};
u8 sendDataBuff[CAN_SEND_LENGTH] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
u8 recvDataBuff[CAN_RECIEVE_LENGTH] = {0};
u8 can_recieve_buffer[CAN_RECIEVE_LENGTH] = {0};
void CAN0_IRQnCallBack(void *device, uint32_t wpara, uint32_t lpara);
void CAN0_gpio_init(void)
{
CKGEN_Enable(CLK_CAN0, ENABLE);
GPIO_SetFunc(GPIOB, GPIO_PIN0, GPIO_FUN3);//CAN0_TX
GPIO_SetFunc(GPIOB, GPIO_PIN1, GPIO_FUN3);//CAN0_RX
// GPIO_SetFunc(GPIOC, GPIO_PIN8, GPIO_FUN0);//CAN0_STB
// GPIO_SetDir(GPIOC, GPIO_PIN8, GPIO_OUT);//ÉèÖÃCAN0ÊÕ·¢Æ÷¿ØÖÆΪIO¿ØÖÆ
//CAN0_TRASCVER_NML;//ʹÄÜCANÊÕ·¢Æ÷
// GPIO_SetPinLevel(GPIOC, GPIO_PIN8, GPIO_LEVEL_LOW);
}
void CAN_InitHardwr(void)
{
CAN_ConfigType canConfig;
CAN_BitrateConfigType canBandrateConfig;
//CAN_FilterControlType CAN_FilterControlTypedef;
CAN0_gpio_init();
/*
ÉèÖò¨ÌØÂÊΪ500K,²ÉÑùµãΪ81.25%.
tSeg1 = (S_SEG_1 + 2); tSeg2 = (S_SEG_2 + 1).
BandRate = (48M / (S_PRESC + 1) / ((S_SEG_1 + 2) + (S_SEG_2 + 1)))
SamplePoint = (tSeg1 / (tSeg1 + tSeg2)).
ÔÚÒѾ֪µÀ²¨ÌØÂÊÓë²ÉÑùµãµÄÇé¿öÏÂ,¾¹ý¼ÆËãºó:tSeg1 = 13,tSeg2 = 3.
*/
canBandrateConfig.PRESC = 11;//·ÖƵºóʱÖÓΪ8M.
canBandrateConfig.SEG_1 = 11;
canBandrateConfig.SEG_2 = 2;
canBandrateConfig.SJW = 2;//Âú×ãS_SJW <= tSeg2¼´¿É.
canConfig.interruptMask = CAN_IRQ_ALL_ENABLE_MSK; //´ò¿ªËùÓÐÖжÏ
canConfig.canMode = CAN_MODE_LOOPBACK_INTERNAL;//CAN_MODE_NORMAL;//
canConfig.clockSrc = CAN_CLKSRC_AHB;
canConfig.tsMode = CAN_TSMODE_FIFO;
canConfig.tsAmount = CAN_TRANSMIT_ONE;
canConfig.interruptEn = ENABLE;//ʹÄÜÖжÏ
canConfig.tpss = DISABLE;
canConfig.tsss = ENABLE;
canConfig.timeStampEn = DISABLE; /*!< Time stamp enable */
canConfig.timeStampClk = CAN_TIME_CLK_DIVIDER_8; /*!< Time stamp clock */
canConfig.timeStampPos = CAN_TIME_STAMP_SOF; /*!< Time stamp position */
canConfig.rom = CAN_RECV_OVER_WRITE;
canConfig.selfAckEn = DISABLE; /*!< Self-Acknowledge enable(when LBME=1) */
canConfig.fdModeEn = DISABLE; /*!< FD enable */
canConfig.fdIsoEn = DISABLE; /*!< FD ISO mode */
canConfig.tdcEnable = DISABLE;
canConfig.filterNum = 0;
//canConfig.filterList = &CAN_FilterControlTypedef;
canConfig.filterList = canFilterControl01;//¸³Öµ¹ýÂËÆ÷ÉèÖÃlist
canConfig.callback = CAN0_IRQnCallBack;
canConfig.normalBitrate = &canBandrateConfig;
g_sendCANMsgInfo.DATA = sendDataBuff;
g_recvCANMsgInfo.DATA = recvDataBuff;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 1;
canConfig.filterList = canFilterControl02;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 2;
canConfig.filterList = canFilterControl03;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 3;
canConfig.filterList = canFilterControl04;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 4;
canConfig.filterList = canFilterControl05;
CAN_Init(CAN0, &canConfig);
canConfig.filterNum = 5;
canConfig.filterList = canFilterControl06;
CAN_Init(CAN0, &canConfig);
}
void CAN_send_data(u32 ID, u8 *buffer)
{
g_sendCANMsgInfo.ID = ID;
g_sendCANMsgInfo.IDE = 1;
g_sendCANMsgInfo.DLC = 8;
for (u8 a=0; a<g_sendCANMsgInfo.DLC; a++)
g_sendCANMsgInfo.DATA[a] = *(buffer+a);
CAN_TransmitMessage(CAN0, &g_sendCANMsgInfo, CAN_TRANSMIT_SECONDARY);//·¢ËÍÊý¾Ý
}
赞0
评论
2021-03-03
赞0
评论
2021-03-03
您需要登录后才可以回复 登录 | 注册