新手上路
- 积分
- 47
- 金钱
- 47
- 注册时间
- 2020-6-8
- 在线时间
- 12 小时
|
发表于 2022-8-31 18:36:37
|
显示全部楼层
/*******************************************************************************
ÎļtÃû3Æ£oCAN1_GPIO_Configuration(void)
oˉêyËμÃ÷£oCAN1¶Ë¿úI/OÅäÖÃoˉêy
*******************************************************************************/
void CAN1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
SYSCFG->CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_4);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
/*******************************************************************************
ÎļtÃû3Æ£oCAN1_Parameter_Configuration(void)
oˉêyËμÃ÷£oCAN12ÎêyÅäÖÃoˉêy
*******************************************************************************/
void CAN1_Parameter_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN,ENABLE);
CAN_DeInit(CAN);CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN,&CAN_InitStructure);
}
/*******************************************************************************
ÎļtÃû3Æ£oCAN1_Filter_Configuration(void)
oˉêyËμÃ÷£oCAN1ÂË2¨ÅäÖÃoˉêy
*******************************************************************************/
void CAN1_Filter_Configuration(void)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
/*******************************************************************************
ÎļtÃû3Æ£oCAN1_NVIC_Configuration(void)
oˉêyËμÃ÷£oCAN1ÖD¶Ï1üàíÅäÖÃ
*******************************************************************************/
void CAN1_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN,CAN_IT_FMP0,ENABLE);
CAN_ITConfig(CAN,CAN_IT_FF0,ENABLE);
}
/*******************************************************************************
ÎļtÃû3Æ£oCAN1_Peripheral_Initialization(void)
oˉêyËμÃ÷£oCAN1íaéè3õê¼»ˉ
*******************************************************************************/
void CAN1_Peripheral_Initialization(void)
{
//CAN1_NVIC_Configuration();
CAN1_GPIO_Configuration();
CAN1_Filter_Configuration();
CAN1_Parameter_Configuration();
}
/*******************************************************************************
ÎļtÃû3Æ£oCAN1_SendMessage(uint8_t*Message,uint8_t Length)
oˉêyËμÃ÷£oCAN1Ïòía·¢ËíÏûÏ¢oˉêy
*******************************************************************************/
uint8_t CAN1_SendMessage(uint8_t*Message,uint8_t Length)
{
uint8_t Box,i=0x00;
CanTxMsg SendMessage;
SendMessage.DLC = Length;
SendMessage.IDE = CAN_ID_STD;
SendMessage.RTR = CAN_RTR_DATA;
SendMessage.StdId = 0x00000001;
SendMessage.ExtId = 0x00000000;
for(i=0;i<Length;i++)
SendMessage.Data[i] = Message[i];
Box = CAN_Transmit(CAN,&SendMessage);
while((CAN_TransmitStatus(CAN,Box)!=CAN_TxStatus_Ok)&&(i<0xf5))i++;
if(i >= 0xf5) return RESET;
return 0;
} |
|