2021-05-23 11:06 AM
volatile 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;
}
}
2021-05-23 04:04 PM
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
2021-05-24 03:38 AM
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.