0% found this document useful (0 votes)
5 views8 pages

CAN Communication and Data Handling Code

The document contains C code for a main program that interfaces with a CAN bus and a UART for data transmission. It includes functions for sending and receiving messages, filtering CAN messages, and decoding received data related to vehicle parameters such as RPM, engine coolant temperature, throttle position, and battery voltage. The program initializes necessary peripherals and runs in an infinite loop to continuously request and send data based on received messages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

CAN Communication and Data Handling Code

The document contains C code for a main program that interfaces with a CAN bus and a UART for data transmission. It includes functions for sending and receiving messages, filtering CAN messages, and decoding received data related to vehicle parameters such as RPM, engine coolant temperature, throttle position, and battery voltage. The program initializes necessary peripherals and runs in an infinite loop to continuously request and send data based on received messages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/* USER CODE BEGIN Header */

/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/


/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/


/* USER CODE BEGIN PTD */
CAN_TxHeaderTypeDef TxHeader;
CAN_RxHeaderTypeDef RxHeader;
CAN_FilterTypeDef mFilter;
/* 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 ---------------------------------------------------------*/


CAN_HandleTypeDef hcan1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */


uint8_t TxData[8] = {0};
uint8_t RxData[8];
uint32_t TxMailbox;
uint8_t flag_send = 0;
uint8_t flag_decode = 0;
uint16_t rpm = 0;
int16_t ect = 0;
float tps = 0;
float igt = 0;
float bav = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/


void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/


/* USER CODE BEGIN 0 */
void Tx (uint8_t b0, uint8_t b1, uint8_t b2){
[Link] = 0x7E0;
[Link] = CAN_ID_STD;
[Link] = CAN_RTR_DATA;
[Link] = 8;
[Link] = DISABLE;

TxData[0] = b0;
TxData[1] = b1;
TxData[2] = b2;

if (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) > 0U)


HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox);
}

void request(){

Tx(0x02, 0x21, 0x00);


HAL_Delay(10);
Tx(0x30, 0x00, 0x00);

void can_filter_7E8 (){

[Link] = 0;
[Link] = CAN_FILTER_FIFO0;
[Link] = CAN_FILTERMODE_IDMASK;
[Link] = CAN_FILTERSCALE_32BIT;
[Link] = ENABLE;

[Link] = (0x7E8 << 5);


[Link] = 0;
[Link] = (0x7FF << 5);
[Link] = 0;

HAL_CAN_ConfigFilter(&hcan1, &mFilter);

void decode_multi_frame (){


if (flag_decode == 0 ){
uint8_t pid = RxData[0];
switch (pid){
case 0x25:
if (RxData[4] == 0xFF) return;
rpm = (256*RxData[4] +RxData[5]) / 4;
break;
case 0x27:
if (RxData[4] == 0xFF) return;
ect = RxData[4] - 40;
break;
case 0x26:
if (RxData[3] == 0xFF) return;
tps = 100.0f * (float)RxData[3] / 255.0f;
break;
case 0x23:
if (RxData[4] == 0xFF) return;
igt = (float)RxData[4] * 0.5f - 64.0f;
break;
case 0x2E:
if (RxData[3] == 0xFF) return;
bav = ((float)RxData[3]*256.0f + (float)RxData[4]) /
1000.0f;
break;

default:
break;
}
}
flag_decode = 1;
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan){
while (HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0) > 0U) {
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) ==
HAL_OK){
decode_multi_frame();
}else{
break;
}
}
}

void send_nextion( uint8_t pid){


char buf[32];
int n = 0;

uint8_t end[3] = {0xFF,0xFF,0xFF};


switch (pid)
{
case 0x25: // RPM
n = snprintf(buf, sizeof(buf), "[Link]=\"%u\"", (unsigned)rpm);
break;

case 0x27: // ECT


n = snprintf(buf, sizeof(buf), "[Link]=\"%d\"", (int)ect);
break;

case 0x26: // TPS


n = snprintf(buf, sizeof(buf), "[Link]=\"%.2f\"",
(double)tps);
break;

case 0x23: // Spark advance


n = snprintf(buf, sizeof(buf), "[Link]=\"%.2f\"", (double)igt);
break;

case 0x2E: // VBAT


n = snprintf(buf, sizeof(buf), "[Link]=\"%.2f\"", (double)bav);
break;

default:
return; // không gửi gì nếu PID không khớp
}

if (HAL_UART_Transmit(&huart1, (uint8_t*)buf, n, 100) == HAL_OK){


HAL_UART_Transmit(&huart1, end, 3, 100);
flag_send = 1 ;
}else{
flag_send = 0 ;
}
}

void send_all(){

send_nextion(0x25);
send_nextion(0x27);
send_nextion(0x26);
send_nextion(0x23);
send_nextion(0x2E);
}
/* 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_CAN1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
can_filter_7E8();
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
request();
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if (flag_decode == 1 ){
send_all();
flag_decode = 0;
}
if (flag_send == 0){
flag_decode = 0;
}
request();
/* 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 RCC Oscillators according to the specified parameters


* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.[Link] = RCC_PLL_ON;
RCC_OscInitStruct.[Link] = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.[Link] = 8;
RCC_OscInitStruct.[Link] = 168;
RCC_OscInitStruct.[Link] = RCC_PLLP_DIV2;
RCC_OscInitStruct.[Link] = 4;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)


{
Error_Handler();
}
}

/**
* @brief CAN1 Initialization Function
* @param None
* @retval None
*/
static void MX_CAN1_Init(void)
{

/* USER CODE BEGIN CAN1_Init 0 */

/* USER CODE END CAN1_Init 0 */

/* USER CODE BEGIN CAN1_Init 1 */

/* USER CODE END CAN1_Init 1 */


[Link] = CAN1;
[Link] = 6;
[Link] = CAN_MODE_NORMAL;
[Link] = CAN_SJW_1TQ;
[Link].TimeSeg1 = CAN_BS1_11TQ;
[Link].TimeSeg2 = CAN_BS2_2TQ;
[Link] = DISABLE;
[Link] = DISABLE;
[Link] = DISABLE;
[Link] = DISABLE;
[Link] = DISABLE;
[Link] = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */

/* USER CODE END CAN1_Init 2 */

/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{

/* USER CODE BEGIN USART1_Init 0 */


/* USER CODE END USART1_Init 0 */

/* USER CODE BEGIN USART1_Init 1 */

/* USER CODE END USART1_Init 1 */


[Link] = USART1;
[Link] = 9600;
[Link] = UART_WORDLENGTH_8B;
[Link] = UART_STOPBITS_1;
[Link] = UART_PARITY_NONE;
[Link] = UART_MODE_TX_RX;
[Link] = UART_HWCONTROL_NONE;
[Link] = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */

/* USER CODE END USART1_Init 2 */

/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */

/* USER CODE END MX_GPIO_Init_1 */

/* GPIO Ports Clock Enable */


__HAL_RCC_GPIOA_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */


}

/* 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 */

You might also like