The relationship between the various cycles in the single-chip microcomputer and the principle of th

时间2018/11/16 18:02:11
发布人管理员
浏览量2635
In a computer, in order to facilitate management, the execution process of an instruction is often divided into several stages, and each stage completes a task. For example, fetch instructions, memory read, memory write, etc., each of these tasks is called a basic operation.

Clock cycle:

The clock period is also called the oscillation period or the crystal oscillator period, which is the number of pulses emitted per unit time of the crystal oscillator. Generally, it is generated by an external oscillator, such as 12MHZ=12×10 to the 6th power, that is, 12,000,000 pulse signals are sent out per second. The time of one pulse is the clock period, which is 1/12 microsecond. Usually called the system clock cycle. It is the most basic and smallest unit of time in a computer.

In the 8051 microcontroller, one clock cycle is defined as a beat (represented by P), and two beats are defined as a state cycle (represented by S).

Machine cycle:

In a computer, in order to facilitate management, the execution process of an instruction is often divided into several stages, and each stage completes a task. For example, fetch instructions, memory read, memory write, etc., each of these tasks is called a basic operation. The time required to complete a basic operation is called the machine cycle. In general, a machine cycle consists of several S cycles (state cycles). One machine cycle of the 8051 series single-chip microcomputer is composed of 6 S cycles (state cycles). As mentioned earlier, one clock cycle is defined as one beat (represented by P), and two beats are defined as one state cycle (represented by S). The machine cycle of the 8051 single-chip microcomputer is composed of 6 state cycles, that is to say, one machine cycle = 6 state cycles = 12 clock cycles.

In a standard 51 single-chip microcomputer, in general, one machine cycle is equal to 12 clock cycles, that is, machine cycle=12*clock cycle, (for the reasons mentioned above) if it is 12MHZ, then the machine cycle=1 microsecond. When the microcontroller works, it fetches instructions from the RoM one by one, and then executes them step by step. The time for the single-chip microcomputer to access the memory once is called a machine cycle, which is a time reference.

The machine cycle is not only of great significance to the execution of instructions, but also the time base of the single-chip timers and counters. For example, a single-chip microcomputer selects a 12MHZ crystal oscillator, then when the value of the timer increases by 1, the actual elapsed time is 1us, which is the timing principle of the single-chip microcomputer.

But in the 8051F310, the CIP-51 microcontroller core adopts a pipeline structure, which greatly improves the instruction execution speed compared with the standard 8051 structure. In a standard 8051, all instructions except MUL and DIV require 12 or 24 system clock cycles, and the maximum system clock frequency is 12-24MHz. As for the CIP-51 core, 70% of the instructions have an execution time of 1 or 2 system clock cycles, and only 4 instructions have an execution time greater than 4 system clock cycles. So pay attention to the changes here when calculating the value of the timer.

Instruction cycle:

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

System clock:

System clock: The system clock is the frequency at which the CPU instructions run, and this is the real frequency of the CPU.

All the internal work of the single chip microcomputer is based on the same trigger signal source generated by the crystal oscillator. This signal is used to synchronize the work steps. We call this signal the system clock. The system clock is generally generated by the crystal oscillator, but the internal system clock of the single chip microcomputer is not It must be equal to the crystal frequency, may be less than the crystal frequency, or may be greater than the crystal frequency, the specific number is determined by the internal structure of the single-chip microcomputer. Normally, there will be an integer multiple relationship with the crystal frequency. The system time is the benchmark of the working rhythm of the whole single-chip microcomputer. Every time it oscillates, the single-chip microcomputer is triggered to perform an operation.

Generally speaking, a single-chip microcomputer has only one clock source. When using an external crystal oscillator, you do not need an internal RC, and when using an internal RC, you do not need an external crystal oscillator. The oscillator oscillates and generates periodic waves. The single-chip microcomputer acts regularly in such periodic waves. For one-shot work, the higher the wave frequency, the faster the monolithic work, and the lower the wave frequency, the slower the single-chip microcomputer works.

With the above concepts, you can correctly understand the working principle of timers. In the 8051F310 microcontroller, there are 3 timers. If timer 1 works in mode 1, such as working mode 1, it is 16-bit The maximum value of the timer is 65535. When adding 1 (=65536), an overflow will occur and an interrupt will occur. So if we want it to count 1000, then the initial value of the timer is 65536-1000, and the result is 64536. This The value is sent to TH and TL, because it is hexadecimal, so the high bit is 64536/256 taking the quotient, and the low bit is 64536% 6 taking the remainder.

Furthermore, how long does each count take? Generally, when we take a 12M crystal oscillator, one cycle is just 1us, and 1000 counts is 1ms. This is because the standard 51 single-chip microcomputer has 12 clock cycles (STC has 6 clocks and 1 Clock mode). So, if our crystal oscillator is 12M, it is better to calculate, if it is other, just use 12 to remove it. For example, if it is 6M, then 12/6=2, and each count is 2us, then you only need to count 500 if you want to time 1ms.

The initial value of the timer is related to the working mode of the timer and the frequency of the crystal oscillator. A machine cycle Tcy=crystal frequency X12, counting times N=timing time t/machine cycle Tcy, then the initial value is X=65536-N, and the resulting number is converted into hexadecimal. Here is an example using timer O working mode 1. If it is other working mode, it cannot be 65535. Working mode 0 is 8192, mode 2, 3 is 256. Here is a formula:

TH=(65536-time/(12/ft))/256

Among them, time is the 100ms to be delayed (100000us), and ft is the frequency of the crystal oscillator. This formula can be simplified to

TH=(65536-time*ft/12)/256

TL=(65536-time*ft/12)%6

In a book, I also saw this calculation of the initial value of the timing:

TH0=-(50235/256); //Reload 100ms timing initial value

TL0=-(50235%6); ///The 6M crystal used here,

Here is a 6M crystal with a delay of 100ms, then according to the principle mentioned above, 6M means each count is 2us, and a 100ms timing means 50,000 counts.

Then, the initial value of the timer should be 65536-50000=15536, and the hexadecimal value is 3CB0. This is the value to be sent to TH(=3C) and TL(=B0).

Write TH0=-(50235/256) in the program; actually it is TH0=0x100-(50235/256); In 51, take a negative number, the result is that its value is reversed +1, or 0x100( What is the result of subtracting the decimal 256)? The result is 3C.

Take STM32F103 as an example to analyze

STM32 TIM generally has advanced timers TIM1, (TIM8 is only available in interconnected products), ordinary timers TIM2, TIM3, TIM4, (TIM5, TIM6, and TIM7 are not available in some devices); today I will only introduce ordinary timers, because I don't know the advanced timer yet! Every ordinary timer has 4 channels!

Let's take a look at this logic diagram first! Let’s discuss the timer issue today! The route I have marked with a red pen is the working route of the timer. The clock is generated by the internal clock, and the PSC is used for frequency division, and then the CNT is counted. There is also an automatic reload register APP on it.

This is the working principle of the frequency divider. We can see that the frequency division coefficient before the frequency divider is set to 1[1], the following [2][3][4] frequency division coefficient is 2, after the frequency division coefficient is changed , The counting cycle has also changed. At the same time, when the prescaler setting takes effect, it will also generate an interrupt signal. Don't worry about this interrupt signal, it will automatically disappear after a system clock cycle, just like I2C!

This is the counting process. As mentioned above, counting is related to the cycle after frequency division; when the count reaches the loaded value, the system will generate a three-signal, of which the overflow signal and the update event will automatically disappear after one clock cycle, and At this time, an update of the interrupt flag bit UIF is triggered. We can use this UPDATE as the timer's interrupt flag signal!

TIM_ITConfig(TIM2, TIM_IT_UPDATE, ENABLE);

Stm32f103xx device function and configuration

3. Stm32f103zet6 timer

The large-capacity STM32F103XX enhanced series products include up to 2 advanced control timers, 4 normal timers and 2 basic timers, as well as 2 watchdog timers and 1 system tick timer.

The following table compares the functions of advanced control timers, normal timers and basic timers:

Timer function comparison

1) Three counting modes of the counter

Up-counting mode: start from 0, count to the preset value of arr, generate an overflow event, and return to re-timing

Down-counting mode: starting from the preset value of arr, counting to 0, generating an overflow event, and returning to re-timing

Center-aligned mode: Count up from 0, count down until arr generates an overflow event, and then count down. After counting to 1, overflow again, and then count up from 0. (This technical method can also be called up/down counting)

2) Advanced control timer (TIM1 and TIM8)

The two advanced control timers (TIM1 and TIM8) can be regarded as three three-phase PWM generators assigned to 6 channels, which have complementary PWM outputs with dead zone insertion, and can also be regarded as complete general-purpose timers. Four independent channels can be used for:

(1) Input capture

(2) Output comparison

(3) Generate PWM (edge or center alignment mode)

(4) Single pulse output

When configured as a 16-bit standard timer, it has the same function as the TIMX timer. When configured as a 16-bit PWM generator, it has full modulation capability (0~100%). In debug mode, the counter can be frozen and the PWM outputs are disabled, thereby cutting off the switches controlled by these outputs. Many functions are the same as the standard TIM timer, and the internal structure is also the same. Therefore, the advanced control timer can cooperate with the TIM timer through the timer link function to provide step or event link function.

3)TlMx

STM32F103XC, STM32F103XD and STM32F103XE enhanced series products have built-in up to 4 standard timers (TIM2, TIM3, TIM4 and TIM5) that can run synchronously. Each timer has a 16-bit auto-loading up/down counter, a 16-bit prescaler and 4 independent channels. Each channel can be used for input capture, output comparison, PWM and single pulse mode Output, can provide up to 16 input capture, output compare or PWM channels in the largest package configuration. They can also work with advanced control timers through the timer link function to provide synchronization or event link functions. In debug mode, the counter can be frozen. Any standard timer can be used to generate: PWM output. Each timer has an independent DMA request mechanism.

These timers can also process signals from incremental encoders, as well as digital outputs from 1 to 3 Hall sensors.

4) Basic timer-TlM6 and TIM7

These two timers are mainly used to generate: DAC trigger signal, and can also be used as a general 16-bit time base counter. Independent watchdog The independent watchdog is based on a 12-bit down counter and an 8-bit prescaler. It is clocked by an internal independent 40kHz RC oscillator; because this RC oscillator is independent of the main clock , So it can run in shutdown and standby mode. It can be used as a watchdog to reset the entire system when a problem occurs, or as a free timer to provide timeout management for applications. It can be configured as software or hardware to start the watchdog through the option byte. In debug mode, the counter can be frozen.

5) Window watchdog

There is a 7-bit down counter in the window watchdog, and it can be set to run freely. It can be used as a watchdog to reset the entire system when a problem occurs. It is driven by the main clock and has an early warning interrupt function; in the debug mode, the counter can be frozen.

6) System time base timer

This timer is dedicated to the real-time operating system and can also be used as a standard down counter. It has the following characteristics:

(1) 24-bit down counter

(2) Automatic reload function

(3) A maskable system interrupt can be generated when the counter is 0

(4) Programmable clock source

7) The clock source of the general-purpose timer;

a: Internal clock (CK_INT)

b: External clock mode 1: External input pin (TIx)

c: External clock mode 2: External trigger input (ETR)

d: Internal trigger input (ITRx): use one timer as a prescaler for another timer

8) The generation of the internal clock of the general fixed period:

From the screenshot, we can see that the clock of the universal timer (TIM2-7) does not come directly from APB1, but reaches the timer module after passing through the prescaler of APB1.

When the prescaler coefficient of APB1 is 1, the frequency multiplier will not work, and the clock frequency of the timer is equal to the frequency of APB1;

When the prescaler coefficient of APB1 is another value (that is, the prescaler coefficient is 2, 4, 8 or 16), this frequency multiplier works, and the clock frequency of the timer is equal to twice the clock frequency of APB1.

Here are a few concepts to analyze, which is also the core concept to understand the function of timers. General-purpose timers are somewhat similar to the operating system's timer beats. You can divide the frequency on the basis of the clock source used by the timer, and then set it. Set the overflow size to realize the timing function. Of course, the automatic reload function is no longer a problem.

The function of prescaler is to make the timer divide again on the basis of the APB clock to make it run independently. Just like the example in the above code, the prescaler coefficient is set to 36000-1, which means that the clock frequency of the timer becomes 72MHz/36000 = 2KHz, and the "count overflow size" can be understood as an automatic loading value, which means Once every x count overflows, an interrupt can be generated. Of course, this frequency is the frequency after prescaler.

Therefore, it can be seen from the above analysis that the timing time of the timer is calculated as:

Tout = (TIM_Period+1)*(TIM_Prescaler+1)/72000000

In the case of this program:Tout= 2000*36000/72000000=1s

It should be noted that the use of 72000000 in the formula is because the clock source used by the timer is 72MHz. If it is configured as another clock source, the corresponding formula should also be changed.

In addition, TIM_ClockDivision is the clock division, which is simply the digital filter function of the timer, which can be set as the default.

13798273129

阿里巴巴店铺

扫描二维码咨询客服

Contact Us:

Operating Hours:

9:00-19:00 (Monday to Saturday)