2023-12-17 10:30 PM - edited 2023-12-17 10:31 PM
I am working with STM32G4. I am generating 1Khz PWM from my Timer2 and TImer1 I am using for delay(I configured it for 50 usec delay with interrupt).
Both timer2 and timer1 working as expected.
1.For every pwm pulse start I need to read adc for 50 usec after pulse start and after 800 usec of pulse start I need to check the GPIO this is my logic.
Now for testing purpose at 50 usec I am setting the led and at 800 usec I am resetting the led. But this is not working as expected.
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_2);
setPWM_DutyCycle(TIM_CHANNEL_2,50);
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
dcheck.flagon_50usec=1;
if(delay.flag_50 == 1){
// HAL_ADC_Start(&hadc2);
// HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
// adcValues[currentIndex] = HAL_ADC_GetValue(&hadc2);
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_SET);
delay.flag_50 = 0;
dcheck.flagon_50usec=0;
dcheck.flagon_800usec=1;
}
if(delay.flag_800usec == 1){
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_RESET);
delay.flag_800usec = 0;
dcheck.flagon_800usec=0;
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim1)
{
// delay.flag_50 = 1;
if(dcheck.flagon_50usec){
dcount.u_50_sec++;
if(dcount.u_50_sec==1){
delay.flag_50 = 1;
dcount.u_50_sec=0;
}
}
if (dcheck.flagon_800usec)
{
dcount.u_800_sec++;
if(dcount.u_800_sec==16){
delay.flag_800usec = 1;
dcount.u_800_sec=0;
}
}
}
}
I checked in the oscilloscope led is high for 1 msec(one pwm pulse) and led is off for 2 msec(2 pwm pulse) it is showing in the scope. Can any one suggest what mistake I am doing. I gave high priority to PWM.
Thanks
Solved! Go to Solution.
2023-12-27 09:23 PM
Hi,
I am able to solve the issue. Thanks for suggestion.
I used HAL library only because all my remaining logic working with HAL.
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_2);
setPWM_DutyCycle(TIM_CHANNEL_2,50);
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
dcheck.flagon_50usec=1;
dcheck.flagon_800usec=1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim1)
{
if(dcheck.flagon_50usec){
dcount.u_50_sec++;
if(dcount.u_50_sec==1){
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_RESET);
delay.flag_50 = 1;
dcount.u_50_sec=0;
dcheck.flagon_50usec=0;
}
}
if (dcheck.flagon_800usec)
{
dcount.u_800_sec++;
if(dcount.u_800_sec==16){
delay.flag_800usec = 1;
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_SET);
dcount.u_800_sec=0;
dcheck.flagon_800usec=0;
}
}
}
}
Above logic worked for me.
Thanks
2023-12-18 01:12 AM - edited 2023-12-18 01:13 AM
You are setting the LED in TIM2 ISR which is invoked at the end of PWM active pulse, so what you observe is quite normal. If you need to synchronize some actions with PWM, use other TIM2 channels in compare mode, so thaty you events will be in sync with TIM2 PWM period.
2023-12-18 03:46 AM
@gbm ,Thanks for the reply. Can you suggest any reference link for compare mode and I am using same delay timer for different delays outside of PWM also in different functions. Is it better to use different timer for the delays other then PWM synced delays.
Thanks
2023-12-18 07:29 AM
I suggest that you read the functional description of timer in the Reference Manual - all the information you need is there. And it's much easier to setup the timer and service its interrupts without using HAL.
2023-12-20 04:08 AM
Hi @gbm ,
I have done some changes in my code.
For every pwm pulse start at 50 usec I am setting the user_led and reseting the led1 and at 800 usec I am resetting the user_led and seting the led1.
But with below code For every pwm pulse at 12 usec setting the user_led and reseting the led1 and at 24 usec (means after 10 usec of setting the user_led and reseting the led1) resetting the user_led and seting the led1 is happening.
HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_2);
setPWM_DutyCycle(TIM_CHANNEL_2,50);
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2){
HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_1); //for 50 usec
HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_3);//for 800 usec
}
}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1){
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, led1_Pin,GPIO_PIN_RESET);
HAL_TIM_OC_Stop_IT(&htim2, TIM_CHANNEL_1);
}
else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3){
HAL_GPIO_WritePin(GPIOB, led1_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_RESET);
HAL_TIM_OC_Stop_IT(&htim2, TIM_CHANNEL_3);
}
}
}
For PWM I configured TImer2 with 1Khz.duty cycle I am setting through function.
For 50 usec channel1 I set pulse to 50 is it correct or not?same with channel3 for 800 usec.
Please suggest.
Thanks
2023-12-21 08:55 PM - edited 2023-12-25 09:09 PM
2023-12-27 09:23 PM
Hi,
I am able to solve the issue. Thanks for suggestion.
I used HAL library only because all my remaining logic working with HAL.
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_2);
setPWM_DutyCycle(TIM_CHANNEL_2,50);
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM2) {
dcheck.flagon_50usec=1;
dcheck.flagon_800usec=1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim == &htim1)
{
if(dcheck.flagon_50usec){
dcount.u_50_sec++;
if(dcount.u_50_sec==1){
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_RESET);
delay.flag_50 = 1;
dcount.u_50_sec=0;
dcheck.flagon_50usec=0;
}
}
if (dcheck.flagon_800usec)
{
dcount.u_800_sec++;
if(dcount.u_800_sec==16){
delay.flag_800usec = 1;
HAL_GPIO_WritePin(GPIOA, user_led,GPIO_PIN_SET);
dcount.u_800_sec=0;
dcheck.flagon_800usec=0;
}
}
}
}
Above logic worked for me.
Thanks