Why does the AVR microcontroller need to write 1 to clear the interrupt flag?

Publisher:WhisperingRainLatest update time:2020-04-18 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Why does the AVR microcontroller need to write 1 to clear the interrupt flag?

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.

Reference address:Why does the AVR microcontroller need to write 1 to clear the interrupt flag?

Previous article:AVR microcontroller collects DS18B20 and displays it on LCD
Next article:Solution to delay function error in Atmel Studio 6

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号