AVR IO port characteristics and applications

Publisher:bobojrtLatest update time:2016-10-06 Source: eefocusKeywords:AVR  Port Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Analysis of AVR's IO port characteristics:

  AVR IO port characteristics and applications - wanghengzhi@126 - Code Beans

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;

Keywords:AVR  Port Reference address:AVR IO port characteristics and applications

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

Luxshare Precision is subject to 337 investigations by the United States involving high-speed external IO connector products
According to the latest news on the Ministry of Commerce website, on January 21, 2021, the U.S. International Trade Commission (ITC) decided to initiate a 337 investigation into certain electrical connectors and cage assemblies and their products. Luxshare Precision Industry Co., Ltd. and Dongguan Luxshare Precision I
[Semiconductor design/manufacturing]
Luxshare Precision is subject to 337 investigations by the United States involving high-speed external IO connector products
STM8 study notes---ordinary IO port simulates serial port function
Serial ports are very common in product applications, but the default serial ports of microcontrollers are often relatively few, and sometimes there are not enough serial ports. So I wondered if I could use ordinary IO ports to simulate serial ports to realize the functions of serial ports. To simulate the serial po
[Microcontroller]
STM8 study notes---ordinary IO port simulates serial port function
[AVR] High Voltage Parallel Programming --- Basic Knowledge
1. Wiring method and port definition                      RDY/BSY busy flag. (0: device is busy 1: waiting for new command) OE output enable bit low level effective WR write pulse low level is effective BS Byte selection 1 (0: select low byte 1: select high byte) XA XTAL action bit 00 Load Flash or EEPROM add
[Microcontroller]
[AVR] High Voltage Parallel Programming --- Basic Knowledge
AVR Timer ICP Function
System functions Use the AVR internal timer for 1S timing and use LED for simple indication! hardware design For details about the I/O structure and related introduction of AVR, please refer to the Datasheet. Here we only briefly introduce some of them. The following is the I/O pin configuration table of AVR: AV
[Microcontroller]
AVR Timer ICP Function
Introduction to MSP430 MCU IO Ports
The IO port is the most basic component for the processor system to communicate with the outside world. From basic keyboards and LEDs to complex peripheral chips, they are all read or controlled through the input and output operations of the IO port. In the MSP430 series, different microcontrollers have different numb
[Microcontroller]
The calling process of s3c6410_map_io()
s3c6410_map_io()的调用流程: //arch/arm/mach-s3c6410/s3c6410.c void __init s3c6410_map_io(struct map_desc *mach_desc, int size) {     /* register our io-tables */     iotable_init(s3c6410_iodesc, ARRAY_SIZE(s3c6410_iodesc));     iotable_init(mach_desc, size);     /* set our idle function */     s3c24xx_idle = s3c
[Microcontroller]
【stm32f407】IO pin multiplexing and mapping
1. What is pin multiplexing? STM32F4 has many built-in peripherals, and the external pins of these peripherals are multiplexed with GPIO. In other words, if a GPIO can be multiplexed as a function pin of a built-in peripheral, then when this GPIO is used as a built-in peripheral, it is called multiplexing. The IO
[Microcontroller]
【stm32f407】IO pin multiplexing and mapping
Treat the symptoms and root causes, and completely solve the problem of AVR microcontroller EEPROM data loss
Compilation environment: WinAVR-20060421 + AVR Studio 4.12.498 Service Pack 4      Basic idea: Each data written to EEPROM is backed up three times, and each backup data is verified by CRC16. As long as an error occurs during system operation and EEPROM data is modified by mistake,            Then, based on the
[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号