PWM video code analysis and explanation

Publisher:心灵律动Latest update time:2023-01-30 Source: zhihuKeywords:PWM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


void loop()

{

    static unsigned char head = 0;

    static unsigned long lastTick = timer0_millis;

    unsigned int i, j;


    for (i = 0; i < ledNum; ++i) // Turn on the light first, then turn it off when the duty cycle reaches the switching point

    {

        if (ledPwm[i] == 0)

            continue;

        digitalWrite(leds[i], LOW);

    }

   

    ledPwm[head] = initPwm; // The water drop head is the brightest


    for (i = 0; i < maxPwm; ++i) // Here is the PWM program for digital port simulation

    {

        for (j = 0; j < ledNum; ++j)

        {

            if (i == ledPwm[j])

                digitalWrite(leds[j], HIGH);

        }

        delayMicroseconds(1);

    }


    // If the delay time has not expired, jump out first without moving the water droplets. 

    if (timer0_millis - lastTick < delayMs) // Since it uses a digital port to simulate PWM, the program has to run non-stop, so delay() cannot be used to delay, otherwise it will get stuck.

        return;


    lastTick = timer0_millis;


    for (i = 0; i < ledNum; ++i) // Process the brightness of each light

    {

        ledPwm[i] = ledPwm[i] * deltaPercent / 100;

        if (ledPwm[i] <= deltaPwm){ ledPwm[i] = 0; }           

        else                      { ledPwm[i] -= deltaPwm; }

            

        if (i == head){ ledPwm[i] = initPwm; }            

    }

    

    head = (head + 1) % ledNum; // Move the water dropper

}

animated cover

Full version of LED "Meteor Shower" effect


Summarize:

1. We understand how PWM is implemented

2. LEDs will "deceive" our eyes at different frequencies. This is a common idea when playing with microcontrollers.

3. By analyzing the LED "meteor shower", we found that it is actually implemented using some simple processing methods and is not as complicated as we imagined.

4. There is a simple algorithm in LED "Meteor Shower". The algorithm is independent of the microcontroller and can also be implemented on other platforms (51, STM32, etc.). At the same time, the algorithm is also the soul of a program.

[1] [2]
Keywords:PWM Reference address:PWM video code analysis and explanation

Previous article:Debugging methods for using new IC chips in microcontroller projects
Next article:(3) Another way to learn the basic components of button control LED lights

Recommended ReadingLatest update time:2024-11-22 20:22

Three-phase voltage type PWM rectifier circuit
 The main circuit of the three-phase voltage-type PWM rectifier has a very fast response and a better input current waveform. When working in a steady state, the output DC voltage remains unchanged, and the switch tube is pulse-width modulated according to the sinusoidal law. The output voltage of the rectifier AC is
[Power Management]
Three-phase voltage type PWM rectifier circuit
Design of switching power supply based on SG3525
With the development of power conversion technology, power MOSFET is widely used in switching converters. For this reason, Silieon General Semiconductor Company (Silieon General) of the United States launched SG3525 to drive n-channel power MOSFET. SG3525 is a current-controlled PWN controller. It can directly compare
[Microcontroller]
Design of switching power supply based on SG3525
Freescale 16-bit MCU (VIII) - PWM module test
1. PWM module introduction        PWM is widely used in the industrial field and is an effective means to achieve D/A conversion and accurate pulse sequence output. Many microcontrollers are equipped with PWM output function. The PWM module of XEP100 microcontroller has the following features: (1) The XEP100 microco
[Microcontroller]
Brief Analysis of PWM Driver and Test Program of Mini2440 Development Board
First look at the circuit schematic #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include
[Microcontroller]
Brief Analysis of PWM Driver and Test Program of Mini2440 Development Board
STM32f4---PWM output experimental code
The pwm.c source file code is as follows:   //TIM14 PWM initialization   //PWM output initialization //arr: automatic reload value psc: clock pre-division number void TIM14_PWM_Init(u32 arr,u32 psc) {                     GPIO_InitTypeDef GPIO_InitStructure;   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;   TIM_OCInit
[Microcontroller]
SiRuiPu releases the first automotive-grade current sensing amplifier TPA132Q! With PWM suppression capability, it helps new energy vehicle motor drive
3PEAK (stock code: 688536), a semiconductor supplier focusing on high-performance analog chips and embedded processors, has launched the TPA132Q, an automotive-grade bidirectional current sensing amplifier with enhanced PWM rejection capability. TPA132Q provides excellent current detection accuracy and fa
[Automotive Electronics]
SiRuiPu releases the first automotive-grade current sensing amplifier TPA132Q! With PWM suppression capability, it helps new energy vehicle motor drive
Cuk PWM DC/DC Converter
The Boost and Buck circuits can be cascaded to form a Cuk circuit. The combination process is shown in the figure. It can be seen from the circuit conversion process in the figure that after the Boost and Buck circuits are properly converted, the switch tube V, diode D, and capacitor C in the two circuits can overla
[Power Management]
Cuk PWM DC/DC Converter
Analysis and Design of Phase-Shifted Full-Bridge ZVS-PWM Converter
1 Circuit principle and working mode analysis 1.1 Circuit Principle Figure 1 shows the circuit topology of the phase-shifted full-bridge ZVS-PWM resonant converter. Vin is the input DC voltage. Si (i=1.2.3, 4) is the power MOS switch tube with the same parameters of the i-th. Di and Gi (i=1, 2, 3, 4) are t
[Power Management]
Analysis and Design of Phase-Shifted Full-Bridge ZVS-PWM Converter
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号