CH32V307 CAN2無(wú)法正常接收?qǐng)?bào)文

CAN1?測(cè)試 收發(fā)都正常,但是CAN2?只能發(fā)送?但接收不了報(bào)文,用你們的demo測(cè)試can2?還是不行只能發(fā)不能收,接收都是采用掩碼模式。

我的can初始化程序?yàn)椋?/p>

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 );

}

接收程序?yàn)椋?/p>

u8 CAN_Receive_Msg(CAN_TypeDef* CANx,u8 *buf )

{

? ? u8 i;

? ? CanRxMsg CanRxStructure;


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

? ? {

? ? ? ? return 0;? //請(qǐng)求不到值 就給上一次值 避免產(chǎn)生跳變

? ? }

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


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

? ? {

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

? ? }

? ? return CanRxStructure.DLC;

}

此外我還有個(gè)問(wèn)題就是關(guān)于SDK庫(kù)中的? void CAN_FilterInit(CAN_FilterInitTypeDef *CAN_FilterInitStruct), 這里面只對(duì)CAN1寄存器進(jìn)行操作,沒(méi)有CAN2,CAN1和CAN2用的都是一個(gè)過(guò)濾器嗎?

估計(jì)是CAN2的過(guò)濾器開(kāi)始組沒(méi)有設(shè)置

image.png


你好,具體可參考這篇博客:https://www.cnblogs.com/wchmcu/p/17078743.html


謝謝,已經(jīng)解決了,后面查出來(lái)是can2的濾波器組沒(méi)設(shè)置正確,我改成CAN_FilterInitSturcture.CAN_FilterNumber = 14;? can2能就能收到了。??


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频