3641 views|0 replies

85

Posts

0

Resources
The OP
 

【ESP32-C3-DevKitM-1】LED PWM for ESP32-C3 [Copy link]

 
  1. LED PWM Reviews

ESP32-C3 has 6 LED PWM generators inside, which can be used for PWM output, with a precision of up to 14 bits, and an input clock of up to 80MHZ . It also supports fractional frequency division, and the duty cycle can be adjusted independently. At the same time, the hardware supports automatic increase or decrease of the duty cycle, which is convenient for the development of LED RGB . For other specific technical details, please refer to the data sheet and reference manual

Following the previous method, create a new project, add a template, as shown below, and then compile and run

Then compile and run, and use the serial port assistant that comes with Eclipse to view it. As shown in the figure below, a 5KHZ square wave will be output with a duty cycle of about 50%

First, let's analyze the program roughly

1. First, configure the timer

Among them, duty_resolution = LEDC_TIMER_12_BIT is the configuration ofPWMresolution bits.The PWMofESP32-C3can be configured to14bits at most. Generally speaking, the higher the resolution bit, thefiner and morePWM14bits meansPWM2^14digitally, so the duty cycle that can be output is finer, that is, the resolution.

.freq_hz = 10000 is to set the frequency. After repeated trial and error (mainly because I didn't look carefully), according to my understanding, first look at the figure below, the frequency is due to the timer clock source LEDC_CLKx , the clock division factor

LEDC_CLK_DIV_TIMERx and the counting range of the counter LEDC_TIMERx_DUTY_RES determine the clock source. The default clock source is 80MHz . The clock division factor includes a 10 -bit integer part and an 8 -bit decimal part, ranging from 1 to 1024. The counting range is 2^duty_resolution. So when duty_resolution is determined , the PWM frequency that can be output at the resolution of duty_resolution has a fixed range , as shown in the table below (there may be some misunderstandings, welcome to correct)

duty_resolution

Minimum frequency that can be set (HZ)

Maximum frequency that can be set (HZ)

LEDC_TIMER_1_BIT

39062.5

40,000,000

LEDC_TIMER_2_BIT

19531.25

20,000,000

LEDC_TIMER_3_BIT

9765.625

10,000,000

LEDC_TIMER_4_BIT

4882.8125

5,000,000

LEDC_TIMER_5_BIT

2441.40625

2,500,000

LEDC_TIMER_6_BIT

1220.703125

1,250,000

LEDC_TIMER_7_BIT

610.3515625

625,000

LEDC_TIMER_8_BIT

305.1757813

312500

LEDC_TIMER_9_BIT

152.5878906

156250

LEDC_TIMER_10_BIT

76.29394531

78125

LEDC_TIMER_11_BIT

38.14697266

39062.5

LEDC_TIMER_12_BIT

19.07348633

19531.25

LEDC_TIMER_13_BIT

9.536743164

9765.625

LEDC_TIMER_14_BIT

4.768371582

4882.8125

Therefore, the selected frequency and resolution must be matched reasonably , otherwise the PWM wave cannot be output.

Except for the above two parameters , I feel that the remaining three parameters are not very important.

2. Configure specific channels and pins

As shown in the figure below, this is also easy to understand. .duty = 0 is the count value corresponding to the duty cycle. The calculation formula is duty = (n/100) * 2^duty_resolution, where n is 0-100 . .flags.output_invert = 0 is to choose whether to invert. Finally, the structure is written through ledc_channel_config

If the duty cycle is fixed, it can be set in this structure. If the duty cycle needs to be changed, it is more convenient to call the following function.

ledc_set_duty(ledc_channel. speed_mode , ledc_channel. channel , LEDC_TEST_DUTY);

ledc_update_duty(ledc_channel. speed_mode , ledc_channel. channel );

If you want to implement hardware gradual duty cycle, you must first add this sentence ledc_fade_func_install(0);

Then

ledc_set_fade_with_time(ledc_channel[ ch ].speed_mode, ledc_channel[ ch ].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);

ledc_fade_start(ledc_channel[ ch ].speed_mode, ledc_channel[ ch ].channel, LEDC_FADE_NO_WAIT);

For details, you can refer to the routine, which is written in great detail.

Finally, I will put a few effect pictures and observe them with a logic analyzer.

The picture below is 4MHZ , 50% , which is still very easy.

The following figure is 5KHZ , the duty cycle automatically changes from 0-50 , and then from 50-0

This post is from Domestic Chip Exchange
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list