Calculation of initial value of 51 MCU timer

Publisher:快乐的舞蹈Latest update time:2016-05-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 

What is a clock cycle? What is a machine cycle? What is an instruction cycle?

Clock cycle 

The clock cycle is also called the oscillation cycle, which is defined as the reciprocal of the clock pulse (it can be understood that the clock cycle is the reciprocal of the crystal oscillator connected to the microcontroller. For example, the time cycle of a 12M crystal oscillator is 1/12 us). It is the most basic and smallest time unit in the computer. In one clock cycle, the CPU only completes one basic action. For a certain microcontroller, if a clock frequency of 1MHZ is used, the clock cycle is 1us; if a clock frequency of 4MHZ is used, the clock cycle is 250ns. Since the clock pulse is the basic working pulse of the computer, it controls the working rhythm of the computer (making every step of the computer unified to its pace). Obviously, for the same type of computer, the higher the clock frequency, the faster the computer works. However, since different computer hardware circuits and devices are not exactly the same, the required clock cycle frequency range is not necessarily the same. The clock range of the 8051 microcontroller we are studying is 1.2MHz-12MHz. In the 8051 microcontroller, one clock cycle is defined as one beat (indicated by P), and two beats are defined as one state cycle (indicated by S).

 

Machine Cycle

 In computers, for ease of management, the execution process of an instruction is often divided into several stages, and each stage completes a task. For example, fetching instructions, memory reading, memory writing, etc., each of these tasks is called a basic operation. The time required to complete a basic operation is called a machine cycle. Generally speaking, a machine cycle consists of several S cycles (state cycles). A machine cycle of the 8051 series microcontroller consists of 6 S cycles (state cycles). As mentioned earlier, a clock cycle is defined as a beat (represented by P), and two beats are defined as a state cycle (represented by S). The machine cycle of the 8051 microcontroller consists of 6 state cycles, that is, one machine cycle = 6 state cycles = 12 clock cycles.

Instruction cycle 

The instruction cycle is the time required to execute an instruction, which is generally composed of several machine cycles. Different instructions require different numbers of machine cycles. For some simple single-byte instructions, in the instruction fetch cycle, the instruction is immediately decoded and executed after it is fetched into the instruction register, and no other machine cycles are required. For some more complex instructions, such as transfer instructions and multiplication instructions, two or more machine cycles are required. 
Usually, instructions that contain one machine cycle are called single-cycle instructions, and instructions that contain two machine cycles are called two-cycle instructions.
 

 

1. Calculation of the initial value of the 10MS timer: 

          1. Crystal oscillator 12M 

             12MHz divided by 12 is 1MHz, which means one second = 1000000 machine cycles. 10ms = 10000 machine cycles.
              65536-10000 = 55536 (d8f0)  
              TH0 = 0xd8, TL0 = 0xf0  
        2. Crystal oscillator 11.0592M 

              11.0592MHz divided by 12 is 921600Hz, which is 921600 machine cycles per second, 10ms=9216 machine cycles.
              65536-9216=56320(dc00)   
              TH0=0xdc, TL0=0x00

  2. Calculation of the initial value of the 50MS timer:   

       1. Crystal oscillator 12M 
             12MHz divided by 12 is 1MHz, which means one second = 1,000,000 machine cycles. 50ms = 50,000 machine cycles.   

             65536-50000=15536(3cb0) 

             TH0=0x3c, TL0=0xb0

       2. Crystal oscillator 11.0592M 

            11.0592MHz divided by 12 is 921600Hz, which means 921600 machine cycles per second, and 50ms=46080 machine cycles. 

            65536-46080=19456(4c00) 

                TH0=0x4c, TL0=0x00 
 

 3. Instructions 

            Take the 12M crystal oscillator as an example: it can execute 1,000,000 machine cycles per second. Each overflow of T is at most 65,536 machine cycles. We should try to minimize the number of overflow interrupts (such as 50ms), so that the interference to the main program is minimized. During development, crystal oscillators of different frequencies may be replaced as needed (for example, the c51 microcontroller uses an 11.0592M crystal oscillator, which is very suitable for generating serial port clocks, while the 12M crystal oscillator is very convenient for calculating the timer time). It is more convenient to use a plug-in type.  
             For 12MHz 1 machine cycle 1us 12/fosc = 1us

             Mode 0 13-bit timer maximum time interval = 2^13 = 8.192ms 

             Mode 1 16-bit timer maximum time interval = 2^16 = 65.536ms 

             Mode 2 8-bit timer maximum time interval = 2^8 = 0.256ms = 256 us Timing 5ms, calculate the initial value of the timer M = 2^KX*Fosc/12 12MHz Mode 0: K=13,X=5ms,Fosc=12MHz Then M = 2^13 - 5*10^(-3)*12*10^6/12= 3192 = 0x0C78 THx = 0CH,TLx = 78H, 
              Mode 1: K=16,X=5ms,Fosc=12MHz Then M = 2^16 - 5*10^(-3)*12*10^6/12= 60536 = 0xEC78 THx = ECH,TLx = 78H,   

             50ms 12MHz THx = 3CH, TLx = B0H, 10ms THx = D8H, TLx = F0H,

            Method 2: Maximum time 2^8Fosc/12 = 0.256ms  

            How do decimal numbers come from? 6MHz One machine cycle 12/6 = 2us Timing 1ms Count initial value x (2^16-x)*2us = 1000us x = 2^16 - 500, TH, TL can be set to -500  
             12MHz One machine cycle 12/12 = 1us 12MHz One machine cycle 12/12 = 1us Timing 50ms Count initial value x (2^16-x)*1us = 50000us x = 2^16 - 50000, TH, TL can be set to -500 Timer Counts the frequency of the internal crystal oscillator Counter 
Counts the number of pulses on the external input CPU pin P3.4 (T0) P3.5 (T1) Negative jump plus one 
When the crystal oscillator is 6MHz, the maximum counting frequency is 500KHz
Reference address:Calculation of initial value of 51 MCU timer

Previous article:Flexible use of 51 single chip microcomputer timer
Next article:51 MCU Timer 2 Counting Example

Recommended ReadingLatest update time:2024-11-16 18:05

51 single chip microcomputer 60s countdown dynamic display simulation diagram and program
51 MCU 60s dynamic display program #include reg52.h #define uint unsigned int #define uchar unsigned char uchar code table ={0xc0,0xf9,0xa4, 0xb0,0x99, 0x92,0x82,0xf8,0x80,0x90}; sbit gew=P2^1; sbit shiw=P2^0; sbit k2=P1^0; uchar num,num1=60,num2,shi,ge; void Init() // Initialization subfunction {         TMOD=0X01;
[Microcontroller]
51 single chip microcomputer 60s countdown dynamic display simulation diagram and program
STC89C51 MCU and ADXL345 three-axis acceleration module to realize a simple pedometer program
The microcontroller source program is as follows: #include "STC51PRJ.h" extern BYTE BUF ; //Receive data buffer /*****************Start signal****************/ void ADXL345_Start() {         SDA = 1; //Pull up the data line         SCL = 1; //Pull the clock line high         Delay5us(); //Delay         SDA = 0; /
[Microcontroller]
1602+ADC0832 smoke alarm system based on 51 single chip microcomputer
This design is based on 51 single-chip microcomputer, with ADC0832 digital-to-analog conversion module, 1602 LCD display module, key control and sound and light alarm module! It can realize real-time monitoring and response of smoke concentration. The following is the simulation directory   . Below is part of the co
[Microcontroller]
1602+ADC0832 smoke alarm system based on 51 single chip microcomputer
51 MCU application of wireless communication module NRF24L01+
This tutorial is very detailed about NRF24L01+, which is very helpful for everyone's learning and debugging. It contains detailed tutorials:   The microcontroller source program is as follows: Next we write the sending program: //********************************** //NRF24L01+ module transmission program // Debu
[Microcontroller]
51 MCU application of wireless communication module NRF24L01+
51 MCU LCD12864 Driver C Language ST7920
main.c #include "reg52.h" #include intrins.h #include string.h #include stdio.h #include stdlib.h #include "LCD12864.h" void main( void ) { This_Lcd(); Lcd_WriteStr(0,0,"QQ137712826"); while ( 1 ) { } } lcd12864.h #include reg52.h #include intrins.h sbit RS = P2 ^ 0; sbit RW = P
[Microcontroller]
51 MCU LCD12864 Driver C Language ST7920
Design of LED dot matrix screen based on 51 single chip microcomputer
LED dot matrix screen has strong luminous brightness and good indication effect. It can produce moving luminous pictures and texts, which are more likely to attract people's attention. It has a large amount of information and can be updated at any time. It has very good advertising and notice effects. Here, the author
[Microcontroller]
Design of LED dot matrix screen based on 51 single chip microcomputer
Micro Programmable Controller Based on AT89C51 Single Chip Microcomputer
Abstract: This paper introduces the design ideas, system hardware configuration and software design methods of a micro programmable controller PLC using the AT89C51 single chip. Finally, an example of the application of this micro programmable controller in water tower water level control is given. Keywords:
[Industrial Control]
Micro Programmable Controller Based on AT89C51 Single Chip Microcomputer
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号