[STC MCU Learning] Lesson 10: Timers and Counters of MCU

Publisher:ShiningSmileLatest update time:2022-08-16 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Key points of this lesson: Register


Part I. Table of Contents

1.10.1. Timer Introduction


1.10.2. Key to software control hardware - registers


1.10.3.51 Introduction to MCU Timer


1.10.4. Introduction to the main registers of the timer


1.10.5. Timer Programming Practice 1


1.10.6. Correction of timing time setting error

Part II: Chapter Introduction


1.10.1. Timer Introduction

    This section introduces the timer from a general perspective, mainly the working principle, function and significance of the timer.

1.10.2. Key to software control hardware - registers

    This section introduces the concept of registers. By studying registers, you can understand the principle of using software to control hardware.

1.10.3.51 Introduction to MCU Timer

    This section will guide you through reading the chapter on timers in the 51 MCU data sheet. By reading the data sheet, you will learn about timers and how to read the data sheet yourself.

1.10.4. Introduction to the main registers of the timer 1

    This section begins to explain the timer-related registers of the 51 MCU. 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 MCU. 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 practices timer programming. Refer to the sample code in the data sheet to rewrite your own code and download it for testing and verification.

1.10.8. Timer Programming Practice 2

    This section practices timer programming. Refer to the sample code in the data sheet to rewrite your own code and download it for testing and verification.

1.10.9. Correction of timing time setting error

    This section provides additional explanation of the 51 MCU timer timing setting issue. This detail was explained incorrectly in the previous course.


Part III: Class Notes

1.10.1. Timer Introduction

1.10.1.1 What is a timer?

(1) An internal peripheral of a SoC (System on Chip, which can be understood as a single-chip microcomputer)

(2) The timer is the CPU’s “alarm clock”

The timer is to SoC what the alarm clock is to people. A single-core CPU is single-threaded and can only do one thing. After finishing this task, it needs a timer to remind it to do another task.


1.10.1.2 What is a counter?

Counters and timers are actually the same thing

(1) The counter is also an internal peripheral in the SoC

(2) As the name implies, a counter is used to count.

For example, a stopwatch (pointer watch) is actually a counter. It moves one grid (that is, counts a number) every unit. Because the counting time period of the stopwatch is fixed, when a certain time comes, we only need to use the count value * the counting time period to get a time period. This time period is the time we set (this is the timer).


(3) The counter can count the number of external pulses (the pulse is actually the level signal flowing into the microcontroller through the pin)

View source image

How long will a pulse last? By using a counter to get the number of pulses, you can know the time!

That is to say: counting internal pulses is called a timer; counting external pulses is called a counter.

The internal pulse is stable, and the time interval between each pulse is fixed, so it can be timed;

External pulses are very unstable. We cannot know the exact time between two pulses and can only count them!


(4) Is it a timer or a counter, and is it controlled by a register?


1.10.1.3 How does the timer work?

(1) Step 1: Set the timer clock source (if necessary)

                 Clock source: Quartz crystal oscillator (crystal oscillator)

(2) Step 2: Initialize clock-related registers

(3) Step 3: Set the timing time (number of counted pulses + time of each pulse)

(4) Step 4: Set up the interrupt handler (external input has higher priority than timing)

For example: the main task is to watch TV, and the timer interrupt program is to boil water!

(5) Step 5: Turn on the timer

(6) During operation: When the timer counts up, an interrupt is generated, and then the interrupt isr is executed


You will understand it better after looking at the code!


1.10.2. Key to software control hardware - registers

How are some hardware controlled by software? It depends on registers!


1.10.2.1 What is a register?

(1)register  [reg]  

(2) Register


   Registered, variable content, generally defined bit by bit

   It is equivalent to a variable, except that this variable is at a fixed address and has a special name.

   Registers are divided into general registers (P0...) and special function registers (IE...)

(3) Registers are accessed using addresses and are programmed like memory

As defined in reg51.h!


1.10.2.2 Working principle of registers

(1) There is a bidirectional influence between registers and hardware

To put it simply, registers also have IO, and are related to hardware being input/output devices!

(2) Software can read and write registers

The program can write values ​​into registers and read the values ​​of registers. For example, when we read the value of a GPIO pin, we are actually reading the register inside the pin.

(3) Summary: Registers are the key to software controlling hardware


1.10.2.3、The key to learning MCU is various registers

Registers are to microcontrollers like words are to English learning!


(1) The study of MCU mainly includes two parts: CPU and various internal peripherals

(2) The programming interface of various internal peripherals is the register

(3) Being familiar with a microcontroller actually means being familiar with its registers

(4) Registers become more complex as the MCU becomes more complex (although there are differences between STM32 and MCU, when you learn, you will find that they are very simple!)

(5) Learn the skills of operating registers using C language - key points

You need to learn the skills of using C language to operate the microcontroller!


1.10.3.51 Introduction to MCU Timer

Refer to data sheet P136 Timer/Counter

If the microcontroller works in 12T mode!

For an 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

For an external 12MHz crystal oscillator, the internal clock frequency is 2MHz, and the clock pulse width is 0.5us (1/2MHz = 0.5us).


The modes and working methods of 51 are complicated, so we don’t need to remember them. Just go through them once. When we learn embedded STM32, it will be much easier!

In the next few sections, we will 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 with 4 names: TF, TR, IE, IT. Each name has 2 symbols, followed by 0 and 1, corresponding to T0 and T1 respectively.

(2)TF: timer flag, timer (overflow) flag


It is read-only (the software only knows the status of the hardware by reading TF1, and does not need to write this bit to set the status of the hardware).

When the timer reaches the set time, 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 handle the interrupt; when the CPU responds, TF is cleared by hardware (the change from 1 to 0 is automatic, and no software intervention is required.)

Some CPU designs need to be cleared by software (mainstream microcontrollers). At this time, the user's program must remember to clear the flag bit, otherwise it will repeatedly enter the interrupt.

=1 overflow, =0 overflow clear

(3)TR: timer run, which is the switch that starts the timer counting.


After we initialize the entire timer, we can start counting by writing 1 to the TR bit.

TR bit and GATE bit have some correlation. GATE is the register bit in TMOD register, let's take a look first!


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


Its function is to show the status change of hardware.

When INT1 interrupt occurs, the hardware automatically sets IE1=1

When the CPU processes the INT1 interrupt, the hardware will automatically set IE1=0 (automatically cleared by the hardware).

(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 triggering method means that the hardware will be judged to generate an interrupt only under certain conditions, so it is actually the condition for the interrupt to occur.

There are generally two types of interrupt triggering: edge triggering and level triggering. Edge triggering is divided into: rising edge triggering, falling edge triggering, and double edge triggering; level triggering is divided into: high level triggering and low level triggering.

=0 External interrupt 1 is low level trigger mode, when INTx input is low level, IE=1

=1 When the external interrupt (INTx) port changes from "1" to "0" on a falling edge, IE=1

1.10.4.2、TMOD(Timer Mode) Timer/Counter Operation Mode Register

(1) GATE: Chinese name is gate control bit


It is in the TMOD register, and there are also 2 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 stops counting.

When GATE=1, the timer is in counter mode. When the timer is used to count, the key is under what conditions it counts and under what conditions it does not count. So when GATE=1, whether it counts depends not only on TR1 but also on the INT1 pin (P3.3). The actual rule is: it will count when TR1=1 and the INT1 pin is also high.

(2) C/T position


Set T0/T1 to work in timer mode or counter mode.

=1 means counter mode

=0 indicates timer mode.

(3) M1 + M0

       The two bits together indicate which working mode T0/T1 is in. There are generally four types: 13-bit, 16-bit, 8-bit auto-reload, and double 8-bit.   

[1] [2]
Reference address:[STC MCU Learning] Lesson 10: Timers and Counters of MCU

Previous article:Application of STC15F2KA60S2 chip dust sensor
Next article:[STC MCU Learning] Lesson 17: LCD1602 Display

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号