1. Use standard C type conversion and pointer to implement
Use standard C casts and pointer concepts to access MCU registers, for example:
#define DDRB (*(volatile unsigned char *)0x25)
analyse as below:
A: The 0x25 in (unsigned char *)0x25 is just a value. Adding (unsigned char *) in front means that 0x25 is an address, and the data type of the data stored at this address is unsigned char. This means that when reading/writing this address, the value of unsigned char should be written in, and the value read out is also unsigned char.
(*(volatile unsigned char *)0x25) is a fixed pointer, which is immutable, not a pointer variable. If you add "*" in front, that is, *(volatile unsigned char *)0x25, it becomes a variable (a normal unsigned char variable, not a pointer variable). If it is #define i (*(volatile unsigned char *)0x25), it is the same as unsigned char i, except that the address of i in front is fixed.
B: The keyword volatile ensures that this instruction will not be omitted for C compiler optimization and requires the value to be read directly each time. For example, when using while(*(unsigned char *)0x25), sometimes the system may not actually read the value of 0x25, but use the value read for the first time. If so, this loop may be an infinite loop. Using volatile requires reading the actual value of 0x25 each time.
In this way, to read/write the SRAM unit with address 0x25, you can directly write DDRB, that is, DDRB is a variable, but the address of the variable is fixed to 0x25. For example:
DDRB = 0xff;
This is much more intuitive and convenient than directly using pointer variables, for example:
unsigned char *p, i;
p = 0x25;
i = *p; //Read the data in the unit with address 0x25 and send it to variable i
*p = 0; //Write 0 to the unit at address 0x25
To sum up, (*(volatile unsigned char *)0x25) can be regarded as a normal variable, which has a fixed address and points to 0x25. However, 0x25 is just a constant, not a pointer, nor a variable.
2. Expand the syntax of the C compiler
Expand the syntax of the C compiler. For example, expand the sfr keyword in the MCS51 series KeilC, as shown below:
sfr P0 = 0x80;
In this way, the 0x80 unit can be directly written to P0.
The following is an introduction to the AVR C compiler's method of accessing MCU registers.
A: Use standard C type conversion and pointers to access MCU registers. Every C compiler supports it for the simple reason that it is standard C.
B: ICCAVR and GCCAVR do not define new data types, and can only use standard C type conversion and pointers to access MCU registers. However, IAR and CodeVisionAVR compilers have expanded ANSI C and defined new data types, so that C language can directly access MCU registers. For example, in IAR:
SFR_B (DDRB, 0x28)
CodeVisionAVR:
sfrb DDRB = 0x28
Thus, PORTB = 0xff; is equivalent to (*(volatile unsigned char *)0x05) = 0xff; and 0x25 is exactly the address of register PORTB in the ATmega48/88/168 device.
GCCAVR Each AVR device does not use direct definition of special function register macros in the header file. For example, a definition in the iomx8.h file is as follows:
#define PORTB _SFR_IO8(0x25)
The following two macro definitions can be found in sfr_defs.h:
#define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr)+0x20)
#define _MMIO_BYTE(mem_addr) (*(volatile unit8_t *)(mem_addr))
It is essentially the same as a direct cast and pointer definition.
In addition, the macro _BV(bit) in GCCAVR is frequently used to operate I/O registers. avr-libc recommends using this macro for register bit operations. It is defined in the file sfr_defs.h as follows:
#define _BV(bit) (1<<(bit))
Here is an example of his use;
DDRB = _BV(PB0) | _BV(PB1); //The device header file has defined PB0 to represent 0 and PB1 to represent 1
It is equivalent to "DDRB = 0x03;". The purpose of writing it this way is to provide program readability. Don't worry that it will generate code larger than "DDRB = 0x03;". The compiler will handle this and will eventually output the same result as "DDRB = 0x03;".
Previous article:Eight methods to solve the EMC problem of single chip microcomputer
Next article:MCU knowledge summary
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- [nRF52840 DK Review] +52840 NFC (Part 2)
- What domestic chips are needed to DIY a fast-charging chromium iron or an electric chromium iron that works as a laptop power supply?
- Bluetooth module communicates with mobile phone
- The compilation of the program sent by serial communication always fails
- Does the clearance below the crystal oscillator refer to the layer where the chip is located or all layers?
- [Automatic clock-in walking timing system based on face recognition] Maixbit K210 initialization loading SD card unstable problem
- How to control three colorful LED light strips with the same signal using three remote controllers
- Electromagnetic compatibility circuit board design: based on Altium Designer platform
- Make driving fun! Designing interactive and non-distracting infotainment systems
- How to select external clock for TI DSP?