Expt 8 CAN Bus
Expt 8 CAN Bus
Theory
The Controller Area Network (CAN) is a robust communication protocol designed to
facilitate communication between microcontrollers and devices in embedded systems,
especially in automotive and industrial applications. CAN is a multi-master, message-
oriented protocol with high immunity to electrical interference.
STM32 microcontrollers have integrated CAN controllers which allow for the
implementation of CAN communication without needing external CAN controllers. Using
STM32CubeIDE, we can configure the microcontroller’s CAN peripheral and write software
to send and receive CAN messages.
Key Concepts in CAN Communication:
Message Frames: Data is transmitted in frames. The most common frame types are
Data Frames, Remote Frames, Error Frames, and Overload Frames.
Identifiers: Each message in CAN is identified by a unique identifier (standard 11-bit
or extended 29-bit).
Data Length Code (DLC): Specifies the number of data bytes in the message.
Bitrate: Speed of communication, commonly 125 kbps, 500 kbps, or 1 Mbps.
Error Handling: CAN provides error detection mechanisms and automatic error
handling.
Procedure
Code Generation and STM32CubeIDE
1. Generate the initialization code open it in STM32CubeIDE.
2. Configure CAN Initialization:
o In main.c, STM32CubeIDE will generate initialization code for the CAN
peripheral. You can add further code to handle the CAN communication.
Implementing CAN Transmission and Reception
1. CAN Transmission:
o Set up a CAN message structure (CAN_TxHeaderTypeDef and uint8_t
TxData[8]) and configure the transmission parameters such as the message
identifier (CAN_ID), data length, and priority.
o Use HAL_CAN_AddTxMessage function to send data through the CAN bus.
***************************************************************************
***
* @file : main.c
* @brief : Main program body
***************************************************************************
***
* @attention
*
* Copyright (c) 2024 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"
CAN_HandleTypeDef hcan;
I2C_HandleTypeDef hi2c1;
TIM_HandleTypeDef htim2;
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/**
* @brief CAN Initialization Function
* @param None
* @retval None
*/
static void MX_CAN_Init(void)
{
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/**
* @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 */
Result:
The STM32 successfully transmits and receives CAN messages, as verified by the CAN
Analyzer or another ECU on the CAN bus. Messages are correctly formatted and adhere to
the CAN protocol (correct IDs, DLC, data content, etc.).
Conclusion
In this experiment, we successfully implemented a CAN communication stack on the STM32
microcontroller using STM32CubeIDE. The STM32 was able to transmit and receive
messages over the CAN bus, demonstrating the microcontroller’s capability to handle robust,
high-speed communication. The experiment highlighted the ease of configuring the CAN
peripheral and integrating communication within an embedded system environment.