AVR port is a true bidirectional port, unlike 51 pseudo bidirectional. This is also an advantage of AVR, just pay attention to DDRn when operating. True bidirectional port is not as convenient as pseudo bidirectional in terms of simulation timing.
DDRn PORTn PINn Explanation: n is the port number: ABCDE
DDRn: Controls whether the port is input or output, 0 is input, 1 is output. My memory method: one is greater than zero, so push it outward, that is, 1 is output, 0 is input.
PORTn: Output signal from the pin. When DDRn is 1, the pin output can be assigned a value through port operation statements such as PORTn=x.
PINn: Read input signal from pin. No matter what the value of DDRn is, the external level of port n can be obtained through x=PINn.
When the pin is configured as an input, if PORTxn is "1", the pull-up resistor will be enabled. The use of the internal pull-up resistor will be discussed again when scanning the keyboard.
For more detailed functions and introduction of the port and the second function of the port, please refer to the data sheet.
Port Pin Configuration
DDxn PORTxn PUD (in SFIOR) I/O Pull-up Resistor Description
0 0 X Input No High-Z state (Hi-Z)
0 1 0 When the input Yes is pulled low by an external circuit , the output current
0 1 1 Input No High Impedance (Hi-Z)
1 0 X Output No Output low level (leakage current)
1 1 X Output No Output high level (source current)
If there are pins that are not used, it is recommended to assign a certain level to these pins. The simplest way to ensure that unused pins have a certain level is to enable the internal pull-up resistor. However, please note that the pull-up resistor will be disabled during reset. If the power consumption during reset is also strictly required, it is recommended to use external pull-up or pull-down resistors. It is not recommended to connect unused pins directly to V CC or GND, because this may cause a surge current when the pin is accidentally used as an output.
Let's look at an example:
void port_init(void)
{
PORTA = 0x03;
DDRA = 0x03;
PORTB = 0x00;
DDRB = 0x01;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00; // It is recommended to set the value to zero
}
PORTA = 0x03; DDRA = 0x03; These two sentences make PA1 and PA0 of PA port in output state, and PA7-PA2 in input state. Here 0x03 is 00000011 in binary , which corresponds to the eight IO ports Pn7--Pn0 from left to right.
Use the marquee program to deeply understand the operation of the IO port:
CODE:
// IC C-AVR application builder : 2006-11-21 9:20:57
// Target : M32
// Crystal: 7.3728M Hz
#include
#include
void _delay(unsigned char n) //delay function definition
{
unsigned char i,j;
for(;n!=0;n--) //n*10ms
{
for(j=100;j!=0;j--) //100us*100=10ms
{
for(i=147;i!=0;i--) //delay 100us
;
}
}
}
int main(void)
{
unsigned char i,j,k; //
PORTA=0xFF; //PA port is set to output high level, the light is off
DDRA=0xFF; //PA port is set to output
while(1)
{
i=1;
for (j=0;j<8;j++) //Loop 8 times, that is, PA0~~PA7 flash in turn
{
PORTA=~i; // Inverted output, low level is valid, the corresponding light is on
for (k=0;k<10;k++) _delay(100); //Delay 100*10=1 second, adjustable i=i<<1; //Shift left by one position, the value of I will change as shown in the following list
// 0b00000001 PA0
// 0b00000010 PA1
// 0b00000100 PA2
// 0b00001000 PA3
// 0b00010000 PA4
// 0b00100000 PA5
// 0b01000000 PA6
// 0b10000000 PA7
}
}
}[Copy to clipboard]
Other IO port operation instructions:
void main(void)
{
PORTA=0xff;
DDRA=0xff; //Output mode, IO port pull-up resistor is valid, 1 is output, 0 is input.
PORTA=0xf0; //etc.
The following three instructions operate only on bits where the digit to the right of the operand is one.
PORTA&=~0x70; // Clear 0x70 to 01110000, that is, clear the * third digit and keep the rest of the digits unchanged.
PORTA|=0x77; //Set 0x77 to 01110111, that is, clear the six bits of *210 and keep the rest of the bits unchanged.
PORTA^=0x70; //Flip 0x70 to 01110000, that is, * three bits, if it is a zero, it becomes 1, and if it is a one, it becomes 0.
(P & 0x80)==0x80; //Check if the seventh bit of p is 1 by bitwise AND. If yes, it is true.
}
About 1<
ADIF is a register variable, which can be called the number 4, and is the same as the definition in the manual, including the definition in the chip header file.
(1<
ADCSR=(1<
ADCSR|=(1<
ADCSR&=~(1<
while(ADCSR&(1<
while(1)
{
while(ADCSR&(1<
{
program......
}
}
Practice makes perfect: Just reading such instructions is very boring. It is a better way to learn from practice. Write all these codes into the microcontroller , debug and run them step by step, see the effects of various ports and registers, and exercise program debugging skills. Why not do it?
Previous article:Design of tire mold inner diameter measurement system based on AVR microcontroller
Next article:Design of speech recognition system based on AVR microcontroller
Recommended ReadingLatest update time:2024-11-16 12:25
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
- 【ufun learning】02 Concept and preliminary circuit design 1
- 【RT-Thread Reading Notes】Reflections on RT-Thread Chapters 7-8
- DC-DC chip LM2596SX-5.0/NOPB
- How does the lwip client let the server know that it has been shut down?
- Small car front-end 16W power supply design
- Bear Pie Huawei IoT operating system LiteOS bare metal driver transplantation 05-E53_SF1 expansion board driver and use
- TI's TPS61200 boost chip can only boost voltage but not reduce voltage during actual testing. Please help.
- Learn to make a flyback switching power supply-2
- Embedded Learning丨4412 Development Board-uboot Source Code-Assembly-Source Code Analysis (I)
- Using the I2C Bus