MCU Learning Journey for Newbies (XIV) --- External Interrupt 1 to Implement Buzzer Alarm

Publisher:淡雅时光Latest update time:2020-03-28 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Objective: Understanding of timers and counters (I)

1. Timer/Counter Introduction

Timer/Counter (T/C for short) is one of the most basic interfaces in a single-chip microcomputer. It can be used for both timing and counting. It is often used for counting, delaying, measuring cycle/pulse width/frequency, providing timing pulse signals, etc.

The 8051 series microcontrollers contain at least two 16-bit timers, and the STC89C51/52 contains three timers. Two of them are basic timers/counters T/C0 and T/C1, and the other is T/C2. They can be configured as timer functions or counter functions through software.

How does the timer/counter work?

The timer/counter is essentially an add-1 counter. When working in the timer mode, it counts the pulses of the oscillator source (crystal oscillator) divided by 12 (12 division is the oscillation period, the unit is us), that is, each machine cycle (12 oscillation periods is 1/12us12=1us) count value is added by 1 (explained the delay function above), when the counter is added to all 1 (the timer counter is 16 bits, then 2*16=65539, minus 1 [because it starts at 0], 65535 is the maximum value of the timer count), then input a pulse to make the counter return to zero.

When working in counter mode, the counting pulse comes from the external pulse input pins P3.4 (T0) and P3.5 (T1). When the T0 or T1 pin has a negative transition (i.e., falling edge, positive transition == rising edge), the count value increases by 1. It takes 2 machine cycles (2us), i.e., 24 oscillation cycles, to identify the negative transition of the pin. Therefore, the maximum frequency of the countable external pulse input to the T0 or T1 pin is 1/24fosc (1/24*12=500KHz). When the crystal oscillator is 12MHZ, the maximum counting frequency is 500KHz. If the frequency is higher than this, the counting will be wrong.


2. Timer/Counter Registers

Timer/Counter Registers

TH1 and TL1 are two registers of timer/counter 1, both are 8 bits, totaling 16 bits. TH1 is the high 8 bits, TL1 is the low 8 bits
TH0 and TL0 are two registers of timer/counter 0, both are 8 bits, totaling 16 bits. TH0 is the high 8 bits, TL0 is the low 8 bits

The T0 pin is P3.4, the T1 pin is P3.5, configured in counter mode to detect external pulse signals

Timer control register TCON,
working mode selection register TMOD (used to determine the working mode and function of the timer/counter)

TCOD see MCU Novice Learning Road (Twelve) ----> https://www.jianshu.com/p/4de17b66a15b

Working mode selection register TMOD

Working mode selection register TMOD


0~3 are the 4 bits of timer/counter 0. M1 and M0 are used to select the working mode. The working modes are 0, 1, 2, and 3. C/T is the selection bit of the counter or timer. If C/T is set to 0, it is timing mode, and if it is set to 1, it is counting mode.

GATE is the gate signal. When GATE=1, the T/C (timer/counter) starts and receives dual control requirements TR0/TR1 and INT0/INT1 are high at the same time. When GATE=0, the T/C starts and receives only the control of TR0/TR1. Usually, GATE is set to 0.


Working mode selection

  • Mode 0: The maximum count value is 2**13-1=8191

  • Method 1: The maximum count value is 2**16-1=65535

  • Method 2: TH stores the initial value, and TL is used for counting. Since it is 8 bits, the maximum value is 255. TL keeps counting, and when it reaches 255, it overflows and clears to 0. Then TH gives the initial value to TL and starts counting again. This is the meaning of automatically reloading the initial value.

  • Mode 3: T0 and T1 are slightly different. Only T0 can be set to mode 3. If it is set to mode 3, TL0 and TH0 are divided into two independent 8-bit counters. TL0 is an 8-bit counter with the same function as mode 0/1. It can be timed and counted.
    TH0 only performs a simple internal timing function. It occupies the control bit TR1 and interrupt flag bit TF1 of timer T1. The start and stop are only controlled by TR1 (this mode is not commonly used)

Keywords:MCU Reference address:MCU Learning Journey for Newbies (XIV) --- External Interrupt 1 to Implement Buzzer Alarm

Previous article:The Path to Learning MCU for Beginners (XV) --- Understanding Timers and Counters (I)
Next article:MCU Novice Learning Road (Thirteen) --- External interrupt 0 to achieve LED flashing

Recommended ReadingLatest update time:2024-11-17 00:52

Analysis of AVR Embedded Microcontroller Based on Programmable Digital Potentiometer
1 Introduction AVR embedded microcontrollers have rich hardware and software resources. The serial I2C interface can meet the requirements of many applications. Two AVR microcontrollers can communicate with each other by directly connecting them through the I2C bus. AVR microcontrollers can also be directly connected
[Microcontroller]
Analysis of AVR Embedded Microcontroller Based on Programmable Digital Potentiometer
Design of power-off protection circuit for single-chip microcomputer
In single-chip computer systems such as digital clocks, some timers and calendar clocks, when the main power supply DC5V is lost, it is called power failure. After power failure, the single-chip computer stops working and the clock also stops. This result is undesirable in many occasions. In order to ensure that the si
[Microcontroller]
Design of power-off protection circuit for single-chip microcomputer
Interface circuit and driver based on ADC081S051 and 51 single chip microcomputer
introduction The signals existing in nature are usually analog signals. In the measurement and control system, the collected analog signals are usually digitized and then handed over to the microprocessor or microcontroller for processing. Therefore, the analog-to-digital converter becomes an indispensable part of the
[Power Management]
Interface circuit and driver based on ADC081S051 and 51 single chip microcomputer
MCU drawing curve
The realization of drawing real-time curves by single-chip microcomputer      If we want to draw real-time curves, that is, the values ​​of different curves will change with time. For example, we draw a curve on the 320*240 screen with the 0-5V voltage value collected by ADC.      We use the dot counting method to a
[Microcontroller]
Looking at the operation strategies of domestic 32-bit MCU players through Hangshun chips
As the core part of embedded applications, MCU is like the central nervous system of terminal products, controlling the behavior of products. With the evolution of the market and other aspects, MCU has also developed from 8-bit and 16-bit to 32-bit and 64-bit. In recent years, driven by emerging applications such as I
[Mobile phone portable]
Summary of precautions for using avr microcontrollers
1: atmega8 a cannot be simulated , but the pins of atmega88 are fully compatible with mega8a, and the resources are similar. You can use 88 for simulation first and then transplant. 2: The 16-bit register related to timer 1 has a temporary register for the high bit. This temporary register cannot be addressed and t
[Microcontroller]
An example of extending the data memory RAM of a single chip microcomputer
RAM is used to store various data. The MCS-51 series 8-bit microcontroller has 128 B RAM memory inside, and the CPU has a wealth of operating instructions for the internal RAM. However, when the microcontroller is used for real-time data acquisition or processing large quantities of data, the RAM provided in the chip
[Microcontroller]
An example of extending the data memory RAM of a single chip microcomputer
51 MCU Naming Rules
89C51 8 represents 8-bit microcontroller 9 represents falsh memory, 0 represents no ROM, and 7 represents eprom memory c represents CMOS technology, and S represents ISP programming mode 1 represents the on-chip program memory capacity, the capacity corresponds to the digit * 4KB 89C52: 8KB capacity   at89
[Microcontroller]
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号