Breathing light designed based on 51 microcontroller

Publisher:BlissfulDreamsLatest update time:2024-03-19 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Project introduction

Breathing light is a common LED lighting effect that can simulate changes in human breathing and make the light look softer and more natural. The 51 microcontroller is a widely used microcontroller with the advantages of small size, low power consumption, and low cost. It is very suitable for controlling LED breathing lights. The breathing light of this project will use PWM (Pulse Width Modulation) technology to control the LED brightness to achieve the effect of a breathing light.


In this project, a 51 microcontroller will be used as the main controller to control the breathing light through programming. Code will be written in C and compiled and debugged using the Keil C51 integrated development environment. Use Proteus simulation software for circuit design and simulation to ensure the correctness and stability of the circuit.

image-20230606214353670

2. Design principles

2.1 PWM technology

PWM is the abbreviation of Pulse Width Modulation, which is a technology that controls circuits by changing pulse width. In digital circuits, PWM is a very common technology, which can be used to control the brightness, speed and other parameters of electronic devices such as motors and LED lights.


The basic principle of PWM technology is to control the output of the circuit by controlling the width and period of the pulse. In a PWM cycle, the circuit will generate a series of pulses at a certain frequency (that is, the PWM frequency). The width of each pulse and the proportion of high-level time to the entire cycle are set by the controller as needed. In this way, precise control of the circuit output can be achieved.


In the LED breathing light project, the breathing light effect can be achieved by using timer simulation PWM technology. Specifically, a timer is used to generate a pulse signal of a certain frequency, and then the brightness of the LED light is controlled by changing the duty cycle of the pulse. When the duty cycle of the pulse gradually increases, the brightness of the LED light will gradually increase until it reaches the maximum brightness; when the duty cycle of the pulse gradually decreases, the brightness of the LED light will gradually weaken until it finally goes out. This allows for a gradient effect similar to human breathing.


2.2 Principle of breathing lamp

Breathing light is a technology that makes LED light into a gradient effect. It can slowly increase and decrease the brightness of the LED within a certain period of time, making the brightness changes of the LED more natural and softer. It is suitable for scenes that require a gradient effect. , such as lighting adjustment, sound rhythm, etc.

The principle of the breathing light is to control the brightness of the LED by changing the duty cycle of the LED's PWM signal. PWM (Pulse Width Modulation) is a common technology for adjusting the amplitude of analog signals. It adjusts the signal amplitude by changing the pulse width of the signal. In a breathing light, the frequency of the PWM signal is higher, and the duty cycle gradually changes over time, thereby achieving a gradual change in LED brightness.

The implementation of breathing lights usually requires the use of a timer and a PWM module. The timer is used to trigger interrupt events regularly and change the duty cycle of the PWM signal in the interrupt processing function to control the brightness of the LED. In the timer interrupt processing function, different PWM duty cycles can be obtained through mathematical functions (such as sine, cosine, etc.) or simple numerical calculations to achieve different breathing light effects.


2.3 51 microcontroller

The 51 microcontroller is a widely used microcontroller with the advantages of small size, low power consumption, and low cost. It is very suitable for controlling LED breathing lights.

STC89C52 is an 8-bit microcontroller based on the MCS-51 core, produced by STC Company in China. It has the characteristics of high cost performance, easy programming, and wide application. It has been widely used in industrial control, communications, home appliance control and other fields.

The main features of STC89C52 microcontroller are as follows:

  1. Using the MCS-51 core, it has an 8-bit data bus and a 16-bit address bus, and can access 64KB of program memory and 64KB of data memory.

  2. Built-in 12MHz crystal oscillator, the frequency division coefficient can be set through software to obtain different system clock frequencies.

  3. It has a variety of peripheral interfaces, including UART, SPI, I2C, timers, interrupts, etc., which can easily implement various applications.

  4. Supports ISP (In-System Programming) programming, and can be programmed online through the serial port or parallel port, which is convenient and fast.

  5. It has a low power consumption mode and can enter different sleep modes through software settings to save system energy consumption.

The STC89C52 microcontroller can be programmed using C language or assembly language. The written program can generate a HEX file through a compiler and then burn it into the chip through a programmer. Due to the wide application and rich information of STC89C52 microcontroller, it is relatively easy to learn and use it.


3. Code implementation

3.1 Automatic breathing light

Because the STC89C52 microcontroller does not have a PWM output function, it can only be implemented using a delay function. The following is the complete code to implement the breathing light effect based on the STC89C52 microcontroller:


#include

 

 #define LED P1

 

 void delay(unsigned int xms)

 {

     unsigned int i, j;

     for (i = xms; i > 0; i--)

         for (j = 110; j > 0; j--);

 }

 

 void main()

 {

     unsigned char i;

     while (1)

     {

         for (i = 0; i < 255; i++)

         {

             LED = i;

             delay(10);

         }

         for (i = 255; i > 0; i--)

         {

             LED = i;

             delay(10);

         }

     }

 }

In this code, the P1 port of the STC89C52 microcontroller is used to control the brightness of the LED light. Through a cycle, the brightness of the LED light gradually increases from 0 to 255, and then gradually decreases from 255 to 0, thus achieving the effect of a breathing light.


In the code, a delay function is used to control the speed of the loop. This function allows the program to delay for a certain period of time to control the brightness change speed of the LED light. In this code, each delay is set to 10 milliseconds. This value can be adjusted as needed to change the effect of the breathing light.


3.2 Button control of light brightness

The following is the complete code for LED light brightness control based on the STC89C52 microcontroller, which uses two buttons to control the brightness and extinction of the LED respectively.


#include

 

 #define LED P1

 

 sbit KEY_UP = P3 ^ 2;

 sbit KEY_DOWN = P3 ^ 3;

 

 unsigned char pwm = 0;

 

 void delay(unsigned int i) {

     while (i--);

 }

 

 void key_scan() {

     if (KEY_UP == 0) {

         delay(1000);

         if (KEY_UP == 0) {

             pwm += 10;

             if (pwm >= 100) {

                 pwm = 100;

             }

         }

     }

     if (KEY_DOWN == 0) {

         delay(1000);

         if (KEY_DOWN == 0) {

             pwm -= 10;

             if (pwm <= 0) {

                 pwm = 0;

             }

         }

     }

 }

 

 void main() {

     TMOD = 0x01; // Set timer 0 to mode 1

     TH0 = 0xFC; //Initial value of timer, used to generate PWM signal at a frequency of 50Hz

     TL0 = 0x67;

     TR0 = 1; // Start timer 0

     ET0 = 1; // Allow timer 0 interrupt

     EA = 1; // Turn on total interrupt

     while (1) {

         key_scan();

     }

 }

 

 void timer0() interrupt 1 {

     static unsigned char cnt = 0;

     if (cnt >= 100) {

         cnt = 0;

     }

     if (cnt < pwm) {

         LED = 0;

     } else {

         LED = 1;

     }

     cnt++;

 }

In the above code, timer 0 is used to generate a PWM signal to control the brightness of the LED. Two buttons are used to adjust the brightness and intensity of the LED. Among them, the KEY_UP button is used to increase the brightness of the LED, and the KEY_DOWN button is used to decrease the brightness of the LED. At each timer interrupt, the brightness of the LED is controlled according to the value of pwm. When cnt is less than pwm, the LED is low level and the LED brightness is high; when cnt is greater than or equal to pwm, the LED is high level and the LED brightness is low.


Reference address:Breathing light designed based on 51 microcontroller

Previous article:Summary table of register functions of 51 microcontroller
Next article:51 microcontroller programming environment construction method

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号