cancel
Showing results for 
Search instead for 
Did you mean: 

How to use multiple channel of timer 3 in stm32h503 mcu

Singh_t
Associate II

hi

anyone help me how to use 2 channel of input capture of timer 3.

i am giving two different external digital pulses to input capture of channel 1 and channel 2.

as currently if i use independently these two channel one by one, they are working ok. if i use both at same time. they didnt work correctly.

as i have taken refernce from follwing link.

https://controllerstech.com/input-capture-in-stm32/

 

please provide your suggestions.

4 REPLIES 4
TDK
Guru

> please provide your suggestions.

Show your code. Explain what "doesn't work" means and what you're expecting instead.

Note that TIM3 has a single counter. The channels do not have independent counters.

If you feel a post has answered your question, please click "Accept as Solution".

hi

following is my code. both capture input has different frequency from two different hall type speed sensor output.

when i use either only one it works fine but when i take capture input from both  then my frequency output is varied. like(exact output of frequency is 324  but after connecting second one, frequency vary from 15 to 324 again and again and in same way Frequency1 variable of 2nd channel.)

kindly provide any suggestion as i am stuck in this.

all initialisation of timer also  has done.

 

 

#define TIMCLOCK   64000000
#define PRESCALAR  64

uint32_t IC_Val1 = 0;
uint32_t IC_Val2 = 0;
uint32_t Difference = 0;
int Is_First_Captured = 0;
float frequency = 0;

uint32_t IC_Val11 = 0;
uint32_t IC_Val21 = 0;
uint32_t Difference1 = 0;
int Is_First_Captured1 = 0;
float frequency1 = 0;


TIM_HandleTypeDef htim3;
void main()
{

HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);


while(1)
{
}

}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
if (Is_First_Captured==0) 
{
IC_Val1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); // read the first value
Is_First_Captured1 = 1;  
}

else    //  capture the second edge
{
IC_Val2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);  // read second value

if (IC_Val2 > IC_Val1)
{
Difference = IC_Val2-IC_Val1;
}
                       else if (IC_Val1 > IC_Val2)
{
Difference = (0xffffffff - IC_Val1) + IC_Val2;
}

float refClock = TIMCLOCK/(PRESCALAR);

frequency = refClock/Difference;

__HAL_TIM_SET_COUNTER(htim, 0);  // reset the counter
Is_First_Captured = 0; // set it back to false
}
}
    if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
if (Is_First_Captured1==0) 
{
IC_Val11 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); // read the first value
Is_First_Captured1 = 1;  
}

else    //  capture the second edge
{
IC_Val21 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);  // read second value

if (IC_Val21 > IC_Val11)
{
Difference1 = IC_Val21-IC_Val11;
}
                       else if (IC_Val11 > IC_Val21)
{
Difference1 = (0xffffffff - IC_Val11) + IC_Val21;
}

float refClock = TIMCLOCK/(PRESCALAR);

frequency1 = refClock/Difference1;

__HAL_TIM_SET_COUNTER(htim, 0);  // reset the counter
Is_First_Captured1 = 0; // set it back to false
}
}
}

 

or i have to use 2 different callback function for different channels.??

TDK
Guru

Please use the </> to post code next time. It makes it infinitely more readable. I edited your post to do this.

 

> __HAL_TIM_SET_COUNTER(htim, 0)

You can't edit the counter value if it's being used by both channels. This is the cause of your error.

But you shouldn't need to do this. There's no issue with letting the counter free-run and using the difference in values in the calculation.

 

> Difference = (0xffffffff - IC_Val1) + IC_Val2;

It's a 16-bit counter. This should probably be.

Difference = (0x10000 - IC_Val1) + IC_Val2;

 

Random examples on the web are generally not as good as the official examples in CubeMX. There's probably one that does input capture and frequency calculation.

If you feel a post has answered your question, please click "Accept as Solution".