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ò)濾器嗎?