[STC MCU learning] Lesson 10: Timers and counters of MCU

[Mr. Zhu's course summary intrusion]

The focus of this lesson: registers

The first part, chapter list

1.10.1. Introduction to Timer

1.10.2. The key to software control hardware-registers

1.10.3.51 Introduction to the timer of the microcontroller

1.10.4. Introduction to the main registers of the timer

1.10.5. Timer programming practice 1

1.10.6. Timing time setting error correction



The second part, chapter introduction


1.10.1. Timer Introduction
    This section introduces the timer from a broad perspective, mainly the working principle, function and significance of the timer.
1.10.2. The key to hardware control by software-registers
    This section introduces the concept of registers, and learns the principles of using software to control hardware by learning registers.
1.10.3.51 Introduction to the Timer of the
    51 Single-chip Microcomputer This section takes you to read the chapters of the timer introduction in the data manual of the 51 single-chip microcomputer. By reading the data manual, you can learn to read the data manual while learning the timer.
1.10.4. Introduction to the main registers of the timer 1
    This section begins to explain the timer-related registers of the 51 single-chip microcomputer. This section mainly focuses on the TF and TR bits in TCON and the GATE bit in TMOD.
1.10.5. Introduction to the main registers of the timer 2
    This section begins to explain the timer-related registers of the 51 single-chip microcomputer. This section mainly focuses on the TF and TR bits in TCON and the GATE bit in TMOD.
1.10.6. Introduction to the main registers of the timer 3
    This section explains the remaining register bits.
1.10.7. Timer programming practice 1
    This section is for timer programming practice. Refer to the sample code in the data manual to rewrite and get your own code and download it for test verification.
1.10.8. Timer programming practice 2
    This section is for timer programming practice. Refer to the sample code in the data manual to rewrite and get your own code and download it for test verification.
1.10.9. Timing time setting error correction
    This section supplementally explained the 51 single-chip timer timing time setting problem, which was wrong in the previous course.

    
Part Three, Classroom Record

1.10.1. Introduction to Timer

1.10.1.1 What is a timer?
(1) An internal peripheral of SoC (system on a chip, it can be understood that the achievement is a microcontroller)
(2) The timer is the "alarm clock" of the CPU. The
timer is like an alarm clock relative to the SoC The meaning is the same relative to people. A single-core CPU is single-threaded and can only do one thing, and it needs a timer to remind you when you finish this thing and do another thing.

1.10.1.2 What is a counter?
Counter and timer are actually the same thing
(1) Counter is also an internal peripheral in SOC
(2) Counter, as its name implies, is used to count.
For example, stopwatch (pointer watch) is actually a counter. Take a grid every other unit (that is, count a number), because the count time period of the stopwatch is fixed, so when a certain time comes, just use the count value * the count time period to get a time period. This time period is our set Time (this is the timer).

(3) The counter can count the number of external pulses (the pulse is actually the level signal that flows into the microcontroller through the pins)
View source image
how often there will be a pulse, and the number of pulses through the counter will know the time!
In other words: counting internal pulses is called a timer; counting external pulses is called a counter.
The internal pulse is stable, and the interval between each pulse is fixed, so it can be timed; the
external pulse is very unstable, it is impossible to know the time between two pulses, only counting!

(4) Is it a timer or a counter? It is controlled by a register.

1.10.1.3 How does the timer work?
(1) Step 1 : Set the timer's clock source first (if necessary)
                 Clock source: quartz crystal oscillator (crystal oscillator)
(2) Step 2 : Initialize the clock related registers
(3) Step 3 : Set the timing time (counting the number of pulses + each pulse time)
(4) Step 4 : Setting Interrupt processing program ( external input priority is higher than timing )
For example: the main task is watching TV, the timer interrupt program is boiling water!
(5) Step 5 : Turn on the timer
(6) When running : The timer will generate an interrupt after the timer counts up, and then execute the interrupt isr

. It will be clearer if you look at the code later!


1.10.2. The key to software control hardware- registers

How can some hardware be controlled by software? It depends on the register!

1.10.2.1 What is a register
(1) register [reg]  
(2) Register

  •    Register, content is variable, generally defined by bit
  •    It is equivalent to a variable, but this variable has a special name at a fixed address.
  •    The registers are divided into general registers (P0...) and special function registers (IE...)

(3) Registers are accessed by addresses, which are programmed like memory
defined in reg51.h!

1.10.2.2. The working principle of the register
(1) There is a two-way influence between the register and the hardware. To put it bluntly, the register
also has IO, which is related to the hardware being an input/output device!
(2) Software can read and write registers.
Programs can write values ​​into registers or read register values, such as the value of our GPIO pins. In fact, we read the registers inside the pins.
(3) Summary: Registers are the key for software to control hardware

1.10.2.3. The key for MCU learning is various registers.
Registers are to MCUs as words are to English learning!

(1) The learning of single-chip microcomputer mainly includes two: CPU and various internal peripherals
(2) The programming interface of various internal peripherals is registers
(3) Familiar with a single-chip microcomputer is actually familiar with its registers
(4) Registers will follow With the complexity of the single-chip microcomputer, it becomes more complicated (Although there are differences between STM32 and single-chip microcomputer, when you go to study, you will find it very simple!)
(5) Learn the skills of operating registers in C language-the key point is
to learn C language manipulation Microcontroller skills!


1.10.3.51 Introduction to the timer of the microcontroller

Refer to the data sheet P136 Timer/Counter

If the microcontroller works in 12T mode!
External 12MHz crystal oscillator, the internal clock frequency is 1MHz, and the clock pulse width is 1us (1/1MHz = 1us).
 
If the microcontroller works in 6T mode with
an external 12MHz crystal oscillator, the internal clock frequency is 2MHz, and the clock pulse width is 0.5us (1/2MHz = 0.5us).
 

51's mode and working method are cumbersome, we don't need to remember, just go through it once, and it will be much easier when we learn the embedded STM32!
In the next few sections, we focus on the main registers of the timer through the manual


1.10.4. Introduction to the main registers of the timer

MSB big endian, LSB little endian P136

1.10.4.1, TCON (Timer Control) timer/counter control register

0b11000011 is from B7-B0
(1) 8 bits, but there are 4 names: TF, TR, IE, IT, and the symbol of each name has 2 One, followed by 0 and 1, corresponding to T0 and T1.
(2) TF: timer flag, timer (overflow) flag bit

  • It is read-only (software only knows the status of the hardware by reading TF1, instead of writing this bit to set the status of the hardware).
  • When the timer expires, it will do two things: the first is to change the TF flag to 1, and the second is to generate an interrupt for the CPU to interrupt processing; when the CPU responds, TF is cleared by hardware (from 1 to 0 It is automatic and does not require software intervention.)
  • There are some CPU designs that require software to clear ( mainstream microcontrollers ). At this time, the user's program must remember to clear the flag bit, otherwise it will repeatedly enter the interrupt repeatedly.
  • =1 overflow, =0 overflow clear

(3) TR: timer run, is the switch to start counting of the timer.

  • After we initialize the entire timer, we can start counting by writing 1 to the TR bit. but!
  • The TR bit and the GATE bit have a certain correlation. GATE is the register bit in the TMOD register, take a look first!

(4) IE: interrupt enable, external interrupt (INTx) request source flag

  • The role is to show the state of the hardware changes.
  • When INT1 is interrupted, the hardware will automatically IE1=1
  • When the CPU processes the INT1 interrupt, the hardware will automatically give IE1=0 (hardware automatic clearing).

(5) IT: Interrput trigger, external interrupt trigger mode control bit

  •   It is used to set the interrupt trigger mode of external interrupt.
  •    The so-called interrupt trigger mode means that the hardware will be judged to generate an interrupt under certain conditions, so it is actually the condition for interrupt generation.
  • Generally, the interrupt trigger mode is: edge trigger and level trigger. Edge trigger is divided into: rising edge trigger, falling edge trigger, double edge trigger; level trigger mode is divided into: high level trigger and low level trigger .
  • =0   External interrupt 1 is low level trigger mode, when INTx input low level, IE=1
  • =1 When the   external interrupt (INTx) port changes from "1"→"0", IE=1

1.10.4.2, TMOD (Timer Mode) timer/counter working mode register

(1) GATE: Chinese name is gate control bit

  • It is in the TMOD register, and there are two corresponding to T0 and T1 respectively.

    Way of working:

  • When GATE=0, the     timer is in timer working mode. At this time, the timer switch is only affected by the TR bit. Specifically, TR=1 starts counting, and TR=0 ends counting.
  • When GATE=1, the timer is in counter working mode . When the timer is used for counting, it is very important to count under what conditions and not count under what conditions. Therefore, when GATE=1, whether to count depends not only on TR1 but also on the INT1 pin (P3.3). The actual rule is: when TR1=1 and the INT1 pin is also high, it will count.

(2) C/T position

  • Set T0/T1 to work in timer mode or counter mode.
  • =1 means counter mode
  • =0 means timer mode.

(3) M1 + M0
       2 bits together indicate which working mode T0/T1 is in, generally there are 4 types: 13-bit, 16-bit, 8-bit auto-reload, double 8-bit.   


1.10.4.3, TLx, THx
TL0+TH0: the 16-bit register of timer 0
TL1+TH1: the 16-bit register of timer 1,
if we say that our pulse width is 0.5us, we need to set the timing for 0.5ms and 4.444ms, Then the counts are:
1000 = 0x3E8 = high 0x3 low 0xE8 => TL0 = 0xE8 TH0 = 0x3
8888 = 0x22B8 = high 0x22 low 0xB8 => TL0 = 0xB8 TH0 = 0x22 to

correct the previous error, overflow refers to the timer exceeding The maximum number of digits!

1.10.5. Timer programming practice 1

1.10.5.1. Experimental task
Originally, the delay in the flicker is realized by the delay function. During the delay, the CPU must be consumed here and cannot do other things. This is the previous shortcoming

Interrupt + timing:
CPU can do the main task ( static digital tube display 0-F ), the timer is 500ms, and the interrupt is generated when the timing is reached, and the LED flashes in the interrupt handler isr! !

1.10.5.2 How to program
1. Ideas:

(1) Timing (timer initialization)

  • Assign a value to TMOD to determine the working mode of T0.
  • Calculate the initial value of the timer count and write it to TH0, TL0
  • Open timer interrupt, assign value to ET0 and EA
  • Set TR0 to start the timer timing.

(2) What should the main program do?
(3) The interrupt handler

can be directly modified in the key interrupt program of the last lesson! 【SCM learning】Lesson 9: Buttons

2. Question thinking: how to calculate 0.5s?
     The machine cycle is the time required for the CPU to complete a basic operation. Machine cycle = 1/single chip clock frequency.

  • The external crystal oscillator of our single-chip microcomputer is 12MHz and works in 12T mode , so the internal clock frequency is 1MHz and the machine cycle is 1us . It can set up to 65535 (under 16-bit timer working mode) pulses , which means that the maximum timing time is 65535*1us=65535us=65.535ms .
  • If you want to set a relatively long time (for example, 500ms), the timer cannot be satisfied directly. The solution is to add up to a long time after multiple timings .

    Timing method: first use the timer to set 50ms, then *10
    
. What is the initial value of our timing 50ms?

  • 12T mode, external crystal oscillator 12MHz , internal clock frequency 1MHz, internal machine cycle: 1us
  • The timing number is: 50ms/1us=50000
  • Initial value: start counting from 65535-50000+1 (because the counter actually counts to 65536 before it overflows) = 15536 ​​=0x3CB0 , the initial value is equal to 0x3CB0
  • TL0 = 0xB0, TH0 = 0x3C (It can also be understood like this: TH0 = 15536/256 , TL0 = 15536%256 )

Let's look at these two pictures together to analyze how we need to program!

  • MCU works at 12T, no choice
  • Let's choose timer 0

Working mode configuration

  • Because it is counted by the internal clock, it is equivalent to the timer mode, GATE = 0
  • Because it is a timer mode, C/T = 0
  • Because 16-bit timer mode is selected, M1 = 0, M0 = 1
  • Combining the above three points: TMOD = 0x01

Set the initial value of the timer (note the manual, 16-bit timer mode, TH0 and TL0 need to be reloaded)

  • TH0 = 0x3C
  • TL0 = 0xB0

Interrupt enable

  • ET0 = 1
  • EA = 1

Timer/counter control configuration

  • Allow counting, TR0 = 1
  • No external interruption involved, IT and IE don’t care
  • IF will automatically change when the count overflows, don’t care

Timer interrupt handler reference manual P141

The final procedure is as follows

#include <reg51.h>
 
/**************代码说明**************************
			本程序采用定时器中断方式,实现了静态数码管显示0-F并且每隔0.5s时,LED1闪一次
				P0--静态数码管
				P1.0 LED1
*************************************************/
 
sbit led = P1^0;//LED接在P1.0

unsigned char  CNT = 10;   //每次计数50ms,重复10次
unsigned char  count;
 
// 独立数码管的段码表
unsigned char val[16] = 
{
	0xc0, 0xf9, 0xa4, 0xb0, 
	0x99, 0x92, 0x82, 0xf8, 
	0x80, 0x90, 0x88, 0x83,
	0xc6, 0xa1, 0x86, 0x8e
};

/*************延时函数*************/
void delay()
{
	unsigned char i = 200,j = 100;
	while(i--)
		while(j--);
}
 
/******************************定时器中断服务程序*************************/
void tm0_isr() interrupt 1 using 1
{
		TH0 = 0x3C;
		TL0 = 0xB0;   //手工重载
	
		if (count--== 0)
		{
			// 说明已经中断了10次了,0.5s已经到了
			led = !led;				// LED取反
			count = CNT;
		}
}
 
/***************************主函数*********************************/
void main(void)
{
		unsigned char i = 0;
		/************定时器初始化*************/
		TMOD = 0x01;
		TH0 = 0x3C;
		TL0 = 0xB0;
		ET0 = 1;
		EA = 1;
		TR0 = 1;
	
		led = 1;
		count = CNT; 
	
		while (1)
		{
			//主线任务
			for (i=0; i<16; i++)
			{
				P0 = val[i];
				delay();
			}
		}
}

1.10.6.3, 51 timer widget use

Download link: 51 timer calculation

Program download link for this section: Timer
This lesson is over!

Guess you like

Origin blog.csdn.net/qq_27148893/article/details/109690947