中级会员
- 积分
- 323
- 金钱
- 323
- 注册时间
- 2016-7-12
- 在线时间
- 65 小时
|
1金钱
void Mcaro_CAN_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
CAN_InitTypeDef CAN_InitStruct;
CAN_InitStruct.CAN_ABOM = ENABLE; /*BUS-off硬件复位*/
CAN_InitStruct.CAN_AWUM = DISABLE; /*硬件自动唤醒模式*/
CAN_InitStruct.CAN_TTCM = DISABLE; /*时间触发通讯模式*/
CAN_InitStruct.CAN_TXFP = DISABLE; /*标识符优先级模式*/
CAN_InitStruct.CAN_NART = ENABLE; /*报文自动重传*/
CAN_InitStruct.CAN_RFLM = DISABLE; /*接收FIFO锁定模式*/
CAN_InitStruct.CAN_Mode = CAN_Mode_Normal;
CAN_InitStruct.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStruct.CAN_BS2 = CAN_BS2_4tq;
CAN_InitStruct.CAN_Prescaler = 9;
CAN_InitStruct.CAN_SJW = CAN_SJW_1tq;
CAN_Init(CAN1, &CAN_InitStruct);
CAN_FilterInitTypeDef CAN_FilterInitStruct;
CAN_FilterInitStruct.CAN_FilterActivation = ENABLE; /*过滤器激活*/
CAN_FilterInitStruct.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStruct.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStruct.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStruct.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStruct.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStruct.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStruct.CAN_FilterNumber = 0;
CAN_FilterInitStruct.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInit(&CAN_FilterInitStruct);
CAN_ITConfig(CAN1, CAN_IT_TME | CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0
| CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1
| CAN_IT_EWG | CAN_IT_LEC | CAN_IT_ERR
| CAN_IT_WKU | CAN_IT_SLK, ENABLE);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = CAN1_RX1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
}
/**
* @brief CANFIFO中断
*/
void CAN1_RX1_IRQHandler(void)
{
/*数据接收中断*/
if (CAN_GetITStatus(CAN1, CAN_IT_FMP0) == SET)
{
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
Mcaro_GPIO_Bitset(LED1, 0);
delay_ms(100);
Mcaro_GPIO_Bitset(LED1, 1);
CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);
}
}
|
|