Program name: Brightness control of light-emitting diode D1
Version: VER1.0
Applicable board: 51hei learning board
Use timer control to generate PWM wave with variable duty cycle to control the brightness of light-emitting diode D1
Press S2, the PWM value increases, the duty cycle decreases, and the D1 light becomes brighter.
Press S3, the PWM value decreases, the duty cycle increases, and the D1 light becomes darker.
When the PWM value increases to the maximum value or decreases to the minimum value, the buzzer will alarm.
******************************************************************/
#include
#include
sbit S2 =P3^4 ; //PWM value decrease key
sbit S3 =P3^5 ; //PWM value increase key
sbit BEEP =P2^3 ; //Buzzer
unsigned char PWM=0x03 ; //Assign initial value
/////////////////////////////////////////
void Beep();
void delayms(unsigned char ms );
void delay(unsigned char t);
/************************************************************/
void main()
{
P1=0xff;
TMOD=0x00 ;
TH0=0x00 ; //Delay constant
TL0=0x00 ; //Frequency adjustment
TH1=PWM ; //Pulse width adjustment
TL1=0xff ;
EA=1;
ET0=1;
ET1=1;
TR0=1 ;
while(1)
{
do{
if(PWM!=0xff)
{PWM++ ;delayms(10);}
else Beep() ;
}
while(S3==0);
do{
if(PWM!=0x02)
{PWM-- ;delayms(10);}
else Beep() ;
}
while(S2==0);
}
}
/************************************************************/
// Timer 0 interrupt service routine (frequency)
/************************************************************/
void timer0() interrupt 1
{
TR1=0 ;
TH0=0x00;
TL0=0x00 ;
TH1=PWM ;
TR1=1 ;
P1=0xfe ; // Start output
}
/************************************************************/
// Timer 1 interrupt service routine (pulse width)
/************************************************************/
void timer1() interrupt 3
{
TR1=0 ;
P1=0xff ; //End output
}
/************************************************************/
//Buzzer subroutine
/****************************************************************/
void Beep()
{
unsigned char i ;
for (i=0 ;i<100 ;i++)
{
delay(100) ;
BEEP=!BEEP ; //Invert Beep
}
BEEP=1 ; //Turn off the buzzer
delayms(100);
}
/************************************************************/
// Delay subroutine
/************************************************************/
void delay(unsigned char t)
{
while(t--) ;
}
/************************************************************/
// Delay subroutine
/************************************************************/
void delayms(unsigned char ms)
{
unsigned char i ;
while(ms--)
{
for(i = 0 ; i < 120 ; i++) ;
}
}
/****************************************************** **********/
Previous article:51 MCU servo control program
Next article:MCU timer to achieve real-time clock program - lcd1602 display
Recommended ReadingLatest update time:2024-11-23 07:44
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- STM32H745I-DISCO
- FRC
- How to program the C2000 on-chip Flash?
- EEWORLD University ---- Tektronix uses mixed signal oscilloscope to explore the secrets of the bus
- New RTOS Tutorial: Getting Started with RT-Thread Kernel in 15 Days
- Come to the thread and tell us what are the key points and difficulties in learning motor control [Reply to get the core]
- Why choose 120Ω? The terminal resistance value recommended by the CAN standard
- Practical Tips | This article teaches you how to choose a power supply topology
- Code Analysis of Event Kernel in RSL10 SDK (Part 1)
- GP8101 practical application evaluation: Bluetooth module output PWM to 0-10V dimming