Embedded C programming and AVR skills (I) - C language environment to access MCU registers

Publisher:星辰耀眼Latest update time:2017-01-03 Source: eefocus 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 the standard C forced type conversion and pointer to implement
the use of standard C forced conversion and pointer concepts to access the MCU registers, for example:
#define DDRB (*(volatile unsigned char *)0x25)

The analysis is as follows:
    A: The 0x25 in (unsigned char *)0x25 is just a value. The addition of (unsigned char *) in front means that 0x25 is an address, and the data type of the data stored at this address is unsigned char, which means that when reading/writing this address, the value of unsigned char must be written in, and the value of unsigned char is also read out.
    (*(volatile unsigned char *)0x25) is a fixed pointer, which is immutable, not a pointer variable. Add "*" in front, that is, *(volatile unsigned char *)0x25, and 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 the i in front is fixed.
    B: The keyword volatile ensures that this instruction will not be omitted for optimization by the C compiler, and requires direct reading of the value 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 0x25 as the address, just write DDRB directly, 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 using pointer variables directly, for example:
    unsigned char *p, i;
    p = 0x25;
    i = *p; //Read the data in the unit with address 0x25 and send it to the i variable
    *p = 0; //Write 0 to the unit with address 0x25
    In summary, (*(volatile unsigned char *)0x25) can be regarded as an ordinary variable. This variable has a fixed address and points to 0x25. 0x25 is just a constant, not a pointer, let alone 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 follows:
    sfr P0 = 0x80;
    In this way, the 0x80 unit can be directly written to P0.
    The following is a brief introduction to the method of accessing MCU registers by the AVR C compiler.
    A: Use standard C type conversion and pointers to access MCU registers. Every C compiler supports this. The reason is simple. This is standard C.
    B: ICCAVR and GCCAVR do not define new data types. Only standard C type conversion and pointers can be used to access MCU registers. IAR and CodeVisionAVR compilers have expanded ANSI C and defined new data types. C language can directly access MCU registers. For example, in IAR:
    SFR_B(DDRB, 0x28)
    In CodeVisionAVR:
    sfrb DDRB = 0x28
    In this way, PORTB = 0xff; is equivalent to (*(volatile unsigned char *)0x05) = 0xff; and 0x25 is exactly the address of register PORTB in the device ATmega48/88/168.
    GCCAVR Each AVR device does not use direct definition of special function register macros in the header file. For example, in the iomx8.h file, a definition is as follows:
    #define PORTB _SFR_IO8(0x25)
    and in sfr_defs.h, the following two macro definitions can be found:
    #define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr)+0x20)
    #define _MMIO_BYTE(mem_addr) (*(volatile unit8_t *)(mem_addr))
    is essentially the same as direct type conversion box pointer definition.
    In addition, the macro _BV(bit) in GCCAVR is frequently used to operate I/O registers. avr-libc recommends using this macro to operate register bits. It is defined in the file sfr_defs.h as follows:
    #define _BV(bit) (1<<(bit))
    The following is an example of its 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 this is to provide program readability. Don't worry that it will generate larger code than "DDRB=0x03;". The compiler will handle this and will eventually output the same result as "DDRB=0x03;".

Reference address:Embedded C programming and AVR skills (I) - C language environment to access MCU registers

Previous article:AVR-atmega16 BOOTLoader Program
Next article:Notes on using avr studio - Issues related to cannot find '*.elf'

Recommended ReadingLatest update time:2024-11-17 00:54

sbit and sfr in single chip c language
1. Both bit and sbit are variable types extended by C51.                 Bit is similar to int char, except that char = 8 bits and bit = 1 bit. They are all variables, and the compiler assigns addresses during the compilation process. Unless you specify, the address is random. This address is the entire addressa
[Microcontroller]
51 MCU--LCD1602 Programming Introduction
 This time I will share my LCD1602 programming experience: Let me first explain that the chip driver of the LCD1602 I am going to talk about below is HD44780. If your LCD1602 driver chip is not HD44780, then the following content is not applicable. My overall understanding of LCD1602 Here I will talk about my ov
[Microcontroller]
51 MCU--LCD1602 Programming Introduction
Principle of LCD display controller based on PIC microcontroller HT1621
The HT1621 LCD display controller is a display component of a multifunctional fully automatic intelligent switch, which can monitor the operation of the power supply line in real time, accurately and online. Once the line has leakage, overload, short circuit, overvoltage, undervoltage and phase loss, the intellige
[Microcontroller]
Principle of LCD display controller based on PIC microcontroller HT1621
Application of Single Chip Microcomputer in Dynamic Measurement of Corrosive Medium Temperature
Electrolyte temperature is an important parameter in the electrolysis process, and currently it is still mainly measured by thermocouples. Since the electrolyte is a highly corrosive medium, in order to avoid corrosion in contact with the medium, although some thermocouples with special protective sleeves have been
[Microcontroller]
Application of Single Chip Microcomputer in Dynamic Measurement of Corrosive Medium Temperature
PIC microcontroller timer/counter module
Preface In single-chip microcomputers, for most beginners, timers and counters are not only widely used, but also a stepping stone for getting started with single-chip microcomputers. Here, the author will focus on this chapter and make a basic introduction to timer/counter learning. --------------------------------
[Microcontroller]
Design of digital frequency meter with AT89S52 microcontroller as the control core
In the field of electronics, frequency is one of the most basic parameters, and has a very close measurement accuracy with the measurement solutions and measurement results of many other electrical parameters. Therefore, the measurement of frequency is particularly important, and the research on frequency measurement
[Microcontroller]
Design of digital frequency meter with AT89S52 microcontroller as the control core
LED dot matrix control design based on single chip microcomputer
   Since the advent of the single-chip microcomputer in the 1970s, it has attracted people's attention and concern for its extremely high cost performance, so it is widely used and developed rapidly. The advantages of single-chip microcomputers are small size, light weight, strong anti-interference ability, low enviro
[Microcontroller]
Why is embedded development more difficult than microcontroller development? That's so right
In fact, there is no standard definition to distinguish between microcontroller and embedded. For developers who have conducted microcontroller and embedded development, they all have their own definitions. Next, let’s talk about my understanding of these two concepts. and insights. First of all, clarify the concept
[Microcontroller]
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号