Programming The ESP32 In C - MCPWM First Example |
Written by Harry Fairhead | |||||
Tuesday, 03 December 2024 | |||||
Page 2 of 4
Setting up the MCPWMTo build a basic MCPWM system we need a timer and an operator. First, to create a timer we use: mcpwm_new_timer(pconfig, ptimerhandle) where pconfig points to mcpwm_timer_config_t, a struct containing:
MCPWM_TIMER_COUNT_MODE_PAUSE MCPWM_TIMER_COUNT_MODE_UP MCPWM_TIMER_COUNT_MODE_DOWN MCPWM_TIMER_COUNT_MODE_UP_DOWN
The key facts are that resolution_hz sets the time for a single counter increment as 1/resolution_hz in seconds and period_ticks sets the time to count from 0 to rollover. For example, setting resolution_hz to 1000000 makes one tick take 1μs and hence setting period_ticks to 20000 makes the time to rollover 20000μs or 20ms, i.e. the PWM frequency is 50Hz. The counter starts at 0 and rolls over at 200000. Given that the default clock is 80MHz, the divider will be automatically set to 4000. So, to set up a timer for a 50Hz PWM signal: mcpwm_timer_config_t timer_config = { .group_id = 0, .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, .resolution_hz = 1000000, .period_ticks = 20000, .count_mode = MCPWM_TIMER_COUNT_MODE_UP, }; mcpwm_timer_handle_t timer = NULL; mcpwm_new_timer(&timer_config, &timer); After the timer we need to create an operator: mcpwm_new_operator(pconfig, poper) where pconfig points to mcpwm_operator_config_t ,a struct with fields:
You can ignore the update fields for the moment as they only control when software-applied updates are acted on. A simple operator that can be used with the timer is: mcpwm_operator_config_t operator_config = { .group_id = 0, }; mcpwm_oper_handle_t oper = NULL; mcpwm_new_operator(&operator_config, &oper); Now that we have an operator and a timer we can connect them together using: mcpwm_operator_connect_timer(oper, timer) The operator contains other modules to generate a PWM signal, but all we need for a simple single PWM signal is a generator that connects the PWM signal to a GPIO line. A new generator can be created using: mcpwm_new_generator(handle_t oper, pconfig, pret_gen) where pconfig points to mcpwm_generator_config_t, a struct with fields:
A simple generator to connect to GPIO line 2 is: mcpwm_generator_config_t generator_config = { .gen_gpio_num = 2, }; mcpwm_gen_handle_t generator = NULL; mcpwm_new_generator(oper, &generator_config, Now we have a minimal MCPWM system configured and ready to be used.
|
|||||
Last Updated ( Tuesday, 03 December 2024 ) |