AVR GCC : Microcontrollers Port Management (Part 2 end)

Posted on

A continuation of the article AVR GCC : Microcontrollers Port Management (Part 1).

>>Once the data transfer direction for the port is configured, you can set the port value to be stored in the appropriate register PORTx.
PORTx – register the port, where x is the port name.

If the output is configured as an output, then the corresponding bit in the register PORTx forms on output high signal, and zero – low signal.

If the output is configured as an input, the corresponding bit in the register PORTx connects to the output internal pull-up resistor, which provides a high level of input in the absence of an external signal.

Set to “1” on all the outputs of port D as follows.

PORTD = 0xff;

A set of “0” on all the outputs of the port D may be the case.

PORTD = 0x00;

For each bit registers can be accessed PORTx and individually as well as in the case of registers DDRx.

For example, the command

PORTD | = 1 <<3;

set to “1” (high signal) on output PD3.

Command

PORTD & = ~ (1 <<4);

set to “0” (low signal) on output PD4.

In AVR GCC and the shift can be made using the _BV (), which performs the bitwise shift and inserts the result in the compiled code.

In the case of functions _BV () the previous two commands will look like.

PORTD | = _BV (PD3); / / set to “1” on line 3 of port D

PORTD & = ~ _BV (PD4); / / set to “0” on line 4 port D

Now try to write some simple programs to better understand how to work with the ports of the microcontroller.

Our first program will consist of only a few lines, and their task will include lighting LEDs connected to the microcontroller.

Connect the LED to a microcontroller in various ways.
Connect the LED to a microcontroller in various ways.
Depending on how you connect the LED will light up any signal from a high level applied to the output of the microcontroller PD1, as in the first figure, or from low-level signal in the case of connection shown The second figure.

/************************************************* ************************
EXAMPLE LED on High-level inputs
Connection example in Figure 1
************************************************** ************************/

# include <avr/io.h>
int main (void) {    / / start the main program
DDRD = 0xff;         / / D port pins are configured as outputs
PORTD | = _BV (PD1); / / set to "1" (high level) on output PD1
}                    / / Closing bracket of the main program


/************************************************* **********************
EXAMPLE LED on low-level signals
Connection example in Figure 2
************************************************** **********************/

# include <avr/io.h>

int main (void) {      / / start the main program
DDRD = 0xff;           / / D port pins are configured as outputs
PORTD & = ~ _BV (PD1); / / set to "0" (low level) on output PD1
}                      / / Closing bracket of the main program

Now try to blink an LED connected as shown in the left figure. For this we use the delay function _delay_ms ().

Function _delay_ms () creates a delay, depending on the argument passed to it, expressed in milliseconds (one second in 1000 milliseconds). The maximum delay can be up to 262.14 milliseconds. If the user submits the function value is greater than 262.14, it will automatically reduce the resolution to 1 / 10 milliseconds, which provides a delay of up to 6.5535 seconds.

Function _delay_ms () is contained in a file delay.h, so we will need to attach this file to the program. In addition to the normal operation of this feature, you must specify the rate at which works a microcontroller, in hertz.

/*************************************
EXAMPLE flashing LED
Connection example in Figure 1
**************************************/

# define F_CPU 1000000UL / / specify the frequency in hertz

# include <avr/io.h>
# include <util/delay.h>

int main (void) {        / / start the main program

DDRD = 0xff;             / / D port pins are configured as outputs

PORTD | = _BV (PD1);     / / set to "1" (high level) on output PD1,
                         / / LED light

_delay_ms (500)          / / wait for 0.5 seconds.

PORTD & = ~ _BV (PD1);   / / set to "0" (low level) on output PD1,
/ / LED to extinguish

_delay_ms (500)          / / wait for 0.5 seconds.

PORTD | = _BV (PD1);     / / set to "1" (high level) on output PD1,
/ / LED light

_delay_ms (500)          / / wait for 0.5 seconds.

PORTD & = ~ _BV (PD1);   / / set to "0" (low level) on output PD1,
                         / / LED to extinguish

}                        / / Closing bracket of the main program

A series of LED flashes, it will be very short. In order to make continuous flashing, you can organize an infinite loop by using an unconditional jump “goto”. The goto statement jumps to the place of the program, the designated mark. Name tags can not contain spaces. After the name tags put a colon. Between the tag name and a colon should be no spaces.

/************************************************* ******
Examples of infinitely flashing LED
Connection example in Figure 1
************************************************** ******/

# define F_CPU 1000000UL / / specify the frequency in hertz

# include <avr/io.h>
# include <util/delay.h>

int main (void) {        / / start the main program

DDRD = 0xff;             / / D port pins are configured as outputs

start:                   / / label for the command goto start

PORTD | = _BV (PD1);     / / set to "1" (high level) on output PD1,
/ / LED light

_delay_ms (250)          / / wait 0.25 seconds.

PORTD & = ~ _BV (PD1);   / / set to "0" (low level) on output PD1,
/ / LED to extinguish

_delay_ms (250)          / / wait 0.25 seconds.

goto start;              / / go to label start

}                        / / Closing bracket of the main program
Gravatar Image
Robotics Technology , Tutorial and News

Leave a Reply

Your email address will not be published. Required fields are marked *