There are two timers T0 and T1 in the standard 51 MCU. T is the abbreviation of Timer. Now many 51 series MCUs will add extra timers. Here we will talk about timers 0 and 1. As mentioned earlier, for each function module of the microcontroller, it is controlled by its SFR, which is a special function register.
The special function registers related to the timer are as follows. You don't need to memorize the names and functions of these registers. You only need to know about it. When you use it, you can check the manual at any time, find the name of each register and each. The role of the register.
The registers in Table 5-1 store the count value of the timer. TH0/TL0 is used for T0 and TH1/TL1 is used for T1.
Table 5-1 Timing value storage registers
TH0 | Timer 0 high byte | 0x8C | 0x00 |
TL0 | Timer 0 low byte | 0x8A | 0x00 |
TH1 | Timer 1 high byte | 0x8D | 0x00 |
TL1 | Timer 1 low byte | 0x8B | 0x00 |
Table 5-2 shows the bit allocation of the timer control register TCON, and Table 5-3 shows the specific meaning of each bit.
Table 5-2 TCON – Bit Assignment of Timer Control Register (Address 0x88, Bit Addressable)
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | IE0 | IT0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7 | TF1 | Timer 1 overflow flag. The hardware is set to 1 whenever Timer 1 overflows. There are two ways to clear: software clear, or hardware clear when entering the timer interrupt. |
6 | TR1 | Timer 1 runs the control bit. The software is set/cleared to start/stop the timer. |
5 | TF0 | Timer 0 overflow flag. The hardware is set to 1 whenever Timer 0 overflows. There are two ways to clear: software clear, or hardware clear when entering the timer interrupt. |
4 | TR0 | Timer 0 runs the control bit. The software is set/cleared to start/stop the timer. |
3 | IE1 | The external interrupt part is independent of the timer and will not be seen for the time being. |
2 | IT1 | |
1 | IE0 | |
0 | IT0 |
Note that in the description in Table 5-3, as long as the hardware is set or cleared, it means that once the condition is met, the MCU will automatically complete the action. As long as the write software is set or cleared, it means that we must Use the program to complete this action, and then follow up with such descriptions and no longer explain. For the TFR, there are four bits of TF1, TR1, TF0, and TR0 that need to be understood. They correspond to T1 and T0 respectively. Let us use Timer 1 as an example. Then timer 0 is the same. First look at TR1. When we write TR1 = 1 in our program, the timer value will be automatically incremented by one every machine cycle. When we write TR1 = 0 in our program, the timer will stop incrementing by one, and its value will remain. No change. TF1, this is a flag, and his role is to tell us that the timer has overflowed.
For example, if our timer is set to 16-bit mode, then TL1 will be incremented once every one machine cycle. When TL1 is added to 255, add 1 and TL1 will become 0. TH1 will be incremented by one. TH1 and TL1 are both 255 (that is, the 16-bit integer consisting of TH1 and TL1 is 65535). After adding 1 again, it will overflow. Both TH1 and TL1 will become 0 at the same time. As soon as it overflows, TF1 will change automatically. Into 1, telling us that the timer overflows, just provide us with a signal, let us know that the timer overflows, it will not have any effect on whether the timer continues to run. At the beginning of this section, we mentioned that the timer has multiple working modes. The selection of the working mode is controlled by TMOD. The bit allocation and description of TMOD are shown in Tables 5-4 to 5-6. The bit functions of TMOD are shown in Table 5. -5 is shown.
Table 5-4 TMOD - Bit Assignment of Timer Mode Register (Address 0x89, Non-Bit Addressable)
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
GATE(T1) | C/T(T1) | M1 (T1) | M0(T1) | GATE(T0) | C/T(T0) | M1 (T0) | M0(T0) |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
T1/T0 | In Table 5-5, the indicator T1 indicates the bit that controls Timer 1, and the T0 indicates the bit that controls Timer 0. |
GATE | When this bit is set to 1, it is the gate bit. The timer is enabled only when „INTx‟ is high and the „TRx‟ control bit is set to 1. The timer starts counting. When this bit is cleared to 0, the timer is set as long as the „TRx clamp is set. x enables the start timing, and is not used to measure the external signal pulse width without interference from the external signal of the microcontroller pin INTx. This is an additional function of the timer, which is not covered in this lesson. |
C/T | Timer or counter select bit. When this bit is cleared, it is used as a timer function (internal system clock) and is set to 1 as a counter function. |
0 | 0 | 0 | It is compatible with the 13-bit timer of the 8048 MCU, and the 8 bits of THn and the 5 bits of TLn form a 13-bit timer. |
0 | 1 | 1 | THn and TLn form a 16-bit timer. |
1 | 0 | 2 | In 8-bit auto-reload mode, THn is reloaded into TLn after the timer overflows. |
1 | 1 | 3 | Timer 1 is disabled and Timer 0 becomes 2 8-bit timers. |
As you may have noticed, the TCON of Table 5-2 is marked with "bitable addressing", while the TMOD of Table 5-4 is labeled "non-bit addressable." This means that if TCON has a bit called TR1, we can directly perform TR1 = 1 in the program. However, operations such as (T1)M1 = 1 in the TMOD are wrong. We have to operate the entire byte at a time, that is, we must operate all the bits of TMOD at one time, and we cannot operate one of them individually. Then we can only modify one of the bits without affecting the value of other bits. What? Of course, you will learn the methods in the follow-up course, and don't care about it now. Table 5-6 lists the operating modes of the Timer 4, where Mode 0 is designed to be compatible with older 8048 Series microcontrollers. Currently, 51 is almost never used in this mode, and Mode 3 is based on my application. Experience, its function can be completely replaced by mode 2, so basically it is not used, then we will focus on learning mode 1 and mode 2. Mode 1, THn and TLn form a 16-bit timer with a count range of 0 to 65535. After overflow, as long as THn and TLn are not reassigned, counting starts from 0. Mode 2 is an 8-bit auto-reload mode. Only TLn is incremented by 1 and the count range is 0 to 255. The value of THn does not change, but the original value is maintained. After TLn overflows, TFn is directly set, and The original value of THn is assigned directly to TLn, and then TLn counts from the newly assigned value. This function can be used to generate the communication baud rate of the serial port. We will use it when we talk about the serial port. In this chapter, we focus on learning mode 1. In order to deepen the understanding of the principle of the timer, let us look at the circuit diagram 5-2 of his mode 1.
Figure 5-2 Timer/Counter Mode 1
I will lead everyone to analyze this diagram together. If you encounter similar diagrams in the future, you can study it yourself. The OSC box indicates the clock frequency because 1 machine cycle is equal to 12 clock cycles, so that d is equal to 12. The door to the right of GATE below is a non-gate circuit, then the right side is an OR gate, and then to the right is an AND gate circuit, you can compare the contents of Section 5-1. As can be seen from the figure, the lower part of the circuit controls the upper part. Let us first look at how the lower side is controlled. Let us take the timer 0 as an example. 1) TR0 and the result of the lower OR gate circuit should be ANDed. If TR0 is 0, the AND operation is definitely 0, so if the timer is to be operated, TR0 must be set to 1. 2) The AND gate result here To get 1, then the result of the previous OR gate must also be 1. In the case where the GATE bit is 1, after a NOT gate becomes 0, or the gate result is to be 1, then INT0, that is, the P3.2 pin must be 1, the timer will work at this time. In the case where the INT0 pin is 0, the timer does not work, which is what the GATE bit does. 3) When the GATE bit is 0, it will become 1 after a NOT gate. No matter what level the INT0 pin is, it will be 1 after the OR gate, and the timer will work. 4) If you want the timer to work, it will automatically increase by 1. There are two ways to see from the figure. The first way is that the switch hits the upper arrow, that is, when C/T = 0, a machine cycle TL It will be added once. When the switch hits the lower arrow, that is, C/T =1, the T0 pin is a pulse from the P3.4 pin, and TL is incremented once, which is the counter function.
Outdoor Bluetooth Mini Speaker
Mini Bluetooth Speaker has a stronge power trumpet, the bass still very shocking, and it can also realize TWS wireless to connection that left and right channels can be play songs at the same time, you will enjoy the rich bass in the outside.
We also use waterproof material to make it, the waterproof Bluetooth speaker grade reaches IPX4, which can prevent rain or domestic water from splashing on the speaker. When using it, remember to close the charging rubber plug,Dont worry about raining or splashing water,let you enjoy the music.
The Portable Bluetooth Speaker function is very smart. It can support AUX, USB, TF card memory reading, Bluetooth to connection, etc. The LED light flashes according to the rhythm of the music, which is very sensational and cool!It supports hand free call then frees your hands.
Bluetooth speaker also has a long battery life. The battery allows the playback time to reach 3H~6H, allowing you to enjoy music anytime and anywhere when you outside without worrying about the battery running out quickly.
Waterproof IPX5 Bluetooth Louder Speaker,Portable Bluetooth Louder Speaker,Heavy Bass Bluetooth Louder Speaker
Shenzhen Focras Technology Co.,Ltd , https://www.focrass.com