Microcontroller breathing light program and waveform demonstration

Publisher:mu22Latest update time:2020-08-05 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I found a breathing light program on the Internet and wrote it myself.
I used a timer to generate pwm to realize the breathing light
. I can also control the speed of the DC motor by referring to the breathing light program. You can go to this page to see https://www.zhihu.com/tardis/sogou/art/83373994

pwm waveform:
1.gif


The microcontroller source program is as follows:

#include

#define uint unsigned int

#define uchar unsigned char


bit flag; //Determine whether to brighten or darken based on flag

uchar count; //Enter interrupt times

uchar pwm; //Turn on or off according to this value

uint updata; //Update the duty cycle according to this value, period 120ms

sbit LED=P2^0; //LED pin

void timer0();

/*********Timer initialization function***********/

void timer0_init()               

{

        TMOD=0x02; //Timer 0, mode 2

        TH0=0xa3; //Assume the crystal oscillator is 11.0592M, the timing is 100us

        TL0=0xa3;               

        ET0=1; //Open timer 0 interrupt

        EA=1; //Open the general interrupt

        TR0=1; //Start the timer

}


void main()

{

        flag=0; //Initial duty cycle rise

        count=0;

        pwm=10; //Pulse period is 2ms, initial duty cycle is 50%

        updata=0;

        LED=1;

        timer0_init();

        while(1);

}


void timer0() interrupt 1

{

        count++;

        updata++;

        if(count==pwm)

                LED = 0;

        if(count==20)

        {

                LED=1;

                count=0;

        }

        if((updata==1200)&&(flag==0)) //120ms arrives, update the duty cycle according to flag

        {

                updata=0;

                pwm++; //Add 1 each time, the duty cycle increases by 5%

                if(pwm==19)

                        flag=1;

        }

        if((updata==1200)&&(flag==1))

        {

                updata=0;

                pwm--;

                if(pwm==1)

                        flag=0;

        }

}


3-Basic parameters of PWM

In the figure above, the value of frequency F is 1/(T1+T2), and the value of duty cycle D is T1/(T1+T2). Frequency modulation can be achieved by changing the number of pulses per unit time; voltage regulation can be achieved by changing the duty cycle. The larger the duty cycle, the larger the average voltage and the larger the amplitude; the smaller the duty cycle, the smaller the average voltage and the smaller the amplitude. The dynamic diagram is shown in Figure 4.

Keywords:MCU Reference address:Microcontroller breathing light program and waveform demonstration

Previous article:Single chip electronic perpetual calendar with lunar calendar display
Next article:51 MCU beginner practice: using BME280 module to obtain temperature, humidity and air pressure data

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号