Summary of Embedded C language programming

Source: Internet
Author: User
Tags volatile

Embedded C language programming knowledge

1. When the assembly line is filled with instructions, the maximum efficiency can be realized. That is, an instruction is executed every clock cycle (only a single-cycle instruction ). If the program jumps, the assembly line is cleared, which requires several clocks to fill the assembly line again. Therefore, using jump commands as few as possible can improve program execution efficiency. To solve the problem, try to use the "Conditional execution" function of commands.

2. In the lpc2200 series:

You can use the following program to delay for 10 milliseconds:

For (I = 0; I <200; I ++)

{

For (j = 0; j <200; j ++ );

}

3. The following statement puts a 16-bit variable in two 8-bit variables.

// IP datagram length of high byte

Ipheaduint8 [10] = (iphead. e_ip.crc & 0xff00)> 8;

// The total length of IP datagram is low byte

Ipheaduint8 [11] = iphead. e_ip.crc & 0x00ff;

4. When assigning initial values to all array elements, you can leave the array length unspecified.

Eg; INTA [] = {1, 2, 3, 4, 5 };

However, if the system returns a random value when outputting an element greater than a [5], you cannot use an element that exceeds the initial value.

 

5. Because ADS does not support printf, it is difficult for us to debug it. We can use Serial Output instead of printf for debugging.

6. Use the or operation to set a position to 1, and the other BITs remain unchanged.

Eg: pinsel0 | = 0x00000005; // set the Serial Port Pin

So that the second and Second positions are the same.

7. function pointer

1> in C, the number of letters directly corresponds to the address of the instruction code generated by the function in the memory. Therefore, the function name can be directly assigned to the pointer to the function.

2> calling a function is actually equivalent to "Call Command + parameter transfer processing + regression position into the stack ", essentially, the core operation is to assign the first address of the target code generated by the function to the PC register of the CPU.

3> because the essence of function calling is to jump to the code of an address unit for execution, you can "call a function that does not exist in the function entity at all.

4> int (* p) (); defines P as a pointer variable pointing to the function. The next function returns the return value of the integer type. * Brackets on both sides of P cannot be omitted, indicating that P is first combined with *, is a pointer variable, and then combined with (), indicating that the Pointer Points to the function.

Difference: int * P () indicates that the return value of this function is a pointer to an integer variable.

Note:

(1) the pointer variable to the function is generally defined:

Data Type (* pointer variable name )();

1> the "Data Type" here refers to the type of the function return value.

(2) functions that return pointer values:

Type name * function name (parameter table)

Eg: int * func (int x, int y)

Func is the function name. After calling it, you can return a pointer to the integer data. X and Y are the form parameters of func.

Differences:

A. Find the first bracket from right to left, which contains the form parameter of the function.

B. The first identifier outside the brackets is the name of the function. The identifier before the function indicates the return value of the function.

8. array pointer

1> int (* P) [4]

It indicates that * P has four elements, each of which is an integer. That is, P refers to an array of four integer elements, that is, P is a row pointer.

2> pointer Array

All elements in an array are pointer-type data, which is called a pointer array. Each element in the pointer array is equivalent to a pointer variable.

Define a one-dimensional pointer array as follows:

Type name * array name [array length]

Eg: int * P [4]:

Function: it is used to point to several strings, Making string processing more convenient and flexible. It is applicable to a two-dimensional string array with different character arrays in each row.

Eg: char * name [] = {"Follow me", "basic", "greatwall "};

9. struct

1> you can use struct variables as real parameters. However, when the struct variable is used as the real parameter, the "value transfer" method is adopted to hand the content of the memory unit occupied by the struct variable to the form parameter in sequence. The parameter must also be a struct variable of the same type.

Eg: pint (SU); // note that Su is a struct.

Note: This transfer method has a large overhead in space and time. If the size of the struct is large, the overhead is considerable.

2> use the pointer of the directly oriented struct variable (OR array) as the real parameter, and pass the address of the struct variable (OR array) to the form parameter.

Eg: Print (& su); // note that Su is a struct here

10. shared body

1> the shared body stores several variables of different data types in the same memory. Variables in the public body share the same memory.

2> the general form of variables defining the public body type is:

Union shared body name

{

Member list;

} Variable list;

3> the same block of memory in the shared body can be used to store several different types of data, but only one member variable can be stored at a certain time. The member that plays a role in the shared object variable is the last data stored.

Eg: Union data

{

Int I;

Char C;

Double D;

};

Union data;

The I, C, and D variables in the shared object variable A are stored from the same address in the memory. Assign values as follows:

A. I = 100;

A. c = 'a ';

At this time, the member I in the shared object variable A has no value, because the memory that stores this value is now used to store the value of member C.

3> the length of the shared body variable depends on the maximum length of its members:

Note:

2.The Memory Length occupied by struct variables is the sum of each member, and each member occupies its own storage space. The length of memory occupied by the shared body variable is the maximum length of its members. Of course, for the purpose of improving the access efficiency, the compiler usually needs to align when compiling and allocating buckets.

2.The alignment operation is based on the maximum basic type. That is, the maximum basic type is used as the basic unit. If the actual length is not an integer multiple of the basic unit, the actual length should be an integer multiple of the basic unit.

(Alignment is not performed in turboc and alignment is performed in Linux)

11. Inconsistent processing of CPU length and memory Bit Width

For example, use a shared body to resolve this conflict:

Union send_temp {

Uint16 words;

Uint8 bytes [2];

} Send_buff;

Eg: send_buff.bytes [0] = A; // here, A is an 8-bit

Send_buff.bytes [1] = B; // here, B is 8 bits;

At this time, 8-bit words are combined into 16-bit word storage.

Send (send_buff.words) to send a 16-bit data each time.

12. C language symbol priority:

1> compound assignment operator number:

A + = 3*5;

Equivalent to a = a + (3*5 );

13. A common debugging strategy is to spread some printf function calls in the program to determine the specific location of the error. However, the output results of these function calls are written to the buffer and are not immediately displayed on the screen. In fact, if the program fails, the buffer may not be actually written, so the error location is incorrect. The solution is to call the fflush function immediately after each printf function for debugging.

Printf ("something or other ");

Fflush (stdout );

14. usage of the keyword volatile

The volatile variable may be used in the following situations:

1> hardware registers of the device (for example, Status Registers)

2> global variables that will be accessed in an interrupt service subroutine

3> variables shared by several tasks in multi-threaded applications.

15. Keyword register usage:

When a variable is frequently read/written, the memory needs to be accessed repeatedly, which takes a lot of access time. Therefore, the C language provides a variable, register variable. These variables are stored in the CPU registers. When used, they do not need to access the memory, but are directly read and written from the registers to improve efficiency. The register variable description is register. Variable control variables with a large number of loops and variables used repeatedly in the loop body can be defined as register variables, and cyclic count is the best candidate for applying register variables.
(1) only local automatic variables and parameters can be defined as register variables. Because register variables are dynamically stored, the amount of data that requires static storage cannot be defined as register variables, including: Inter-module global variables, intra-module global variables, and local static variables;
(2) register is a "recommended" keyword, meaning that the program recommends that the variable be placed in the register, but the variable may not become a register variable because the condition is not met, it is stored in the memory, but the compiler does not report an error (there is another "suggested" keyword in C ++: inline ).

14 program code has been burned in flash or ROM. We can allow the CPU to directly read and execute the code from it, but this is usually not a good solution, we 'd better copy the target code in flash or ROM into RAM after the system is started, and then execute the code to speed up instruction obtaining;

The access speed of the CPU to various types of memory is basically:

CPU internal RAM> external synchronization Ram> external asynchronous Ram> flash/ROM

15. macro definition

In C, macros are the only way to generate embedded code. For embedded systems, macros are a good method to replace functions to meet performance requirements.

1> macro-defined "image" function;

2> macro definition is not a function, so all "Parameters" must be included ";

3> macro definition may cause side effects. Therefore, do not input "Parameters" with side effects to the macro definition ".

 

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.