cancel
Showing results for 
Search instead for 
Did you mean: 

CAN communication not working

cannotworking
Associate

I am using stm32 mini m4 when i run the can code and debug it The can communication is not starting. when i checked with oscilloscope the can h and can l is always high. Please help me with the code.

#include "main.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 ---------------------------------------------------------*/

CAN_HandleTypeDef hcan1;

 

/* USER CODE BEGIN PV */

 

/* USER CODE END PV */

 

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

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_CAN1_Init(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_CAN1_Init();

/* USER CODE BEGIN 2 */

HAL_CAN_Start(&hcan1);

 

 

uint32_t mb;

CAN_TxHeaderTypeDef msg;

uint8_t data[] = {1};

msg.StdId = 0x13;

msg.IDE = CAN_ID_STD;

msg.RTR = CAN_RTR_DATA;

msg.DLC = 1;

// msg.TransmitGlobalTime = DISABLE;

 

// HAL_CAN_Start(&hcan1);

 

 

 

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

if(HAL_CAN_Start(&hcan1)!= HAL_OK)

{

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);

HAL_Delay(1000);

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);

HAL_Delay(1000);

}

/* BUSS 21120-00 14VDC 20A 6167G11USER CODE BEGIN 3 */

if (HAL_CAN_AddTxMessage(&hcan1, &msg, data, &mb) != HAL_OK)

{

Error_Handler();

}

HAL_Delay(200);

 

 

 

 

}

/* USER CODE END 3 */

}

1 REPLY 1
Issamos
Lead II

Hello @cannotworking 

I suggest you to take a look at the configuration of this tutorial. It may help you finding the issue. Also, why are you starting your can peripheral before declaring the tx parameters. Finally, double-check your hardware and gpio configuration.

Best regards.

II