给你发个能用的代码吧。遇到了跟你同样的问题,我的解决方法是把 CAN_FilterConfig(&stcFilter, Enable); 这个函数 提前执行。希望能帮到你,
顺便吐槽一句,想支持一把国产真特么不容易!!!就这还想抢stm32的市场,且不说产品做得怎么样,虽然我这个用起来了,但是费死了劲。手册写得不明不白的,只能靠蒙。
还有什么波特率设置之类的,特么的就不能跟串行口一样把波特率作为参数传进去?设4个寄存器。谁特么有空看这些设置?
void hal_can_init(void)
{
PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_CAN, Enable);
PORT_SetFunc(PortB, Pin13, Func_Can1_Rx, Disable);
PORT_SetFunc(PortB, Pin14, Func_Can1_Tx, Disable);
stc_can_init_config_t stcCanInitCfg;
stc_can_filter_t stcFilter;
stc_can_txframe_t stcTxFrame;
//<<Can filter config
stcFilter.enAcfFormat = CanStdFrames;
stcFilter.enFilterSel = CanFilterSel1;
stcFilter.u32CODE = 0x00000601ul;
stcFilter.u32MASK = 0x1FFFF000ul;
CAN_FilterConfig(&stcFilter, Enable);
MEM_ZERO_STRUCT(stcCanInitCfg);
MEM_ZERO_STRUCT(stcFilter);
MEM_ZERO_STRUCT(stcTxFrame);
stcCanInitCfg.stcCanBt.PRESC = 2u - 1u;
stcCanInitCfg.stcCanBt.SEG_1 = 5u - 2u;
stcCanInitCfg.stcCanBt.SEG_2 = 3u - 1u;
stcCanInitCfg.stcCanBt.SJW = 3u - 1u;
stcCanInitCfg.stcWarningLimit.CanErrorWarningLimitVal = 10u;
stcCanInitCfg.stcWarningLimit.CanWarningLimitVal = 16u - 1u;
stcCanInitCfg.enCanRxBufAll = CanRxNormal;
stcCanInitCfg.enCanRxBufMode = CanRxBufNotStored;
stcCanInitCfg.enCanSAck = CanSelfAckEnable;
stcCanInitCfg.enCanSTBMode = CanSTBFifoMode;
CAN_Init(&stcCanInitCfg);
CAN_IrqCmd(CanRxIrqEn, Enable);
} |