IO Port Programming
IO Port Programming
S u m a i y a Ta s n i m
Lecturer(Provisional)
Department of CSE
Va r e n d r a U n i v e r s i t y
CSE-425
Lecture-2
Reference: Eighth chapter of textbook
Acknowledgment: Some websites and Shayban Nasif Tonmoy Sir
Sections
• AVR in C
• AVR I/O programming
Why program the AVR in C?
Assembly language produces a hex file that is much smaller than C, but
programming in Assembly is tedious and time consuming while:
• It is easier and less time consuming to write in C
• C is easier to modify and update
• One can use code available in function libraries
• C code is portable to other microcontrollers with little or no modification
C data types for AVR
Some data types widely used by C compilers:
Here, X indicates that the port is available. The 40-pin AVR has four ports for
using any of the ports as an input or output port, it must be accordingly
programmed. In AVR microcontroller not all ports have 8 pins. For example:-in
the ATmega8, Port C has 7 pins.
AVR Registers
• Each I/O port in AVR microcontroller has three registers associated with it.
They are designated as PORTx, DDRx and PINx. For example: - in case of Port B
we have PORTB, DDRB, and PINB.
• Each of I/O registers is 8 bits wide, and each port has a maximum of 8 pins,
therefore each bit of I/O registers affects one of the pins.
• For accessing I/O registers associated with the ports the common relationship
between the registers and the pins of AVR microcontroller is used.
The relation between the Registers and the Pins of AVR is shown below:
Data Direction Register configures the data direction of port pins. These registers are
used for determining whether port pins will be used for input or output. On writing 1 to a
bit in DDRx makes corresponding port pin as output, while writing 0 to a bit in DDRx
makes corresponding port pin as input.
For example:
For making all pins of port A as output pins: DDRA = 0b11111111 (in binary) or DDRA = 0xFF (in hexa)
For making all pins of port A as input pins: DDRA = 0b00000000 (in binary) or DDRA = 0x00 (in hexa)
For making lower nibble of port B as output and higher nibble as input: DDRA = 0b00001111
PORTx register(Pin Output Register)
The PORTx bits in the PORTx register have two functions. They can control the output state
of a pin and the setup of an input pin.
As an Output:
When port is configured as output then PORTx register is used. When we set bits in DDRx to
1, corresponding pins becomes output pins. Now we can write the data into respective bits
in PORTx register. This will immediately change the output state of pins according to data we
have written on the ports.
If a '1' is written to the bit when the pin is configured as an output pin, the port pin is driven
high. If a ‘0’ is written to the bit when the pin is configured as an output pin, the port pin is
driven low. For example:
To output 0xFF data on port B: To output 0xFF data on port B:
DDRB = 0b11111111; //set all the pins of port B as outputs DDRB = 0b11111111; //set all the pins of port B as outputs
PORTB = 0b11111111; //all the pins are made high PORTB = 0b00000000; //all the pins are made low
As an Input:
If a '1' is written to the bit when the pin is configured as an input pin, the pull-up resistor is
activated. If a ‘0’ is written to the bit when the pin is configured as an input pin, the port
pin is tri-stated.
Note: While using on chip Analog to Digital Converter (ADC), ADC port pins must be used
as tri stated input.
To make port B as input with pull-ups enabled:To make port B as tri stated input:
DDRB = 0x00; //use port B as input DDRB = 0x00; //use port B as input
PORTB = 0xFF; //enable all pull ups PORTB = 0x00; //Disable pull-
ups register and make it tri state
PINx register(Pin Input Register)
PINx register used to read the data from port pins. If port is made output, then reading
PINx register will give a data that has been output on port pins.
There are two input modes. Either we can use port pins as internal pull up or as tri stated
inputs as it has been explained before. For example:
DDRA = 0x00; //Set port A as input
PORTA= 0xFF; //enable all pull-ups
x = PINA; //Read contents/data from the pins of port A
Practice Problems:
I. Write an AVR C program to toggle all the bits of PORT B 200 times.
II. Write an AVR C program to toggle the third bit of port B continuously
with a 100 mili-second delay.
III. Write an AVR C program to toggle 4 bits of port C continuously with a
70% duty cycle.
IV. Leds are connected to the pins of port B. Write an AVR code to show
from 0-255 on the leds.
V. Leds are connected to port B. Write an AVR program that shows the leds
term on sequentially a 100 millisecond delay.
Logic operations in C:
Logic operators:
Neil Gaiman