Interrupt principle of single-chip microcomputer and detailed explanation of timer

1. Interrupt system

1.1. The concept of interruption

What is an interrupt: When the CPU is processing an event A, another event B occurs that requests the CPU to process (an interrupt is generated), and then the CPU temporarily interrupts the currently executing task to process event B. After event B, it returns to the position where it was interrupted and continues to execute the original event A. This process is collectively called interruption.

1.2. Interrupt flow diagram


The event that causes the CPU to interrupt is called the interrupt source . The interrupt source sends an interrupt request to the CPU , and the CPU temporarily interrupts the original execution of event A and turns to event B. After the event B is processed, it continues to return to the original interrupted place (this process is called interrupted return, and the original interrupted place is the breakpoint ), and continues to execute the original event.


1.3. Interrupt Priority


1.4 Benefits of Interruption

(1): Improve the efficiency of the CPU

The CPU is the command center of the computer. It communicates with peripheral devices (such as buttons, displays, etc.) in two ways: query and interrupt.

1: Query: No matter whether the peripheral i/o needs to be serviced, the CPU will query it in turn at regular intervals. With this query method, the CPU needs to spend some time doing the query service work.

2: Interrupt: Actively tell the CPU when the peripheral device needs communication services, the CPU stops the current work to process the interrupt program, thereby improving the work efficiency of the CPU.

(2): Real-time processing is possible

The peripheral device may send a signal to request an interrupt at any time, and the CPU will process it in time after receiving the request to meet the needs of the real-time system

(3): The fault can be dealt with in time

Failures will inevitably occur during the operation of the computer system, eg: power interruption, memory error, peripheral equipment not working properly, etc. At this time, a request can be sent to the CPU of the interruption source through the interrupt system to solve the failure.

2. The steps of using the timer

1. Turn on the total interrupt:

EA(ENABLE ALL) = 1;

2. Set the timer working mode:

TMOD Register: Timer/Counter Mode Control Register  

Among them, M1 and M0 are the setting bits of the working mode of the timer, and 4 working modes can be set in total.

Mode 0 (M1M0 = 00): 13-bit timer/counter

Mode 1 (M1M0 = 01): 16-bit timer/counter //usually use mode 1

Mode 2 (M1M0 = 10): 8-bit auto-reload timer/counter

Mode 3 (M1M0 = 11): T0 is divided into two independent 8 for timer/counter; T1 stops counting in this mode


3. The timer fills the initial value

Filling the initial value points:

①Single-chip crystal oscillator frequency: 12M, the oscillation period is 1/12us

②The standard 51 single-chip microcomputer is 12T, that is, 12 clock cycles, 12 x 1/12 us = 1us, that is, 1us is added once.

For example: set the timer to work mode 1, and set the initial value to 10ms:

TMOD = 0x01;

TH0 = (65536 - 10000);//10000 represents 10000 1us

TL0 = (65536 - 10000);

4. Turn on the timer interrupt

ET0 = 1:ENABLE TIME0

ET1 = 1:  ENABLE TIME1

5. Turn on the timer (start counting)

TR0 = 1: Turn on Timer 0

TR1 = 1: Turn on Timer 1


6. Write interrupt service function (ISR)

Note: Interrupt service functions cannot have parameters and return values


Timer usage program


Notice:

1: Both timers and interrupts belong to the internal resources of the single-chip microcomputer. There is no chip on the development board. At the same time, once the timer initialization program overflows, it will automatically execute the timer interrupt subroutine instead of calling ourselves. These All are directly controlled by hardware.

2: The timer calculates a fixed pulse, and its timing can be calculated. It has a better effect than the delay function and can improve the efficiency of the CPU, because the delay function needs to consume the CPU to execute. During this period, the CPU cannot perform other functions, and the timer is needed when , which is automatically called by the CPU.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848333&siteId=291194637