Analyze the IO pin Pxn. Only when DDRxn is 1, the controllable unidirectional switch will work, and the value of PORTxn can be sent to Pxn through the controllable unidirectional switch.
Conclusion: When DDRxn=1, it is in output state. The output value is equal to PORTxn. Therefore, DDRxn is the direction register. PORTxn is the data register.
Analyze the pull-up resistor. When the potential of E is 0, that is, when D is 1, the pull-up resistor is effective.
From the analysis of the AND gate input, the pull-up resistor is effective only when the following conditions are met at the same time
1. PUD is 0
2. DDxn is 0
3. PORTxn is 1
The conclusion is: the pull-up resistor is effective only when DDRxn = 0, that is, the pin is defined as input state, PORTxn = 1, and UPD is set to 0.
Analyze Pxn and SLEEP. Only when SLEEP = 0, the controllable switch 2 is turned on, SD1 does not work, the input of the Schmitt trigger is equal to Pxn, and the signal is sent to the synchronizer for reading.
Conclusion: Pxn can be read by the AVR regardless of whether it is in input or output state. Input can only be read when SLEEP=0.
Notes on the use of AVR IO ports:
If there are pins that are not used, it is recommended that these pins be given a certain level. The simplest way to ensure that unused pins have a certain level is to enable the internal pull-up resistor.
If you have just defined the input status of a pin and want to read it back immediately, you can insert a _nop() statement before reading it back.
When the system is reset, all DDR bits are 0 and all Port bits are 0, so the pull-up resistor will fail during reset.
How to use C language to manipulate AVR's IO ports (taking ICCAVR as an example):
Example 1: Define PB0 as output, and the output is high level
DDRB=BIT(0); //define PB0 as output
PORTB|=BIT(0); //PB0 outputs high level
Example 2: Define PB0 and PB1 as outputs, and both PB0 and PB1 are high level
DDRB|=BIT(0)|BIT(1); //Define PB0 and PB1 as output
PORTB|=BIT(0)|BIT(1); // PB0, PB1 output high level
Example 3: Flip the value of the PB0 data register, that is, if it is 1, it becomes 0, and if it is 0, it becomes 1
PORTB^=BIT(0); //Flip PB0 port
Example 4: Flip the values of the PB0 and PB1 data registers, that is, if it is 1, it becomes 0, and if it is 0, it becomes 1
PORTB^=BIT(0)|BIT(1); // Flip PB0 and PB1 ports
Example 5: Define PB2 and PB3 as input without pull-up resistors
DDRB&=~(BIT(2)|BIT(3)); //Define PB2 and PB3 as input
PORTB&=~(BIT(2)|BIT(3)); // Set PORT to 0, no pull-up resistor
Example 6: Define PB2 and PB3 as inputs with pull-up resistors. That is, when these pins are not referenced, the default value is high level.
SFIOR&=~BIT(PUD); // Set the pull-up resistor control bit PUD of the SFIOR register to 0. This sentence may not appear in the entire code, or may only appear once, because it is a control bit that controls all pull-up resistors.
DDRB&=~(BIT(2)|BIT(3)); //Define PB2 and PB3 as input
PORTB|=BIT(2)|BIT(3); // Set PORT to 1 to meet another condition of the pull-up resistor
Example 7: The difference between DDRB=BIT(0)|BIT(1) and DDRB|=BIT(0)|BIT(1)
Assume that before executing the above two instructions, the status of DDRB is: 1000 0000
If DDRB=BIT(0)|BIT(1) is executed, the state of DDRB becomes: 0000 0011
If DDRD|=BIT(0)|BIT(1) is executed, the state of DDRB becomes: 1000 0011
The first sentence will clear all previous states, and the second sentence will retain the previous states.
In practical applications, the latter sentence is more commonly used.
Example 8: Besides using BIT(3), is there any other way to set the third bit to 1?
DDRB|=BIT(3);
DDRB|=1<<3;
DDRB|=0x08;
DDRB|=0b00001000;
Previous article:AVR timer CTC mode test
Next article:IAR For AVR Two-wire Serial Interface TWI Application
Recommended ReadingLatest update time:2024-11-15 13:29
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- dsp2812 ADC application experience
- The questions for this national competition were released at 7:30 this morning. What do you think?
- [Ateli Development Board AT32F421 Review] -TEST03 ADC Test
- The talented photographer photographed 150 countries in 10 years, capturing the ultimate beauty of Chinese ink painting and astonishing the world!
- Can a multi-turn potentiometer be used for direction detection?
- SHT31 evaluation + mobile phone APP connection successful
- Analog Dialogue Reading Notes - High-Speed Op Amp Layout Tips
- Identification and identification of SMD diodes
- How to learn Altera Cyclone IV
- si114x infrared sensor