Professional Documents
Culture Documents
I/OPortOperationsinAVRmaxEmbedded
maxEmbedded Index
Categories
Home
Home
Atmel AVR
mE Index
Tools
Getting Started
SearchThisSite...
Atmel AVR
Electronics
More
About
Search maxEmbedded
Search for:
Search
Popular
MON
20
Recent
Random
FRI
24
Register
Okay, now I hope you are familiar with the term register. If not, then you
must have heard of it. Basically, a processor register is a memory space
within the CPU itself so that they can be accessed very frequently and fast.
These registers are linked with the operation of the MCU. Lets consider the
following memory space.
TUE
06
RF Module Interfacing
without Microcontrollers
Posted by Mayank in Electronics
THU
16
THU
20
Making an RF Car
Subscribe to maxEmbedded
http://maxembedded.com/2011/06/portoperationsinavr/
1/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
Subscribe to maxEmbedded
EmailAddress
Subscribe
Here, you can see that I have represented 8 bits together to form a memory
of 1 byte. Note the sequence in which the bits are numbered. They are
numbered as 7, 6, 5, , 1, 0. This is because the bits are numbered from the
MaxEmbe
4.2Klikes
Least Signicant Bit (LSB) to the Most Signicant Bit (MSB). From the
knowledge of digital logic, we know that the last bit is the LSB whereas the
rst bit is the MSB. Hence, Bit 0 = LSB and Bit 7 = MSB.
LikePage
ADMUX Register
Here, the name of the register is ADMUX (dont worry about the name, we
will discuss it later). Also, note that each bit also has a name and an initial
value. Each bit of the register can have a value of either 0 or 1 (initially it is
0). Now suppose, I write
http://maxembedded.com/2011/06/portoperationsinavr/
2/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
ADMUX=0b01000111;
This means that the ADMUX register has been updated as follows:
DDRx Register
As I mentioned earlier, the GPIO pins are the digital I/O pins i.e. they can act
as both input and output. Now, how do we know that the pin is an output
pin or input? The DDRx (Data Direction Register) helps in it.
DDRx initializes the port. Have a look at its bit structure.
http://maxembedded.com/2011/06/portoperationsinavr/
3/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
DDRx Register
PORTx Register
http://maxembedded.com/2011/06/portoperationsinavr/
4/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
PORTx Register
The PORTx register determines whether the output should be HIGH or LOW
of the output pins. In simplied terms, once the DDRx register has dened
the output pins, we need to set them to give an output of HIGH or LOW. The
PORTx register goes as follows.
PORTx Register
PINx Register
The PINx register gets the reading from the input pins of the MCU. The
register goes as follows:
http://maxembedded.com/2011/06/portoperationsinavr/
5/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
PINx Register
The register declaration procedure stands the same. Also note that the
names of the bits of PORTx and PINx registers are same.
Now, lets consider the following statements:
DDRC=0b10110001
PINC=0b01001011
INPUT=0b01001011/*ThisisnotaCexecutableline,thislineisjustforexplanation*/
Here, the highlighted bits correspond to the pins that are initialized as input
by the DDRx. In the second line, the PINx register is dened. Well, this line is
just to explain the concept, practically, we always use PINx as a condition
(like in IF or in WHILE loop). As per the second statement, the PINx
command reads the values only at the input pins.
Now, consider the following set of statements:
DDRC=0b10110001
PINC=0b01011010
INPUT=0b01001010/*ThisisnotaCexecutableline,thislineisjustforexplanation*/
Here, you can compare it with the example I gave for PORTx. Since the
magenta-highlighted bit is an output pin, PINx cannot change its properties.
Hence, in general, PINx cannot modify the properties of a pin which is
initialized as output by DDRx and vice versa.
//letsassumea4VsupplycomestoPORTC.6andVcc=5V
if(PINC==0b01000000)
PORTC=0x0B;
else
PORTC=0x00;
Code Explained:
DDRC = 0x0F; is equivalent to DDRC = 0b00001111; This means that the
pins PC0PC3 are output pins (can be manipulated using PORTC) and
pins PC4PC7 are input pins (whose levels determine the value of PINC).
PORTC = 0x0C; is equivalent to PORTC = 0b00001100; This means that the
pins PC2 and PC3 have a HIGH voltage (Vcc = 5V) and pins PC0 and PC1
have LOW voltage (0V). The other pins have low voltage by default.
if (PINC = 0b01000000) checks the input voltage at pin PC6. Since it is
mentioned in the comment that a 4V is supplied to PORTC.6 (same as pin
PC6), this condition is true (as 4 > 2.5, where 2.5V is the threshold, 5/2 =
http://maxembedded.com/2011/06/portoperationsinavr/
6/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
2.5).
Since the if condition is true, PORTC = 0x0B; is executed.
If the if condition is not satised, PORTC = 0x00; will be executed.
We can also put it inside a while loop to run it continuously i.e. it always
checks for the voltage at pin PC6, and the outputs at PC0, PC1 and PC3 go
high only if the voltage at PC6 is greater than 4V.
DDRC=0x0F;
while(1)
{
//aconditionof4VsupplytoPORTC.6andVcc=5V
if(PINC==0b01000000)
PORTC=0x0B;
else
PORTC=0x00;
}
To learn how to code and simulate using AVR Studio 5, visit this page.
So, here we end up with the port operations that can be performed with an
AVR MCU. Though the concept has been explained using ATMEGA16/32, the
concepts are equally good for any AVR MCU! Just go through the datasheet
in order to get an idea of the registers.
Thank you for reading this! The comments box is down below! ;)
Loved it? Share it!
47
More
Like this:
Like
3bloggerslikethis.
Related
In "Getting Started"
In "Atmel AVR"
AVR Basics
In "Atmel AVR"
168 Comments
Older Comments
sahil January 27, 2016
REPLY
7/9
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
REPLY
Older Comments
Leave a Reply
Enteryouremailaddress
SubscribeNow
18
Shares
16
Copyright
Tags
accelerometer adapter
adc avr
interrupts
basics
counters cv
8/1/2016
I/OPortOperationsinAVRmaxEmbedded
counters cv
basics lter circuit i2c ir sensor matlab
msp430 basics opencv pcb design
power supply printed circuit boards pwm
raspberry pi rf communication rf
Unported License.
Copyright 2011-2014 by
maxEmbedded. Some rights
reserved.
Home
Copyright
Privacy Policy
Credits
Sitemap
Contact
maxEmbedded is a free and open source platform to share knowledge and learn about the concepts of robotics, embedded systems and computer vision. People from
around the world who are enthusiastic about these topics and willing to support the open source community are invited to share their information, knowledge and
expertise by means of written tutorials and videos on the website.
SubscribeNow
PoweredbySumoMe
Enteryouremailaddress
SubscribeNow
18
Shares
16
http://maxembedded.com/2011/06/portoperationsinavr/
9/9