Have timer interrupt MCU just fun

Time:2019-01-11 4:31:40

What is MCU interrupt processing? Overall interrupts are single-chip microcomputer has the ability to deal with internal and external random events, in particular word is single chip microcomputer in handling a task, you can pause the processing of the task, to deal with more important tasks, such as the task after the processing task before then, can be said to be characteristic of the single chip microcomputer is the most important. For example, xiaoming is doing his homework at home. At this moment, someone knocks at the door. At this moment, xiaoming stops doing his homework and gets up to open the door. In this example, "someone knocks on the door" is an external motivation event, and xiaoming's stop of homework is a response to this external motivation event. Xiaoming continues to do homework after opening the door to greet his friend, so this is a complete interruption.


The priority of the interrupt


Continue to take xiaoming as an example, the same xiaoming is doing his homework, and there is a hot kettle boiling water on the side, at this time someone is knocking at the door, at this time xiaoming gets up to open the door; When xiao Ming went to the door, the kettle was boiling and the kettle was beeping. Xiao Ming had two choices. Instead, turn off the power to the kettle before opening the door. Which of the two is more important, the more important the higher the priority. It depends on the importance of someone knocking on the door and boiling hot water in xiaoming's heart. Back to the MCU, we can set the priority of the interrupt excitation event. This series USES AT89C52 MCU, which has 6 interrupt sources. Here we make a table. You first have such a concept, this section will only use timer 0 interrupt implementation LED flashing, other such as external interrupt when used to understand.


Xiao Ming's example you should know what is the MCU interrupt and interrupt priority this property, there is a reminder, is that we need to use an interrupt source, in addition to open the interrupt source, but also must set the global interrupt allowed bit EA. In other words, when xiaoming is doing his homework, he must first allow other events to happen. Otherwise, xiaoming will keep doing his homework until he finishes it, no matter what happens in the outside world. This is quite understandable.


Interrupt allowed register IE is used to set the opening and closing of each interrupt source, the register can be bit addressing, that is, a single operation of the register, when the single-chip reset IE all clear, that is to close the interrupt.


EA is the global interrupt allowed bit, ET2 is the timer 2 interrupt allowed bit, ES is the serial port interrupt allowed bit, ET1 is the timer 1 interrupt allowed bit, EX1 is the external interrupt 1 allowed bit, ET0 is the timer 0 interrupt allowed bit, EX0 is the external interrupt 0 allowed bit, are set 1 when open, clear 0 when closed.


Microcontroller timer interrupt


Microcontroller timer is essentially a plus 1 counter, by high 8 bits and low 8 bits two registers. The TMOD register is the timer working mode register, which determines the working mode and function; The TCON register is the control register that controls the start, stop, and overflow flags of T0 and T1. When working in timer mode, plus 1 operation is to count internal machine cycles (1 machine cycle is equal to 12 clock cycles, 1 clock cycle is equal to the reciprocal of crystal oscillator). When the timer is allowed to interrupt, the register composed of high 8 bits and low 8 bits will be filled to full 1, and then another plus 1 operation will clear it. At this time, the TF0 or TF1 overflow mark position 1 in the TCON register, and the microcontroller will generate timer interrupt excitation.


TMOD is used to set the working mode of the timer. It cannot be used for bit addressing. When the MCU is reset, TMOD is all cleared to zero.


Where GATE bit is GATE control bit:


GATE =0, timer start and stop is only controlled by TRX (X=0 or 1) in TCON register;


GATE = 1, timer start and stop are jointly controlled by TRX (X=0 or 1) in the TCON register and the level state on the external interrupt pin INT0 or INT1.


C/T bit is timer/counter mode selection bit:


Is the counter mode when C/T = 1; Timer mode is used when C/T = 0.


M1 and M0 are the working mode selection bits, and each timer has 4 working modes, which are set by the combination of M1 and M0, as shown in the following table.


Timer control register TCON can address bit, used to set timer interrupt start, stop and overflow, MCU reset zero.


TF1: overflow flag bit of timer 1.


When timer 1 counts full overflow when by hardware buy 1, and apply for to enter interrupt, after entering interrupt service program by hardware automatic qing 0, that is to say when interrupt with timer this bit need not be operated artificially completely, our task writes good interrupt service program namely.