C51 Program Optimization

Publisher:Bby1978Latest update time:2016-12-14 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  1. Pointer:

  For most compilers, the code generated by using pointers is shorter and more efficient than that generated by using arrays. However, in Keil, the opposite is true. The code generated by using arrays is shorter than that generated by using pointers. Usually, the use of self-increment and self-decrement instructions and compound assignment expressions (such as a-=1 and a+=1, etc.) can generate high-quality program code. The compiler can usually generate instructions such as inc and dec. When using instructions such as a=a+1 or a=a-1, many C compilers will generate two to three bytes of instructions.


  2. Remainder operation:

  a=a%8;  -->  a=a&7;

  Note: Bit operations can be completed in just one instruction cycle, while most C compilers use subroutines to complete the "%" operation, which results in long code and slow execution. Usually, if you only need to find the remainder of a square of 2n, you can use bit operations instead.


  3. Square operation:

     a=pow(a,2.0);  -->  a=a*a;

  Note: In microcontrollers with built-in hardware multipliers (such as the 51 series), multiplication is much faster than squaring because the squaring of floating-point numbers is achieved by calling a subroutine. In AVR microcontrollers with built-in hardware multipliers, such as ATMega163, multiplication can be completed in just 2 clock cycles. Even in AVR microcontrollers without built-in hardware multipliers, the subroutine for multiplication is shorter in code and faster in execution than the subroutine for squaring.

  If we are looking for the cube, such as: a=pow(a,3.0); --> a=a*a*a; the improvement in efficiency is more obvious.


  4. Multiplication and division:

  a=a*4;  -->  a=a<<2;

  b=b/4;  -->  b=b>>2;

In fact, as long as you multiply or divide by an integer, you can use the shift method to get the result, such as:

     a=a*9  -->  a=(a<<3)+a


  5. Delay function:

  The commonly used delay functions are in the form of self-increment:


void delay (void)

{

    unsigned int i;

    for (i=0;i<1000;i++)

    ;

}  


  Change it to a self-decrementing delay function


void delay (void)

{

    unsigned int i;

    for (i=0;i<1000;i--)

    ;

}   


  The delay effects of the two functions are similar, but the code generated by almost all C compilers for the latter function is 1 to 3 bytes less than the former code, because almost all MCUs have instructions for transferring to 0, and the latter method can generate such instructions.


  6. While loop and do...while loop

  Of the two loops, the length of the code generated by the do...while loop after compilation is shorter than that of the while loop. To write good C language, beautiful macro definitions are very important. Using macro definitions can prevent errors, improve portability, readability, convenience, etc.


Keywords:C51 Reference address:C51 Program Optimization

Previous article:Several issues to note about C51 interrupt functions
Next article:C51 library function accumulation

Recommended ReadingLatest update time:2024-11-16 12:58

Interface program between clock chip DS1302 and MCS51 microcontroller C51
#pragma small #include #include /******************************************** * DS1302  PIN  Configuration * ******************************************** sbit DS_CLK = P1^6 sbit DS_IO = P1^5; sbit DS_RST = P1^4; /******************************************** * Shift Data from Mcu in DS1302 * ************************
[Microcontroller]
C51 Programming 6- Bidirectional I/O Port and Quasi-bidirectional I/O Port
Through the previous input and output content (LED control and button use), we have a basic understanding of controlling the I/O port. If you need to output high or low levels, you can write "1" or "0" to the pin; if you need to read the I/O level, you can directly determine whether the pin is high or low. The input
[Microcontroller]
C51 Programming 6- Bidirectional I/O Port and Quasi-bidirectional I/O Port
Learn the basics of C51 5 "Operators"
5. Operators      Turbo C has a wide range of operators, which can be divided into three categories: arithmetic operators, relational and logical operators, and bitwise operators. In addition, there are some operators used to complete special tasks. They are introduced below.      5.1 Arithmetic Operators     
[Microcontroller]
C51 MCU Study Notes Serial Communication
Introduction Serial communication is a communication method between the microcontroller and the PC. Communication mode: parallel, serial, synchronous, asynchronous (most commonly used) Transmission direction: simplex, half-duplex (different time), full-duplex basic structure Related registers SCON serial por
[Microcontroller]
C51 MCU Study Notes Serial Communication
How to embed assembly directly in KEIL C51
Details: How to embed assembly code directly in KEIL C51 Methods for embedding assembly code directly: 1. To embed assembly code in a C file, add the assembly code as follows: #pragma ASM ; Assembler Code Here #pragma ENDASM 2. In the Project window, right-click the C file containing the assembly code, select "
[Microcontroller]
Manually compile and link to generate the hex file of c51
This is purely a personal preference. The system UI is getting better and better now, but I still like the command line inexplicably, it's a kind of nostalgia. Once again, this is just a personal hobby. Although it is manually compiled, it still requires a Keil installed environment. detailed steps: 1. Use Not
[Microcontroller]
STC89C52RC MCU Extra Chapter | 03 - Understanding the data types supported by the C51 compiler
When we were learning C language before, there were data types supported by C language, they were called standard C, now for 51 single chip microcomputer, it is slightly different from the data type of standard C, and some keywords are appended. In the standard C language, the basic data types, such as char, int, sh
[Microcontroller]
STC89C52RC MCU Extra Chapter | 03 - Understanding the data types supported by the C51 compiler
C51_LCD1602 display
#include #include #define uchar unsigned char #define uint unsigned int   sbit rs=P2^6; sbit rw=P2^5; sbit en=P2^7;   uchar shuma ={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09}; //                   0     1     2     3     4     5     6     7     8                       void delay(uchar a)
[Microcontroller]
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
Guess you like

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号