Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

本人学习STM32_CAN通信协议,参考博主代码,提几个疑问! #4

Open
ZzlYang opened this issue May 17, 2023 · 0 comments

Comments

@ZzlYang
Copy link

ZzlYang commented May 17, 2023

本人使用STM32F103ZET6型号单片机,CAN1的TX RX引脚复用到GPIOD_0和GPIOD_1上面。
image
在参考了博主的资料后以下是本人的GPIO初始化代码,如有问题还请帮忙指正!

static void CANPort_GPIO_Config(void)
{	
	/*-----------------1.使能GPIO_D并重映射为CAN1---------------------*/
	/*-----------------1.1使能GPIO_D1 CAN TX引脚---------------------*/
	 GPIO_InitTypeDef  GPIO_InitStructure;
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); 		//使能GPIOD的时钟                                                                
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);    //使能CAN1时钟
	 
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;			//CAN TX发送引脚
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_Init(GPIOD,&GPIO_InitStructure);
	
	/*-----------------1.2使能GPIO_D0 CAN RX引脚---------------------*/
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  //上拉输入
	 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
	 GPIO_Init(GPIOD,&GPIO_InitStructure);			//初始化TX引脚

	 
	 GPIO_PinRemapConfig(GPIO_Remap2_CAN1,  ENABLE);
	
	

}

static void CAN_Mod_Config(void)
{		
	/*--------CAN配置结构体---------------*/
		CAN_InitTypeDef CAN_InitStructure;
		CAN_FilterInitTypeDef   CAN_FilterInitStructure;
		CAN_DeInit(CAN1);											//CAN结构体复位
		CAN_StructInit(&CAN_InitStructure);		//CAN结构体初始化
#if CAN_INT_ENABLE
    NVIC_InitTypeDef        NVIC_InitStructure;	 
#endif
//	  CAN_InitStructure.CAN_TTCM=DISABLE; //MCR-TTCM 关闭时间触发通信模式使能
//		CAN_InitStructure.CAN_ABOM=ENABLE; //MCR-ABOM 使能自动离线管理
//		CAN_InitStructure.CAN_AWUM=ENABLE; //MCR-AWUM 使用自动唤醒模式
//		CAN_InitStructure.CAN_NART=DISABLE; //MCR-NART 禁止报文自动重传
//		CAN_InitStructure.CAN_RFLM=DISABLE; //MCR-RFLM 接收 FIFO 不锁定
	/*------------------设置CAN的工作模式-------------------------*/
		CAN_InitStructure.CAN_TTCM=DISABLE;         //非时间触发通信模式  
		CAN_InitStructure.CAN_ABOM=DISABLE;         //软件自动离线管理   
		CAN_InitStructure.CAN_AWUM=DISABLE;         //睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
		CAN_InitStructure.CAN_NART=ENABLE;          //禁止报文自动传送 
		CAN_InitStructure.CAN_RFLM=DISABLE;         //报文不锁定,新的覆盖旧的  
		CAN_InitStructure.CAN_TXFP=DISABLE;         //优先级由报文标识符决定 
		CAN_InitStructure.CAN_Mode= CAN_Mode_Normal;  //模式设置:CAN_Mode_Normal 普通模式,CAN_Mode_LoopBack 回环模式;
		
	/*--------------------设置CAN的通信速率----------------------------*/
	//CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1)
	//例如CANbps=APB1总线频率30000000/4/(8+6+1))=500k bps
	//总体配置方向: Tseg1>=Tseg2  Tseg2>=tq; Tseg2>=2TSJW 
			CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;  //设置项目(1~4)
			CAN_InitStructure.CAN_BS1=CAN_BS1_6tq; //设置项目(1~16)
			CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //设置项目(1~8)
			CAN_InitStructure.CAN_Prescaler=10;			//设置项目(1~1024)
		
			CAN_Init(CAN1,&CAN_InitStructure);
		/*---------------设置过滤器------------------------*/
		CAN_FilterInitStructure.CAN_FilterNumber = 0;
		CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;   //屏蔽位模式
    CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;  //32位宽 
    CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;    //32位ID
    CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32位MASK
    CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0
    CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//激活过滤器0
    CAN_FilterInit(&CAN_FilterInitStructure);           //滤波器初始化
		
#if CAN_INT_ENABLE 	//以下是用于CAN中断方式接收的设置
    CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);              //FIFO0消息挂号中断允许.            
    NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     // 主优先级为1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次优先级为0
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant