void CAN_Model(uint8_t Ch,uint16_t bulv)
{
GPIO_InitType GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_AFIO, ENABLE);
GPIO_PinsRemapConfig(GPIO_Remap2_CAN1,ENABLE);
// GPIO_PinsRemapConfig(AFIO_MAP6_CAN1_GRMP,ENABLE);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_1;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_PU;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_CAN1, ENABLE);
//
CAN_ConfigModel(0,bulv);
//
CAN_NVIC_ModelConfig();
}
void CAN_ConfigModel(uint8_t Ch,uint16_t bulv)
{
CAN_InitType CAN_InitStructure;
CAN_FilterInitType CAN_FilterInitStructure = {0};
CAN_InitStructure.CAN_TTC = DISABLE;
CAN_InitStructure.CAN_ABO = DISABLE;
CAN_InitStructure.CAN_AWU = DISABLE;
CAN_InitStructure.CAN_NART = ENABLE;
CAN_InitStructure.CAN_RFL = DISABLE;
CAN_InitStructure.CAN_TFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_1tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 48;
CAN_Init(CAN1, &CAN_InitStructure); //<SYSCLK=240M, APB1CLK=120M, CAN Baudrate=(240/2)*1000K)/((1+9)*(3+7+2)) = 1000K
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0;
CAN_FilterInitStructure.CAN_FilterMskIdHigh=0x0;
CAN_FilterInitStructure.CAN_FilterMskIdLow=0x0;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(CAN1, &CAN_FilterInitStructure);
//
CAN_INTConfig(CAN1,CAN_INT_RFP0,ENABLE);
//
CAN_ClearINTPendingBit(CAN1, CAN_INT_TSME);
//
CAN_INTConfig(CAN1,CAN_INT_TSME,ENABLE);
}
|