Later, learning to learn to understand the meaning of the interruption, but for the first time to contact the microcontroller, it is still difficult to explain clearly.
So here I am a metaphor. Suppose you have a ringtone and flashing light when you call in the living room, and you read in the room, then when you have a call, you hear the ringtone, then put down the book on your hand, and record your page number with a bookmark, then go out and listen phone. After listening, return to the room and continue to see your book from the location marked by the bookmark.
OK, analyze the above actions, the phone rings and is heard by you, it is interrupted; you mark the location with a bookmark, it is on-site protection; listening to the phone, is the execution of the interruption; after listening to the phone, you want to get from you The place marked just now continues to read the book, that is, after the execution of the interrupt is completed, return to the original interrupt to continue the execution of the program. This is the process of interruption.
Assuming there is no interruption, what would you do? You will use the scanning method: the phone will not ring, only the flash, but you have to read in the room, then you can only go through a few paragraphs and run out to see if there is a phone call. Come, if not, run back to read the book, if you have, then listen to the call. Obviously, the scanning method is very inefficient, because every time you read a book, you have to take the time to look at the flash of the phone to determine if there is a phone call, so the efficiency of your reading is greatly reduced. And scanning method has one of the biggest drawbacks, that is, interrupt loss, Imagine if you scan too much interval (it is a long time to read the phone), then you are likely to lose a few important calls.
In fact, the above metaphor is a good representation of the role of the interrupt - in fact, the interrupt is to deal with emergencies.
For the MCU, there are too many unexpected things, such as the user inputting data to the MCU, and pressing the button, which is something that the MCU itself cannot estimate. The sudden entry of foreign data is also an emergency. These external burst signals are generally handled by an external interrupt of the microcontroller. An external interrupt is actually an interrupt caused by a change in the state of a pin, which will be said later.
Here we introduce the timer and timer interrupts:
In measurement control systems, real-time clocks are often required to implement timing control, timing measurements, or timing interrupts. Counters are also often needed to count external events. There are two (enhanced three) 16-bit timer counters T0, T1, referred to as timer 0 and timer 1, respectively, in the MCS-51 microcontroller, both of which are programmable timer counters.
——The above P is copied from the first paragraph of “Single-chip microcomputer and interface technology†on page 94-_-#!!!!!
In fact, you ask what is the timer, I really don't know what words to use. When I was just learning, I heard the phrase "Timer is a valuable resource on the microcontroller." I didn't understand why this resource was precious? Then I slowly realized the greatness of the timer. Of course, this takes a certain amount of time to comprehend. Here you can still look down:)
At the beginning, I said, when you read this textbook, you need at least one basic book of the single-chip computer. Any book will be bought at random. Anyway, the current MCU book is copying me and copying you. All the same, the most important one is the example. Pick up your book and look at the interrupt register and timer register of the microcontroller. Let's start writing the program. The time is not waiting for people D~~~~
Single-chip mode 0 is 13-bit, never used, there are 16 people without you using 13-bit? Really waste also ~~~
Then calculate the load value, 16 bits, that is 16-bit binary, 2 to the 16th power, is 65536. The 16-bit timer is self-added from a so-called load value, and is added to 65536. If you allow the timer to be interrupted, an interrupt will come. So the calculation of the 16-bit mode load value is very simple:
65536 - the number of pulses you want to calculate = load value
But we usually want to time, not just how many pulses, so the number of pulses you want to calculate is converted into time. It is said that the time of one pulse is a machine cycle, and the machine cycle of 51 is 12/crystal. value. If your crystal is 12M, then this is 1US. 1US integer, what counts well, so you now know why so many 12M, 24M crystal oscillator bought, are prepared for 51.
#i nclude "reg51.h"
Void initTimer(void)
{
TMOD=0x1;
TH0=0xd8;
TL0=0xf0;
}
Void timer0(void) interrupt 1
{
TH0=0xd8;
TL0=0xf0;
//add your code here.
}
Void main(void)
{
initTimer();
TR0=1;
ET0=1;
EA=1;
While(1);
}
The above code is a program that is scheduled to be 10000US under the 12M crystal oscillator, that is, an interrupt occurs in the 10MS. initTimer() is the initialization function that sets the timing value and interrupt.
10000US is 100000 machine cycles under 12M crystal oscillator, 65536-100000=55536=D8F0 (hexadecimal), so the above is TH0=0xd8; TL0=0xf0;
Void timer0(void) interrupt 1 is the timer 0 interrupt function. We don't need to pay attention to the assembly interrupt first. We only need to know that when the interrupt comes, the program will automatically jump to timer0 (void). This function runs. Will return to the original interrupt to continue the original program.
Interrupt is the keyword in C51, the function followed by interrupt is interrupt function, then which interrupt is it? Please refer to: Keil Software – Cx51 compiler user manual Chinese full version introduced at the beginning, (name too Long, later called the KEIL Handbook, page 125.
Interrupt number interrupt address
0 0003H
1 000BH
2 0013H
3 001BH
4 0023H
5 002BH
6 0033H
7 003BH
8 0043H
9 004BH
10 0053H
11 005BH
12 0063H
13 006BH
14 0073H
15 007BH
16 0083H
17 008BH
18 0093H
19 009BH
20 00A3H
21 00ABH
22 00B3H
23 00BBH
24 00C3H
25 00CBH
26 00D3H
27 00DBH
28 00E3H
29 00EBH
30 00F3H
31 00FBH
See, the interrupt number is the corresponding interrupt entry address. As for what is the interrupt entry address? That is an address segment that the microcontroller automatically jumps into when an interrupt occurs.
For timer 0, it is 000BH, so it corresponds to interrupt number 1. Other interrupt addresses look at the book and you will know. As can be seen from the above table, KEILC supports 32 interrupts, but until now, I have not seen 51 microcontrollers with 32 interrupts :)
Ok, go into debugging practice, type the above code, or download my compiled project directly:
Timer0
After entering the project, you need to set something up:
Debug Information is debugging information. You can select this thing to simulate in C language (whether it is soft or hard), otherwise your simulation environment is compiled!
Soft simulation settings:
Go till main() jumps directly to the MAIN function during simulation. If not selected, it will start running at address 0. Here is a concept. In C51, the mian() function does not represent the 0 address. You must know that keilc needs to be initialized at the beginning, such as clearing the RAM content, setting the stack, etc. You don't need to do it. Your user program does not include these. After these initializations, KEIL will automatically jump to your mian for processing.
Software simulation can choose crystal oscillator, we choose 12M. The following things are selected according to the pictures in the picture. The details will be discussed later.
If the compilation is successful, then we will start the first software simulation:
The above is already in the simulation state, because we chose Debug Information so we can debug under C, and because we chose Go till main () we can see that at the beginning there is an arrow pointing to the first of main () Words. The dark stuff next to the program, that is, the thing I circled, is the effective code segment, the program will only run there, that is, the yellow arrow will only run in that area. Now that you can simulate it, you can choose a single step and run at full speed. The specific simulation of KEIL will be explained in the next chapter.
Wireless Speaker,Bluetooth Speakers,Bt Wireless Speaker,Portable Bluetooth Speaker
Guangzhou YISON Electron Technology Co., Limited , https://www.yisonearphone.com