Microcontroller and Embedded systems Tutorials on: Using the AVR Studio 6 Simulator and Debugger Input/output Ports First we have to discuss about I/O ports. Generally AVR microcontrollers have four I/O ports named as PORTA, PORTB, PORTC, PORTD up to PORTG (ATMEGA128). Take example as ATMEGA8 or ATMEGA 16 or ATMEGA32 microcontrollers, these are any having four I/O ports and each port having 8 I/O lines. These lines are bi-directional means we can use these I/O lines either input or output. In addition to each this pin has some of other functions like ADC, timers, interrupts, serial communication pins and other pins. To perform any operation with general purpose I/O (GPIO) pins there is a need to configure three registers. Those registers named as DDRx, PORTx, PINx (here ‘x’ indicates the name of the register A or B or C or D). Each registers of these three registers are 8-bit registers means generally each port has 8 pins as like each register has 8-bits and each pin refers on bit of register. If we want to configure any pin of the port we can configure the corresponding bits of all three registers. In this article I am taking example as ATMEGA128 microcontroller. Now consider pins 35-42 of the microcontroller, if we want to configure these 8-pins, there is a need of configure corresponding three registers of 8-bits. Here explaining clearly, in ATMEGA128 pin 35-42 refers PORTC. So if we want to configure PORTC, we need to configure DDRC, PORTC and PINC registers These 8-bits are divided into two 4-bit groups and named as lower nibbles upper nibbles. 0-3 bits are called as lower nibbles and 4-7 bits are called as upper nibbles.
Full Description of how to use AVR studio 6 for programming AVR devices
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Microcontroller and Embedded systems
Tutorials on:
Using the AVR Studio 6 Simulator and Debugger
Input/output Ports
First we have to discuss about I/O ports. Generally AVR microcontrollers have four I/O ports named as
PORTA, PORTB, PORTC, PORTD up to PORTG (ATMEGA128). Take example as ATMEGA8 or
ATMEGA 16 or ATMEGA32 microcontrollers, these are any having four I/O ports and each port having 8 I/O lines. These lines are bi-directional means we can use these I/O lines either input or output. In
addition to each this pin has some of other functions like ADC, timers, interrupts, serial communication
pins and other pins. To perform any operation with general purpose I/O (GPIO) pins there is a need to configure three registers. Those registers named as DDRx, PORTx, PINx (here ‘x’ indicates the name of
the register A or B or C or D).
Each registers of these three registers are 8-bit registers means generally each port has 8 pins as like each
register has 8-bits and each pin refers on bit of register. If we want to configure any pin of the port we can configure the corresponding bits of all three registers. In this article I am taking example as ATMEGA128
microcontroller. Now consider pins 35-42 of the microcontroller, if we want to configure these 8-pins,
there is a need of configure corresponding three registers of 8-bits. Here explaining clearly, in
ATMEGA128 pin 35-42 refers PORTC. So if we want to configure PORTC, we need to configure DDRC,
PORTC and PINC registers
These 8-bits are divided into two 4-bit groups and named as lower nibbles upper nibbles. 0-3 bits are
called as lower nibbles and 4-7 bits are called as upper nibbles.
Configuring the PORT:
If we want to configure one port; there is a need to configure corresponding three registers of the port. So
now we are configuring the PORTC, so we need to configure DDRC, PORTC and PINC registers as described below:
DDRx register: The name of the register is Data Direction Register. The name only indicates complete use of the
register that is data direction. There are only two directions. Those are controller to module and module to
controller. If data direction is from module to controller that is input, if data is passing from controller to
module that is output. So the two directions are input direction and output direction
This register is used to assign the pin of the port as either input direction or output direction. In
this register the port and pin indicated as “DDRxn”, here ‘x’ indicates the port name and ‘n’
indicates the pin number. For example pin 38 refers to 4th pin of PORTC is defined as DDRC4.
Generally these GPIO pins are digital I/O pins that mean these are having only two logics as
logic0 and logic1. Same like these DDR also. If DDRxn is written as logic one, the pin is
configured as output pin. If DDRxn is written as logic zero, the pin is configured as input pin.
Up to here we are discussing as logic 0 and logic 1, but how to know what is logic 0 and what is
logic 1, how both are differs. Generally most of the microcontrollers drives the 5V, and before
setting the pin either input or output there is 5V of potential at each GPIO pin of controller.
Microcontrollers have some threshold voltage levels. The threshold level is indicated as half of
the driving voltage. If the driving voltage of controller is 5V, the threshold voltage of the
controller is 2.5V. So the controller knows that if voltage level is below the threshold level, it
sense as logic zero and above the threshold level it sense as logic one.
So in this sense logic zero means DDRxn configured as input pin and logic one means DDRxn
configured as output pin. If pin is configured as input, the internal pull-up resistors will be on. If
we want to turn-off the pull-up resistors, the pin has to be configured as output.
DDRx register
Example
To configure some bits of PORTC as inputs and some bits as outputs where configure 0,2,4,6
pins as inputs and 1,3,5,7 pins as outputs.
Solution
Generally the register initial value is zero means register bits are configured to input. So change
require bits to logic one to change as output.
Generally we are using shift operation to assign value to bit of register. Take bit0 of DDRC, the
initial value of the bit is zero. If we want to change the bit to one, just we can use simple shift
operation as 1<<DDRC0. If we want to change remaining bits, that is also same but OR
operation is performed between the bits. Now in our example the code is as follows: