"Beginner's C51 Self-study Notes" Timer (I)

Publisher:TurquoiseLatest update time:2022-02-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Some knowledge about CPU timing:

Oscillation period: The period of the oscillation source that provides timing signals for the microcontroller (crystal oscillator period or external oscillation period).


State cycle: 2 oscillation cycles are 1 state cycle, represented by S. Oscillation cycle is also called S cycle or clock cycle. Machine cycle: 1 machine cycle contains 6 state cycles and 12 oscillation cycles.


Instruction cycle: The total time taken to complete one instruction, which is measured in machine cycles.


Some knowledge about timers:

The 51 single-chip microcomputer has two sets of timer/counters, which are called timer/counters because they can be used for both timing and counting.


The timer/counter and the microcontroller's CPU are independent of each other. The timer/counter works automatically without the CPU's involvement.


The timer/counter in the 51 single-chip microcomputer adds 1 to the data in the register based on the internal clock of the machine or the external pulse signal. 


structure:

The essence of the timer/counter is a 16-bit add-on counter, which consists of two registers, THx and TLx, with high and low 8 bits. TMOD is the working mode register of the timer/counter, which determines the working mode and function; TCON is the control register, which controls the start and stop of T0 and T1 and sets the overflow flag.

TMOD is used to set its working mode; TCON is used to control its startup and interrupt application.

Working mode register TMOD Working mode register TMOD is used to set the working mode of the timer/counter. The lower four bits are used for T0 and the upper four bits are used for T1.

GATE is the gate bit. When GATE=0, the timer/counter can be started by setting TR0 or TR1 in TCON to 1 through software. When GATA=1, the timer/counter can be started by setting TR0 or TR1 to 1 through software and the external interrupt pin INT0/1 is also at a high level.


C/T: Timing/counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.    

M1M0: Working mode setting bit. The timer/counter has four working modes.

TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software. (Timer 1)

TR0 (TCON.4): T0 operation control bit, its function is similar to TR1. (Timer 0)

TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.

TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1

Reference address:"Beginner's C51 Self-study Notes" Timer (I)

Previous article:"Beginner's C51 Self-study Notes" Interrupt (External Interrupt 0)
Next article:"Beginner's C51 Self-study Notes" - Four working modes of timer (Part 2)

Recommended ReadingLatest update time:2024-11-16 16:32

52 MCU Timer2 Interrupt Application
/* This file is an example to use timer2 in mode 0 This program is used for timer 2 mode 0*/  #define MSB_reload_value 0x36  /* msb reload value exemple high bit reload value: delay 60ms*/  #define LSB_reload_value 0x36  /* lsb reload value example low bit reload value*/  #include "reg_c51.h" //Files in the cur
[Microcontroller]
C51 memory types and physical areas of 51 microcontrollers
1. The data area has a small space, so only variables that are frequently used or require high computing speed are placed in the data area, such as the count value in a for loop. 2. It is best to place local variables in the data area. Because the space of local variables can be overwritten (the local variable spa
[Microcontroller]
Data types of single chip microcomputer C language C51
The data types of C51 are divided into basic data types and combined data types, which are basically the same as the data types in standard C, but the char type is the same as the short type, and the float type is the same as the double type. In addition, C51 also has special function register types and bit types spec
[Microcontroller]
Data types of single chip microcomputer C language C51
A problem about data and xdata encountered in C51 has been solved
environment: I defined a structure variable in a C file, and then the variable was only used by a function in this file. Then I called the function in an interrupt to change the output state of an IO port. As a result, I could not achieve the desired effect when executing it. struct BE   {       unsigned int Coun
[Microcontroller]
Keil C51's 13th keyword extension of C language: sfr
sfr is used to define special function registers. The usage is as follows: sfr name = address; name is the register name address is the address of the register Example: sfr P0 = 0x80; /* P0 port, address is 0x80 */ sfr P1 = 0x90; /* P1 port, address is 0x90 */ sfr P2 = 0xA0; /* Port 2, address 0xa0 */ sfr P3 = 0xB
[Microcontroller]
C51/C52 serial port principle and reference code
1. What is a serial port (RS232 9-pin serial port)       The serial port is a basic external interface that most of our microcontroller units (MCUs) have. Generally, the most basic function of the serial port is debugging, and it can also be used as a data communication interface (with a smaller amount of data). -
[Microcontroller]
C51/C52 serial port principle and reference code
"Beginner's C51 Self-study Notes" implementation of running water lamp (shift operation)
#include reg52.h #define uchar unsigned char  #define uint unsigned int   void delay(void) { uchar a,b; for(a=0;a 200;a++) for(b=0;b 200;b++); }   void main() { uchar k,i; while(1) { k=0xfe; //11111110 for(i=0;i 8;i++) { P0=k; delay(); k=k 1; //11111100 k=k|0x01; //The end becomes 1 1111110
[Microcontroller]
General Rules for C51 MCU Programming
Introduction: This standard specifies the specifications that programmers must follow when designing programs. This standard is mainly for the C51 programming language and the Keil compiler, including typesetting, comments, naming, variable usage, code testability, program efficiency, quality assurance, etc. 1. Sing
[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号