配置如下:
/**
*\*\name CAN_CONFIG
*\*\fun CAN configure.
*\*\param none
*\*\return none
**/
void CAN_CONFIG(void)
{
RCC_APB1_Peripheral_Clock_Enable(RCC_APB1_PERIPH_CAN);
/* Initializes CAN*/
CAN_NVIC_Configuration();
CAN_GPIO_Configuration();
CAN_Reset(CAN);
CAN_Structure_Initializes(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.TTCM = DISABLE;
CAN_InitStructure.ABOM = DISABLE;
CAN_InitStructure.AWKUM = DISABLE;
CAN_InitStructure.NART = DISABLE;
CAN_InitStructure.RFLM = DISABLE;
CAN_InitStructure.TXFP = ENABLE;
CAN_InitStructure.OperatingMode = CAN_NORMAL_MODE;
/* Baud rate calculation = CAN clock/(BaudRatePrescaler*(TBS1+TBS2+1)) */
CAN_InitStructure.RSJW = CAN_RSJW_1TQ;
CAN_InitStructure.TBS1 = CAN_TBS1_8TQ;
CAN_InitStructure.TBS2 = CAN_TBS2_7TQ;
CAN_InitStructure.BaudRatePrescaler = 4;
/*Initializes the CAN */
CAN_Initializes(CAN, &CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.Filter_Num = 0;
CAN_FilterInitStructure.Filter_Mode = CAN_FILTER_IDLISTMODE;
CAN_FilterInitStructure.Filter_Scale = CAN_FILTER_32BITSCALE;
CAN_FilterInitStructure.Filter_HighId = CAN_STD_ID_LIST_32BIT_H(0x00000400);
CAN_FilterInitStructure.Filter_LowId = CAN_STD_ID_LIST_32BIT_L(0x00000400);
CAN_FilterInitStructure.FilterMask_HighId = CAN_STD_ID_LIST_32BIT_H(0x00000200);
CAN_FilterInitStructure.FilterMask_LowId = CAN_STD_ID_LIST_32BIT_L(0x00000200);
CAN_FilterInitStructure.Filter_FIFOAssignment = CAN_FIFO0;
CAN_FilterInitStructure.Filter_Act = ENABLE;
CAN_Filter_Initializes(&CAN_FilterInitStructure);
/* IT Configuration for CAN */
CAN_Config_Interrupt_Enable(CAN, CAN_INT_FMP0);
}
/**
*\*\name CAN_NVIC_Configuration
*\*\fun CAN NVIC configure.
*\*\param none
*\*\return none
**/
void CAN_NVIC_Configuration(void)
{
NVIC_InitType NVIC_InitStructure;
/* NVIC priority group set */
NVIC_Priority_Group_Set(NVIC_PER2_SUB2_PRIORITYGROUP);
/* NVIC configure */
NVIC_InitStructure.NVIC_IRQChannel = CAN_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Initializes(&NVIC_InitStructure);
}
/**
*\*\name CAN_GPIO_Configuration
*\*\fun CAN GPIO configure.
*\*\param none
*\*\return none
**/
void CAN_GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
/* Initializes the GPIO */
GPIO_Structure_Initialize(&GPIO_InitStructure);
/* configure CAN pin */
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOB);
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
GPIO_InitStructure.Pin = GPIO_PIN_6;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF11_CAN;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_7;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF11_CAN;
GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure);
} 用的都是官方例程,只是把引脚修改了下,没办法通信,麻烦帮忙看下,谢谢。
|