Two ways to access MCU registers using C language

Publisher:数据旅人Latest update time:2015-03-06 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The special function register SFR of the microcontroller is an SRAM unit with a fixed SRAM address. There are two ways to access it in the C language environment.

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;".

Reference address:Two ways to access MCU registers using C language

Previous article:Eight methods to solve the EMC problem of single chip microcomputer
Next article:MCU knowledge summary

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号