How to use AVR I/O port

Publisher:真诚友爱Latest update time:2013-12-10 Source: eefocusKeywords:AVR  register Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The relationship between the AVR microcontroller register DDRx PORTx PINx and the corresponding IO port (x represents a port, such as port A, port B, etc.)

The following table uses the second bit PB2 of port B as an example and assumes that PB2 is floating.

DDRB.2


PORTB.2


Read the result of PINB.2


Status of pin PB2

1
1
1
PB2 push-pull output

1

1
0
0
PB2 push-pull output

 0

0
1
1
PB2 weak pull-up, can be used as input

0
0
×
PB2 high impedance, can be used as input

When reading PINB.2, the actual level of the PB2 pin is read.

If PB2 is directly connected to VCC, then the result of reading PINB.2 at any time is 1

If PB2 is directly connected to GND, then the result of reading PINB.2 at any time is 0.
The following is a standard C language example:


#include    

unsigned char abc; //define a variable

void main(void) //main function

{

DDRB = 0b11110000;

PORTB = 0b11001100;    

while (1) //main loop

{   

    abc = PINB; //Read the actual level of port B

}

}


If the entire B port is left floating,

Then the result of abc is: 0b110011**

If the 7th bit of port B is connected to GND, the 0th bit is connected to VCC, and the other bits are left floating,

Then the result of abc is: 0b010011*1 (PB7 works in the "short circuit" state)

The “*” indicates uncertainty, which can be regarded as 0 in an ideal state.

Port declaration: include
#include "D:\\ICC_H\\CmmICC.H"
#define OUT_BUZ sbi(DDRB,3) //PB3
#define BUZ_ON cbi(PORTB,3)
#define BUZ_OFF sbi(PORTB,3)
/*--------------------------------------------------------------------
Program name:
Program function: Notes
: Tips: Input: Return: --------------------------------------------------------------------*/ void main(void) { OUT_BUZ; //Set the corresponding IO port as outputwhile (1) { BUZ_ON; //I call delay50ms(20); BUZ_OFF; //I don’t call delay50ms(20); } }














During system debugging
, change the statement: delay50ms(20); to the statement: delay50ms(1);. You can hear that the calling frequency is higher, it is so noisy!


Taking ATMEGA16 as an example, this book explains each functional component of AVR in a relaxed and humorous way, and provides Protel circuit diagram and ICCAVR source code.
All the information is found on the Internet, and I have sorted it out. Let's learn it together!

Lesson 1 AVR IO output LED display program

System Function
Use AVR to control 8-bit LED, flash when you want it to flash, flash when you don't want it to flash, flash left and right, flash desperately, and demonstrate the "lighting technique" of AVR microcontroller.
Hardware Design
For details about the I/O structure and related introduction of AVR, please refer to the Datasheet. Here we only give a brief introduction to some of them. The following is the I/O pin configuration table of AVR:
AVR I/O Port Pin Configuration Table
DDRXn PORTXn PUD I/O Mode Internal Pull-up Resistor Pin Status Description
0 0 X Input invalid tri-state (high impedance)
0 1 0 Input valid Output current (uA) when external pin is pulled low
0 1 1 Input invalid tri-state (high impedance)
1 0 X Output invalid push-pull 0 Output, sink current (20mA)
1 1 X Output invalid push-pull 1 Output, output current (20mA)
Although the AVR I/O port can output a large current enough to light up a lamp when it outputs "1" alone, the total I/O output of the AVR is limited after all. Therefore, experienced lamp liters consider that there may be other strenuous work to do in addition to lighting the lamp, and will design the AVR I/O port to light up the lamp when it outputs "0" and turn off the lamp when it outputs "1". This connection method is also called "current injection connection method". [page]

AVR main control circuit schematic (click on the image to enlarge, no magnifying glass required!)

LED control circuit schematic (click on the image to enlarge, no magnifying glass required!)
Software Design

The following part is copied from TXT and copied to the web page. There are many spaces missing in the code part, so it is a bit messy. Please understand!

//Target system: Based on AVR MCU
//Application software: ICC AVR
/*0101010101010101010101010101010101010101010101010101010101010101010101010101010101
----------------------------------------------------------------------
Experiment content:
Turn on the light, and let the light flash left and right, flash as hard as possible.
----------------------------------------------------------------------
Hardware connection:
Switch the LED indicator enable switch of the PD port to the "ON" state.
----------------------------------------------------------------------
Notes:
(1) If you have loaded the library program, please copy the "ICC_H" folder under the "library program" in the root directory of the CD to disk D.
(2) Please read carefully: "Product Information\\Development Board Experiment Board\\SMK Series\\SMK1632\\Instructions" in the root directory of the CD
----------------------------------------------------------------------
101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010*/

#include
#include "D:\\ICC_H\\CmmICC.H"
#define LED_DDR DDRD
#define LED_PORT PORTD
/*--------------------------------------------------------------------
Program name:
Program function:
Notes:
Tips:
Input:
Return:
--------------------------------------------------------------------*/
void main(void)
{
uint8 i,j;
LED_DDR=0XFF;
while(1)
{
for(i=0;i<4;i++)
{
LED_PORT^=0xFF; //I flash! Flash as hard as I can!
delay50ms(10);
}
j=0x01;
for(i=0;i<8;i++)
{
j<<=1;
LED_PORT=j; //I flash to the left!
delay50ms(10);
}
j=0x80;
for(i=0;i<8;i++)
{
j>>=1;
LED_PORT=j; //I flash to the right!
delay50ms(10);
}
}
}


The purpose of this section is to learn the IO output function of AVR. For AVR, it is different from the traditional 51 microcontroller and needs to set the IO pin direction. Perform the following debugging
:
(1) Change the IO direction, that is, change "LED_DDR=0XFF;" to "0X00" and observe the phenomenon.
(2) Change the statement: delay50ms(10); to the statement: delay50ms(1);. You can see that the LED flashes faster and your eyes are dazzled!

Things need to be used flexibly. The following is a watch made of LED, and the interior is made of AVR and ATmega48. Please think about how to realize the following functions.

The IO port of the AVR microcontroller is a standard bidirectional port. First, you need to set the state of the IO port, that is, input or output.

The DDRx register is the port direction register of the AVR microcontroller. By setting DDRx, the state of the x port can be set.
If the corresponding bit of the DDRx port direction register is set to 1, the corresponding bit of the x port is in the output state. If the corresponding bit of the DDRx port direction register is set to 0, the corresponding bit of the x port is in the input state.
For example:
DDRA = 0xFF; //Set all ports of port A to output state, because the binary corresponding to 0xFF is 11111111b

DDRA = 0x0F //Set the high 4 bits of port A to input state and the low 4 bits to output state, because the binary corresponding to 0x0F is 00001111b


The PORTx register is the output register of the AVR microcontroller. After the port output state is set, the corresponding bit of port x can be input high or low to control the external device by setting PORTx.
For example:
PORTA = 0xFF; // All port lines of port A output high

PORTA = 0x0F; //Port A high 4 bits output low level, low 4 bits output high level

Tip:
Use bitwise logical operators to set specific ports.

PORTA = 1<<3; //Port A, the 4th position is high level, the others are low level, it should be 00000001, after shifting 3 bits left, it is 00001000
PORTA = 1<<7; //Similarly, the 8th position is high level

Sometimes we want to set a certain bit of the port to a high level, but keep the high and low levels of other bits unchanged. How can we do this? C language is very powerful, there is a way! As follows:

PORTA |=1<<3; //The 4th bit of port A is high, and the high and low levels of other bits are not affected.
The above statement is a simplified way of writing. It can be broken down into:
PORTA = PORTA | (1<<3); //The number 1 is shifted left by 3 bits and then bitwise ORed with port A. The result is that the 4th bit of port A is high, and the high and low levels of other bits are not affected.

Then everyone will ask, how to set a certain bit to a low level while keeping the high and low levels of other bits unchanged? It is recommended that you think about it for 1 minute before reading the following content.
PORTA &=~(1<<3); // Explain, first shift 1 left by 3 bits to become 00001000b, then invert it bit by bit to become 11110111b, and then perform a bitwise AND operation with port A, so that the 4th bit of port A is set to a low level, and the high and low levels of other bits remain unchanged.
The decomposed statement is:
PORTA = PORTA & (~(1<<3)); // The result is the same

Flip the high and low levels of the corresponding bit of a port, that is, the original high level becomes a low level, and the low level becomes a high level. Haha! It's so simple!

PORTA = ~PORTA; //Invert PORTA bit by bit and assign it to PORTA

There is also a bitwise logical operation called XOR, which is also very interesting. It can realize level flipping. If you are interested, you can read the book. It can give you an idea!

Here's another question!
We all know that given two variables a and b, the common way to swap two variables in programming is to define an intermediate variable c, and then:

c=a;
a=b;
b=c;

The contents of variables a and b are exchanged through the intermediate variable c!
But think about whether we can use C language to exchange variables a and b without using intermediate variables? The answer is definitely yes, because C language is very powerful!
However, I still hope that everyone will think about it before looking at the answer, and then analyze it carefully after reading the answer to experience the cleverness of programming!
Answer:
The bitwise XOR logic operation of C language is used. Since there is no intermediate variable and the speed of logical operation is very fast, the whole exchange process is much faster than the conventional method!

a ^= b;
b ^= a;
a ^= b;

The process is to XOR a with b, XOR b with a, and then XOR a with b and it's done!

XOR logic table
1 ^ 1 0
0 ^ 1 1
1 ^ 0 1
0 ^ 0 0

adm is amazing. You know all this. You seem to be an expert in programming.

For problems like swapping variables, it is difficult for beginners to figure out the solution on their own if you have not read relevant information.

int a,b;

a=3;

b=5;

a=a+b //a=8 b=5

b=ab //a=8 b=3

a=ab //a=5 a=3

This is just a matter of algorithmic skills, and it is now rare to encounter a situation where there is not enough memory.

For problems like swapping variables, it is difficult for beginners to figure out the solution on their own if you have not read relevant information.

int a,b;
a=3;
b=5;

a=a+b //a=8 b=5
b=ab //a=8 b=3
a=ab //a...


I learned another trick, which is really clever! They are similar in nature.
I learned these from others, but logical operations are more than twice as fast as arithmetic operations. I wrote a program and simulated it in AVR Studio! The
program is as follows:
#include

void main (void)
{
int a=10,b=20;
unsigned char x=30,y=40;

a = a + b;
b = a - b;
a = a - b;

x ^= y;
y ^= x;
x ^= y;
while (1);
}
First, just for calculation, arithmetic operation takes 8 clock units, and logical operation takes 3 clock units, because arithmetic operation involves negative numbers. What about
variable assignment? Int assignment takes 4 clock units, and unsigned char assignment takes 2 clock units.
In summary, arithmetic operation takes 12 units, and logical operation takes 5 units, which is 2.4 times more efficient! After the project is compiled, a .cof debugging file will be generated (I use ICC, CV should also have it). Use AVR Studio to open this .cof file, select the processor model (M16) and enter the software simulation. Press Alt+O shortcut keys to set the processor frequency, so that you can see the running time, otherwise you can only see the running clock, and the time is not accurate. Next is to press F11 to execute in a single step, and F10 to execute a process at once, such as: loop, function, etc. The clock and running time can be reset at any time by right-clicking the mouse, so that the numbers are more intuitive and there is no need to add or subtract.

Keywords:AVR  register Reference address:How to use AVR I/O port

Previous article:Design of solar controller based on AVR
Next article:Multi-point temperature measurement system based on AVR microcontroller and DS18B20

Recommended ReadingLatest update time:2024-11-17 01:46

Embedded Learning Notes 16 - Advantages of AVR Microcontrollers
1. Fast speed, high cost performance, rich on-chip resources, strong IO port driving capability, many optional models, good confidentiality (the program is not easy to be deciphered), easy to learn and simple to develop. 2. Types of AVR microcontrollers (1) ATtiny series: such as tiny13, tiny15, and tiny26, which ha
[Microcontroller]
Medium frequency power supply test system based on AVR microcontroller
1 Introduction The measurement and monitoring of electrical parameters are an important part of the power system. Starting from the engineering practice of the test system, this paper completes the measurement and real-time monitoring of the voltage, current, frequency, power factor, active power and other
[Power Management]
AVR macro assembly and expressions
The assembler provides some pseudo instructions. Pseudo instructions are not directly converted into operands but are used to adjust the location of the program in the memory. All pseudo instructions such as macro initialization memory are given in Table 4.2 1 BYTE stores bytes in variables The BYTE directive saves
[Microcontroller]
AVR Development Arduino Method (Part 7) Embedded Operating System FreeRTOS
FreeRTOS can provide functions such as task management, queue management, interrupt management, resource management and memory management. Because it takes up less resources, it can run on the Arduino UNO R3 development board. You can download it from https://github.com/greiman/FreeRTOS-Arduino , unzip the FreeRTOS-
[Microcontroller]
AVR Microcontroller Learning (10) ATmega16 ADC
Analog-to-Digital Conversion Technology Overview Performance indicators: speed and accuracy AVR analog-to-digital converter   Conversion rate: the number of samples per second. Common units: SPS (times per second) KSPS (thousands per second) MSPS (millions per second). The faster
[Microcontroller]
AVR Microcontroller Learning (10) ATmega16 ADC
AVR MCU Learning (IX) How to use the iic bus and TWI module
IIC bus definition and characteristics How the I2C Bus Works How to use AVR's TWI module AT24C02IIC inter integrated circuit  bus is a two-wire serial bus developed by PHILIPS for connecting microcontrollers and their peripherals. The I2C bus originated in the 1980s and was originally developed for audio and v
[Microcontroller]
AVR MCU Learning (IX) How to use the iic bus and TWI module
ATtiny13 128kHz on-chip oscillator
The ATtiny13 128 kHz on-chip oscillator is a low-power oscillator that provides a clock frequency of 128 kHz. This frequency is specified at 3V, 25°C. This clock is used as the system clock by programming the CKSEL fuse bit to "11". When this clock source is selected, the startup time is determined by the SUT fuse bit
[Microcontroller]
ATtiny13 128kHz on-chip oscillator
Building your own AVR RTOS (Part 2: Artificial Stack)
Part 2: Artificial Stack In the instruction set of the microcontroller, a class of instructions is specifically used to deal with the stack and PC pointer. They are rcall relative call subroutine instruction icall indirect call subroutine instruction ret subroutine return instruction reti interrupt return instructio
[Microcontroller]
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号