Design requirements:#
Using PWM to drive 8 LEDs
The human eye cannot detect when all lights go out
Lighting should have animation effects
Design Overview:
According to the design requirements, in order to more intuitively illustrate the pulse width modulation technology (PWM), the animation effect of the neon light is in the form of a flowing light. The required single-chip microcomputer chip is STC89C52, and the hardware tool used is a smart car based on STC89C52 developed by Huaqing Yuanjian. The car is equipped with the required LED light module, and the 8 LED lights are controlled by port P1. STC89C52 is a low-power, high-performance 8-bit microcontroller, which is an enhanced version of the 80C51 single-chip microcomputer, but like the 80C51 single-chip microcomputer, it does not have a PWM hardware module, so we need to write a program to simulate the PWM square wave by software.
PWM is a square wave that can realize digital signal control of analog circuits. It has two important parameters: cycle or frequency, and duty cycle. Duty cycle = high level time/cycle, with a minimum of 0% and a maximum of 100%. The duty cycle is adjusted to control the proportion of high and low levels, thereby regulating the brightness and darkness of the LED light. In STC89C52, the P1 port is valid at low level. When the P1 port is set to 0, the LED light is on, and when it is set to 1, the light is off. If the level output signal of the P1 port is adjusted to 50% high and 50% low, that is, the high level and the low level account for 50% in a cycle, the LED light controlled by the P1 port will flicker.
Many 51 single-chip microcomputer development boards on the market are similar, and all use the P1 port to control the LED lights, so the schematic diagram and physical picture of the smart car are not provided here.
source code:#
Software simulation of PWM square wave is usually implemented using a timer.
Reference code 1:
#include
//Define a global variable pwm, and control the PWM duty cycle by accumulating the global variable
unsigned int pwm = 0;
/*
Use the timer and I/O port to output PWM square wave to achieve the brightness change of 8 LED lights
*/
/*Timer interrupt service function*/
void Timer0() interrupt 1
{
pwm++;
if(pwm == 500)
{
P1 = 0xfe; //The first light is on
}
else if(pwm == 1000)
{
P1 = 0xff; //The first light is off
}
else if(pwm == 1500)
{
P1 = 0xfd; //The second light is on
}
else if(pwm == 2000)
{
P1 = 0xff;
}
else if(pwm == 2500)
{
P1 = 0xfb; //The third light is on
}
else if(pwm == 3000)
{
P1 = 0xff;
}
else if(pwm == 3500)
{
P1 = 0xf7; //The 4th light is on
}
else if(pwm == 4000)
{
P1 = 0xff;
}
else if(pwm == 4500)
{
P1 = 0xef; //The 5th light is on
}
else if(pwm == 5000)
{
P1 = 0xff;
}
else if(pwm == 5500)
{
P1 = 0xdf; //The 6th light is on
}
else if(pwm == 6000)
{
P1 = 0xff;
}
else if(pwm == 6500)
{
P1 = 0xbf; //The 7th light is on
}
else if(pwm == 7000)
{
P1 = 0xff;
}
else if(pwm == 7500)
{
P1 = 0x7f; //The 8th light is on
}
else if(pwm == 8000)
{
P1 = 0xff;
pwm = 0;
}
}
void main()
{
TMOD |= 1<<1; //Change the state of the bit through the shift operator "<<"
TMOD &= ~(1<<0); //Set the timer/counter to work in mode 2
TMOD &= ~(1<<2); //Select the timing working mode
TMOD &= ~(1<<3); //Gate bit: start the timer by the run control bit TR
TL0 = 156;
TH0 = 156; //100us enters an interrupt, 0.1 milliseconds
ET0 = 1; //Timer 0 interrupt
EA = 1; //CPU interrupt
TR0 = 1; //Start timer 0
while(1) //Prevent the program from running away
;
}
Reference code 2:
/*The first file is a custom header file
The pwm_led_ctl function is declared in this header file
*/
#ifndef _LED_H
#define _LED_H
//Control a light
char pwm_led_ctl(unsigned int led_num);
#endif
/*
Second file
This source file is used to implement the pwm_led_ctl function
*/
#include
#include "led.h"
//Software simulates PWM square wave and adjusts PWM duty cycle through variable pwm
char pwm_led_ctl(unsigned int led_num)
{
unsigned int pwm;
for(pwm = 0;pwm <= 2000;pwm++)
{
if(pwm == 1000)
{
P1 &= ~(1<
}
else if(pwm == 2000)
{
P1 |= 1<
}
}
return 0;
}
/*
The third file
Main function source file
*/
#include
#include "led.h"
/*Timer interrupt service function*/
void Timer0() interrupt 1
{
unsigned int i;
for(i=0;i<8;i++)
{
pwm_led_ctl(i);
}
}
void main()
{
TMOD |= 1<<1; //Change the state of the bit through the shift operator "<<"
TMOD &= ~(1<<0); //Set the timer/counter to work in mode 2
TMOD &= ~(1<<2); //Select the timing working mode
TMOD &= ~(1<<3); //Gate bit: start the timer by the run control bit TR
TL0 = 156;
TH0 = 156; //100us enters an interrupt, 0.1 milliseconds
ET0 = 1; //Timer 0 interrupt
EA = 1; //CPU interrupt
TR0 = 1; //Start timer 0
while(1) //Prevent the program from running away
;
}
Previous article:Analysis of the interrupt response process of MCS-51
Next article:Automatic doorbell based on STC51 single chip microcomputer
- Popular Resources
- Popular amplifiers
- 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)
- Learn ARM development (4)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- GoKit Case Study: Gizwits IoT Development Platform’s Pet House Transformed into an Air Quality Detector
- Embedded licensing: why not and why not?
- I found a 1963 "Electron Tube Handbook"
- How to extract the effective value of fundamental wave and each harmonic in MATLAB?
- Analysts predict Bluetooth location services will grow at a CAGR of 32% by 2025
- TI blog post - Introduction to basic parameters of analog switches and multiplexers
- Bluetooth chip
- How many versions of the C language main function are there? Which one is right?
- stm32f4 serial port 3 prompts NE error
- Design and implementation of asynchronous FIFO based on Verilog HDL.pdf