51 MCU initial value calculation method

Daddy's amateur MCU learning: the calculation method of the initial value when the timer runs1(51MCU)

//==========51 MCU ========

/* The timer runs in work mode 1

TMOD = 0x01; // timer 0

TMOD = 0x10; // Timer 1

TMOD =0x11;// Timer 0/1 work at the same time

*/

When the timer is working, how to determine the timing time ?

Suppose there is a 5L bucket, and now we need to fill 3L of water into the bucket , how do we know that there is enough 3L ? The answer is to put 2L of water into the bucket first , and then continue to fill it. When the water overflows, it means that 3L of water is enough.

(Take timer 0 as an example) overflow: the same is true for single-chip microcomputers. When the timer in the single-chip microcomputer runs in working mode 1 , the timer storage register is incremented by 1 every time a machine cycle passes , and the count length is 65535 (hexadecimal: FFFFFF ), so 65536+1 will overflow, and the register value will start counting from 0 after overflow, then: timing period = 65536 - initial value, when the initial value is 0 , timing period = 65535 machine cycles

Therefore, when timing is required, first load the initial value into the register, and let the register start counting from the initial value. When it reaches 65536 , the register overflows to 0. After the overflow, TF0 == 1 , indicating that the timing value has arrived. If you need to use Cycle timing, after overflow, the overflow flag ( TF0 ) must be reset to zero first ( zeroing method: 1. Program clearing, then TF0 = 0 ; 2. Entering the timer interrupt to achieve hardware automatic clearing.).

Second, the initial value must be reloaded into the register to start the calculation.

 

clock period = 1/ clock frequency;

Machine cycle (timer storage register plus 1 time) = 12 × clock cycle,

Taking the 11.0592MHz frequency crystal oscillator as an example, the machine cycle (seconds) = 12× ( 1/11059200 )

When the initial value is 0 , the maximum timing time is 65535×12× ( 1/11059200 ) = 71ms ( approximately ) , so when the program needs timing, it is usually necessary to determine a suitable initial value according to the timing time.

Assuming that a time of 30 milliseconds needs to be timed, then 0.03 = A × machine cycle, it can be calculated that A = 0.03/ machine cycle = ( 0.03 × 11059200 ) / 12 , then after A machine cycle, it is 30 milliseconds timing time

It can be calculated that A = 27648

Therefore, the initial value = 65536 - 27648 = 37888 , and 37888 is converted into hexadecimal to 9400, so the upper eight bits TH0 = 0x94, and the lower eight bits TL0 = 0x00 ;

Formula summary :

initial value = 65536 - timing time (seconds) / machine cycle

clock period = 1/ clock frequency;

Machine cycle (timer storage register plus 1 time) = 12 × clock cycle ,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325862523&siteId=291194637