1. Initialize the timer
2. Write an interrupt subroutine (also called an interrupt function, in layman's terms, it is to call a function)
Initialize the timer
1) Set the timer/counter mode (in the front)
2) Enable the timer interrupt (timer/counter 0 or 1 or other)
3) Enable the timer/counter (timer/counter 0 or 1 or other)
4) Enable the general interrupt
5) Assign a value to the timer
Note: The above steps can be disrupted but not omitted. (If some conditions are initialized in other functions, they can be omitted here)
Writing an interrupt subroutine
void tim0_isr (void) interrupt 1
1) void tim0_isr(void) (This is the function that needs to be executed after the interrupt)
2) interrupt 1 (There is a detailed description of the interrupt number before the interrupt number)
Code
#include
sbit LED=P0^7; //define the LED pin to be lit
unsigned int i=0;
void tim0_isr (void) interrupt 1 //This is the interrupt execution function
{
i++; //Every interruption i+1
}
void csh_dsq_0() // Initialize timer/counter 0
{
TMOD = 0x02; // Use mode 3 (auto-reload mode)
TH0 = 0x00; // Timer 0 reload data (here is the value assigned when given)
TL0 = 0x00; // Initialization (here is the value assigned when given)
ET0 = 1; // Turn on timer 0 interrupt
TR0 = 1; // Enable timer 0
EA = 1; // Turn on general interrupt
} void main()
{
LED = 0;
csh_dsq_0(); // Call interrupt initialization
while (1)
{
if (i == 5000) // Interrupt 5000 times and LED turns on or off once
{
LED = ~LED; // LED turns on or off
i = 0; // Clear i to restart the interrupt count
}
}
}
Previous article:A brief introduction to the principles of computer communication
Next article:1602 screen with infrared remote control (to achieve cursor movement) and new understanding of 1602
Recommended ReadingLatest update time:2024-11-16 13:47
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
- Introduction to MCU GPIO Programming
- EEWORLD University Hall----Programmable ASIC Design (Sichuan University)
- The GS waveform oscillation of MOSFET can be eliminated in this way!
- When using a crystal oscillator, you should pay attention to the relationship between temperature and
- Accelerometer and gyroscope
- Download of Siemens e-book "PCB Manufacturing Process - Optimization through Digital Transformation" has started~
- DP83822I Industrial Ethernet PHY auto-negotiation function and its strap resistor configuration
- 【DIY】Remodeling the mobile phone battery charger
- Modern processors and optimized code performance
- TMS320F28035 ECAN interrupt sending and receiving