How to use AVR I/O

Publisher:心有归属Latest update time:2016-10-26 Source: eefocusKeywords:AVR  I 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 output while(1) { BUZ_ON; //I call delay50ms(20); BUZ_OFF; //I don't call delay50ms(20); }  } System debugging Change the statement: delay50ms(20); to the statement: delay50ms(1); You can hear that the frequency of calling is higher, it's so noisy!

















 

Taking ATMEGA16 as an example, this article uses a relaxed and humorous way to explain each functional component of AVR, and provides Protel circuit diagrams 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 LEDs, so that they can flash when you want to, and not flash when you don't want to, flash left and right, and flash desperately, demonstrating the "lighting technique" of AVR microcontrollers.
Hardware Design
   For details about the I/O structure and related introduction of AVR, please refer to the Datasheet. Here is only a brief introduction of 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 when external pin is pulled low (uA) 
0 1 1 Input invalid tri-state (high impedance) 
1 0 X Output invalid push-pull 0 Output, absorb 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 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 the lamp when outputting "0" and turn off the lamp when outputting "1". This connection method is also called "current injection connection method".

AVR main control circuit schematic diagram (click on the picture to enlarge, no magnifying glass is needed!) 

LED control circuit schematic diagram (click on the picture to enlarge, no magnifying glass is needed!) 
Software design

The following part is copied from TXT and copied to the web page. The code part has many spaces omitted, which is quite 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 the library program is loaded, please copy the "ICC_H" folder under the "library program" in the root directory of the CD to the D drive
(2) Please read carefully: "Product Information\Development Board Experiment Board\SMK Series\SMK1632\Instructions" in the root directory of the CD
----------------------------------------------------------------------
10101010101010101010101010101010101010101010101010101010101010101010101010101010101010*/

#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);
}

}

System debugging
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 it makes your eyes dizzy!

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 AVR microcontroller is a standard bidirectional port. First, we 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, we can set the state of the x port.
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 value 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 value corresponding to 0x0F is 00001111b


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

PORTA = 0x0F; //The upper 4 bits of port A output low level, and the lower 4 bits output high level

Tip:
Use bitwise logical operators to set specific ports.

PORTA = 1<<3; //The 4th bit of port A is high level, and the others are low level. It should be 00000001. After shifting 3 bits to the left, it is 00001000
PORTA = 1<<7; //Similarly, the 8th bit is high level

Sometimes we expect a certain bit of the port to be set to high level, but the high and low levels of other bits should remain unchanged. How to do it? C language is very powerful, there is a way! As follows:

PORTA |=1<<3; //Realize the 4th bit of port A as high level, 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 level, 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, and the high and low levels of other bits remain 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 bitwise invert it to become 11110111b, and then do 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

Reverse the high and low levels of the corresponding bits of a port, that is, the original high level becomes a low level, and the low level becomes a high level, haha! So simple!

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

There is also an XOR bitwise logic operation, which is also very interesting. It can achieve level reversal. If you are interested, please read the book. It is considered to be a thought for everyone!

Another small question!
Everyone knows that if two variables a and b are known, the common method to exchange two variables in programming is to define an intermediate variable c, and then:

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

The exchange of the contents of variables a and b is completed through the intermediate variable c!
But think about whether C language can be used to complete the exchange of variables a and b without 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 ingenuity of programming!
Answer:
The bitwise XOR logic operation of C language is used. Since there is no intermediate variable and the logic operation is very fast, the whole exchange process is much faster than the conventional method!

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

the process is a XOR b, b XOR a, and then a XOR b!

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.

If you haven't read the relevant materials, it's hard for beginners to figure out the problem of swapping variables.

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

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


Learn another trick, it's really clever! They have the same purpose.
I learned these from others by reading materials, 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 of all, just for calculation, arithmetic operations use 8 clock units, and logical operations use 3 clock units, because arithmetic operations involve negative numbers.
What about variable assignment? Int assignment takes 4 clock units, and unsigned char assignment takes 2 clock units.
In summary, arithmetic operations take 12 units, and logical operations take 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 inaccurate. Next, 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 cleared at any time with the right mouse button, so that the numbers are more intuitive and no addition or subtraction is required.

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

Previous article:Several USART interface functions of AVR
Next article:How to use C language to manipulate AVR IO ports (taking ICCAVR as an example)

Recommended ReadingLatest update time:2024-11-16 14:26

Ordinary IO port simulation to achieve I2C communication and application analysis
According to the I2C communication specification (for details, please refer to " Brief Introduction to I2C Bus "), the I2C communication between the microcontroller (master device) and the slave device can be realized through the common IO port simulation, in which SCL is realized by delaying the high and low level ch
[Microcontroller]
Learn 51 MCU from me (I): MCU minimum system composition and I/O output control
  Single-chip microcomputer is a technology with strong practicality. Many beginners are often confused when learning single-chip microcomputer technology development and don't know where to start. To this end, the author combined his many years of experience in using single-chip microcomputers to specially design the
[Microcontroller]
Learn 51 MCU from me (I): MCU minimum system composition and I/O output control
ATmega32 I/O Memory
For the definition of ATmega32's I/O space, see P283 "Register Overview". All ATmega32 I/O and peripherals are placed in I/O. I/O locations can be accessed through IN and OUT instructions to transfer data between 32 general working registers and I/O. I/O registers with addresses 0x00 - 0x1F can also be directly bit-a
[Microcontroller]
New i-ToF image sensor enables smaller 3D camera systems
New i-ToF image sensor enables smaller 3D camera systems, improving quantum efficiency while optimizing cost Munich, Germany, March 28, 2023 - Infineon Technologies AG and pmd, a premium partner in the 3D ToF field, have jointly launched the IRS2976C Time-of-Flight (ToF) VGA sensor, a
[sensor]
New i-ToF image sensor enables smaller 3D camera systems
AVR MCU Taxi Meter Proteus Simulation and Program
The microcontroller source program is as follows: #include iom16v.h #include macros.h #include "stdio.h" #include "delay.h" #define uchar unsigned char #define uint unsigned int char smg_zx ={0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f,0x76,0X38};// char smg_wx ={0B00000001,0B00000010,0B00000100,0b00001000,0
[Microcontroller]
AVR MCU Taxi Meter Proteus Simulation and Program
Design of LED rotating screen display system based on AVR microprocessor ATmega16L
introduction With the rapid development of science and technology, LED display screens have become a new type of electronic screen advertising media. Compared with traditional inkjet and photo advertising images that are rigid and inactive billboards and advertising light boxes, LED display screens bring people clea
[Microcontroller]
Design of LED rotating screen display system based on AVR microprocessor ATmega16L
Design Method of I2C Bus EEPROM Driver in Linux
1 Introduction I2C (Inter-Integrated Circuit1 bus) is a 2-wire serial bus developed by Philips for connecting microcontrollers and their peripherals. It is a special form of synchronous communication with the advantages of few interface lines, simple control method, small devic
[Power Management]
Design Method of I2C Bus EEPROM Driver in Linux
AVR Lesson 5: Buzzer
span style="font-size:18px;" /* *info:buzzer *author:chenlu *date:2014.11.20 */ //input the head file so that the program can work normally //iom16v---know the register //macros---know the BIT(x) #include iom16v.h #include macros.h //use those can make your study very conveninet #define uint unsigned int #define u
[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号