Programming The ESP32 In C - MCPWM First Example |
Written by Harry Fairhead | |||||
Tuesday, 03 December 2024 | |||||
Page 3 of 4
Setting ActionHaving “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:
MCPWM_TIMER_DIRECTION_UP MCPWM_TIMER_DIRECTION_DOWN
MCPWM_TIMER_EVENT_EMPTY MCPWM_TIMER_EVENT_FULL MCPWM_TIMER_EVENT_INVALID
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, 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 TimerWe 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:
Once you have set its restart behavior, you can start and stop the timer using software:
If you have finished using the timer you can delete it:
You can also change the timer’s period:
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:
|
|||||
Last Updated ( Tuesday, 03 December 2024 ) |