Write MCU latency function with C51

Source: Internet
Author: User
According to the simple research on the Keil C latency program of 51 single-chip microcomputer, I also personally tested and calculated some existing delay functions.

Assume that the single-chip microcomputer uses a clock frequency of 12 MHz, and the cycle of a single machine is 1 us.
After referring to the simple study of the 51 MCU Keil C latency program, we can know that the most accurate latency function obtained in Keil C will be void delay (unsigned char t)
{
While (-- t );
}

The disassembly code is as follows:

Two machine cycles are required for executing the DJNZ command. The RET command also requires two machine cycles. Based on the input t, the specific time delay is as follows without calculating the time required to call delay:

T Delay Time (us)
1 2 × 1 + 2 = 4
2 2 × 2 + 2 = 6
N 2 × N + 2 = 2 (N + 1)

When delay (1) is called in the main function, the disassembly is as follows:

When delay () is called, two more commands are executed. mov r, # data requires one machine cycle, and LJMP requires two machine cycles. That is, 3 us is required to call delay.

Keil C simulation and computing process:

In addition to the call time, the accurate calculation time delay is compared with the Keil C simulation as follows: (the simulation results and calculation results are very similar)

T Delay Time (us) Simulation 11.0592Mhz clock (us)
1 3 + 2 × 1 + 2 = 7 | 7.7 (actual) 7.60
2 3 + 2 × 2 + 2 = 9 | 9.9 9.76
N 3 + 2 × N + 2 = 2N + 5 | (2N + 5) * 1.1 /
3 11 | 12.1 11.94
15 35 | 38.5 37.98
100 205 | 225.5 222.44
255 515 | 566.5 558.81

That is to say, the precision of this delay function is 2 us, the minimum time delay is 7 us, and the maximum time delay is 3 + 255 × 2 + 2 = 515us.
The actual use of the 11.0592MHz clock, the accuracy of this delay function will be 2.2us, the minimum time delay is 7.7us, the maximum time delay is 566.5us.
This time delay function is accurate enough for single-bus communication with DS18B20.

Now, we change the clock to the actual frequency of 11.0592MHz. Each machine cycle is about 1.1us.
Now let's analyze the previously used latency function: // The latency function. For the 11.0592MHz clock, for example, I = 10, the latency is about 10 ms.
Void delayMs (unsigned int I)
{
Unsigned int j;
While (I --)
{
For (j = 0; j <125; j ++ );
}
}

Its disassembly code is as follows:

Analysis: T indicates A machine cycle (call time is negligible relative to the latency of this ms level) 1 C: 0000 mov a, R7; 1 T
2 DEC R7; 1 TB low 8-bit bytes minus 1
3 MOV R2, 0x06; 2 T
4 jnz c: 0007; 2 T if the low 8-bit byte is not 0, it will jump to C: 0007
5 DEC R6; 1 TB low 8-bit bytes is 0, then the 8-bit high byte is reduced by 1
6 C: 0007 orl a, R2; 1 T
7 jz c: 001D; 2 T if the height of 8 digits is also reduced to 0, then RET
8 clr a; 1 tb a cleared
9 MOV R4, A; 1 T R4 put high
10 MOV R5, A; 1 T R5 low
11 C: 000D clr c; 1 t c cleared
12 mov a, R5; 1 T
13 subb a, # 0x7d; 1 t a = A-125
14 mov a, R4; 1 T
15 subb a, #0x00; 1 T
16 jnc c: 0000; 2 t a is zero, then jump to C: 0000
17 INC R5; 1 T R5 increased by 1
18 CJNE R5, #0x00, C: 001B; 2 T R5> 0, jump to C: 000D
19 INC R4; 1 T
20 C: 001B sjmp c: 000D; 2 T
21 C: 001D RET

For delayMs (1), it takes 12 TB to jump to 21 when the execution reaches 7th, that is, 13.2us.
For delayMs (2 ), it takes 9 T + 13 T + 124x10 T + 7 T + 12 T = 9 T + 13 T + 1240 T + 7 T + 12 T = 1281 T = 1409.1us.
For delayMs (3), it takes 9 Tb x (3-1) + (13 TB + 124 × 10 TB + 7 TB) x (3-1) + 12 TB
= 1269T × (3-1) + 12 T = 2550 T = 2805us.
For delayMs (N), N> 1, the time required is 1269T × (N-1) + 12 T = 1269NT-1257T = (1395.9N-1382.7) us.

Simulation of delayMs (1) = 0.00166558 s = 1.67 ms using Keil C is as follows:


The analysis shows that the specific computing latency is compared with the Keil C simulation latency as follows:

I Time Delay Simulation latency
1 13.2us 1.67 ms
2 1409.1us 3.31 ms
3 2805us 4.96 ms
N (1395.9N-1382.7) us
10 12.6 ms 16.50 ms
20 26.5 ms 32.98 ms
30 40.5 ms 49.46 ms
50 68.4 ms 82.43 ms
100 138.2 ms 164.84 ms
200 277.8 ms 329.56 ms
500 696.6 ms 824.13 ms
1000 1394.5 ms 1648.54 ms
1500 2092.5 ms 2472.34 ms
2000 2790.4 ms 3296.47 ms
5 5.6 ms 8.26 ms
73 100.5 ms 120.34 ms
720 1003.7 ms = 1 s 1186.74 ms

After delayMs (10) is calculated, the latency is 12576.3us, which is about 12.6 ms, which is nearly 10 ms.

As long as delayMs (1) differs greatly from the calculation results and simulation results, the rest are close to each other and within the acceptable range.

After the above analysis, we can see that using C language for latency is not very accurate, but not very accurate. If a statement changes, the latency may be different, the compilation commands generated by the compiler may be different.

PS:
For each 51 microcontroller Assembly command length and the required machine cycle is summarized as follows: From: http://bbs.mcustudy.com/printpage.asp? Boolean ID = 2 & ID = 1454
Appendix E-8051 Instruction Set

Arithmetic Operations

Mnemonic Description Size Cycles
Add a, Rn Add register to Accumulator (ACC). 1 1
Add a, direct Add direct byte to ACC. 2 1
Add a, @ Ri Add indirect RAM to ACC. 1 1
Add a, # data Add immediate data to ACC. 2 1
Addc a, Rn Add register to ACC with carry. 1 1
Addc a, direct Add direct byte to ACC with carry. 2 1
Addc a, @ Ri Add indirect RAM to ACC with carry. 1
Addc a, # data Add immediate data to ACC with carry. 2 1
Subb a, Rn Subtract register from ACC with borrow. 1
Subb a, direct Subtract direct byte from ACC with borrow 2 1
Subb a, @ Ri Subtract indirect RAM from ACC with borrow. 1 1
Subb a, # data Subtract immediate data from ACC with borrow. 2 1
Inc a Increment ACC. 1 1
INC Rn Increment register. 1 1
INC direct Increment direct byte. 2 1
INC @ Ri Increment indirect RAM. 1 1
Dec a Decrement ACC. 1 1
DEC Rn Decrement register. 1 1
DEC direct Decrement direct byte. 2 1
DEC @ Ri Decrement indirect RAM. 1 1
Inc dptr Increment data pointer. 1 2
Mul AB Multiply A and B Result: A <-low byte, B <-high byte. 1 4
Div AB Divide A by B Result: A <-whole part, B <-remainder. 1 4
Da a Decimal adjust ACC. 1 1
Logical Operations

Mnemonic Description Size Cycles
Anl a, Rn AND Register to ACC. 1 1
Anl a, direct AND direct byte to ACC. 2 1
Anl a, @ Ri AND indirect RAM to ACC. 1 1
Anl a, # data AND immediate data to ACC. 2 1
ANL direct, a and acc to direct byte. 2 1
ANL direct, # data AND immediate data to direct byte. 3 2
Orl a, Rn OR Register to ACC. 1 1
Orl a, direct OR direct byte to ACC. 2 1
Orl a, @ Ri OR indirect RAM to ACC. 1 1
Orl a, # data OR immediate data to ACC. 2 1
ORL direct, a or acc to direct byte. 2 1
ORL direct, # data OR immediate data to direct byte. 3 2
Xrl a, Rn Exclusive OR Register to ACC. 1 1
Xrl a, direct Exclusive OR direct byte to ACC. 2 1
Xrl a, @ Ri Exclusive OR indirect RAM to ACC. 1
Xrl a, # data Exclusive OR immediate data to ACC. 2 1
XRL direct, A Exclusive or acc to direct byte. 2 1
XRL direct, # data XOR immediate data to direct byte. 3 2
Clr a Clear ACC (set all bits to zero). 1 1
Cpl a Compliment ACC. 1 1
Rl a Rotate ACC left. 1 1
Rlc a Rotate ACC left through carry. 1 1
Rr a Rotate ACC right. 1 1
Rrc a Rotate ACC right through carry. 1 1
Swap a Swap nibbles within ACC. 1 1
Data Transfer

Mnemonic Description Size Cycles
Mov a, Rn Move register to ACC. 1 1
Mov a, direct Move direct byte to ACC. 2 1
Mov a, @ Ri Move indirect RAM to ACC. 1 1
Mov a, # data Move immediate data to ACC. 2 1
MOV Rn, A Move ACC to register. 1 1
MOV Rn, direct Move direct byte to register. 2 2
MOV Rn, # data Move immediate data to register. 2 1
MOV direct, A Move ACC to direct byte. 2 1
MOV direct, Rn Move register to direct byte. 2 2
MOV direct, direct Move direct byte to direct byte. 3 2
MOV direct, @ Ri Move indirect RAM to direct byte. 2 2
MOV direct, # data Move immediate data to direct byte. 3 2
MOV @ Ri, A Move ACC to indirect RAM. 1 1
MOV @ Ri, direct Move direct byte to indirect RAM. 2 2
MOV @ Ri, # data Move immediate data to indirect RAM. 2 1
Mov dptr, # data16 Move immediate 16 bit data to data pointer register. 3 2
Movc a, @ A + DPTR Move code byte relative to DPTR to ACC (16 bit address). 1 2
Movc a, @ A + PC Move code byte relative to PC to ACC (16 bit address). 1 2
Movx a, @ Ri Move external RAM to ACC (8 bit address). 1 2
Movx a, @ DPTR Move external RAM to ACC (16 bit address). 1 2
MOVX @ Ri, A Move ACC to external RAM (8 bit address). 1 2
MOVX @ DPTR, A Move ACC to external RAM (16 bit address). 1 2
PUSH direct Push direct byte onto stack. 2 2
POP direct Pop direct byte from stack. 2 2
Xch a, Rn Exchange register with ACC. 1 1
Xch a, direct Exchange direct byte with ACC. 2 1
Xch a, @ Ri Exchange indirect RAM with ACC. 1 1
Xchd a, @ Ri Exchange low order nibble of indirect RAM with low order nibble of ACC. 1 1
Boolean Variable Manipulation

Mnemonic Description Size Cycles
Clr c Clear carry flag. 1 1
CLR bit Clear direct bit. 2 1
Setb c Set carry flag. 1 1
SETB bit Set direct bit. 2 1
Cpl c Compliment carry flag. 1 1
CPL bit Compliment direct bit. 2 1
Anl c, bit AND direct bit to carry flag. 2 2
Anl c,/bit AND compliment of direct bit to carry. 2 2
Orl c, bit OR direct bit to carry flag. 2 2
Orl c,/bit OR compliment of direct bit to carry. 2 2
Mov c, bit Move direct bit to carry flag. 2 1
MOV bit, C Move carry to direct bit. 2 2
JC rel Jump if carry is set. 2 2
JNC rel Jump if carry is not set. 2 2
JB bit, rel Jump if direct bit is set. 3 2
JNB bit, rel Jump if direct bit is not set. 3 2
JBC bit, rel Jump if direct bit is set & clear bit. 3 2
Program Branching

Mnemonic Description Size Cycles
ACALL addr11 Absolute subroutine call. 2 2
LCALL addr16 Long subroutine call. 3 2
RET Return from subroutine. 1 2
RETI Return from interrupt. 1 2
AJMP addr11 Absolute jump. 2 2
LJMP addr16 Long jump. 3 2
SJMP rel Short jump (relative address). 2 2 2
JMP @ A + DPTR Jump indirect relative to the DPTR. 1 2
JZ rel Jump relative if ACC is zero. 2 2
JNZ rel Jump relative if ACC is not zero. 2 2
Cjne a, direct, rel Compare direct byte to ACC and jump if not equal. 3 2
Cjne a, # data, rel Compare immediate byte to ACC and jump if not equal. 3 2
CJNE Rn, # data, rel Compare immediate byte to register and jump if not equal. 3 2
CJNE @ Ri, # data, rel Compare immediate byte to indirect and jump if not equal. 3 2
DJNZ Rn, rel Decrement register and jump if not zero. 2 2
DJNZ direct, rel Decrement direct byte and jump if not zero. 3 2
Other Instructions

Mnemonic Description Size Cycles
NOP No operation. 1 1
Other can view the microcontroller basis-Li guangdi compilation, P70 "MCS-51 MCU instruction summary"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.