AVR MCU Introduction ---- MEGA Port Operation

Publisher:森绿企鹅Latest update time:2017-12-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    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?


Reference address:AVR MCU Introduction ---- MEGA Port Operation

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

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号