stm32hal库can使用记录

一、单片机最小系统板使用can

资料:https://blog.csdn.net/yoie01/article/details/136921034
https://blog.csdn.net/qq_36561846/article/details/117431769

  • 这里面有一个问题,最小系统板没有can收发器,所以没法直接用,只能测试一下回环模式

二、stm32f103vet6测试达妙电机

  • 我用的是正点原子的战舰V3的板子,这个板子要调一下板子上的连接帽,将io口复用。
  • 注意一下,can通信是需要反馈的,也就是can总线上如果没有通信的目标设备,can报文会一直待在单片机的邮箱内。一般邮箱满了,其实就代表通信失败了。
  • 过滤器在测试阶段其实不用管,只要在cubemx生成的基础上,加上启动can的代码,就算启动了。剩下的就是设置发送和接收的数据缓存区和报文配置了
  • 波特率的设置上,波特率=APB1/预分频/(TimeSeg1+TimeSeg2+1)
  • 这个代码是在上面那两个资料上改的,把所有可以省略的代码去除了,只给电机发送了一个初始化的命令
#include "main.h"
#include "can.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define CAN_ID ((uint32_t)0x01)

CAN_RxHeaderTypeDef CanRx;//接收消息报文信息
CAN_TxHeaderTypeDef CanTx;//发送消息报文信息
uint8_t RxData[8];			//接收数据区
uint8_t TxData[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfc};//发送数据区
//uint8_t TxData[8] = {0x94,0x7a,0x7f,0xf0,0x08,0x19,0x97,0xff};//发送数据区
uint8_t Flag_Rx = 0;		//接收到数据的标志
uint32_t pTxMailbox = 0;	//发送邮箱盒子
uint8_t test = 10;
uint8_t test2 = 0;
/********************************************
@function:Can_Config,对于Can的CubeMx外的基础配置
@param:void
@return:HAL_StatusTypeDef
@date:2021.5.31
@note:
********************************************/
HAL_StatusTypeDef Can_Config(void)
{

	
	
	
	CanTx.DLC = 8;				//数据长度
	CanTx.StdId = CAN_ID;
	CanTx.IDE = CAN_ID_STD;		//标准帧模式
	CanTx.RTR = CAN_RTR_DATA;	//数据帧
				
	//CanTx.TransmitGlobalTime = DISABLE;//时间戳



	

	
	if(HAL_CAN_Start(&hcan) != HAL_OK) //启动CAN
	{
		return HAL_ERROR;
	}
	

	
	

	
	return HAL_OK;
}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)//报文接收中断
{
	if(HAL_CAN_GetRxMessage(hcan, CAN_FILTER_FIFO0, &CanRx, RxData) == HAL_OK)
	{
		Flag_Rx = 1;
	}
	
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	Can_Config();
  /* USER CODE END 2 */



/* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

		while( HAL_CAN_GetTxMailboxesFreeLevel( &hcan ) == 0 );  //等待邮箱空闲
	  if(HAL_CAN_AddTxMessage(&hcan, &CanTx, TxData, &pTxMailbox) != HAL_OK)//发送数据
	  {
		test2 = test2 + 1;
	  }else{
			test = test - 1;
		}

	  //HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

你可能感兴趣的:(stm32,嵌入式硬件,单片机)