As long as the rational use, C can still achieve unexpected results. Many friends complain that C efficiency is much worse than the Assembly, in fact, if the compiler principle of Keil C has a more in-depth understanding, it is possible to use the appropriate syntax, so that the generated C code to achieve optimization. Even though this may seem unlikely, there are some simple principles to follow: 1. Use the unsigned data structure as much as possible. 2. Try to use the char type, it is not enough to use int, then the long. 3. If possible, do not use floating-point type. 4. Use concise code, because by experience, concise C code can often generate concise target code (although not in all cases to be established).
void Delay1 (unsigned char i)
{
while (-a);
}
Absent-minded compilation, look at the source:
; FUNCTION _delay1 (BEGIN)
; SOURCE Line # 13
;----Variable "I" Assigned to Register "R7"----
; SOURCE Line # 14
0000? C0004:
; SOURCE Line # 15
0000 Dffe DJNZ R7,? C0004
; SOURCE Line # 16
0002? C0006:
0002 RET
; FUNCTION _delay1 (END)
Day ~ ~ ~ The Miracle appeared ... I think this program should already be able to meet the needs of the general situation. If a table is listed:
I delay TIME/US
1 5
2 7
3 9
.....
When the delay time is calculated, the time of the 2 clock cycles spent in the Lcall statement of the calling function has been counted.
Learn from http://www.elecfans.com/emb/danpianji/20140825352241.html
How to improve the accuracy of C language delay compared to assembly language