cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Cyclic Frame with 100ms using a timer

Azizz
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

8 REPLIES 8
mƎALLEm
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

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.

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

 

 

Available for consulting/freelancing , hit me up in https://github.com/javiBajoCero

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.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Azizz
Associate III

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

mƎALLEm
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Azizz
Associate III

Which software are you using to visualize the CAN frames?


@Azizz wrote:

Which software are you using to visualize the CAN frames?


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

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.