cancel
Showing results for 
Search instead for 
Did you mean: 

I cannot find a program example (HAL_TIM_IC_CaptureCallback() for the input capture mode described in 14.3.6 PWM input mode (13902_STM32_ReferenceManual_11.pdf ). The attached user code works as I expect, but it is unclear to me if everything is correct.

RGünt.1
Associate

0693W00000APLUMQA5.jpgvolatile uint32_t udwIC1_Val1 = 0;

volatile uint32_t udwIC2_Val1 = 0;

volatile uint16_t uwDuty_Cycle = 0;

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)

{

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)

{

udwIC1_Val1 = htim->Instance->CCR1;

udwIC2_Val1 = htim->Instance->CCR2;

uwDuty_Cycle = (udwIC2_Val1 * 100) / udwIC1_Val1;

udwFrequency = HAL_RCC_GetPCLK1Freq() / udwIC1_Val1 / htim->Instance->PSC;

}

}

2 REPLIES 2

The Reference Manual describes the hardware, not one particular piece of software ("library") Cube/HAL is. For that you have to resort to Cube's manual.

I don't use Cube but the code appears to be OK to me.

Please note that the measured values are off by a couple of cycles (maybe literally 2) due to internal delay between the input signal's edge and the moment when the counter reset actually occurs. This is not documented in ST's materials. This may be OK for your application, if better accuracy is desired, don't use the reset mode of slave-mode controller, simply leave the counter free-running, and calculate period/duty by subtracting consecutively captured values.

JW

RGünt.1
Associate

First of all, thank you for your quick reply and for pointing out a possible measurement inaccuracy caused by resetting the counter.

With kind regards

R.G.