本人用的是STM32F10X_CL,两个CAN,均没有重映射;
CAN1_RXD : PA11
CAN1_TXD : PA12
CAN2_RXD : PB12
CAN2_TXD : PB13
相关代码如下:
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure CAN1 and CAN2 IOs **********************************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
/* Configure CAN1 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN2 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN1 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN2 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Remap CAN1 and CAN2 GPIOs */
//GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE);
//GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
/* Configure CAN1 and CAN2 **************************************************/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
/* CAN1 and CAN2 register init */
CAN_DeInit(CAN1);
CAN_DeInit(CAN2);
/* Struct init*/
CAN_StructInit(&CAN_InitStructure);
/* CAN1 and CAN2 cell init */
CAN_InitStructure.CAN_TTCM = DISABLE; //禁止时间触发通信模式
CAN_InitStructure.CAN_ABOM = ENABLE; //通过软件请求使CAN退出离线状态
CAN_InitStructure.CAN_AWUM = DISABLE; //自动唤醒:由软件清除CAN_MCR寄存器的Sleep位唤醒
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE; //FIFO未被锁定,溢出时新报文覆盖未被读走的报文
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_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
#if CAN_BAUDRATE == 1000 /* 1MBps */
CAN_InitStructure.CAN_Prescaler =6;
#elif CAN_BAUDRATE == 500 /* 500KBps */
CAN_InitStructure.CAN_Prescaler =12;
#elif CAN_BAUDRATE == 250 /* 250KBps */
CAN_InitStructure.CAN_Prescaler =24;
#elif CAN_BAUDRATE == 125 /* 125KBps */
CAN_InitStructure.CAN_Prescaler =48;
#elif CAN_BAUDRATE == 100 /* 100KBps */
CAN_InitStructure.CAN_Prescaler =60;
#elif CAN_BAUDRATE == 50 /* 50KBps */
CAN_InitStructure.CAN_Prescaler =120;
#elif CAN_BAUDRATE == 20 /* 20KBps */
CAN_InitStructure.CAN_Prescaler =300;
#elif CAN_BAUDRATE == 10 /* 10KBps */
CAN_InitStructure.CAN_Prescaler =600;
#else
#endif /* CAN_BAUDRATE == 1000 */
/*Initializes the CAN1 and CAN2 */
CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN2, &CAN_InitStructure);
/* CAN1 filter init */
CAN_FilterInitStructure.CAN_FilterNumber = 1; // 0 ~ 27
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = DISABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN2 filter init */
CAN_FilterInitStructure.CAN_FilterIdHigh =0x2460;
CAN_FilterInitStructure.CAN_FilterNumber = 15;
CAN_FilterInit(&CAN_FilterInitStructure);
/* Transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; //抢占式优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0; //响应式优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1; //抢占式优先级
NVIC_Init(&NVIC_InitStructure);
}
void CAN1_RX0_IRQHandler(void)
{
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
}
/**
* @brief This function handles CAN2 RX0 Handler.
* @param None
* @retval None
*/
void CAN2_RX0_IRQHandler(void)
{
CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);
}
|