2020-11-10 01:27 AM
Hi guys,
I am working on Motor Control and I need to set up Combined PWM and ADC trigger syncronize together.Last situation is PWMs working and ADC is trigered so when the ADC is triggered ,interrupt event is running in HAL_ADCEx_InjectedConvCpltCallback that I put in it Led Toggle what I see is trigger event is not runnning in every pwm period.Trigger event is running 1 time and no interrupt 2 times and again running interrupt.If I am gonna tell it in different way --> run wait wait run wait wait run ... Like that ,I am not sure drawing right but probably enough.Also HAL_TIM_TriggerCallback doesn't work.
_____-----_-----_______-----_-----_______-----_-----________-----_-----_______-----_-----___ PWM 1
____-------_-------____-------_-------____-------_-------_____-------_-------____-------_-------___ PWM 2
___--------_--------___--------_--------___--------_--------___--------_--------___--------_-------___ PWM 3
-------------______________________________________________----------------------------------- LED inside interrupt
Codes below
//----------------------- tim.c -----------------------//
#include "tim.h"
extern uint32_t timerPeriod;
TIM_HandleTypeDef htim1;
void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED2;
htim1.Init.Period = timerPeriod;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_OC5REF_RISING_OC6REF_FALLING;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{ Error_Handler(); }
sConfigOC.Pulse = 0;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{ Error_Handler(); }
sConfigOC.Pulse = 0;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{ Error_Handler(); }
sConfigOC.OCMode = TIM_OCMODE_PWM2;
sConfigOC.Pulse = 1000;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_5) != HAL_OK)
{ Error_Handler(); }
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 200;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{ Error_Handler(); }
HAL_TIM_MspPostInit(&htim1);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
__HAL_RCC_TIM1_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn);
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM1)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM1 GPIO Configuration
PA7 ------> TIM1_CH1N
PB0 ------> TIM1_CH2N
PB1 ------> TIM1_CH3N
PA8 ------> TIM1_CH1
PA9 ------> TIM1_CH2
PA10 ------> TIM1_CH3
*/
GPIO_InitStruct.Pin = PWM_UL_Pin|PWM_UH_Pin|PWM_VH_Pin|PWM_WH_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = PWM_VL_Pin|PWM_WL_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
//----------------------- main.c -----------------------//
int main(void)
{
HAL_Init();
SystemClock_Config();
mcuFreq = HAL_RCC_GetSysClockFreq();
timerPeriod = (uint32_t)((mcuFreq / 10000) - 1);
MX_GPIO_Init();
MX_TIM1_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
__HAL_UART_FLUSH_DRREGISTER(&huart1);
__HAL_TIM_ENABLE_IT(&htim1,TIM_IT_TRIGGER | TIM_IT_UPDATE);
if(HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK); Error_Handler();
HAL_ADCEx_InjectedStart_IT(&hadc1);
valBuf[0] = setDutyPercentage(40);
valBuf[1] = setDutyPercentage(50);
valBuf[2] = setDutyPercentage(80);
if(HAL_TIMEx_GroupChannel5(&htim1, (TIM_GROUPCH5_OC1REFC |\
TIM_GROUPCH5_OC2REFC |\
TIM_GROUPCH5_OC3REFC)) != HAL_OK) Error_Handler();
//if(HAL_TIMEx_GroupChannel5(&htim1, TIM_GROUPCH5_OC3REFC) != HAL_OK) Error_Handler();
if( HAL_TIM_Base_Start(&htim1) != HAL_OK ) Error_Handler();
if(HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1) != HAL_OK) Error_Handler();
if(HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_1) != HAL_OK) Error_Handler();
if(HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_2) != HAL_OK) Error_Handler();
if(HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_2) != HAL_OK) Error_Handler();
if(HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_3) != HAL_OK) Error_Handler();
if(HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_3) != HAL_OK) Error_Handler();
if(HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_5) != HAL_OK) Error_Handler(); //ADC Trigger
TIM1->CCR1 = valBuf[0];
TIM1->CCR2 = valBuf[1];
TIM1->CCR3 = valBuf[2];
HAL_UART_Receive_IT(&huart1, uartRxBuf, 1);
while (1)
{
u16TimerVal = __HAL_TIM_GET_COUNTER(&htim1);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;
RCC_OscInitStruct.PLL.PLLN = 85;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{ Error_Handler(); }
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{Error_Handler(); }
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC12;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{Error_Handler(); }
}
uint32_t setDutyPercentage(uint32_t val)
{
return (( val * (timerPeriod - 1)) / 100);
}
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
{
ADConvertedValues[2] = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_3);
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
}
//This interrupt doesn't work
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
{
//HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
__HAL_TIM_CLEAR_FLAG(&htim1,TIM_IT_TRIGGER);
}
Solved! Go to Solution.
2020-11-12 03:04 AM
Solved.Because of discontinuous mode
2020-11-12 03:04 AM
Solved.Because of discontinuous mode
2020-11-19 01:19 AM
Dear @Community member
Happy for you.
Feel free to ask any other questions you want.
Best regards
Laurent Ca...