944 views|2 replies

157

Posts

0

Resources
The OP
 

【STM32F746 Nucleo】2. CubeMX develops CAN communication [Copy link]

  1. CubeMx
    1.1 CubeMx loads the development board HAL library, and can also load the chip HAL library

    2 CubeMX has already configured SYS, you need to check it

    3 The clock configuration is also selected as the default
  1. CAN communication configuration
    Here you need to configure the clock division factor to get the desired communication baud rate.
    The new version of CubeMX shows the CAN baud rate after division, Baud Rate 750000bit/s, which is 750Kbit/s


    5.RCC clock source view settings

    6.Program is generated as .c and .h files

    7.Set the project file name Set to generate MDK development environment, set the save path of the code project
  1. Pin Function Pin
    Check the location of the hardware circuit pins and find the power supply location.

  2. CAN communication settings
    3.1 Design a CAN communication module, and then use Dupont line to connect, as shown in the figure:

    3.2 CAN communication settings
    The communication frequency is set to 250K

    3.3 After the program code is developed, perform a communication test
  3. Software code
    4.1 CAN send and receive function code
    CAN_TxHeaderTypeDef TXHeader;
    CAN_RxHeaderTypeDef RXHeader;
    uint8_t TXmessage[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
    uint8_t RXmessage[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uint32_t pTxMailbox = 0;
    uint32_t CAN_ID = 0x01;
    int counT_A = 0;
    int counT_B = 0;
    void CAN_Config(void)
    {
    	CAN_FilterTypeDef  sFilterConfig;/*配置CAN过滤器*/
    	sFilterConfig.FilterBank = 0;                     //过滤器0
    	sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
    	sFilterConfig.FilterIdHigh = 0x0000;              //32位ID
    	sFilterConfig.FilterIdLow = 0x0000;
    	sFilterConfig.FilterMaskIdHigh = 0x0000;          //32位MASK
    	sFilterConfig.FilterMaskIdLow = 0x0000;
    	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;//过滤器0关联到FIFO0
    	sFilterConfig.FilterActivation = ENABLE;          //激活滤波器0
    	sFilterConfig.SlaveStartFilterBank = 14;
    	if(HAL_CAN_ConfigFilter(&hcan1,&sFilterConfig) != HAL_OK)//初始化过滤器
    		{
    			Error_Handler();
    		}
    	if(HAL_CAN_Start(&hcan1) != HAL_OK)//打开can
    		{
    			Error_Handler();
    		}
    		if(HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)//开启接受邮邮箱0挂起中断
    		{
    			Error_Handler();
    		}
    //	/*配置传输过程*/
    //	TXHeader.StdId = 0x321;
    //	TXHeader.ExtId = 0x01;
    //	TXHeader.RTR = CAN_RTR_DATA;
    //	TXHeader.IDE = CAN_ID_STD;
    //	TXHeader.DLC = 2;
    //	TXHeader.TransmitGlobalTime = DISABLE;
    }//发送函数
    void CAN_senddata(CAN_HandleTypeDef *hcan,uint32_t can_id)
    {
    	TXHeader.StdId=can_id ;
    	TXHeader.ExtId=0x12345000;//0x12345000
    	TXHeader.DLC=8;
    	TXHeader.IDE=CAN_ID_EXT;
    	TXHeader.RTR=CAN_RTR_DATA;
    	TXHeader.TransmitGlobalTime = DISABLE;
    	HAL_CAN_AddTxMessage(hcan,&TXHeader,TXmessage,&pTxMailbox);
    }
    
    uint8_t	RxData[8];
    uint8_t ubKeyNumber=0;
    /**
    * [url=home.php?mod=space&uid=159083]@brief[/url] Rx Fifo 0 message pending callback
    * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
    *  the configuration information for the specified CAN.
    * @retval None
    */
    void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
    {
    		/* Get RX message */
    		if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RXHeader, RxData) != HAL_OK)
    		{
    		/* Reception Error */
    		Error_Handler();
    		}
    
    		/* Display LEDx */
    		if ((RXHeader.StdId == 0x321) && (RXHeader.IDE == CAN_ID_STD) && (RXHeader.DLC == 2))
    		{
    
    		 ubKeyNumber = RxData[0];
    		}
    }

    4.2 Declare and call function code
    void CAN_Config(void);
    
    extern uint8_t TXmessage[8];
    
    extern uint8_t RXmessage[8];
    
    extern uint32_t CAN_ID;
    
    extern int counT_A;
    
    extern int counT_B;
    
    void CAN_senddata(CAN_HandleTypeDef *hcan,uint32_t can_id);
    
    uint32_t CAN1_Receive_Msg(uint8_t *buf);
     /* USER CODE BEGIN WHILE */
      while (1)
      {
    		//CAN1_Receive_Msg(RXmessage);
    		HAL_GPIO_WritePin(GPIOB, LD1_Pin|LD3_Pin|LD2_Pin, GPIO_PIN_RESET);
    		HAL_Delay(500);
    		HAL_GPIO_WritePin(GPIOB, LD1_Pin|LD3_Pin|LD2_Pin, GPIO_PIN_SET);
    		HAL_Delay(500);
    		CAN_senddata(&hcan1,0x08A);
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
      }

    4.3 Project package files
  4. can.rar (26.21 MB, downloads: 16)
This post is from stm32/stm8

Latest reply

Is this a CAN and USB converter module?   Details Published on 2023-9-28 08:54
 

7462

Posts

2

Resources
2
 

Very detailed, thanks for sharing!

This post is from stm32/stm8
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 

6075

Posts

6

Resources
3
 

Is this a CAN and USB converter module?

This post is from stm32/stm8
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list