The function and use method of register of microcomputer entry

Registers configured in memory

The indispensable thing in hardware control is special memory called registers. Controlling the hardware from the program is accomplished by reading and writing the memory of the register.
register
The program means that the CPU reads and writes memory information and performs operations. You can control the hardware by reading and writing this memory.

Confirm the memory location of the register

The microcomputer provides a unique register area in the memory.
Memory map
The graph representing the memory of such a microcomputer is called a memory map. You can also view the placement of program memory and static memory by viewing the memory map.

In this microcomputer, the memory address is represented by 2Byte. In other words, the size of the pointer variable is 2Byte!

Hardware control using registers

Register access method from program

Let us see how to access registers specifically from the program.

IO.PCR6         = 0x01;
IO.PDR6.BYTE    = 0x01;

This is the write process to the register.

You can see that you are visiting as a member of the structure. The register is a special memory, but you can access it just like such an ordinary variable.

volatile

Volatile (Bolatile) is the partner of modifiers such as const and static. Volatile is not explained in general C language books, etc., but in embedded development, modifiers that must appear when register definitions.

Learned the compiler in the C language learning chapter. The first task of the compiler is translation, but when translating, we will improve the program code, which is called optimization. Through this optimization, you can reduce the size of the program you create, improve useless processing efficiency and increase running speed.
For example, the next program will never output the "Hello" of the printf statement because the value of the flg variable is initialized with 1. The compiler determines this situation, deletes the unexecuted program, and then translates it.
Insert picture description here

However, this optimization sometimes has a bad effect on the program. Volatile is a modifier used to suppress (do not perform optimization) optimization.
Insert picture description here

By assigning the volatile modifier in the variable definition, you can give the compiler an instruction "Don't optimize this variable!".

#define   IO  (*(volatile struct st_io *)0xFFD0) /* IO    Address*/

It can be seen from this definition that the real identity of IO is the address information replaced by the macro definition. The address information of this constant uses the pointer function to access memory by overwriting the type of the st_io structure.

Therefore, C language can directly access any memory location without using variables. You can directly access the register memory in the fixed address of the memory in the microcomputer, which is why the C language is still used in the embedded development field.

Guess you like

Origin blog.csdn.net/qq_18191333/article/details/107423947