Why does AVR use write 1 as a means to clear the interrupt flag bit?

Publisher:LIGANG888Latest update time:2017-11-24 Source: eefocusKeywords:AVR 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.

The following is my personal analysis and explanation for reference.

1. First of all, from the hardware perspective, the usual read and write processing unit is based on 8BIT bytes, because the data bus is generally a multiple of 8 bits. This makes it inconvenient to operate on bits. 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. For RAM operations, direct writing is generally used, so there is basically no direct bit operation instruction for RAM. For registers, direct bit operations can be performed, but if bit operations can be implemented for all registers, 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, but in standard C, there is no concept of bit variables, and the smallest unit is also byte. Therefore, the hardware design should also consider the advantages of C language.

The above is the reason for my analysis. Because it has exceeded the direction of my research (I focus on application), it may not be comprehensive or biased. Let's go back to AVR itself.

We can notice:
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. They all operate on a certain bit of the register based on register addressing.

3. In addition to 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 2 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 bit in SREG and the bits of 32 general registers. If you want to set one bit of 32 registers (for example, set it to 1), you must first use an instruction to set T in SREG to 1, and then use the BLD instruction to write the value of T to a certain bit of the register. It takes 2 CKs.
    b) SBI, CBI. These two instructions are instructions for setting 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, so 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 it first, modify 1 bit, and then write it back. This ensures that other bits remain unchanged, but it takes 2 CKs.

4. It is precisely because of point 3 (b) that the registers of I/O ports such as PA, PB, PC, PD, etc. are all in the first 32 I/O spaces, so that convenient individual bit-by-bit control of I/O ports is achieved.

5. Different C compilers have different bit processing. ICC and IAR basically have no extended bit processing, and they process it according to standard C because they consider portability more. CVAVR expands 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 take a look at the processing of interrupt flag bits. In AVR, the processing of interrupt flag bits adopts different processing methods 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 manual of M16, we find that the external interrupt flag register GIFR (0X3A) and the interrupt flag register TIFR (0X38) of T/C 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 and 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 1 position to 1 in C? Usually everyone thinks that the correct statement is: XXXX |= 0B00000001; its function is to read XXXX first, and then OR it with 0B00000001, so that the lowest bit is 1, and the other bits remain unchanged. Actually, 3 assembly instructions are required. Change 1 position to 0: XXXX &= 0B11111110; 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 "0" interrupt flag bit, then how should it be defined if writing "1" to the interrupt flag bit? The interrupt flag bit 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 easy to cause some misunderstandings.
a) Only TIFR = 0B00000001 can be used for TIFR and GIFR, because only these two registers are all interrupt flag bits.
b) For some other interrupt flags, if there are some non-interrupt flags in the register where they are located, you must use the writing method of XXXX |= 0B00000001.
c) For the setting of non-interrupt flags, you must still use the form of XXXX |= 0B00000001.

===
...

Keywords:AVR Reference address:Why does AVR use write 1 as a means to clear the interrupt flag bit?

Previous article:Using the Arduino Minimum System Board as an AVR MCU Programmer
Next article:IARAVR interrupt service routine problem

Recommended ReadingLatest update time:2024-11-15 14:28

avr利用pwm控制led光暗及峰鳴器音量大小
//ICC-AVR application builder // Target : M16   // Crystal: 4.0000Mhz   #include iom16v.h    #include macros.h    #define uchar unsigned char #define uint unsigned int void port_init(void); void timer0_init(void); void init_devices(void); void delay_short(uint t); uchar scan_key(void); void port_in
[Microcontroller]
AVR ATMEGA8 Serial Port USART
The avr serial port configuration is very simple, just configure a few registers to send and receive; But there are a few things to understand: 1. Once the serial port is successfully configured, the IO function is automatically occupied, which is different from LPC or STM8/32 (registered configuration is required); 2
[Microcontroller]
AVR MCU 4-digit digital tube counting C program
The 4-digit digital tube of the AVR microcontroller counts and cycles from 0000 to 5000. The bit select terminal is connected to the lower 4 bits of the PC, and the segment select terminal is connected to the PA port. The program is as follows: #include iom16v.h //header file #include macros.h //header file #defin
[Microcontroller]
AVR basic hardware circuit and analysis
Design of the minimum system of single-chip microcomputer AVR basic hardware circuit design and analysis (ATmega16 function board) AVR DB-CORE Ver2.3 Atmega16 development board Our website mall offers this minimum system for sale: 99 yuan AVR learning kit AVR learning board AVR development board easyavr m16  , ATm
[Microcontroller]
AVR basic hardware circuit and analysis
AVR serial port usage
#include #define uchar unsigned char  #define uint unsigned int #define BUFFER_LENGTH 11 unsigned char Receive_Calc = 0; unsigned char UART_Receive_Buffer ={0}; unsigned char UART_Send_Buffer ={'w',' F',0,0,'V',0,'A',0,0,'R',0}; //#################### #####################################  /*Serial por
[Microcontroller]
How does Linux run on 8-bit AVR microcontrollers?
      Update log 2       April 3, 2012 1:00 AM PDT: Uploaded new source code archive; increased emulator frequency (6.5KHz- 10KHz) using FPM (Fast Page Mode) mode of memory and modified i-cache (instruction cache) configuration file; updated porting guide, including kernel image, new smaller ramdisk (virtual disk) a
[Microcontroller]
How does Linux run on 8-bit AVR microcontrollers?
Toshiba LB1847 stepper motor driver chip pdf data and AVR microcontroller source code
ATMEGA16A uses Toshiba LB1847 chip to drive stepper motor Toshiba LB1847 pin diagram:   LB1847 Typical Application Circuit Diagram     ton : Output ON time toff : Output OFF time tm : FAST DECAY time in MIX DECAY mode tn : Noise cancelling time MIX DECAY logic setting DECAY pin : L MD pin : 1.5V to
[Microcontroller]
Toshiba LB1847 stepper motor driver chip pdf data and AVR microcontroller source code
AVR Application Experience-Classic
AVR has the characteristics of quick learning and simple development. However, to fully appreciate and utilize the advantages of AVR, application engineers also need to continuously learn and improve their hardware and software design and development capabilities through practice. "The layman sees the excitement, th
[Microcontroller]
Latest Microcontroller Articles
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号