2018-03-26 03:53 AM
Hi, i have implemented the example TIM_OCToggle from stm32f40xx to stm32f103xx, both work fine.
I want a event in the moment change state output pin of the channels OC, as occurs in the events:HAL_TIM_PeriodElapsedCallback or HAL_TIM_PWM_PulseFinishedCallback
int main(void)
{ HAL_Init(); SystemClock_Config(); InitConfigTIM2_OC(); while (1) { }}static void InitConfigTIM2_OC(void)
{ TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC; uint32_t uhPrescalerValue = 0; uhPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;hTIM2.Instance = TIM2;
//hTIM2.Init.Prescaler = 2000-1; hTIM2.Init.Prescaler = uhPrescalerValue; hTIM2.Init.CounterMode = TIM_COUNTERMODE_UP; hTIM2.Init.Period = 50; hTIM2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; hTIM2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; HAL_TIM_Base_Init(&hTIM2); HAL_TIM_OC_Init(&hTIM2);sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.Pulse = uhCCR3_Val; HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_3);sConfigOC.Pulse = uhCCR4_Val;
HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_4);HAL_TIM_MspPostInit(&hTIM2);
HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_3);
HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_4); }void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{ if(htim->Instance == TIM2) { if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) { uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val)); } if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) { uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val)); } }}#stm32f103-eventSolved! Go to Solution.
2018-04-11 11:58 PM
Resolved:
For mode Compare Output is absolutely necessary the value for Period(for HAL library) equal to 65535(filled registry of 32 bits)
View the schematic:
int main(void)
{HAL_Init();SystemClock_Config();InitConfigTIM2_OC();while (1){}}static void InitConfigTIM2_OC(void)
{ TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC;uint32_t uhPrescalerValue = 0;
uhPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;
hTIM2.Instance = TIM2;
hTIM2.Init.Prescaler = uhPrescalerValue;hTIM2.Init.CounterMode = TIM_COUNTERMODE_UP;hTIM2.Init.Period = 65350; // <<<<-----------------------------------------------HERE!!!hTIM2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;hTIM2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;HAL_TIM_Base_Init(&hTIM2);HAL_TIM_OC_Init(&hTIM2);sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;sConfigOC.Pulse = uhCCR3_Val;
HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_3);sConfigOC.Pulse = uhCCR4_Val;
HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_4);HAL_TIM_MspPostInit(&hTIM2);
HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_3);
HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_4);}void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{ if(htim->Instance == TIM2){ if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {BSP_LED_Toggle(LED_USB);
// for view on oscilloscope
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val)); } if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) {m++; // for view on Stm32Studio
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val)); }}}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{ if(htim->Instance == TIM2) { m4++; }}2018-03-26 09:43 AM
You probably want to enable the CC interrupt(s), but I don't use Cube/HAL so don't know what's the proper way to do it - maybe using HAL_TIM_OC_Start_IT()?
JW
2018-03-26 12:20 PM
Yes, this function
HAL_TIM_OC_Start_IT() i
s called on final of InitConfigTIM2_OC()
2018-03-26 01:48 PM
And without using the HAL, as it should be?
2018-03-26 06:56 PM
You are using HAL,
you can look inside that function and see what they have done.
I would go to the callback, and add some lines in the User section.
2018-03-27 03:16 AM
Well it's just setting the interrupt bits in TIMx_DIER; but if you say it's been called (and you can verify it by reading out and checking the TIMx_DIER in debugger) then the problem is elsewhere.
JW
2018-04-11 11:58 PM
Resolved:
For mode Compare Output is absolutely necessary the value for Period(for HAL library) equal to 65535(filled registry of 32 bits)
View the schematic:
int main(void)
{HAL_Init();SystemClock_Config();InitConfigTIM2_OC();while (1){}}static void InitConfigTIM2_OC(void)
{ TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC;uint32_t uhPrescalerValue = 0;
uhPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;
hTIM2.Instance = TIM2;
hTIM2.Init.Prescaler = uhPrescalerValue;hTIM2.Init.CounterMode = TIM_COUNTERMODE_UP;hTIM2.Init.Period = 65350; // <<<<-----------------------------------------------HERE!!!hTIM2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;hTIM2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;HAL_TIM_Base_Init(&hTIM2);HAL_TIM_OC_Init(&hTIM2);sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;sConfigOC.Pulse = uhCCR3_Val;
HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_3);sConfigOC.Pulse = uhCCR4_Val;
HAL_TIM_OC_ConfigChannel(&hTIM2, &sConfigOC, TIM_CHANNEL_4);HAL_TIM_MspPostInit(&hTIM2);
HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_3);
HAL_TIM_OC_Start_IT(&hTIM2, TIM_CHANNEL_4);}void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{ if(htim->Instance == TIM2){ if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {BSP_LED_Toggle(LED_USB);
// for view on oscilloscope
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3); __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val)); } if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) {m++; // for view on Stm32Studio
uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4); __HAL_TIM_SET_COMPARE(&hTIM2, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val)); }}}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{ if(htim->Instance == TIM2) { m4++; }}