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.
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:
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.
Built-in 12MHz crystal oscillator, the frequency division coefficient can be set through software to obtain different system clock frequencies.
It has a variety of peripheral interfaces, including UART, SPI, I2C, timers, interrupts, etc., which can easily implement various applications.
Supports ISP (In-System Programming) programming, and can be programmed online through the serial port or parallel port, which is convenient and fast.
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.
Previous article:Summary table of register functions of 51 microcontroller
Next article:51 microcontroller programming environment construction method
- Popular Resources
- Popular amplifiers
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- There is a section of LCD code in the atomic stm32f1 tutorial that is not clear, please help me take a look
- Please recommend a 3.3V output chip
- Signal Integrity Analysis (Bergding)
- A multifunctional data acquisition system based on USB and FPGA
- COCOFLY Tutorial - Crazy Shell Drone Series Quick Start [4] Flight Control Firmware Burning
- GD32E231 DIY Competition (1)——KEIL platform built
- Can GaN technology create superior RF crystals?
- Today at 10:00 AM R&S Live Broadcast with Prizes [USB 3.2 Compliance Test]
- Let's talk about five cents: Today's manager is standing at work
- 【DFRobot wireless communication module】Hardware analysis