2025-03-09 8:49 AM - last edited on 2025-03-10 2:12 AM by mƎALLEm
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.
Solved! Go to Solution.
2025-03-10 3:07 AM - edited 2025-03-10 3:09 AM
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):
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.
2025-03-10 2:17 AM
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.
2025-03-10 2:40 AM
2025-03-10 2:44 AM - edited 2025-03-10 2:47 AM
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
2025-03-10 3:07 AM - edited 2025-03-10 3:09 AM
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):
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.
2025-03-10 3:27 AM
Hello,
Thank you for your response. For the 1ms, i was talking about the TIM3 counter Clock after the prescaler division.
2025-03-10 3:34 AM
And this is the CAN frames transmission each 100ms:
Attached the project where PB8 and PB9 are used for CAN_Tx/Rx as PA11 and PA12 are not exposed on the board connector.
2025-03-10 3:43 AM
Which software are you using to visualize the CAN frames?
2025-03-10 3:52 AM
@Azizz wrote:
Which software are you using to visualize the CAN frames?
It's a logic analyzer from https://www.saleae.com/