I. Overview
Use 51 single chip microcomputer to control LED to realize the gradual change process from dark to bright and then from bright to dark.
II. Procedure
#include <STC15F2K60S2.H>
#include "MacroAndConst.h"
sbit LED = P1^5;
#define LED_ON() LED = 0; //LED is on
#define LED_OFF() LED = 1; //LED off
#define LED_PWM_LIMIT_MAX 99
#define LED_PWM_LIMIT_MIN 0
static u8 s_u8TimeCounter = 0; //interrupt count
static u8 s_u8LedDirection = 0; //LED direction control 0: gradually brighten 1: gradually fade out
static u8 s_u8LedPWMCounter = 0; //LED duty cycle
void Timer0Init(void)
{
AUXR |= 0x80; //Timer clock 1T mode
TMOD &= 0xF0; //Set timer mode
TL0 = 0x5C; //Set the initial timing value
TH0 = 0xF7; //Set the initial value of timing
TF0 = 0; // Clear TF0 flag
TR0 = 1; //Timer 0 starts timing
ET0 = 1; // Enable timer 0 interrupt
}
void Timer0Isr(void) interrupt 1
{
static int8 s_u8PWMCounter = 0;
TH0 = 0xF7; //Timer initial value (200us interrupt once)
TL0 = 0x5C;
if(++s_u8TimeCounter >= 100) //Adjust LED duty cycle every 20ms
{
s_u8TimeCounter = 0;
//If the direction changes gradually, the duty cycle increases
if((s_u8LedPWMCounter <= LED_PWM_LIMIT_MAX)&&(s_u8LedDirection == 0))
{
s_u8LedPWMCounter++;
if(s_u8LedPWMCounter > LED_PWM_LIMIT_MAX)
{
s_u8LedDirection = 1;
s_u8LedPWMCounter = LED_PWM_LIMIT_MAX;
}
}
//If the direction changes gradually, the duty cycle decreases
if((s_u8LedPWMCounter > LED_PWM_LIMIT_MIN)&&(s_u8LedDirection == 1))
//There is a pit here. If you write it as ">=", you will not achieve the expected effect.
{
s_u8LedPWMCounter--;
if(s_u8LedPWMCounter <= LED_PWM_LIMIT_MIN)
{
s_u8LedDirection = 0;
s_u8LedPWMCounter = LED_PWM_LIMIT_MIN;
}
}
s_u8PWMCounter = s_u8LedPWMCounter; //Get the duty cycle of LED
}
if(s_u8PWMCounter > 0) //If the duty cycle is greater than 0, the LED will be on, otherwise it will be off
{
LED_ON();
s_u8PWMCounter--;
}
else
{
LED_OFF();
}
}
Previous article:Beginner's guide to 51 MCU - simple water lamp program
Next article:51 single chip microcomputer--LED flashing, simple experiment of running water light
Recommended ReadingLatest update time:2024-11-16 21:53
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University ----3
- What development environment is used for bare metal programming of imax6ull
- SparkRoad serial port to localbus to achieve command control
- EEWORLD University Hall----Live Replay: STMicroelectronics SiC Products and Industrial Application Guide
- Pre-registration for the prize-winning live broadcast | Explore the high-tech of network cameras with Infineon
- msp430f149 baud rate setting
- Topmicro Smart Screen: Remote update tools download
- According to this IPC-7351 software, how to select the pad type.
- Changed the layout again
- National Award-winning Works: Temperature Automatic Control System