0% found this document useful (0 votes)
62 views

IO Port Programming

This document provides an overview of AVR programming in C. It discusses why C is preferable to assembly for AVR programming, common C data types used for AVRs, I/O port pins and registers, and examples of using logic, shift, and assignment operators to manipulate individual port pins for operations like toggling or inverting bits. The document is intended as a reference for basic AVR I/O programming concepts in C.

Uploaded by

hefawoj62
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

IO Port Programming

This document provides an overview of AVR programming in C. It discusses why C is preferable to assembly for AVR programming, common C data types used for AVRs, I/O port pins and registers, and examples of using logic, shift, and assignment operators to manipulate individual port pins for operations like toggling or inverting bits. The document is intended as a reference for basic AVR I/O programming concepts in C.

Uploaded by

hefawoj62
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

AVR Programming in C

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:

Data type Size in Bits Data range/usage


Unsigned char 8 0 to 255
char 8 -128 to +128
Unsigned int 16 0 to 65,535
int 16 -32,768 to +32,767
Unsigned long 32 0 to 4,294,967,295
long 32 -2,147,483,648 to +2,147,483,648

float 32 ±1.175e-38 to ±3.402e38


double 32 ±1.175e-38 to ±3.402e38
I/O Port pins and their functions
• In AVR microcontroller family, there are many ports available for
I/O operations, depending on which family microcontroller we
choose. For the ATmega32 40-pin chip, 32 Pins are available for I/O
operation. The four ports PORTA, PORTB, PORTC, and PORTD are
programmed for performing desired operation.
• The number of ports in AVR family varies depending on number of
pins available on chip. The 8-pin AVR has port B only, while the 64-
pin version has ports A to ports F, and the 100-pin AVR has ports A
to ports L.
The table showing Numbers of ports in some AVR family members is shown
below:

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:

Each port has 3 control I/O registers associated with it


DDRx register(Data Direction Register)

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:

For reading the data from port A:

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:

Input Input AND OR XOR Inverter


A B A&B A|B A^B Y = ~B
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1
1 1 1 1 0
Bit wise shift Operators in C:

Operation Symbol Format of shift operation


Shift right >> Data>>number of bits to be shifted right
Shift left << Data<<number of bits to be shifted left

The following shows some example of shift operators in C:


I. 0b00010000>>3 = 0b00000010 //shifting right 3 times
II. 0b00010000<<3 = 0b10000000 //shifting left 3 times
III. 1<<3 = 0b00001000 //shifting left 3 times
Compound Assignment Operator in C:

Operation Abbreviated Expression Equal C expression


And assignment a&=b a = a&b
Or assignment a|=b a = a|b
Using operators:

• To set/high a specific bit, ‘|’ operator is used which is symbol of ‘OR’ . It


makes that particular bit as 1.
• To set/high a specific bit: Register| = (1<<BIT)
• Example: Make the bit 5 of port C high.

• Solution: PORTC| = (1<<PINC5)


• Depicted as: PORTC = PORTC | 0b00100000
• Depicted as: PORTC| = (1<<5)
• To clear/low a specific bit, ‘&’ operator is used which is symbol of ‘AND’ . It
makes that particular bit as 0.
• To clear/low a specific bit: Register& = (~ (1<<BIT))
• Example: Make the bit 5 of port C low.

• Solution: PORTC& = ~(1<<PINC5)


• Depicted as : PORTC = PORTC & 0b11011111
• Depicted as : PORTC& = (~(1<<5))
• To toggle a specific bit, ‘^’ operator is used which is symbol of ‘XOR’ . It
makes that particular bit as 0 if it was 1 previously and vice versa.
• To toggle a specific bit: Register^ = (1<<BIT)
• Example: Toggle the bit 5 of port C without disturbing rest of the pins.

• Solution: PORTC^ = (1<<PINC5)


• Depicted as : PORTC = PORTC ^ 0b00100000
• Depicted as : PORTC^ = (1<<5)
• To invert a bit string, ‘ ~ ’ operator is used which is symbol of ‘NOT’ or
‘Inverter’ .
• To invert a bit string: Register = ~ (Register)
• Example: Toggle all the pins of port C continuously.
• Solution: PORTC = ~(PORTC)
“A Book is a Dream
You Hold in Your Hand”

Neil Gaiman

You might also like