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 all members, 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 [0] = 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 );
From: tiger-john