AnsweredAssumed Answered

Problem with generating PWM using TIM2

Question asked by chaib.aymane on Jun 6, 2014
Hello,
I am working to a project when I have to control speed using an IRQ, which generate pulse each 5 ms to achieve the reference value, the problem that I have, is that when the reference is achieved, the µC continue to send pulse. Follow my code :

#include "stm32f4xx.h"
#include <stdio.h>
#include "stm32f4xx_tim.h"
#include "config_Vitesse.h"

void TIM2_IRQHandler(void);
void LED_Config(void);
void INTTIM_Config_test(void);

GPIO_InitTypeDef  GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

uint16_t                period;
uint16_t                pulse;
uint16_t                 res;
uint16_t                 res_prev=0;

int main(void)
{
        /* Compute the value for the ARR register to have a period of 20 KHz */
        period = (SystemCoreClock/20000)  - 1;

        SystemInit();
       // init_vitesse();
        INTTIM_Config_test();

        while (1) {
                    }
}

void TIM2_IRQHandler(void)
{
    init_vitesse();


    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
        {
            TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
            GPIO_ToggleBits(GPIOG, GPIO_Pin_8);

            /* Start ADC3 Software Conversion */
            ADC_SoftwareStartConv(ADC3);
            while(ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC) == RESET);
            res = ADC_GetConversionValue(ADC3);
            res = (u32)(res)/4;

                    if (res<res_prev)
                    {
                        res_prev--;
                        pulse = (u16)((u32)((6*(u32)(period - 1) / 100ul) +
                                ((u32)(res_prev*2.4)*(u32)(period - 1)) /(1024ul*100ul)));
                        TIM8->CCR1 = pulse;
                    }

                    else if (res>res_prev)
                    {
                        res_prev++;
                        pulse = (u16)((u32)((6*(u32)(period - 1) / 100ul) +
                                ((u32)(res_prev*2.4)*(u32)(period - 1)) /(1024ul*100ul)));
                        TIM8->CCR1 = pulse;
                    }

                    else
                    {
                        res_prev=res;
                        pulse = (u16)((u32)((6*(u32)(period - 1) / 100ul) +
                                ((u32)(res_prev*2.4)*(u32)(period - 1)) /(1024ul*100ul)));
                        TIM8->CCR1 = pulse;
                    }
        }

}

void INTTIM_Config_test(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 5000 - 1;  // 1 MHz down to 1 KHz (1 ms)
  TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  /* TIM IT enable */
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}

void LED_Config(void)
{
  /* GPIOD Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);

  /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOG, &GPIO_InitStructure);

}


#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{
   while (1)
  {}
}
#endif

and when I delete this part

                    else
                    {
                        res_prev=res;
                        pulse = (u16)((u32)((6*(u32)(period - 1) / 100ul) +
                                ((u32)(res_prev*2.4)*(u32)(period - 1)) /(1024ul*100ul)));
                        TIM8->CCR1 = pulse;
                    }

from the code, it dont work.

Thank you for help

Outcomes