Skip to main content
Graduate II
March 9, 2025
Solved

CAN Cyclic Frame with 100ms using a timer

  • March 9, 2025
  • 4 replies
  • 1749 views

Hi everyone,
I'm using the STM32F407 for CAN Communication with the Loopback Mode. I'm trying to send a cyclic frame using a timer.
I'm using the timer2 with a clock of 84MHz (APB1), a prescaler of 8399 so i will have a tick every 1ms and a period of 1000 - 1 because i need a cyclicity of message sending every 100ms.
I only get the data transmitted to the RxData by calling  CAN_Rx_Callback(&hcan1); in the main function.
This way, i'm not sure if i'm sending data every 100ms or i'm sending data all in once. 
How can i update my code to resolve this problem.

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    By testing your configuration and by adding a pin that toggles inside the timer interrupt, it seems the timer callback is firing each 100ms (PD0 is toggling):

    mALLEm_0-1741600958537.png

     

     

    void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim){
    	HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_0);
    
    //	if (htim->Instance == TIM2){
    //		CAN_SendMessage();
    //	}
    }

     

     

    So I don't know how you did conclude that the interrupt is firing each 1ms?

    I'm attaching the project for your reference.

    PS:

    1- you are using HSI as a source clock for CAN communication. It's OK when you are in loopback mode but when you move to Normal mode you need to use the external crystal.

    2- No need to call the CAN callback in main(): CAN_Rx_Callback(&hcan1);. It's managed by interrupt.

    Hope I've answered your question.

     

    4 replies

    Technical Moderator
    March 10, 2025

    Hello,

    This is a timer related question. So for now forget about CAN communication and try to generate a periodic timer interrupt with 100ms of interval. 

    You need also to attach your ioc file as others can help you efficiently.

    AzizzAuthor
    Graduate II
    March 10, 2025

    Hello,

    Thank you for your response. Here is my .ioc file for reference. Let me know if you have any feedback or if I should make any modifications.

    Graduate II
    March 10, 2025

    This is a community, engage in a conversation, maybe people will help you along the debugging way.
    if you just drop your broken code for others to fix maybe youll be better hiring a freelancer
    https://www.fiverr.com/gigs/stm32


    If it was up to me ill try toggle a LED each 100ms , if you own an osciloscope try sniffing those CANbus lines.
    If you dont try getting one of these CAN-USB, great tools

    Javier1_0-1741600073501.png

     

     

    AzizzAuthor
    Graduate II
    March 10, 2025

    Hello, 
    Thank you for your response. For the 1ms, i was talking about the TIM3 counter Clock after the prescaler division.

    Graduate II
    March 10, 2025

    @Azizz wrote:

    Hello, 
    Thank you for your response. For the 1ms, i was talking about the TIM3 counter Clock after the prescaler division.


    Why create a timer when you already have the System Tick at 1ms?

    See this video on how you can use HAL Tick and a Timer Callback to do many things https://www.youtube.com/watch?v=o0qhmXR5LD0

    Technical Moderator
    March 10, 2025

    And this is the CAN frames transmission each 100ms:

    mALLEm_0-1741602698262.png

    Attached the project where PB8 and PB9 are used for CAN_Tx/Rx as PA11 and PA12 are not exposed on the board connector.

    AzizzAuthor
    Graduate II
    March 10, 2025

    Which software are you using to visualize the CAN frames?

    Technical Moderator
    March 10, 2025

    @Azizz wrote:

    Which software are you using to visualize the CAN frames?


    It's a logic analyzer from https://www.saleae.com/