AVR Lab Manual
AVR Lab Manual
STUDENT VERSION
for
MICROCONTROLLER LAB
(SIXTH SEMESTER, COURSE CODE: 6139)
PROGRAM OUTCOMES
PO1: Basic and Discipline specific knowledge: Apply knowledge of basic
mathematics, science and engineering fundamentals and engineering
specialization to solve the engineering problems.
PO2: Problem analysis: Identify and analyse well-defined engineering problems
using codified standard methods.
PO3: Design/ development of solutions: Design solutions for well-defined
technical problems and assist with the design of systems components or processes
to meet specified needs.
PO4: Engineering Tools, Experimentation and Testing: Apply modern
engineering tools and appropriate technique to conduct standard tests and
measurements.
PO5: Engineering practices for society, sustainability and environment:
Apply appropriate technology in context of society, sustainability, environment
and ethical practices.
PO6: Project Management: Use engineering management principles
individually, as a team member or a leader to manage projects and effectively
communicate about well-defined engineering activities.
PO7: Life-long learning: Ability to analyse individual needs and engage in
updating in the context of technological changes.
Rough record and Fair record are needed to record the experiments conducted
in the laboratory. Rough records are needed to be certified immediately on
completion of the experiment. Fair records are due at the beginning of the next lab
period. Fair records must be submitted as neat, legible, and complete.
In the fair record, the index page should be filled properly by writing the
corresponding experiment number, experiment name , date on which it was done and
the page number.
1. Title: The title of the experiment should be written in the page in capital letters.
2.Exp No: And Date: In the top margin, experiment number and date should be
written.
3. Aim: The purpose of the experiment should be written clearly.
4.Principle/Theory: Simple algorithm should be written
5. Procedure: Steps for doing the experiment.
6. Program: Simple working of the algorithm should be written.
7. Results: The results of the experiment must be summarized in writing and should
be fulfilling the aim.
SAFETY PROCEDURES
Problem Statement:
The safety instructions are presented to the attention of the students as a mean of
preventing accidents while performing experiments and activities in Software lab of
the department .The purpose is to draw attention to the risks involved in lab activities
to prevent human suffering and damage to equipment.
Inform the lab in charge about dangerous conditions and faults in the lab or nearby
environment.
Do not do any action that may harm people or equipment in the lab.
Do not misuse any of the tools or instruments belong to the lab.
Strict discipline should be maintained in the laboratory.
Turn off cell phones before entering the lab.
At the end and beginning of laboratory, follow 5S procedures and leave the work
table clean and tidy.
Electrical Safety:
Consult Electrical Engineering section available in the campus for electrical safety
queries.
The lab equipment is powered from electrical sockets installed on the tables.
Do not use equipment that is powered from a damaged socket.
Do not use equipment that is powered from flexible cable with damaged insulation
or if it‟s plug is not assembled properly.
Emergency Switches:
The laboratory has circuit breakers, which is located in the main panel. Identify the
place.
In an emergency condition, switch off circuit breakers immediately.
Result:
Familiarization of safety precautions performed
AIM:
To Familiarize Installing AVR Studio.
PROCEDURE:
Any microcontroller requires software called Integrated
Development Environment – IDE for writing program. An IDE is the handy software
that acts as text editor, debugger, assembler and compiler all in one package. A text
editor is simply like note pad software for writing the code. The text editor comes as
a package along with the IDE. Here in this book whatever program we write for
microcontroller will be called as source code or simply code. An assembler interprets
a code written in assembly language to machine code. The compiler converts codes
written in C into machine codes. In our case, the compiler is called cross compiler. A
cross compiler converts instructions into machine code or low-level code for a
computer other than that on which it is run. Here the target computer is the
ATMEGA micro controller. A debugger assists detection and correction of errors in
code.
Atmel AVRStudio is the Integrated Development Environment (IDE) for developing
and debugging embedded Atmel AVR applications. The AVR Studio IDE gives you
a seamless and easy-to-use environment to write, build, and debug your C/C++ and
assembler code. AVR studio can be down loaded from www.atmel.com.
Three popular versions of AVR studio are available for use with ATMEGA
processors. AVR studio 7 is latest addition into the series.
RESULT:
Familiarized the installation of AVR Studio.
AIM:
To Familiarize the Working of AVR Studio.
PROCEDURE:
Now you have successfully created your first AVR Project! Now to further
proceed with it, we need to write a code, which is discussed in the subsequent
chapters.
RESULT:
Familiarized the Working of AVR Studio.
AIM:
Write an AVR assembly language program to perform the Addition of
numbers various data formats.
OBJECTIVE
After completing this experiment the student will able to understand various data
formats of AVR microcontroller. THEORY AVR microcontroller supports various
data formats like binary, hexadecimal and decimal.
Binary - A binary number is a combination of 0‟s and 1‟s. E.g: LDI
R16,0B00101101
Decimal - A decimal number system consists of numbers between 0-9. E.g:
LDI R17,10
Hexa Decimal - An hexadecimal number system consist of numbers 0-9 and
Alphabets A-F. E.g: LDI R18,22H
ASCII - ASCII means American standard code for information interchange Is
a character encoding standard for electronics communication. ASCII
(American Standard Code for Information Interchange) is the most common
format for text files in computers and on the Internet. In an ASCII file, each
alphabetic, numeric, or special character is represented with a 7- bit binary
number (a string of seven 0s or 1s). 128 possible characters are defined.
E.g:LDI R16,‟32‟
SAMPLE OUTPUT:
INPUT VALUES:
OUTPUT
AFTER 1ST ADDITION --------------
AFTER 2ND ADDITION--------------
INPUT VALUE
OUTPUT
AFTER FINAL ADDITION---------
RESULT
AIM
Write an AVR assembly language program to perform various arithmetic
operations.
OBJECTIVE
After completing this experiment the student will able to understand various
arithmetic operations in AVR microcontroller, and a generalized concept of
arithmetic operators in AVR.
THEORY
SAMPLE OUTPUT
INPUT:
OUTPUT:
RESULT
AIM
Write an AVR Assembly language program to count number of 0‟s and 1‟s in
a binary number.
OBJECTIVE
After completion of this experiment the student able to understand the usage
of shift and rotation instructions.
THEORY
Shift and Rotate instructions shifts the bits in the destination operand by one
or more position either right or left.
1. Various Shift instructions :
Shift Left
Instruction Format:
LSL Destination, bits_shifted
The Shift Left instruction performs a left shift on the destinations
operand, filling the lowest bit with 0. The highest bit is moved into
the Carry Flag.
Eg: MOV CL,5
LSL AX,CL
Shift Right
Instruction format:
LSR destination, bits shifted
The Shift Right instruction performs a right shift on the destinations
operand, filling the lowest bit with 0. The lowest bit is moved into the
Carry Flag.
Eg: MOV CL,5
LSR AX,CL
SOURCE CODE
SAMPLE OUTPUT
INPUT :
R16=
R17=
R18=
R19=
OUTPUT:
RESULT
AIM
Write an AVR assembly language program to convert the given ASCII
number into Packet BCD.
OBJECTIVE
After completion of this experiment the student will be able to understand the
concept simple code conversion
THEORY
To convert ASCII to packed BCD, you first convert it to unpacked BCD, and
then combine it to make packed BCD. For example, for 4 and 7 the keyboard gives
34 and 37, respectively. The goal is to produce 4711 or "0100 0111", which is
packed BCD. This process is illustrated next.
Key ASCII Unpacked BCD Packed BCD
4 34 00000100
7 37 00000111 01000111 which is 47H
SOURCE CODE
SAMPLE OUTPUT
INPUT:
R16=
R17=
OUTPUT:
R16=
RESULT
AIM
Write an AVR assembly language program to convert the given Packed BCD
number into ASCII.
OBJECTIVE
After completion of this experiment the student will be able to understand the
concept simple code conversion
THEORY
In many systems we have what is called a real-time clock (RTC). The
RTC provides the time of day (hour, minute, second) and the date (year, month, day)
continuously, regardless of whether the power is on or off (see Chapter 16). This
data, however, is provided in packed BCD. For this data to be displayed on a device
such as an LCD, or to be printed by the printer, it must be in ASCII format. To
convert packed BCD to ASCII, you must first convert it to unpacked BCD. Then the
unpacked BCD is tagged with 011 0000 (30H). The following demonstrates
converting packed BCD to ASCII.
SOURCE CODE
INPUT:
R16=
R17=
OUTPUT:
R16=
R17=
RESULT
AIM
Write a program to clear R20 register, then add 3 to R20 10 times; then send
the sum to PORTB.
OBJECTIVE
THEORY
ATMEGA ports are 8 bit wide. Each port has 3 eight bit registers associated.
Each bit in these registers configures pins of associated port. Bit 0 of these registers
is associated with Pin 0 of the port, Bit1 of these registers is associated with Pin1 and
so on.
These three registers are
DDRx register
PORTx register
PINx register
X may be replaced by A,B,C or D based on the PORT you are using.
DDRx register
SOURCE CODE
SAMPLE OUTPUT
INPUT:
R16=
R18=
R20=
OUTPUT:
R20=
RESULT
BIT MANIPULATION-I
AIM
A switch is connected to pin PB2. Write a program to check the status of the
switch and perform followings:
1. If Switch=0, send the letter „N‟ to PORTD.
2. If Switch=1, send the letter „Y‟ to PORTD.
OBJECTIVE
After completing this experiment the student able to understand the various bit
manipulation operators in AVR microcontroller.
THEORY
AVR microcontroller has functionality for single bit manipulation. The various
single bit manipulators are:
SBI I/O Reg, Bit - Sets a Specified bit in I/O register. This
Instruction operates on the lower 32 I/O
Registers –addresses 0-31.
Eg: SBI PORTB,5
CBI I/O Reg, Bit - Clears a specified bit in I/O register. This
Instruction operates on the lower 32 I/O
Register –addresses 0-31.
Eg: CBI PORTB,5
SBIS I/O Reg, Bit - This instruction tests a single bit in an I/O
Register and skips the next instruction if the
Bit is set. This instruction operates on the
Lower 32 I/O register- addresses 0-31.
Eg: SBIS PORTB,5
SBIC I/O Reg, Bit - This instruction tests a single bit in an I/O
Register and skips the next instruction if the
Bit is cleared. This instruction operates on the
Lower 32 I/O register-addresses 0-31.
Eg: SBIC PORTB,5
SAMPLE OUTPUT
INPUT:
PORTD= //INITIAL VALUE OF PORTD
OUTPUT:
PORTD= // WHEN PB2=0(OUTPUT IS „N‟)
PORTD= // WHEN PB2=1(OUTPUT IS „Y‟)
RESULT
OBJECTIVE
After completion of this experiment the student should be able to
understand the operation of ATMEGA PORT as output. The operations of PORT are
visualized by interfacing LEDs to PORT pins. In addition, this experiment will
provide preliminary experience on programming microcontrollers on Embedded C.
THEORY
In creating a time delay using assembly language instructions, one must
be mindful of two factors that can affect the accuracy of delay:
SOURCE CODE
SAMPLE OUTPUT
INPUT:
PORTD =
OUTPUT:
PORTD = // AFTER FIRST EXECUTION
PORTD = // AFTER SECOND EXECUTION
RESULT
OBJECTIVE
After completing this experiment the student able to understand the various
bit manipulation operators in AVR microcontroller.
THEORY
SAMPLE OUTPUT
INPUT:
PORTB=
OUTPUT:
PORTB= //AFTER FIRST EXECUTION
PORTB= //AFTER SECOND EXECUTION
PORTB= //AFTER THIRD EXECUTION
RESULT
DATA SERIALIZATION IN C
AIM
Write an AVR C program to send out the value 44H serially one bit at a time
via PORTC, The LSB should go out first.
OBJECTIVE
After completing this experiment the student will able to understand the data
serialization in AVR microcontroller, and a generalized concept of various bitwise
operators in AVR C.
THEORY
Serializing data is a way of sending a byte of data one bit at a time through a
single pin of the microcontroller. There are two ways to transfer a byte of data
serially:
1. Using the Serial Port.
2. The second method of serializing data is to transfer data one bit at a time and
control the sequence of data and spaces between them.
3. The data serialization can be done in either bit wise or byte wise
• We can send the data bit-wise to a particular pin and also we can store a
byte of data in continuous bits of an i/o port.
The AVR C supports various bitwise operators such as,
❖ Shift Right It shifts the bits to right by a specified number of time.
Format: data >> number of bits to be shifted right.
E.g: 0b00010000 >>3 =0b00000010
❖ Shift left It shifts the bits to left by a specified number of time.
Format: data << number of bits to be shifted left.
E.g: 0b00010000 <<3 = 0b10000000
OUTPUT
INPUT: Y=
RESULT
AIM
Write an AVR C program to count the number of 1‟s and 0‟s in a given binary
number.
OBJECTIVE
After completion of this experiment the student able to understand the usage
of various shift and rotation instructions.
THEORY
Shift and Rotate instructions shifts the bits in the destination operand by one
or more position either right or left.
OUTPUT
INPUT: y =
OUTPUT:
PORT B= //NUMBER OF ZEROES
PORT C= //NUMBER OF ONES
RESULT
AIM
Write an AVR C program to convert the given ASCII number into Packet
BCD.
OBJECTIVE
After completion of this experiment the student will be able to understand the
concept simple code conversion
THEORY
To convert ASCII to packed BCD, you first convert it to unpacked BCD, and
then combine it to make packed BCD. For example, for 4 and 7 the keyboard gives
34 and 37, respectively. The goal is to produce 4711 or "0100 0111", which is
packed BCD. This process is illustrated next.
4 34 00000100
7 37 00000111 01000111 which is 47H
OUTPUT
INPUT:
x= ‟ ‟
y= ‟ ‟
OUTPUT:
PORTB=
RESULT
AIM
Write an AVR C program to convert the given Packed BCD number into
ASCII.
OBJECTIVE
After completion of this experiment the student will be able to understand the
concept simple code conversion
THEORY
In many systems we have what is called a real-time clock (RTC). The RTC
provides the time of day (hour, minute, second) and the date (year, month, day)
continuously, regardless of whether the power is on or off (see Chapter 16). This
data, however, is provided in packed BCD. For this data to be displayed on a device
such as an LCD, or to be printed by the printer, it must be in ASCII format. To
convert packed BCD to ASCII, you must first convert it to unpacked BCD. Then the
unpacked BCD is tagged with 011 0000 (30H).
OUTPUT
INPUT:
x=
OUTPUT:
PORT B=
PORT C=
RESULT
AIM
Write an AVR C program to get a data from PINB and send to the I/O register of
PORTC continuously.
OBJECTIVE
THEORY
ATMEGA ports are 8 bit wide. Each port has 3 eight bit registers associated.
Each bit in these registers configures pins of associated port. Bit 0 of these registers
is associated with Pin 0 of the port; Bit1 of these registers is associated with Pin1
and so on.
These three registers are
➢ DDRx register
➢ PORTx register
➢ PINx register X may be replaced by A,B,C or D based on the PORT you are
using.
❖ DDRx register DDRx (Data Direction Register) configures data direction of the
port pins. Which, writing 0 to a bit in DDRx makes corresponding port pin as input,
while writing 1 to a bit in DDRx makes the corresponding port pin as output.
Example:
• to make all pins of port B as input, DDRA = 0b00000000;
• to make all pins of port A as output pins : DDRB= 0b11111111;
• to make lower nibble of port B as output and higher nibble as
input : DDRB = 0b00001111;
In hexadecimal representation, it can be written as DDRB = 0x0F;
SAMPLE OUTPUT
INPUT:
PINB=
OUTPUT:
PORTC=
RESULT
AIM
OBJECTIVE
THEORY
_delay_ms()
_delay_us()
❖ Using AVR Timers.
OUTPUT
RESULT
TIMER/COUNTER PROGRAMMING
AIM
Write an AVR C program to toggle all bits of PORTB alternatively, with some
delay. Use TIMER0 normal mode and pre scalar option to generate delay.
OBJECTIVE
After completing this experiment the student will able to understand the Usage
of timers in AVR microcontroller.
THEORY
❖ TIMER 0
• It is an 8-bit timer.
• The register where the counting takes place is the TCNTn register, where n is
become 0, 1, 2 etc. It counts automatically and overflows and restarts again.
• CS02, CS00 bits in the TCCR0 are used to choose the clock source.
D2 D1 D0
0 0 0 - Timer/counter Stops.
0 0 1 - No Pre scalar
0 1 0 - Clk / 8
0 1 1 - Clk / 64
1 0 0 - Clk /256
1 0 1 - Clk /1024
1 1 0 - External source to falling edge.
1 1 1 - External Source to raising edge.
Department of Computer Engineering, GPTC Perumbavoor Page 46
• WGM is used for Timer mode selection.
Various modes are:
D6 D3
0 0 - Normal mode
0 1 - CTC (Clear Timer on Compare Match)
1 0 - PWM, phase correct
1 1 - Fast PWM
❖ TIMER 2
• It is an 8-bit timer.
• It cannot be used as a counter, because it does not support external clock.
• It can be used as a real time clock.
SOURCE CODE
OUTPUT
BEFORE EXECUTION--------------------: PORTB=
AFTER DELAY-----------------------------:PORTB=
RESULT
OBJECTIVE
After completion of this experiment the student will be able to interface 4x4
keyboards. He / She will be able to generalize embedded C programs.
THEORY
Matrix Keypads are commonly used in calculators, telephones etc where a large
number of input switches are required. We know that matrix keypad is made by
arranging push button switches in row and columns. If the 16 switches are straightly
connected to microcontroller we need 16 inputs pins. But arranging switches in matrix
form, we can read the status of each switch using 8 pins of the microcontroller.
2. Now each Column is scanned. If any switch belongs to 1st row is pressed
corresponding column will pulled down (logic LOW) and we can detect the
pressed key.
#define
#include
#include
/* the following is done as the part of generalizing the code This code can be used any
where, all you need to do is change PORT below*/
#define
#define
#define
Program
OUTPUT
RESULT
LCD INTERFACING
AIM
OBJECTIVE
After completion of this experiment the student will be able to interface LCD
displays to micro controller.
THEORY
LCD display is an inevitable part in almost all embedded projects. We will look
about interfacing 16×2 LCD with ATMEGA. In order to understand the interfacing
first you have to know about the module. It consists of 16 rows and 2 columns of
5×7 or 5×8 LCD dot matrices. They are available in a 16 pin package with back
light, contrast adjustment function and each dot matrix has 5×8 dot resolution. The
pin numbers, their name and corresponding functions are shown in the table below.
LCD COMMANDS
Command Function
0F LCD ON, Cursor ON, Cursor blinking ON
01 Clear Screen
02 Return Home
04 Decrement Cursor
06 Increment Cursor
0E Display ON, Cursor blinking OFF
80 Force Cursor to beginning of 1st line
C0 Force Cursor to beginning of 2nd line
38 Use 2 lines and 5x7 Matrix
83 Cursor line 1 Position 3
3C Activate 2nd line
08 Display OFF, Cursor OFF
C1 Jump to 2nd line Position 1
0C Display ON, Cursor OFF
C1 Jump to second line, position one
C2 Jump to second line , position two
LCD INITIALIZATION
The steps that have to be done for initializing the LCD display is
given below and these steps are common for almost all applications.
• Send 01H for clearing the display and return the cursor.
The steps for sending data to the LCD module are given below.
I have already said that the LCD module has pins namely RS, R/W
and E. It is the logic state of these pins that make the module to
determine whether a given data input is a command or data to be
displayed.
• Make R/W low.
• Make RS=0 if data byte is a command and make RS=1 if the data
byte is a
data to be displayed.
PROGRAM
/* --------------------------------------------------------- This program displays string
given. It
uses RS - PD4 RW - PD5 AND EN - PD6 Data bus is PORTB ----------------------------------
-------
-----------------*/
#include <util/delay.h>
//----------------------------------------------------
target board
//------------------------------------------------------//
// ------------------------------------------------------------- //
-------- ------------ -----------------
-------- ------------ -----------------
// ----------------------------------------------------------- //
// -----------------------------------------------------------
// ------------------------------------------------------- //
// -------------------------------------------------------
// ------------------------------------------------------ //
//-------------------------------------------------------
OUTPUT
RESULT