Use the microcontroller timer 0 to generate multiple time intervals. In the program, use timer 0 to interrupt once every 50ms, and then use global variables tt, t1, t2, t3, t4, t5 to count the number of interrupts to obtain 100ms, 1s, 2s, 4s, 8s, 16s intervals. In each timing interval, turn the LED light on and off once. The source program is as follows:
#include < AT89X51.H>
unsigned char tt,t1,t2,t3,t4,t5; //Global time interval variable
unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,
0x82,0xF8,0x80,0x90}; //Seven-segment digital tube decoding table
void main(void) //Main function
{
TMOD=0x01; //Timer 0 runs in working mode 1, GATE=0, C/T=0
TH0=(65536-15536)/256; //Set the initial value of the high 8 bits of timer 0
TL0=(65536-15536)%256; //Set the initial value of the low 8 bits of timer 0
//If the machine cycle is 1μs, the count is 50000, which is equivalent to 50ms.
TR0=1; //Start timer
ET0=1; //Enable timer 0 interrupt
EA=1; //Enable total interrupt
while(1) //Infinite loop
{
if(t1>5) P2_0=0; else P2_0=1; //Blink the LED according to the time interval variable value
if(t2>10) P2_1=0; else P2_1=1;
if(t3>20) P2_2=0; else P2_2=1;
if(t4>40) P2_3=0; else P2_3=1;
if(t5>80) P2_4=0; else P2_4=1;
P1=dispcode[t4/10]; //Display the tens digit of interval variable t4
P0=dispcode[t4%10]; //Display the ones digit of interval variable t4
}}
void t0(void) interrupt 1 //Timer 0 interrupt service routine
{
TH0=(65536-15536)/256; //Set the initial value of the high 8 bits of timer 0 (3CH)
TL0=(65536-15536)%256; //Set the initial value of the low 8 bits of timer 0 (B0H)
tt++; //Every time an interrupt occurs, tt increases by 1
if(tt==2) //If tt is equal to 2, which is equivalent to 100ms, execute the following statement
{
tt=0; //Clear tt, so the count value of tt is 0---1---2 (0), each interval is 50ms
t1++;
if(t1==10){t1=0;}
t2
++;
if(t2==20){t2=0;}
t3++;
if(t3==40){t3=0;}
t4++;
if(t4==80){t4=0;}
t5++;
if(t5==160){t5=0;}
P2_6=~P2_6; //Every 100ms, the LED connected to P2_6 flashes once
} }
Previous article:51 MCU timer 0 interrupt service routine
Next article:51 MCU timer 0 works in mode 2
Recommended ReadingLatest update time:2024-11-16 14:58
- 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
- [Xiaomeige SoC] Generate preloader under win10 and report Failed to open gdrive/…/uboot-socfpga.tar.gz
- Looking for a voltage detection circuit or chip, 3v to 12v
- You must call ros::init() before creating the first NodeHandle
- Some comments for the forum
- How can dust people reply to receive confirmation information? . .
- GD32E103 eval can be compiled with IAR
- AD19 Software
- When the switching power supply current measuring resistor is placed in these 6 positions!
- [Repost] What are the grounding techniques for electronic design?
- Is this true? Most people who buy Apple phones are "invisible poor people"