You are on page 1of 33

AVR Microcontroller

Course

DAY 4
Overview

C Language Review
What is AVR
AVR ATmega 16/32
Pin Description
Digital Input Output
I/O Registers
LED Blinking Project
Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag
AVR ATmega 16/32

Digital Input Output:


 Digital I/O is the main characteristic of
microcontrollers which mean the ability to
directly monitor and control hardware
 Practically all microcontrollers have at least 1-2
digital I/O pins that can be directly connected to
hardware (within the electrical limits of the
controller)

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


AVR ATmega 16/32

Digital Input Output:


 I/O pins are generally grouped into ports of 8
pins, which can be accessed with a single byte
access.
 Pins can either be input only, output only, or most
commonly, bidirectional means both input and
output.

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


AVR ATmega 16/32

Digital Input Output:


 Apart from digital I/O capabilities, most pins
have one or more alternate functions to save pins
and keep the chip small.
 All other modules of the controller which require
I/O pins, like the analog module or the timer, use
in fact alternate functions of the digital I/O pins.

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


AVR ATmega 16/32

Digital Input Output:


 The application programmer can select which
function should be used for the pin by enabling
the functionality within the appropriate module.
 If a pin is used for the analog module, then it is
lost for digital I/O and vice versa, so the
hardware designer must choose carefully which
pins to use for which functions

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


AVR ATmega 16/32

Digital Input Output:


 All port pins have individually selectable pull-up
resistors with a supply-voltage invariant
resistance.
 All I/O pins have protection diodes to both VCC
and Ground as indicated in Figure

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


AVR ATmega 16/32

Digital Input Output:

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


AVR ATmega 16/32

Digital Input Output:


 Three I/O memory address locations are
allocated for each port, one each for the
 Data Direction Register  DDRx
 Data Register  PORTx
 Port Input Pins  PINx

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag


Overview

C Language Review
What is AVR
AVR ATmega 16/32
Pin Description
Digital Input Output
I/O Registers
LED Blinking Project
Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag
AVR ATmega 16/32

I/O Register Description:


Data Direction Register
Each bidirectional port has its own DDR,
which contains one bit for each pin of the port.
The functionality of a pin (input or output) is
determined by clearing or setting its bit in the
DDR.
Input  0
Output 1

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


Data Direction Register
This register is used to select the direction
of this pin.
To define a port as an Input port, so DDRx
is written logic zero( 0 ).
To define port as an Output port,so DDRx
is written logic one(1).

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


What is Microcontroller

I/O Register Description:


Data Direction Register
Different pins of a port may be configured
differently, so it is perfectly okay to have three
pins configured to output and use the other
five as inputs.
After a reset, the DDR bits are generally
initialized to input. Reading the register
returns its value.

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


Data Direction Register
Examples:
DDRB=0xFF; define port B as output port
DDRC=0x00; define port B as input port
DDRD=0b00001111; define the first 4 bits
are defined as output and the remaining
are defined as input

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register:
It is used to control the output data of port
and generate output voltage level
according to sent data logic 1  5 volts
logic 0  0 volts
Data can be directed to all the port directly
or to one bit in certain port

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register:
This register is used in two modes
One for input defined port
One for output defined port

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register:
If port is defined as input:
If PORT is written logic one the pull-up
resistor is activated.
If PORT is written logic zero the pull-up
resistor is deactivated or the pin has to be
configured as an output pin

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register:
In addition, the Pull-up Disable PUD bit
in SFIOR control the pull-up function for
all pins

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register:
When this bit is written to one, the pull-
ups in the I/O ports are disabled even if the
DDxn and PORTxn Registers are
configured to enable the pull-ups
When this bit written to zero so it is
enabled according to DDxn and PORTxn

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PULL UP Resistor:

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register:
If port is defined as output:
If PORT is written logic one the port pin is
driven high (one).
If PORT is written logic zero the port pin
is driven low (zero).

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register
Examples:
DDRB=0xFF; define port B as output port
DDRC=0x00; define port B as input port
PORTC=0xFF; activate pull up resistor for
all pins in input port C

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT Register
Examples:
PORTB=0xA5; port B will have output
data as 10100101
PORTB=0x86; port B will have output
data as 10000110

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PIN Register
It is used to read the data on the port pin
regardless the direction of the port whether
input or output
The Port Input Pins I/O location (PINx)is
read only, while the Data Register and the
Data Direction Register are read/write.

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PIN Register
Examples:
PORTB=0xA5; port B will have output
data as 10100101
PORTB=0x86; port B will have output
data as 10000110

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT A specific Registers

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT B specific Registers

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT C specific Registers

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


AVR ATmega 16/32

I/O Register Description:


PORT D specific Registers

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


Overview

C Language Review
What is AVR
AVR ATmega 16/32
Digital Input Output
I/O Registers
Pin Description
LED Blinking Project
Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag
LED Blinking Project

LED Blinking Project


 Now lets design our first project of AVR which
is blinking LED
 We need to connect a led with resistance to a pin
in PORTA as an example and make it blink and
control this blinking time

Copy right reserved Confidential Material | Arduino Course by Eng.Ahmed Serag


Questions

Copy right reserved Confidential Material | AVR Course by Eng.Ahmed Serag

You might also like