正点原子阿波罗F429+STM32CubeMX+CAN收发

1、前言

出口打印等前期配置请看上一章内容点击跳转
can通讯的原理我就不说了。大家可以自行百度,我推荐一篇文章点击跳转
代码下载地址点击跳转

2、正文

(1)、打开上个项目,加上can1,因为只有1个开发版,所以设置为环回模式,有两个扳子的话可以试试can通讯,(原子的例程是老版本的了,cubeMX新建的项目和原子的有点不兼容。但有很大的参考意义)
正点原子阿波罗F429+STM32CubeMX+CAN收发_第1张图片

2.1、参考代码

2.1.1main.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * 

© Copyright (c) 2020 STMicroelectronics. * All rights reserved.

* * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */
/* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "can.h" #include "dma.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "lk_sys.h" /* 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 */ /* 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_DMA_Init(); MX_USART1_UART_Init(); MX_CAN1_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { lk_run(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 15; RCC_OscInitStruct.PLL.PLLN = 216; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Activate the Over-Drive mode */ if (HAL_PWREx_EnableOverDrive() != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses 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_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != 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 */ /* 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, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

2.1.2 lk_sys.c

#include "lk_sys.h"

#include "lk_can.h"



void lk_run(void)
{
	lk_send_u("程序开始\r\n");
	lk_uart_idle_init();
	lk_can_init();
	while (1)
	{
		if(lk_uart1.b_rx_state)
		{
			lk_send_u("lk_uart1.rx_number = %d\r\n",lk_uart1.rx_number);
			lk_send_u("内容为:");
			HAL_UART_Transmit_DMA(&huart1,lk_uart1.rx_buff,lk_uart1.rx_number);
			lk_send_u("\r\n");
			lk_uart1.b_rx_state = 0;
			HAL_UART_Receive_DMA(&huart1,lk_uart1.rx_buff,lk_uart_rx_buff_max_size);
		}
		
		lk_can_send();
		
		HAL_Delay(1000);
		if(HAL_CAN_GetRxFifoFillLevel(&hcan1,CAN_RX_FIFO0))
		{
			lk_can_rx();
		}
		lk_send_u("1秒\r\n");
	}
}


2.1.3 lk_sys.h

#ifndef __lk_sys_h_
#define __lk_sys_h_

#include "lk_stdio.h"

#include "lk_uart.h"

void lk_run(void);

#endif


2.1.4 lk_can.c

#include "lk_can.h"


CAN_TxHeaderTypeDef   TxHeader;
CAN_RxHeaderTypeDef   RxHeader;
uint8_t               TxData[8];
uint8_t               RxData[8];

void lk_can_init(void)
{
	CAN_FilterTypeDef  sFilterConfig;
	
	sFilterConfig.FilterBank = 0;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = 0x0000;
	sFilterConfig.FilterIdLow = 0x0000;
	sFilterConfig.FilterMaskIdHigh = 0x0000;
	sFilterConfig.FilterMaskIdLow = 0x0000;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;
	
	if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
	{
    /* Filter configuration Error */
		Error_Handler();
	}
	HAL_CAN_Start(&hcan1);
	
	memset(TxData,0,8);
}

void lk_can_send(void)
{
	TxData[7]++;
	TxHeader.DLC = 8;
	TxHeader.ExtId = 0x12;
	TxHeader.IDE = CAN_ID_STD;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.StdId = 0x12;
	TxHeader.TransmitGlobalTime = DISABLE;
	HAL_CAN_AddTxMessage(&hcan1,&TxHeader,TxData,(uint32_t*)CAN_TX_MAILBOX0);
}
void lk_can_rx(void)
{
	memset(RxData,0,8);
	if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK)
	{
		lk_send_u("\r\n");
		lk_send_u("RxHeader.DLC = %d\r\n",RxHeader.DLC);
		lk_send_u("can接收内容为:");
		lk_send_u("%d\r\n",RxData[7]);
	}
}


2.1.5 lk_can.h

#ifndef __lk_can_h_
#define __lk_can_h_

#include 
#include "lk_config.h"
#include "lk_stdio.h"


#include "can.h"



void lk_can_init(void);
void lk_can_send(void);
void lk_can_rx(void);

#endif


连接好开发版,编译下载就可以看到串口的内容的。自己可以再修改下其它代码测试下。
正点原子阿波罗F429+STM32CubeMX+CAN收发_第2张图片

你可能感兴趣的:(STM32)