I have read a lot of relevant information on the question of "Why AVR uses writing 1 as a means to clear the interrupt flag bit". The AVR manual does not explain why, but only emphasizes "writing 1 to clear the interrupt flag bit". At the same time, I have also seen many new chips, such as DSP, which also use writing 1 to clear the flag bit. But I have not found a more professional or fundamental explanation. If anyone has knowledge or information in this area, you are welcome to discuss and learn in depth.
Below is my personal analysis and interpretation for your reference.
1. First of all, from the hardware point of view, the usual read and write processing unit is based on 8-bit bytes, because the data bus is generally a multiple of 8 bits. This makes bit operations inconvenient, and you cannot directly write 1 bit (which will change other bits). You need to read the register first, then change 1 bit, and finally write back, which takes more time.
2. RAM operations are generally written directly, so there are basically no direct bit operation instructions for RAM. Registers can be directly operated, but if all registers can be operated, the hardware structure will be very complex and large, so a compromise must be adopted.
3. The current trend is to use C language to write system programs. However, in standard C, there is no concept of bit variables, and the smallest unit is also a byte. Therefore, the advantages of C language should also be considered in hardware design.
The above is the reason for my analysis. Because it has gone beyond the direction of my research (I focus on applications), it may not be comprehensive or biased. Now let's go back to the AVR itself.
We can notice that:
1. AVR has no "bit" space, that is, there is no separate "bit" address. All bit addressing is based on 8-bit registers, so the basic addressing method is mainly based on registers.
2. Therefore, AVR does not have special bit addressing instructions. It has very few bit operation instructions of its own. All of them operate on a certain bit of the register based on register addressing.
3. In addition to the direct operation instructions for the bits in the status register SREG (SREG is too special and must have dedicated bit operation instructions), there are only two instructions that can operate on the bits of other registers.
a) BST, BLD. The cycle of this instruction is 1CK. It is an instruction to exchange data between the T flag in SREG and the bits of 32 general registers. If you want to set one bit of the 32 registers (for example, set it to 1), you must first use the instruction to set T in SREG to 1, and then use the BLD instruction to write the value of T to a bit of the register. It takes 2 CK time.
b) SBI, CBI. These two instructions are used to set the bits of the registers in the first 32 (note: only the first 32 I/O spaces!) I/O spaces. The execution time of these two instructions is 2 CKs. Most of the instructions for AVR register operations are 1 CK, but why do these two instructions require 2 CKs? The reason is that when writing, 8 bits are still written together, so to change 1 bit, you need to read first, modify 1 bit, and then write back. This ensures that the other bits remain unchanged, but it takes 2 CKs.
4. Due to point 3 (b), the registers of I/O ports such as PA, PB, PC, and PD are all in the first 32 I/O spaces, thus realizing convenient individual bit-by-bit control of I/O ports.
5. Different C compilers have different bit processing. ICC and IAR basically do not extend bit processing, and process it according to standard C, because they consider portability more. CVAVR extends bit variables (placed in 32 working registers) and bit operations (only for the first 32 registers in the I/O space), so it is more convenient for users to use. But please note that CVAVR cannot implement bit operations for the last 32 registers in the I/O space.
Finally, let's look at the processing of interrupt flags. In AVR, the processing of interrupt flags is different according to different situations, which has been explained in the English description above. Some are cleared by hardware when entering the execution interrupt, and some are cleared by hardware after reading a certain register. Software clearing usually writes "1", why?
Looking at the M16 manual, I found that the external interrupt flag register GIFR (0X3A) and the T/C interrupt flag register TIFR (0X38) are all in the last 32 addresses of the I/O space, and all of them are interrupt flag registers. Therefore, whether it is ICC, IAR, or CVAVR, you cannot use SBI or CBI instructions for bit operations, and you can only write 8 bits of a register at the same time.
So, how do you usually change the position of 1 to 1 in C? The correct statement is usually: XXXX |= 0B00000001; its function is to read XXXX first, then OR it with 0B00000001, so that the lowest bit is 1, and the other bits remain unchanged. In fact, 3 assembly instructions are required. Change the position of 1 to 0: XXXX &= 0B11111110; it also requires 3 assembly instructions.
AVR uses writing "1" to clear "0" interrupt flag (writing "0" does not affect the flag), so the statement can directly use TIFR = 0B00000001, and only two assembly lines are needed. Clear the lowest flag bit to "0" while ensuring that other flag bits remain unchanged. (!!! Note that it is wrong to use TIFR |= 0B00000001!!! Because if other bits are 1, they will be cleared instead)
In addition, if writing "0" clears the interrupt flag, how should writing "1" to the interrupt flag be defined? The interrupt flag should be set to 1 by hardware. If the software can set it to 1, it will bring more trouble.
In fact, the above English explanation is still incomplete and can easily cause some misunderstandings.
a) You can only use the statement TIFR = 0B00000001 for TIFR and GIFR, because only these two registers contain all interrupt flag bits.
b) For some other interrupt flag bits, if there are some non-interrupt flag bits in the register where they are located, you must use the writing method of XXXX |= 0B00000001.
c) For the setting of non-interrupt flag bits, you must use the format of XXXX |= 0B00000001.
Previous article:AVR microcontroller collects DS18B20 and displays it on LCD
Next article:Solution to delay function error in Atmel Studio 6
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University Hall----Live Replay: ADI - Noise Generation in Switching Power Supplies and How to Reduce Power Supply Noise
- Battery power collection
- How to set up PCB routing like this?
- Dear bachelors, do you cook your dinner yourself or eat out?
- Share an ARM OS project package (based on STM32F103 on-chip resources).
- MSP-EXP430F5529LP Development Board 005-PWM Library Function + Clock Configuration
- EEWORLD University ---- RF Circuit Basics
- EEWORLD University ---- ARM Programming
- C6678 shared memory problem
- Design and practice of common analog systems for e-sports based on TI devices (Part 1)