简体   繁体   中英

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?

如果您拥有多个伺服电机而不是PWM通道,那么您可以使用PWM发生器IC,例如TL594,在网上找到它!

Since you omitted a few important details like the part number of your MCU, or the so-called MCVE , I can only guess here.

As you are adding more instructions to it, your timer interrupt may not finish before the next one comes. You can decrease the interrupt frequency, but it would make more sense to abandon software interrupts and use the hardware PWM generators.

STM32 general-purpose timers have up to 4 PWM outputs.

Edit: in an earlier port you've mentioned that you are using STM32F103RET. That MCU actually has 24 hardware PWM outputs.

See the chapter on general purpose timers in your reference manual, or section 2.5 in ST Application note AN2403 STM32 cross-series timer overview on how to use them.

You have 3 choice!
1)use encoders ic
or
2) pwm generator ic
or
3)just use multi arm cortex!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM