CH32V307 CAN2无法正常接收报文

CAN1?测试 收发都正常,但是CAN2?只能发送?但接收不了报文,用你们的demo测试can2?还是不行只能发不能收,接收都是采用掩码模式。

我的can初始化程序为:

void CAN_Mode_Init( u8 tsjw, u8 tbs2, u8 tbs1, u16 brp, u8 mode )

{

? ? GPIO_InitTypeDef GPIO_InitSturcture={0};

? ? CAN_InitTypeDef CAN_InitSturcture={0};

? ? CAN_FilterInitTypeDef CAN_FilterInitSturcture={0};


? ? RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB, ENABLE );

? ? RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN1, ENABLE );

? ? RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN2, ENABLE );


? ? GPIO_PinRemapConfig( GPIO_Remap2_CAN1, ENABLE);

//can1 pin

? ? GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_1;

? ? GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_AF_PP;

? ? GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;

? ? GPIO_Init( GPIOD, &GPIO_InitSturcture);


? ? GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_0;

? ? GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IPU

? ? GPIO_Init( GPIOD, &GPIO_InitSturcture);

//can2 pin

? ? GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_13;

? ? GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_AF_PP;

? ? GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;

? ? GPIO_Init( GPIOB, &GPIO_InitSturcture);


? ? GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_12;

? ? GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_IPU;

? ? GPIO_Init( GPIOB, &GPIO_InitSturcture);


? ? CAN_InitSturcture.CAN_TTCM = DISABLE;?

? ? CAN_InitSturcture.CAN_ABOM = DISABLE;?

? ? CAN_InitSturcture.CAN_AWUM = DISABLE;?

? ? CAN_InitSturcture.CAN_NART = ENABLE;??

? ? CAN_InitSturcture.CAN_RFLM = DISABLE;?

? ? CAN_InitSturcture.CAN_TXFP = DISABLE;?

? ? CAN_InitSturcture.CAN_Mode = mode;

? ? CAN_InitSturcture.CAN_SJW = tsjw;

? ? CAN_InitSturcture.CAN_BS1 = tbs1;

? ? CAN_InitSturcture.CAN_BS2 = tbs2;

? ? CAN_InitSturcture.CAN_Prescaler = brp;

? ? CAN_Init( CAN1, &CAN_InitSturcture );

? ? CAN_Init( CAN2, &CAN_InitSturcture );


? ? CAN_FilterInitSturcture.CAN_FilterNumber = 0;


? ? CAN_FilterInitSturcture.CAN_FilterMode = CAN_FilterMode_IdMask;

? ? CAN_FilterInitSturcture.CAN_FilterScale = CAN_FilterScale_32bit;

? ? CAN_FilterInitSturcture.CAN_FilterIdHigh = 0x0201;

? ? CAN_FilterInitSturcture.CAN_FilterIdLow = 0x0000;

? ? CAN_FilterInitSturcture.CAN_FilterMaskIdHigh = 0x0000;

? ? CAN_FilterInitSturcture.CAN_FilterMaskIdLow = 0x0000;



? ? CAN_FilterInitSturcture.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;

? ? CAN_FilterInitSturcture.CAN_FilterActivation = ENABLE;

? ? CAN_FilterInit( &CAN_FilterInitSturcture );

}

接收程序为:

u8 CAN_Receive_Msg(CAN_TypeDef* CANx,u8 *buf )

{

? ? u8 i;

? ? CanRxMsg CanRxStructure;


? ? if( CAN_MessagePending(CANx, CAN_FIFO0) == 0)

? ? {

? ? ? ? return 0;? //请求不到值 就给上一次值 避免产生跳变

? ? }

? ? CAN_Receive(CANx, CAN_FIFO0, &CanRxStructure);


? ? for( i=0; i<8; i++ )

? ? {

? ? ? ? buf[i] = CanRxStructure.Data[i];

? ? }

? ? return CanRxStructure.DLC;

}

此外我还有个问题就是关于SDK库中的? void CAN_FilterInit(CAN_FilterInitTypeDef *CAN_FilterInitStruct), 这里面只对CAN1寄存器进行操作,没有CAN2,CAN1和CAN2用的都是一个过滤器吗?

估计是CAN2的过滤器开始组没有设置

image.png


你好,具体可参考这篇博客:https://www.cnblogs.com/wchmcu/p/17078743.html


谢谢,已经解决了,后面查出来是can2的滤波器组没设置正确,我改成CAN_FilterInitSturcture.CAN_FilterNumber = 14;  can2能就能收到了。  


只有登录才能回复,可以选择微信账号登录