Programming The ESP32 In C - MCPWM First Example
Written by Harry Fairhead   
Tuesday, 03 December 2024
Article Index
Programming The ESP32 In C - MCPWM First Example
Setting up the MCPWM
Setting Action
A Simple example

Setting Action

Having “wired up” a timer and an operator and connected this to a GPIO line, we can configure it to produce a PWM signal. The idea here is that you can now specify what actions the generator should take when events occur. As already explained, there are two types of events, timer events and comparator events. As we haven’t, as yet, configured a comparator we only have timer events and there are only two such events – when the timer reaches zero, tez and when it reaches its peak count, tep.

You can set an action on a timer event using:

mcpwm_generator_set_action_on_timer_event(gen, ev_act)

where ev_act is a mcpwm_gen_timer_event_action_t struct:

  • direction One of:

			MCPWM_TIMER_DIRECTION_UP
			MCPWM_TIMER_DIRECTION_DOWN
  • event One of:

MCPWM_TIMER_EVENT_EMPTY
MCPWM_TIMER_EVENT_FULL
MCPWM_TIMER_EVENT_INVALID
  • action One of:

MCPWM_GEN_ACTION_KEEP
MCPWM_GEN_ACTION_LOW
MCPWM_GEN_ACTION_HIGH
MCPWM_GEN_ACTION_TOGGLE

You can set up two generators, corresponding to the A and B outputs, per operator.

As we only have a timer we can only set two possible events, timer empty or full. We could set the output high on one and low on the other, but that would produce a very short pulse as the time to go from full to empty is one tick. The only reasonable action is to toggle the output on one of the timer events:

 mcpwm_gen_timer_event_action_t event_action = {
    .direction = MCPWM_TIMER_DIRECTION_UP,
    .event = MCPWM_TIMER_EVENT_EMPTY,
    .action = MCPWM_GEN_ACTION_TOGGLE
  };
  mcpwm_generator_set_action_on_timer_event(generator, 
                                                 event_action);

There is a macro that simplifies specifying an event and action:

MCPWM_GEN_TIMER_EVENT_ACTION(dir, ev, act)

Using this we can rewrite the previous code as:

  mcpwm_generator_set_action_on_timer_event(generator, 
   MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, 
      MCPWM_TIMER_EVENT_FULL, MCPWM_GEN_ACTION_TOGGLE));

There are similar functions and macros for setting comparator events and actions.

As well as responding to events, the generator can also force a change in output at any time via the function:

mcpwm_generator_set_force_level(generator, level, 
hold_on)

which immediately sets the output to level. If hold_on is true then the level is held until it is released by calling the function with hold_on false.

Controlling the Timer

We now have a PWM setup that is ready to generate a signal. To do this all we need is to finish configuring the timer and enable it.

The first thing we have to do is set how the time is reloaded at the end of each period:

mcpwm_timer_start_stop(timer, command) 

where command is one of:

  • MCPWM_TIMER_STOP_EMPTY Timer stops when reaches zero

  • MCPWM_TIMER_STOP_FULL Timer stops when reaches peak

  • MCPWM_TIMER_START_NO_STOP Timer runs continuously

  • MCPWM_TIMER_START_STOP_EMPTY Timer starts and stops at zero

  • MCPWM_TIMER_START_STOP_FULL Timer starts and stops at peak

Once you have set its restart behavior, you can start and stop the timer using software:

  • mcpwm_timer_enable(timer)
  • mcpwm_timer_disable(timer)

If you have finished using the timer you can delete it:

  • mcpwm_del_timer(timer)

You can also change the timer’s period:

  • mcpwm_timer_set_period(timer, period_ticks)

This is the same as setting period_ticks in the struct when you first set up the Timer. Notice that when the update is actually performed depends on the setting of:

  • update_period_on_empty When timer counts to zero

  • uint32_t update_period_on_sync On sync event



Last Updated ( Tuesday, 03 December 2024 )