Nothing Special   »   [go: up one dir, main page]

Comsats University Microprocessor Systems & Interfacing EEE-342

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

COMSATS UNIVERSITY

MICROPROCESSOR SYSTEMS & INTERFACING


EEE-342

Lab # 02 Introduction to AVR Microcontroller Hardware Circuitry


and Digital I/O Ports

Name

Registration
Number

Instructor’s Name MA’AM ASMA RAMAY


Lab # 02 Introduction to AVR Microcontroller Hardware Circuitry
and Digital I/O Ports

OBJECTIVES:
 Understand the minimal circuit required to start using a microcontroller
 Learn to program (download code to program memory of) a microcontroller using
Arduino board.
 To understand and use digital I/O ports of AVR microcontroller

REQUIRED TOOLS:

SOFTWARE TOOLS:
 Atmel Studio (Version 7) or AVR Studio (Version 4)
 Proteus ISIS
 Arduino IDE
 AVRDUDESS

HARDWARE TOOLS:

PRE-LAB

2.1 ARDUINO NANO


Arduino is open-source electronics prototyping platform based on flexible, easy-to-use hardware
and software. Arduino Nano is a surface mount breadboard embedded version with integrated
USB. It is a smallest, complete, and breadboard friendly Arduino Nano is programmed using the
Arduino Software (IDE).

ARDUINO NANO SPECIFICATIONS:


Microcontroller Atmel ATmega328

Operating Voltage 5V

Input Voltage 7-12 V


Digital I/O Pins 14 (of which 6 provide PWM output)

Analog Input Pins 8 DC Current per I/O Pin 40 mA Flash Memory 32 KB (of which 2KB used
by bootloader) SRAM 2 KB EEPROM 1 KB Clock Speed 16 MHz

ARDUINO NANO FEATURES:


• Automatic reset during program download
• Power OK LED
• TX, RX, L LED
• Auto sensing/switching power input
• Small mini-B USB for programming and serial monitor
• ICSP header for direct program download
• Standard 0.1” spacing DIP (breadboard friendly)
• Manual reset switch
Figure 2.1 shows pinout of Arduino Nano and Figure 2.2 shows schematics of Arduino Nano.

FIGURE 2.1 PINOUT OF ARDUINO NANO


FIGURE 2.2 SCHEMATICS OF ARDUINO NANO
2.1.1 HARDWARE PROGRAMMING AVR MICROCONTROLLER USING ARDUINO:
AVRDUDE - AVR Downloader Uploader - can be used to upload the Hex file generated by
software programming in AVR microcontrollers. It is a program for downloading and uploading
the on-chip memories of Atmel's AVR microcontrollers. It can program the Flash and EEPROM,
and when supported by the serial programming protocol, it can program fuse and lock bits.
Avrdude is a command line program, AVRDUDESS is a GUI for AVRDUDE. It supports all
programmers and MCUs that AVRDUDE supports.

2.2 INTRODUCTION TO AVR DIGITAL INPUT/OUTPUT:


Digital I/O is the most fundamental mode of connecting a microcontroller to the outside world.
Atmel AVR ATMega 328P provides 23 I/O pins to interact with outside world in the form of
logic values. These pins are usually organised in groups and referred to as a port. Port B and Port
D have 8 pins each whereas Port C consists of 7 pins.
In Arduino nano, 20 pins are available for digital I/O. PC6 is connected with reset circuit and
PB6 and PB7 are connected with crystal oscillator. So these 3 pins are not available for digital
I/O in Arduino nano.
Each of the AVR Digital I/O port is associated with three I/O registers.
 DDRx
 PORTx
 PINx
Where x is the port B, C or D.

2.2.1 DDRX - PORT X DATA DIRECTION REGISTER:


DDRx is an 8-bit register which stores configuration information for the pins of Portx. The bits
in this register set the data direction of the individual pins of a port. The direction of each pin can
be input or output. Writing a 1 in the pin location in the DDRx makes the physical pin of that
port an output pin and writing a 0 makes that pin an input pin.
DDRD= 0x00; //Configures Port D as input
DDRB=0xFF; //Configures Port B as output

The prefix 0x signifies hexadecimal number. 0xFF means decimal 255 or binary 11111111
which means all the bits in register are at high logic level. 0x00 means all the bits in the register
are at low logic level. Each physical pin of a port is configured independently and thus a port can
have some of its pins configured as input and the others as output pins. For example:

DDRD = (1<<0)|(1<<4)|(1<<5)|(1<<7);
This is equivalent to

DDRD = 0b10110001;

or
DDRD=0xB1;

In the above example, port D is initialized such that the pins PD0, PD4, PD5 and PD7 are output
pins whereas pins PD1, PD2, PD3 and PD6 are input pins.
We can change the value of a single bit without disturbing the previous value of all the other bits
of the register. For example:

DDRD |= (1<<5);

This will set bit 5 of DDRD while retaining the values of all other bits.

Similarly,

DDRD &= ~(1<<5);


This will clear bit 5 of DDRD while retaining the values of all other bits.

2.2.2 PORTX- PORT X DATA REGISTER:

PORTx is an 8-bit register which stores the logic values that are to be transferred on the physical
pins of Portx if the pins are configured as output pins. In short, once the direction of DDRx
register is defined as an output pins, we need to set them to give an output of HIGH logic or
LOW logic. So to write values to a port, you need to write the values to the “PORT” register of
that port.

PORTD = (1 << 0)|(1 << 3)|(1 << 6);


This is equivalent to
PORTD = 0b01001001;
Or

PORTD = 0x49;

2.2.3 PINX- PORTX INPUT PINS REGISTER:

PINx is an 8-bit register that stores the logic value, the current state, of the physical pins on
Portx. When you set any port as input you have to read its status using this register.

PINB = (1 << 0)|(1 << 3)|(1 << 6);

This is equivalent to
PINB = 0b01001001;

Or
PINB = 0x49;

IN LAB-TASKS

Task-1: Write the given code in AVR studio and compile it. Use AVR dudes to upload the
hex file on the controller. Now see, the led will blink when connected to the controller.

Code:

#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
int main()
{
DDRB = 0b11111111;
while(1)
{
PORTB = 0b00001000;
_delay_ms(1000);
PORTB = 0b00000000;
_delay_ms(1000);

}}
Task 2: Switches are connected to one port of ATmega328p for input and LEDs are
connected to another port for output. Using these, perform a task assigned by your lab
instructor. Students should write the code for given task, simulate it on proteus and then
implement it on hardware.
/*This code will configure Port B for output and then toggle Port B pin 3 with a delay of
1000ms*/
#include <avr/io.h> /*This header file includes the apropriate I/O definitions for the device
*/
#define F_CPU 16000000UL //XTAL Frequency =16MHz
#include <util/delay.h> /*This header file is required for generating delays*/
int main ()
{ // Write your code here
DDRB = 0b00000000; //Configure Port B as Output
DDRD = 0b11111111;
while (1) //This loop will run forever
{unsigned char x; x=PINB;
if(x== 0b00000000)
PORTD = 0b00001000;
else if (x==0b0000001)
PORTD=0b00010000;
else if (x==0b00000010)
PORTD=0b10000000 ;
else if (x==0b00000011)
PORTD=0b10001100 ;
else
PORTD=0b00000000 ;
}}

CIRCUIT IN PROTEUS

CRITICAL ANALYSIS / CONCLUSION


The objectives of this were to learn about:

AVR DIGITAL INPUT/OUTPUT: Atmel AVR ATMega 328P provides 23 I/O pins to interact
with outside world in the form of logic values. Port B and Port D have 8 pins each whereas Port C
consists of 7 pins. In Arduino nano, 20 pins are available for digital I/O.
Interfacing of devices was done with microcontroller and in turn program microcontroller in
order to perform specified tasks.

TASK 1 OUTCOMES: In this task given code was built on AVR studio and later simulated on
proteus. Afterwards .Hex file was burned to ATMega328P. The code was implemented on
hardware using AVRDUDUESS.

TASK 2 OUTCOMES: In this task given code was built on AVR studio and later simulated on
proteus. The code configured Port B for output and then toggle Port B pin 3 with a delay of
1000ms. Afterwards .Hex file was burned to ATMega328P. The code was implemented on
hardware using AVRDUDUESS.

You might also like