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

The ESP32 S3 has two PWM generators - a simple one aimed at driving LEDs and a more complex one designed to make motor control easy. To get you started using it we consider a simple example. This is an extract from Harry Fairhead's latest book,  Programming the ESP32 Using C and the Espressif IoT Development Framework

Programming The ESP32 In C
Using The Espressif IDF

By Harry Fairhead

espC360

Available as a softback, hardback and kindle from Amazon

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
  9. The Motor Control PWM
         Extact: MCPWM First Example ***NEW!
  10. Controlling Motors And Servos
  11. Getting Started With The SPI Bus
  12. Using Analog Sensors
  13. Using The I2C Bus
  14. One-Wire Protocols
         
    Extract: The S3's RGB LED 
  15. The Serial Port
  16. Using WiFi
          Extract:Socket Web Client
  17. Direct To The Hardware
  18. Free RTOS 

<ASIN:1871962919>

<ASIN:187196282X>

The Motor Control PWM

The motor control pulse width modulation facility, MCPWM, is much more flexible than the LED pulse width modulation covered in the previous chapter, but this makes it harder to get started with. It has many features that you probably don’t want to use at first and it can be difficult to understand its basic operation. To make things easier, we can ignore many of the more advanced modules in the MCPWM and only work with the minimum needed to get started.

Many of the simpler things that the MCPWM can do can also be done using the LED PWM system. Indeed, if you want hardware fade then the LED PWM is your only choice. The MCPWM has a lot of special features that make it possible to generate multiple PWM signals with fairly complex phase relationships which are needed to drive motors and for general signaling. It also provides facilities to synchronize PWM with outside sources, react to error states by braking the motor and inserting deadtime into multiphase PWM to drive bridge circuits.

ESP32 MCPWM

At first you only need to concentrate on the basic structure of a MCPWM configured to produce a simple PWM signal:

 pwmmot0


There are two complete MCPWM systems and each one has three timers and three operators each controlling two outputs, A and B, making a total of three pairs of outputs. Each pair works with the same timer and so runs at the same frequency, but they can differ in duty cycle and phase. The operator contains all of the additional modules that can change the output, including a single comparator.

pwmmot1

The timer is the core of the system and it counts down or up or alternately up and down. It can generate events when it reaches zero or its high value. The comparator associated with the generator can raise an event when the count reaches a set value. The generators can change the state of either output on the basis of the events received from the timer or the comparator. For example, the generator could be set up to toggle one of the outputs each time the timer count reached zero and this would produce a basic PWM signal, or it could set one line high on a zero event and low on a max count event.

There are three types of timer event:

  • MCPWM_TIMER_EVENT_EMPTY Timer counts to zero

  • MCPWM_TIMER_EVENT_FULL Timer counts to peak

  • MCPWM_TIMER_EVENT_INVALID Should never happen

There is also a comparator event:

  • MCPWM_CMPR_ETM_EVENT_EQUAL Timer equals the threshold value

In response to an event the generator can perform any of four actions:

  • MCPWM_GEN_ACTION_KEEP Keep the same level

  • MCPWM_GEN_ACTION_LOW Force to low level

  • MCPWM_GEN_ACTION_HIGH Force to high level

  • MCPWM_GEN_ACTION_TOGGLE Toggle level

 

You can see that there are multiple ways of generating a signal. For example, you could set two timer events and toggle the output on zero and peak, or you could set a single timer event and set the output high on zero and a comparator event to set the output low when the count is reached, and so on. The important point to notice is that, while you have a choice how to generate a given PWM signal, exactly how you do it alters its phase relationship with other generated PWM signals. This can seem complicated so initially we will focus on generating a single PWM signal without worrying about phase issues.



Last Updated ( Tuesday, 03 December 2024 )