Understanding # define sreg (* (volatile unsigned char *) 0x5f)

Source: Internet
Author: User

[Conversion] understanding # define sreg (* (volatile unsigned char *) 0x5f)
This definition is always very strange. I don't know why. I have a little bit of experience today. Please submit more bricks to the prawns ~~~

Embedded system programming requires programmers to use C language to access a fixed memory address. Since it is an address, according to the syntax rules of the C language, the number of addresses should be pointer type. Therefore, after knowing the memory address to be accessed, such as 0x5f,
The first step is to forcibly convert it to the pointer type.
(Unsigned char *) 0x5f, the AVR sreg is an eight-bit register, so 0x5f is forcibly converted
Unsigned char type.
The volatile keyword indicates that the variable may be unexpectedly changed, so that the compiler will not assume the value of the variable. This "unexpected change" is not changed by the program, but by the hardware-Unexpected.
Step 2: unreference the pointer variable to operate the content of the address pointed to by the pointer.
* (Volatile unsigned char *) 0x5f
Step 3: carefully enclose the parameters in the # define macro in brackets. This is a good habit, so # define sreg (* (volatile unsigned char *) 0x5f)

Similarly, if a 32-bit processor is used to access a 32-bit memory address, you can define # define ram_addr (* (volatile unsigned long *) 0x0000555f) as follows)
Then you can use the C language to read and write the memory address.
Read: TMP = ram_addr;
Write: ram_addr = 0x55;
Zhiwei is published on AVR Microcontroller

The definition is not volatile because its value may change and everyone knows why it has changed;
If you need to constantly judge a memory data in a loop operation, such as waiting for the I flag position of sreg, because sreg is also mapped to the SRAM space, in order to speed up, the compiler may compile the code to read the sreg into Register, and then constantly judge the corresponding bit of register. Instead of reading the sreg, this is certainly not good, because the program or other events (such as interruptions) will change the sreg, and the result is probably an endless loop. If it is defined as a volatile variable, the compiled code is as follows: each time a variable is operated, it is read from the memory.
# After define sreg (* (volatile unsigned char *) 0x5f), you can perform the following basic operations,
Unsigned char temp, * PTR;
Temp = sreg; Save the sreg value to temp
Sreg = temp; assign the Temp value to sreg
PTR = & sreg; do not know whether or not, please try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.