Embedded C language (a)

A keyword

Many of these are the C language teaching materials for computer-oriented programming, so often ignored some of the less frequently used to explain keywords, and embedded in tend to see a lot less common keywords, it is worth us to further understand.

1, register sum auto

In the computer operation fastest CPU, now reach 3GHZ, and the corresponding memory is relatively much slower speed, the fastest access registers and cache, because of its large volume and is not suitable for large-capacity, so both joined together only way to improve efficiency. Program code stored in memory, when the data, which is sent to the register, so that the CPU access, use, returned to save memory.
register modifier imply a corresponding compiler variables to register variables will be frequently used, if possible, should be stored in the CPU registers to speed up its storage speed. When using register variables, please note:

  1. The register variables are to be type should be acceptable to the type of the CPU registers, the register means is a single variable variable variable length should be less than equal to the register length (the length of the plastic)
  2. Can not fetch address register variable use of character "&", since this variable is not a memory address (not the memory)
  3. Only local variables and parameters can be shaped as registers and global variables not
  4. Static variables may not be defined as a register variable
    auto appear means, the current variable scope of local variables of the current function or code segments, meaning that the current variables allocated on the stack memory. (Compilers generally ignore auto)

2、continue、break和return

** continue: ** end of the current cycle, beginning next cycle
** break: ** out of the current cycle
** return: ** subroutine return statement can return a value or no return value
error beginners often make is confuse the difference between the break and continue, continue just to end the current round of cycle and then the next cycle (if any) and the break is to exit the cycle.

3, extern and static

static static area can be used to modify variables and functions, modified variables can be divided into local and global variables, they are present in the memory

  1. Static local variable: the static variable is stored in the area, the life cycle of the entire program life cycle, only within the scope of the corresponding function, therefore only be able to modify the corresponding function, relatively safe.
  2. Static global variable: the static variable is stored in the area, the life cycle of the life cycle of the entire program, designed to correspond to the scope of the document, even if the use extern in other files can not be invoked to declare it.
  3. Static functions: defining the scope of a function of only the current file, the benefit is a function of the same name can be avoided in different files (once defined as a static function, the function can only be called within the same file)
    extern : declare external, represents a variable or function is defined as a function or a variable already defined in other files, the page is no longer necessary to redefine the file.

4, volatile and const

volatile : volatile variable declarations, register with the opposite effect, its role is to place the compiler variable as a register variable, is defined as a register variable is sometimes a side effect of a reduction of the read-write memory, for example
when selecting an external memory chip, we often need a pin as a chip select signal CS, if the pin is not added is defined by the compiler volatile error register variables that may become less error occurs

int *Memory_CS=0x40ff000u;					//定义Memory_CS指向外接存储器的片选引脚的输出寄存器
*Memory_CS=0//低电平选中
.....
*Memory_CS=1;                                //重新拉高引脚

Since Memory_CS the compiler errors to compile a register variable is not read twice in the course of 0x40ff000 address is written, the compiler will think first writing is invalid and thus ignored the first chip-select select.

const : read-only variable, once directly modify read-only variable, the compiler will prompt an error. const object has its unique role rule "ignores the data type from left to right to see that the object is modified const", for example

char * const P="abcd";        //定义一个字符指针P指向字符串abcd的起始地址    P为常量
P[1]='1';					//修改P[1](*P)的内容允许,编译器可以通过
P="abcd";					//重新改写P所指向地址不允许,编译器报错

const char *P ="abcd";				//定义一个字符指针P指向字符串abcd的起始地址    *P为常量
P[1]='1';					//修改P[1](*P)的内容不允许,编译器报错
P="abcd";					//重新改写P所指向地址允许,编译器通过

5、typedef

typedef : commonly used in the complex declaration defines an alias, a standard format for the declaration typedef alias; Example typedef unsigned long uint32;
Notably typedef is common language style structure

typedef struct GpioMemMap
{
	uint32_t PDDR;							//Gpio 模块中具体的寄存器分布			
	.......
	uint32_t PDOR;
}	volatile * Gpio_MeMMapPtr;				//定义了一个Gpio模块寄存器分布结构体,并为其起一个volatile类型的 别名指针变量

(Gpio_MeMMapPtr) 0x400ff000u equivalent (volatile * GpioMemMap {...}) 0x400ff000u;

6, struct, enum and union

struct : structure type, need to assign a specific memory block length practical problems often, when the space for the same data type, we often use an array type to manage, but if the inside of the space required by the different lengths dispensing (dispensing module in the example above Gpio internal register address), the best way is to structure. The following format

struct 结构体名
{
	结构体成员列表
} 结构体变量;
typedef struct 结构体名
{
	结构体成员列表
} 该结构体类型别名;

** enum: ** enumeration name, the computer can calculate the data, and the problem we often encounter in everyday life is often not directly linked to the numbers, we need to establish a link between them, this Contact us can be called enumeration column

enum week						
{
	monday = 0//在monday 和 0之间建立联系 在my_week类型变量中 monday 与0等价 ,赋值不是必须的 默认从0开始赋值
	tuesday=1//赋值不是必须的 , 默认为前一个成员加一值
	.....
	sunday =6}  my_week;			//枚举变量
my_week = monday;

If you feel the same again so defined enumerations, you can use typedef an alias.
Union : Commonwealth, and the definition and use of the structure is similar, except that it share the same internal members a variable memory space. example

typedef union
{
	unsigned long DW;
	unsigned short W[2];
	unsigned char B[4];
}Dtype;
 int main ()
 {
	Dtype a;
	a.DW=0x100000000;
	printf("a.B[3]=0x%x\n",a.B[3]);
	return 0;		
}

Result aB [3] = 0x10 to determine changes internet (high address in the high byte) to little endian mode
Similarly aW access read-write memory can also be the same content space

Published 25 original articles · won praise 6 · views 3518

Guess you like

Origin blog.csdn.net/bojin4564/article/details/105350427