[Self-study 51 single chip microcomputer] 5 --- timer, digital tube, logical operation,

Publisher:JoyfulHarmonyLatest update time:2021-10-21 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction to timers, static digital tube displays, logical operators and logic circuit symbols


1. Logical operations and logic circuits

Introducing the concept of logic: In life, logic has two logical values, 'true' and 'false', and logic corresponds to C language and digital circuits. 'True' corresponds to all 'non-zero values' in C language or digital circuits, and 'false' corresponds to 'zero value'.


1.1 C language logical operators

Assume two byte variables A and B, and the result of a certain logical operation between them is F.


The following are logical operators (operating on the overall value of the variable)


&& Logical AND: F = A && B. When the values ​​of A and B are both true (non-zero values, the same below), the operation result is F.

is true (the specific value is 1, the same below); when any one of the values ​​of A and B is false (that is, 0, the same below), the result F is false (the specific value is 0, the same below).

|| Logical OR: F = A || B, if either A or B is true, F is true; if both A and B are false, F is false.

! Logical NOT: F = !A, if the value of A is false, F is true; if the value of A is true, F is false.


The following are bitwise operators (operating on each bit of the variable)


& Bitwise AND: F = A & B, perform AND operation on each bit in the two bytes A and B, and combine each bit result into the total result F. For example, A = 0b00111100, B = 0b11011101, then the result F is equal to 0b00011100.

| Bitwise OR: F = A | B, for example, A = 0b00111100, B = 0b11011101, F is equal to 0b11111101.

~ Bitwise inversion: F = ~A, which means negating each bit in byte A (i.e. inverting it). For example, if A = 0b00111100, F equals 0b11000011.

^ Bitwise XOR: XOR means that if the values ​​of the two sides of the operation are different (that is, different), the result is true.

If they are the same, the result is false. F = A ^ B, A = 0b00111100, B = 0b11011101, the result F is equal to 0b11100001.


1.2 Logic Circuit Symbols

The following figure shows the commonly used circuit symbols for digital circuits. Focus on recognizing popular symbols abroad and remember them by looking them up.

2. Timer learning (key points, not difficult points)

2.1 Introduction to Clock Cycle and Machine Cycle

Clock cycle: It is the smallest time unit in the timing. The calculation method is: Clock cycle = 1 / clock source frequency. (Note: 1. The unit is seconds 2. The time source frequency is the crystal oscillator of the microcontroller. The crystal oscillator of the blogger's STC89C52RC crystal oscillator circuit is: 11.0592M)


Machine cycle: The shortest time for a microcontroller to complete an operation. A machine cycle of a 51 microcontroller under the standard architecture is equal to 12 clock cycles (12 / clock cycle S). For some enhanced 51 microcontrollers, a machine cycle is equal to 4 clock cycles or 1 clock cycle. The following explains the 51 microcontroller under the standard framework.


2.2 Introduction to timers

The timer and counter in the microcontroller are the same internal module. Readers can understand the counter function by themselves. Let us introduce the timer function.


Timer: used for timing. The standard 51 single-chip microcomputer has two timers, T0 and T1. There is a register inside the timer. The value of the register automatically increases by 1 every time a machine cycle passes. The machine cycle can be understood as the counting cycle of the timer. Just like a clock, the number automatically increases by 1 every second, and the register automatically increases by 1 every 12/11059200 seconds. When the clock adds 60 seconds, the second automatically jumps to 0. This situation is called overflow in single-chip microcomputers and computers. Different working modes of the timer have different overflow situations. If a 16-bit timer is added to 65535, adding 1 will overflow, and the value will directly become 0 after the overflow.


2.2.1 Timer register introduction

The timer module has 6 registers, which are introduced one by one below. Readers can check the manual for the use of these registers.


Timing value storage register

There are four registers in Table 5-1 for storing the count value of the timer, where TH0 and TL0 are used for timer 0, and TH1 and TL1 are used for timer 1.


TCON – Timer Control Register

(Note: Bit addressability means that each bit of the register can be operated separately. For example, one bit TR1 of the register can be directly operated as TR1 = 1 by the program.)

(Note: When the hardware is set to 1 or cleared to 0, it means that once the conditions are met, the microcontroller will automatically complete the action. When the software is set to 1 or cleared to 0, it means that we must use the program to complete the action.)


Table 5-2 is the bit allocation of the timer control register TCON, and Table 5-3 is a description of the specific meaning of each bit. In the TCON register, TF0 and TR0 are used for timer 0, and TF1 and TR1 are used for timer 1. Let's take timer 1 as an example. The same is true for timer 0. TR1 is equal to 1. The timer value increases by 1 every machine cycle. If it is equal to 0, it stops increasing by 1. The function of TF1 is to tell us that the timer has overflowed. For example, in the 16-bit mode of the timer, TL1 will increase by 1 every machine cycle. After adding to 255, TL1 will become 0 after adding 1 again, and TH1 will increase by 1. This cycle repeats. When TL1 and TH1 are 255, adding 1 will overflow. At this time, TL1 and TH1 will become 0, and TF1 will automatically become 1, providing readers with an overflow signal.


TMOD – Timer Mode Register

(Note: Non-bit addressability means that each bit of the register cannot be operated individually. For example, the operation of M1 (T1) = 1 in TMOD is wrong. If we want to operate, we must operate the entire byte at a time, that is, we must operate all the bits of TMOD at once)


The above table shows the bit allocation of TMOD.

Table 5-5 is the bit description of TMOD. The GATE bit is for readers to understand. The C/T bit is used to select the timer or counter function. C/T=0 selects the timer function, and C/T=1 selects the counter function.

Table 5-6 introduces the modes in TMOD4. Modes 0 and 3 are rarely used now. Readers can learn about them on their own. Modes 1 and 2 are introduced below.


Mode 1: THn and TLn form a 16-bit timer with a counting range of 0 to 65535. After overflow, as long as THn and TLn are not reassigned, the counting starts from 0.


Mode 2: It is an 8-bit automatic reload mode. Only TLn counts by adding 1. The counting range is 0 to 255. The value of THn does not change but keeps the original value. After TLn overflows, TFn is directly set to 1, and the original value of THn is directly assigned to TLn, and then TLn starts counting from the newly assigned number. For example, in this mode, TH1 = 0x66, TL1 = 0x00, TL1 adds 1 every machine cycle. When TL1 = 0xFF, TF1 will become 1 after adding 1, and TL1 will be assigned the value of TH1 and then count, that is, TL1 starts counting from 0x66, and the value of TH1 remains unchanged.


2.2.2 Timer Mode Working Circuit Logic Diagram

Timer Mode 1 Working Circuit Logic Diagram

Explanation: Take timer 1 as an example, and the same goes for timer 0. OSC is the clock source frequency, and d is the multiple of the microcontroller machine cycle to the clock cycle.


When C/T = 0, the timer function is enabled, and when C/T = 1, the counter function is enabled.

To achieve the control effect, the circuit composed of TR1 and the OR gate must be ANDed. The AND operation is true only when both are true. Therefore, TR1 must be 1, and the result of the OR operation of the circuit composed of the OR gate must be 1.

There are two ways for the OR gate to output 1: (1) When the INT1 pin is 1, no matter what the GATA bit is, the result of the OR operation is 1, and Timer 1 Mode 1 is enabled. (2) When GATA is 0, it becomes 1 after the NOT operation. No matter what the INT1 bit is, the result of the OR operation is 1, and Timer 1 Mode 1 is enabled.


Timer Mode 2 Working Circuit Logic Diagram

Explanation: The circuit logic diagram of mode 2 is similar to that of mode 1, so it will not be explained in detail. In this mode, the timer acts as an 8-bit counter (TLn) that can be automatically reloaded. When TLn overflows, it is not only set to TFn, but also the content of THn is reloaded into TLn. The content of THn is preset by software and remains unchanged during reloading.


2.3 Application of timer

2.3.1 Steps to use the timer

The following uses timer 0 as an example, and the same applies to timer 1.


Set the timer mode register TMOD and select the timer operating mode.

Set the initial value of the counting registers TH0 and TL0.

Set the timer control register TCON and set TR0 to 1 to start the timer counting.

By judging the TF0 bit in TCON, the overflow of the timer is monitored.


2.3.2 Calculate how to use a timer to set time

We know that the machine cycle of the standard 51 microcontroller is 12 clock cycles. Taking the STC89C52RC microcontroller as an example, its machine cycle is: 12/11059200 seconds. If the timing is 0.02s, it is assumed that x machine cycles are required, then x* (12/11059200) = 0.02. By calculating, x is equal to 18432. Since the overflow value of the 16-bit timer is 65536, then let TH0 and TL0 be equal to the initial value y. After 18432 machine cycles, the overflow occurs, thus achieving the timing of 0.02s. The y value is calculated as 47104 through 65536-18432, which is converted to hexadecimal 0xB800 (link: hexadecimal conversion method), that is, TH0 = 0xB8, TL0 = 0x00.


2.3.3 Control the flashing of the small light through the timer

Program to control the small light to flash at 0.5HZ frequency

#include


sbit LED = P0^7;

sbit ADDR3 = P1^3;

sbit ADDR2 = P1^2;

sbit ADDR1 = P1^1;

sbit ADDR0 = P1^0;

sbit ENLED = P1^4;


void main()

{

unsigned char cnt = 0; //Define a counting variable to record the number of overflows of T1

ENLED = 0;

ADDR3 = 1;

[1] [2]
Reference address:[Self-study 51 single chip microcomputer] 5 --- timer, digital tube, logical operation,

Previous article:[Self-study 51 single-chip microcomputer] 6 --- Introduction to digital tube dynamic display and interrupt system
Next article:[Self-study 51 single-chip microcomputer] 4---base conversion, C language variable types and operators

Recommended ReadingLatest update time:2024-11-16 12:51

STM8-TIMER implements system tick timer
There is no system tick timer in STM8, so the timer is used to implement the function of the system tick timer, generating an interrupt every 1ms. This time, the high-speed internal clock is used, and the clock frequency is 2Mhz 1. hours.c #include "timer.h" void systic_init(void) // TIMER2_CH1 - PB0 { CLK_Peri
[Microcontroller]
STM32L151C8 periodically wakes up from standby mode (RTC Wakeup Timer)
#include "stm32l1xx.h" #include "system_stm32l1xx.h" #include "OLED.h" #include "delay.h" void RtcWakeUpConfig(void); u8 RtcInit(void); u8 RtcConfig(void); int main() {     DelayInit(); //Delay initialization     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); //Enable PWR clock     if(PWR_GetFlagStatus(PWR_FL
[Microcontroller]
STM32L151C8 periodically wakes up from standby mode (RTC Wakeup Timer)
PIC32MZ tutorial -- Timer Interrupt
  An interrupt is an internal or external event that requires quick attention from the controller. The PIC32MZ architecture provides a rich interrupt system that can manage up to 190 sources of interrupts. Each interrupt source can have a unique piece of code, called the Interrupt Service Routine (ISR) directly associ
[Microcontroller]
MSP430F413 uses Timer_A to make a virtual serial port
The HT824 keypad uses serial communication with the mainboard. When doing high and low temperature tests, communication problems often occur. Using an oscilloscope to track the communication waveform, it is found that the baud rate changes when the temperature changes. Theoretically, it should not change. The delay_us
[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
Guess you like

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号