Talk about C51 programming standards

Publisher:春水碧于天Latest update time:2011-02-27 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Now, C51 has been widely promoted and applied in the programming of single-chip microcomputers. It is regarded as the mainstream design program of single-chip microcomputers. It can even be said that it is a language that single-chip microcomputer developers must master.

As a tool, the ultimate goal is to achieve functions. Under this premise, we hope that our programs can be easily understood by others, or that we can easily understand other people's programs, so that we can achieve twice the result with half the effort in group cooperation development. When requesting help online, if you can post the program in a standardized way, netizens will find it easier to understand your problem, and you will get help from netizens faster. Otherwise, people will not understand it after reading it for a long time, which will not achieve the expected effect. Therefore, in order to facilitate the exchange of source programs and reduce obstacles in cooperative development, I hope everyone can discuss the programming standards of C51. I think it will be a very meaningful thing to put forward the suggestions that everyone thinks are good, and then make a summary as a standard that everyone agrees on. I will first put forward some of my own ideas to stimulate discussion.

1. Notes

1. Use Chinese;
2. Initial notes:

File (module) annotation content:
company name, copyright, author name, modification time, module function, background introduction, etc. Complex algorithms need to be accompanied by process descriptions;

for example:

/*Module name: LCD module LCD model: HD44780 */

/*Created by: zhaojunjie Date: 2001-06-08 */



/*Version:

Comments at the beginning of a function:
function name, function, input description, return, function description, process handling, global variables, call examples, etc. Complex functions need to include variable usage descriptions;

/************************************************************************
*
* Function name: v_LcdInit
* Function description: LCD initialization
* Function description: Initialization command: 0x3c, 0x08, 0x01, 0x06, 0x10, 0x0c
* Calling function: v_Delaymsec(),v_LcdCmd()
* Global variables:
* Input: None
* Return: None
* Designer: zhao Date: 2001-12-09
* Modifier: zhao Date: 2001-12-09
* Version:
***********************************************************************/

3. Comments in the program:

Modification time and author, notes for easy understanding, etc. The notes should be concise, clear and concise, and self-explanatory sentences should not be annotated.

2. Naming:

The naming must have certain practical significance.

1. Naming of constants: all in uppercase.

2. Variable naming:
add a prefix to the variable name. The prefix reflects the data type of the variable and is in lowercase. The first letter that reflects the meaning of the variable is capitalized, and the others are lowercase.
Variable data types:
unsigned char prefix uc signed char prefix sc
unsigned int prefix ui signed int prefix si
unsigned long prefix ul signed long prefix sl
bit prefix b pointer prefix p

Example: ucReceivData receives data
3. Structure naming:

4. Function naming:
the first letter of the function name is capitalized. If it contains two words, the first letter of each word is capitalized.
Function prototype description includes: references to external functions and internal functions. External references must indicate the function source on the right: module name and file name. For internal functions, just comment on its definition file name;

3. Editing Style

1. Indentation: Indentation is in Tab units, and one Tab is four spaces. Preprocessing statements, global data, function prototypes, titles, additional instructions, function descriptions, labels, etc. are all written in top-aligned order. The "{" and "}" of a statement block are aligned in pairs and aligned with the previous line;

2. Spaces: Data and functions should have appropriate spaces between their types and modifiers and should be aligned as appropriate. Keywords should be left blank in principle, such as
if ( ... ), etc. The space rules for operators are as follows: "->", "[", "]", "++", "--", "~", "!", "+", "-" (for positive and negative signs), "&" (addressing or reference), "*" (for pointers), etc. should not have spaces on both sides (unary operators refer to the side connected to the operand), other operators (including most binary and ternary operators) should have one space on both sides, "(", ")" operators should have one space inside, and when defining a function, they can be aligned with more or no spaces as appropriate, but this is not necessary when implementing a function. The "," operator should only have one space after it, and it can be left blank or with more spaces when alignment is required. Comments added after a statement line should be separated from the statement with appropriate spaces and aligned as much as possible.

3. Alignment: In principle, closely related lines should be aligned, including alignment of types, modifiers, names, parameters, etc. In addition, the length of each line should not exceed the screen too much. If necessary, wrap the line appropriately. Wrap the line at "," or at the operator as much as possible. After wrapping the line, it is best to start with the operator, and the following lines are indented with the first line of the statement, but the statement is still indented with the first line, that is, if the next line is "{", it should be aligned with the first line.

4. Blank lines: Leave two blank lines between each part of the program file structure. If it is not necessary, leave only one blank line. Generally, leave two blank lines between each function implementation.

5. Modifications: After the version is sealed, the old statements must be closed with a , and cannot be deleted or modified by yourself, and must be recorded in the modification history of the file and function.

6. Formal parameters: When defining a function, the formal parameters are described directly in brackets after the function name without further explanation.

Reference address:Talk about C51 programming standards

Previous article:Application of C language in single chip microcomputer development
Next article:C51 Questions and Answers

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

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号