最近在用107的板子在调试CAN,参考了网上的代码,用CAN1通讯,板子上对应的IO为PD0,PD1,已经remap,中断接收,轮循发送,自发自收没有问题,NORMAL模式下就无法收到CAN盒的数据帧,CAN盒也收不到板子发送的数据帧(追踪到发送状态一直是CAN_TxStatus_Pending),用示波器量过,发送和接收时总线上都是有差分信号出来的,检查了代码,发现好像也没啥问题,求指正!
void can_configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* GPIOB Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD , ENABLE);
/* Enable CAN1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure CAN1 pin(PB5): RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure CAN1 pin(PB6): TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);
/* CAN1 register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 9; //500kHz
CAN_Init(CAN1,&CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
}
发送:
void CanWriteData(void)
{
CanTxMsg TxMessage;
/* transmit */
TxMessage.StdId = 0x55;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0x55;
TxMessage.Data[1] = 0x55;
TxMessage.Data[2] = 0x55;
TxMessage.Data[3] = 0x55;
TxMessage.Data[4] = 0x55;
TxMessage.Data[5] = 0x55;
TxMessage.Data[6] = 0x55;
TxMessage.Data[7] = 0x55;
CAN_Transmit(CAN1,&TxMessage);
}
接收:
void CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
recv_frame.id = RxMessage.StdId;
recv_frame.data_buff[0] = RxMessage.Data[0];
recv_frame.data_buff[1] = RxMessage.Data[1];
recv_frame.data_buff[2] = RxMessage.Data[2];
recv_frame.data_buff[3] = RxMessage.Data[3];
recv_frame.data_buff[4] = RxMessage.Data[4];
recv_frame.data_buff[5] = RxMessage.Data[5];
recv_frame.data_buff[6] = RxMessage.Data[6];
recv_frame.data_buff[7] = RxMessage.Data[7];
can_recv_flag = ENABLE;
} |