Search code examples
timerstm32pwmservo

generate infinity pwm with stm32 with 2 timers


I have 10 servo motor and I want to derive all of them.I used 2 timers interrupt with different timer frequency to generate different PWM frequency for each pin. the pins connected to servos, I derived one servo with each pin, .the code is this:

Edited:

     void TIM2_IRQHandler(void)
     {      

if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET ) 
{
    TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);   

    GPIO_ResetBits(SERVO_PORT , FUEL_PIN);
    GPIO_ResetBits(SERVO_PORT , SPEED_PIN);
    GPIO_ResetBits(SERVO_PORT , RPM_PIN);
    GPIO_ResetBits(SERVO_PORT , AIR_PRESURE_PIN);
    GPIO_ResetBits(SERVO_PORT , OIL_ENGINE_PRESURE_PIN);
    GPIO_ResetBits(SERVO_PORT , OIL_GEARBOX_PRESURE_PIN);
    GPIO_ResetBits(SERVO_PORT , OIL_TEMPERATURE_PIN);
    GPIO_ResetBits(SERVO_PORT , COOLER_WATER_TEMPERATURE_PIN);

    //GPIO_ResetBits(GPIOD,GPIO_Pin_3);

    CurrentDegree = 0;           
}
}

void TIM4_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM4 , TIM_IT_Update) != RESET ) 
{
    TIM_ClearITPendingBit(TIM4 , TIM_FLAG_Update);        

    CurrentDegree++; 
    if(CurrentDegree < Desired)
    {
        GPIO_SetBits(GPIOD , GPIO_Pin_3);
    }
    else
    {
        GPIO_ResetBits(GPIOD,GPIO_Pin_3);       
    } 

    if(CurrentDegree < GetSpeed())
    {
        GPIO_SetBits(SERVO_PORT , SPEED_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , SPEED_PIN);
    }

    if(CurrentDegree < GetRpm())
    {
        GPIO_SetBits(SERVO_PORT , RPM_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , RPM_PIN);
    }       

    if(CurrentDegree < GetFuel())
    {
        GPIO_SetBits(SERVO_PORT , FUEL_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , FUEL_PIN);
    }

    if(CurrentDegree < GetAirPresure())
    {
        GPIO_SetBits(SERVO_PORT , AIR_PRESURE_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , AIR_PRESURE_PIN);
    }       

    if(CurrentDegree < GetOilEnginePresure())
    {
        GPIO_SetBits(SERVO_PORT , OIL_ENGINE_PRESURE_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , OIL_ENGINE_PRESURE_PIN);
    }   

    if(CurrentDegree < GetOilGearboxPresure())
    {
        GPIO_SetBits(SERVO_PORT , OIL_GEARBOX_PRESURE_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , OIL_GEARBOX_PRESURE_PIN);
    }  

    if(CurrentDegree < GetOilTemperature())
    {
        GPIO_SetBits(SERVO_PORT , OIL_TEMPERATURE_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , OIL_TEMPERATURE_PIN);
    } 

    if(CurrentDegree < GetCoolerWaterTemperature())
    {
        GPIO_SetBits(SERVO_PORT , COOLER_WATER_TEMPERATURE_PIN);
    }
    else
    {
        GPIO_ResetBits(SERVO_PORT , COOLER_WATER_TEMPERATURE_PIN);
    }     
   }     
 }

the generated pwm works fine for 5 pins in port A. but when I increase the number of ports, the stm32 hangs. how I can to increase pins?


Solution

  • if you have multi servo motors more than PWM Channels, so you can use PWM Genrator IC like TL594, find it in the web!