Forum: Mikrocontroller und Digitale Elektronik STM32 PWM ON/OFF


von mimi m. (Firma: mumu) (ysp)


Lesenswert?

What is the best way to switch the output of a pin between  PWM ON and 
PWM OFF (Pin state high)?

Basically what I want to get is:

High(10ms) -- PWM(20ms) -- High(50ms)...

The time tells the Duration of the pins state. I have the PWM working as 
it should but just do not know how to switch it ON and OFF.

This is the way I initialized the timer:

  TIM_TimeBaseStructure.TIM_Period = TIM_ARR;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

The channel:
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = TIM_CCR;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

Is it the right way to use something like:

  TIM4->CCR4=0;
or
  TIM_SetCompare1(TIM4,0);

But this was not working for me. Thanks for your ideas.

von tom (Gast)


Lesenswert?

1. put an external pull-up resistor on your pwm-output pin.
2. use (systick) timer for a at least msecs-based timer functions
3. switch by software between pwm-out mode and dio input mode at your 
desired msecs interval.
4. have fun

von Stump (Gast)


Lesenswert?

If I understand correctly what you are trying to achieve, it's as simple 
as setting the PWM value to 100% duty cycle (on), then to 50% duty cycle 
(or whatever PWM value you want) and so on.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.