0% found this document useful (0 votes)
77 views92 pages

MM_Lab Manual_LDCE_EC_SEM_IV_2025

The document is a laboratory manual for the Microprocessor & Microcontroller course at L.D. College of Engineering, detailing objectives, outcomes, and practical experiments for students. It outlines the vision and mission of the educational institution and department, as well as program outcomes and specific objectives related to electronics and communication engineering. The manual includes instructions for using Arduino hardware and software, along with a series of experiments aimed at developing practical skills in microprocessor and microcontroller applications.

Uploaded by

kansarasujal72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views92 pages

MM_Lab Manual_LDCE_EC_SEM_IV_2025

The document is a laboratory manual for the Microprocessor & Microcontroller course at L.D. College of Engineering, detailing objectives, outcomes, and practical experiments for students. It outlines the vision and mission of the educational institution and department, as well as program outcomes and specific objectives related to electronics and communication engineering. The manual includes instructions for using Arduino hardware and software, along with a series of experiments aimed at developing practical skills in microprocessor and microcontroller applications.

Uploaded by

kansarasujal72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

A Laboratory Manual for

Microprocessor & Microcontroller


(3141008)

B.E. Semester 4 (E.C. Engineering)

Electronics & Communication Engineering Department

L.D. College of Engineering, Ahmedabad

Directorate of Technical Education,


Gandhinagar, Gujarat

Enrolment No. _________________ Batch : _______________

Name of the student: _________________________________________


L.D. College of Engineering, Ahmedabad

CERTIFICATE
This is to certify that Mr/Miss

____________________________________Enrolment No. _____________

of B.E. (E.C.) SEM-IV of this institute (GTU Code: 028) has

satisfactorily completed the practical work of the subject

Microprocessor & Microcontroller prescribed by Gujarat

Technological University during the academic term December-

2024 to April-2025.

Date: Signature of the faculty

Lab Manual of Microprocessor & Microcontroller (3141008) Page 2


Vision of Commissionerate of Technical Education, Gujarat State

 To provide globally competitive technical education;


 Remove geographical imbalances and inconsistencies;
 Develop student friendly resources with a special focus on girls’ education and
support to weaker sections;
 Develop programs relevant to industry and create a vibrant pool of technical
professionals.
Vision of the Institute

Enhance the capabilities of students and faculties in advanced technical


education and research to help communities and the nation to be at the
forefront of technological and sustainable development consistent with our
cultural and spiritual ethos.
Mission of the Institute

 To provide an inclusive and equitable environment for technical education.


 To provide state of the art infrastructure to propel multi-disciplinary research.
 To groom students to be effective innovators who can contribute significantly to the
economic growth of the nation.
 To collaborate with institutions, industries and engage with communities with an aim
to foster cross-learning that expands outreach, inculcates spirit of entrepreneurship
and citizenship among students and faculties.
Vision of the department

To bring out technically competent, socially responsible and ethically sound


electronics and communication engineers for the nation.
Mission of the department

 Upgrade learning resources and pedagogical skills for effective teaching-learning


process
 Foster research culture and strengthen networking with institutions, industries,
research organization and alumni
 Encourage students for professional ethics and social responsibility
 Facilitating students to work on innovative projects related to electronics and
communication engineering.

EC Department, L.D. College of Engineering, Ahmedabad Page 3


Programme Outcomes (POs)

Engineering knowledge: Apply the knowledge of mathematics, science,


PO 1 mechanical engineering fundamentals, and specialization to the solution of complex
engineering problems.
Problem analysis: Identify, formulate, review research literature, and analyze
PO 2 complex mechanical engineering problems reaching substantiated conclusions using
first principles of mathematics, natural sciences, and mechanical engineering.
Design/development of solutions: Design solutions for complex mechanical
engineering problems and design system components or processes that meet the
PO 3 specified needs with appropriate consideration for the public health and safety, and
the cultural, societal, and environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge and
PO 4 research methods including design of experiments, analysis and interpretation of
data, and synthesis of the information to provide valid conclusions.
Modern tool usage: Create, select, and apply appropriate techniques, resources, and
PO 5 modern engineering and IT tools including prediction and modeling to complex
mechanical engineering activities with an understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge
PO 6 to assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
Environment and sustainability: Understand the impact of the professional
PO 7 engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and
PO 8 responsibilities and norms of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member or
PO 9 leader in diverse teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to
PO 10 comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work,
PO 11 as a member and leader in a team, to manage projects and in multidisciplinary
environments.
Life-long learning: Recognize the need for, and have the preparation and ability to
PO 12 engage in independent and life-long learning in the broadest context of technological
change.

Lab Manual of Microprocessor & Microcontroller (3141008) Page 4


Programme Specific Objectives (PSO)

PSO PSO statement


Apply fundamental concepts of electronics and communication engineering to
PSO 1
identify and solve real life problems.

Utilize analytical and design skills to inculcate habit of working individually


PSO 2
and in a group through experiments and project work.

PSO 3 Ability to use modern tools to produce environment friendly ideas and products

Programme Educational Objectives (PEO)

Sr.
PEO PEO statement
No.
To make students capable for the post-graduate courses, research, successful
1 PEO 1 career in industries or entrepreneurship in the field of electronics and
communication engineering.
2 PEO 2 To design, draft, erect and maintain electronics production.
To acquire communication skills to effectively communicate problems and
3 PEO 3 solutions of electronics and communication engineering individually as well as
in a team.
To be engaged in lifelong learning and adapt changing technological and
4 PEO 4
social needs.
To be ethically and professionally sensitive to the consequence of work for
5 PEO 5
promising career.

Course outcomes:

After learning through theory and laboratory sessions students will ….

1. Explain the architecture of 8085 Microprocessor and AVR 8-bit


Microcontroller.(Understand)
2. Differentiate microprocessor and microcontroller and describe the
importance and function of each pin of AVR ATmega32
Microcontroller. (Remember)
3. Learn and analyze assembly language programs for AVR
Microcontroller (Analyze)
4. Develop embedded C language programs for AVR Microcontroller
(Create)
5. Interface I/O peripheral devices with AVR microcontroller to develop
embedded system (Apply)

EC Department, L.D. College of Engineering, Ahmedabad Page 5


INDEX

Sr. Objective(s) of Experiment Page


No. No.
1 Understand Arduino open source hardware and programming
environment and write program to blink LED using Arduino
instructions, C language & Assembly language.
2 Interface Digital/Analog input output
interfacing module with Arduino board
and write programs related to I/O module

3 Generate PWM waveform and change


intensity of LED connected with Arduino
board.

4 Write and execute Arduino program for serial communication.


Transmit temperature value through serial communication and store it
in spreadsheet or text file
5 Write and execute Arduino program to display message and numbers
on LCD
6 Write and execute Arduino program to read analog value. Sense
temperature using LM35 sensor and display temperature value on LCD
7 Write assembly language programs for ATMega32 Microcontroller and
simulate using ATMEL Studio
8 Understand hardware of ATMega32 Kit. Write program to flash LEDs,
Read status of switches, Display count values on seven segment
display. Upload programs in the kit one by one and execute.
9 Write program to display message on first line of LCD & GLCD.
Simulate circuit in PROTEUS software.
10 Write program to rotate stepper motor in half step and full step mode
in Assembly as well as C language. Simulate program using PROTEUS
software
11 Write program to rotate DC motor in clockwise and anti-clockwise
direction in Assembly as well as C language. Simulate program using
PROTEUS software.
12 Observe waveforms of I2C and SPI communication and understand I2C
and SPI protocol
13 Write Arduino program to receive IR Signal from IR remote and operate
Electrical device based on switch pressed.
14 Report of Student Mini Project
(Along with Circuit diagram and its related program in Arduino,
Assembly or C language)

Lab Manual of Microprocessor & Microcontroller (3141008) Page 6


Practical – Course Outcome matrix
Sr. CO CO CO CO CO
Objective(s) of Experiment
No. 1 2 3 4 5
Understand Arduino open source hardware and
programming environment and write program to
1. √ √
blink LED using Arduino instructions, C
language & Assembly language.
Interface Digital/Analog input output
2. interfacing module with Arduino board √
and write programs related to I/O module

Generate PWM waveform and change


3. intensity of LED connected with Arduino √
board.

Write and execute Arduino program for serial


communication. Transmit temperature value
4. √
through serial communication and store it in
spreadsheet or text file
Write and execute Arduino program to display
5. √ √
message and numbers on LCD
Write and execute Arduino program to read
6. analog value. Sense temperature using LM35 √ √
sensor and display temperature value on LCD
Write assembly language programs for
7. ATMega32 Microcontroller and simulate using √
ATMEL Studio
Understand hardware of ATMega32 Kit. Write
program to flash LEDs, Read status of switches,
8. Display count values on seven segment display. √ √
Upload programs in the kit one by one and
execute.
Write program to display message on first line of
9. LCD & GLCD. Simulate circuit in PROTEUS √
software.
Write program to rotate stepper motor in half
step and full step mode in Assembly as well as C
10. √ √
language. Simulate program using PROTEUS
software
Write program to rotate DC motor in clockwise
and anti-clockwise direction in Assembly as well
11. √ √
as C language. Simulate program using
PROTEUS software.
Observe waveforms of I2C and SPI
12.
communication and understand I2C and SPI

EC Department, L.D. College of Engineering, Ahmedabad Page 7


protocol

Write Arduino program to receive IR Signal from


13. IR remote and operate Electrical device based on √ √
switch pressed.
Report of Student Mini Project
14. (Along with Circuit diagram and its related √
program in Arduino, Assembly or C language)

Instructions to the students:

 Write Assembly language and C programs using ATMEL Studio or


Microchip Studio

 Simulate Assembly and C Language programs using PROTEUS software


or any other available simulation software in lab or in your personal
laptop

 Install Arduino open source software to write, compile and download


programs in Arduino UNO board and Arduino Nano board.

 Student has to construct mini-project based on AVR (Either Arduino


Nano/ Arduino UNO or any other development board)

 Suggested list of mini-projects based on Arduino board will be given by


faculty. However students are free to choose any mini-project. It is
desirable if students find out some real life problem related to society or
industry and solve it.

 Students shall organize work in group and make records of experiments

 Students should refer websites and literature along with datasheet


relevant to this course

 Student should develop a habit of submitting the experimentation work


as per the schedule and s/he should be well prepared for the same.

 Student shall develop the habits of evolving more ideas, innovations,


skills etc. apart from those included in scope of manual.

Lab Manual of Microprocessor & Microcontroller (3141008) Page 8


EXPERIMENT NO. 1

AIM: Understand Arduino open source hardware and programming


environment and write program to blink LED.

Objectives:
 To install Arduino open source software
 To learn Arduino UNO and nano open source hardware
 To learn programming of Arduino UNO and Nano boards

Introduction of Arduino hardware:


Arduino board is an open-source platform used for building electronics
projects. Arduino boards are coming in different variety. The Arduino
platform has become quite popular because it is very easy to use it and
program it. It can be connected to PC using USB cable and easy to transfer
program from PC to AVR microcontroller residing in Arduino board. Details
about the board and software are available on Arduino website:
http://www.arduino.cc/ So many sample programs, projects based on
Arduino and details of all different types of boards available on this website.
Everything is open-source so schematic diagrams are also given.

Some Arduino boards and their AVR microcontrollers:

1. Arduino UNO – ATmega328 (8 and 168)


2. Arduino Nano– ATmega328 (168)
3. Arduino Pro – ATmega328 (168)
4. Arduino LilyPad– ATmega328
5. Arduino Mega – ATmega1280 & 2560
6. Sanguino – ATmega644 & 1284
7. Leonardo – ATmega32u4
8. Micro– ATmega32u4

Some Arduino boards having 32 bit ARM Microcontrollers:


1. Arduino Due – SAM3X8E Cortex-M3
2. Arduino Zero– SAMD21G18, Cortex M0+
3. Arduino MKR1000 – SAMW25 SoC having SAMD21 Cortex-M0+
4. Arduino Yun– ATmega32u + Atheros AR9331 Linux (MIPS)

EC Department, L.D. College of Engineering, Ahmedabad Page 9


To start with we can use Arduino UNO board or Arduino nano board.
Arduino UNO board is as shown in the following figure.

This Arduino UNO board uses ATMEL AVR Microcontroller ATMEGA328P.


ATMega238P Microcontroller is 28 pin, 8 bit RISC microcontroller having
total 23 programmable I/O lines, 6 channel 10 bit ADC, two 8 bit
timer/counter, one 16 bit timer/counter with compare and capture mode. It
has 32 general purpose 8 bit registers R0 to R31. Students are advised to
refer datasheet for more information about the chip.

Following pins are available on Arduino UNO board.

 GND (3): Three GND (Ground) terminals are available to connect ground to
external circuit.
 5V (4) & 3.3V (5): The 5V pin supplies 5 volt and the 3.3V pin supplies 3.3
volt of power. External circuit which requires 5V or 3.3 V DC can be
connected at these pins.
 Analog (6): Six Analog pins are available which is labelled as A0 through A5.
These pins can read the signal from an analog sensor (like a temperature
sensor, light sensor, weight sensor (strain gauge) and convert it into a digital
value which can be displayed on LCD. We can process this analog signal and
take decision based on value of it.
 Digital I/O: Digital input/output pins (0 through 13 on the UNO) are
available. These pins can be used for both digital input (like telling if a
button is pushed) and digital output (like powering an LED).

Lab Manual of Microprocessor & Microcontroller (3141008) Page 10


 PWM (8): Please notice the tilde (~) sign next to some of the digital pins (3, 5,
6, 9, 10, and 11 on the UNO). These pins act as normal digital pins, but can
also be used for supplying Pulse-Width Modulation (PWM) output.
 AREF (9): Stands for Analog Reference. Most of the time you can leave this
pin alone. It is sometimes used to set an external reference voltage (between
0 and 5 Volts) as the upper limit for the analog input pins.
 RESET: Reset button is given to reset microcontroller.
 External power socket: Usually Arduino board takes power by USB cable
when connected to LAPTOP or Desktop. But once program is downloaded in
it and if we want to use this board as standalone board, we can connect
external +5V through this external power connector.
 Tx and Rx pin and LEDs: Digial pins 0 and 1 is also used for serial
communication. Tx and Rx LEDs are blinking when serial communication is
going on.
 USB connector: USB connector is used to connect Arduino with PC/Laptop
with USB cable. It is also giving +5V power required by the board. USB cable
is used to download program into AVR chip (From PC side it is called
uploading).

Pin mapping of Arduino Board and ATMega328P microcontroller chip:

This pin mapping is useful if students want to write program using ATMEL
Studio or other tools and want to program Arduino board.

EC Department, L.D. College of Engineering, Ahmedabad Page 11


Specifications of Arduino UNO Board:
 Maximum DC current per I/O pin: 40 mA
 Flash memory: 32 KB (ATmega328) of which 0.5 KB used by boot-
loader
 SRAM: 2 KB (ATMega328)
 EEPROM: 1KB (ATMega328)
 Clock: 16 MHz
 Digital I/O pins: 14
 Analog I/O pins: 6
Note:
Care should be taken while interfacing external devices with Arduino board.
Do not draw more current from I/O pins, if devices required more than
current specified above, driver circuit should be used. To drive DC motor or
Stepper motor using Arduino board, separate driver circuit like IC LM293D
should be used. Similarly to drive electromagnetic relay or solenoid
switching transistor SL-100 should be used. If external circuit requires large
current, separate power supply should be used for that circuit.

About Arduino Programming:


You can download latest version of Arduino software from the website:
http://www.arduino.cc/ and installation is very simple. Screenshot of
Arduino programming environment is shown below:

Lab Manual of Microprocessor & Microcontroller (3141008) Page 12


Arduino is coming with large set of sample programs. Students can use this
sample programs and modify it according to hardware and application.
Steps to use sample program:
1. File -> Examples->Basics->Blink
2. This will open original Blink sketch in a new window. We will work in
that window now. Do not bother about old window.
3. Now select the board and programming port using
4. Tools-> Board -> ARDUINO UNO. This will select our board.
5. Now select the programming interface: Tools-> Serial Port-> COMx.
This will select virtual serial port created by USB to Serial Driver. To
know on which COM port Arduino is connected use device manager of
PC or Laptop.
6. Press VERIFY Button (or from menu Sketch->Verify). Bottom right
shows progress bar. Bottom left line shows “Compiling Sketch…” and
after some time after the compilation is over it displays “Done
compiling”. If there were any compilation errors they will be
highlighted in red at the bottom of the screen
7. Press UPLOAD button, second on top left, performs dual tasks of
compilation as well as transportation of machine code to the board.
When we press UPLOAD button, the IDE bottom line shows first
Compiling Sketch and then Uploading…. If the board is connected and
powered, the uploading will begin in 2-3 seconds. The Debug LED as
well as Tx and Rx LEDs on board will blink on the board indicating
that machine code file is being transferred from PC to board. The time
it takes depends upon length of program but it is usually 2 to 10
seconds.

8. If uploading of machine code is successful then a message “Done


Uploading” will appear. (It means that machine code is now stored in
non-volatile or flash memory or ROM of the MCU and can be run
again and again without downloading.)

Sample program to blink LED:


Let us understand first program of blinking LED. This program blinks LED
available on Arduino board. LED is connected to pin 13 (Port pin PB5)
Arduino program has two major parts: [1] setup() it is just like initialization.
All initialization is done in setup() part. [2] loop() Program which needs to
run continuously is kept in this part. loop() is just like while(1) infinite loop
in pure “C” programming language.

EC Department, L.D. College of Engineering, Ahmedabad Page 13


Program:
/* This program blinks LED connected at pin 13 */
/*setup function runs once when you press reset or power the board
*/
void setup()
{
// initialize digital pin 13 (PB5) as an output.
pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever just like while(1)

void loop()
{
digitalWrite(13, HIGH); // turn the LED on connected at PB5
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off
delay(1000); // wait for a second
}

Using C language instructions in Arduino software:

void setup()
{
DDRB=DDRB | (1<<5); //Set pin PB5 as output pin (LED is connected)
}

void loop()
{
PORTB=PORTB | (1<<5); //Set PB5 pin
delay(100);
PORTB=PORTB & ~(1<<5); //Clear PB5 pin
delay(100);
}

To start with we can use Arduino UNO or Arduino NANO board. Pin diagram
of Arduino NANO is given below.

DDRB means Data Direction Register for PORTB

DDRB=DDRB|(1<<5); instruction is equivalent to DDRB=DDRB|0x20;

Lab Manual of Microprocessor & Microcontroller (3141008) Page 14


This Arduino NANO board uses ATMEL AVR Microcontroller ATMEGA328P.
ATMega238P Microcontroller is 28 pin, 8 bit RISC microcontroller having
total 23 programmable I/O lines, 6 channel 10 bit ADC, two 8 bit
timer/counter, one 16 bit timer/counter with compare and capture mode.

EC Department, L.D. College of Engineering, Ahmedabad Page 15


Details on Arduino Nano board:
 Arduino Nano Board comes with Mini-B USB interface.
 This board can be powered by laptop +5V supply which is given
through USB cable.
 It can be powered by 6V-20V DC supply which converts it into 5V and
3.3V
 However, when we use high current drawing devices like LCD display
& Motor etc, external 12V power supply must be used.
 While programming Arduino Nano board, select that board in Arduino
software before downloading code into it.

:: WORKSHEET::
Activity #1:

Write instructions to initialize PB0, PD0 and PD1 pins as output pins. Write
program to blink LEDs connected on these pins.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 16


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #2:

Write program in C language to blink 8 LEDs connected at PORTD one by


one.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #3:

Write program to blink Arduino LED (Connected at pin 13, PB5 pin) in
increasing and decreasing rate continuously. Rate should increase from 1
second to 5 second and then rate should decrease from 5 second to 1
second. Monotonically increasing and decreasing rate. Write program either
using Arduino instructions or C language(Hint: Use For loop or While loop
for this purpose)

EC Department, L.D. College of Engineering, Ahmedabad Page 17


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #4: Write difference between microprocessor and


microcontroller.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 18


EXPERIMENT NO. 2

AIM: Interface Digital/Analog input output interfacing module with


Arduino board and write programs related to I/O module
Objectives:
 Understand I/O devices of Digital/Analog I/O (DAIO) module
 To interface Switch, LEDs, Buzzer, LDR with Arduino board and write
programs for it.

Introduction of Digital/Analog Input output module:


Digital/Analog I/O (DAIO) module has following devices:

Digital Inputs to any microcontroller


a. Four Switches
b. Infrared receiver TSOP 1738
Analog Inputs to Any Microcontroller
a. Two variable Resistance
b. One Light Sensor
c. One Temperature Sensor
Use as Digital and Analog Outputs for any Microcontroller
a. Eight LEDs
b. One Buzzer

Layout of DAIO module:

EC Department, L.D. College of Engineering, Ahmedabad Page 19


Schematic diagram of DAIO module:

We will need male/female wires to connect I/O board with Arduino card.

Lab Manual of Microprocessor & Microcontroller (3141008) Page 20


Program:
Write program to detect push-button press and make LED on if push-button
switch is pressed. Consider push-button switch connected to digital pin
number 11 (PB3). Use internal LED (Which is connected to Pin 13 (PB5)
const int SW = 3; // Push button switch with Pin No. 2
const int LED = 5; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

DDRB=DDRB & ~(1<<SW); //Set PB3 pin input


DDRB=DDRB | (1<<LED);; //Set PB5 pin output
}

void loop() {
if(PINB & (1<<SW))
{
PORTB=PORTB & ~(1<<LED);
}
else
{
PORTB=PORTB | (1<<LED);
}
}

 Connect Vcc and Ground of Arduino with DAIO module


 Connect Digital Pin 2 of Arduino board with SW4 pin of DAIO module
 Verify/compile and upload program in Arduino board
 Press Switch and observe status of LED
 digitalRead() function is used to read input pin and digitalWrite() is
used to write in output pin. Pin number is specified in the bracket.

:: WORKSHEET::
Activity #1: Connect 4 LEDs of DAIO module with Arduino digital pins 2 to
5. Write following program, compile, upload and execute the program and
observe LEDs.

void setup()
{
DDRD = 0x3C; //Pins PD2 to PD5 output pins
}
void loop() {
PORTD=0x3C;
delay(1000);
PORTD=0x00;
delay(1000);
}

EC Department, L.D. College of Engineering, Ahmedabad Page 21


Observation:

_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________

Explain program:

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________

Activity #2: Modify program given in activity 1 such that all 4 LED becomes
ON one by one after 1 second and then turn OFF one by one. Write program
again. Compile and execute this program in Arduino board.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Microprocessor & Microcontroller (3141008) Page 22
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #3: Connect +Vcc and Gnd of Arduino board with DAIO module.
Digital pin 2 with Buzzer pin of DAIO module. Write program to sound
buzzer for 1 second and OFF for 2 second. Repeat this five times.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #4: Modify program given in activity 3 such that when SW1 of
DAIO module is pressed, Buzzer should sound otherwise it should remain
OFF. Connect Buzzer of DAIO module with Digital pin 7.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 23


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #5: Prepare OR gate with SW1 and SW2. Write program such that
when SW1 or SW2 or both are pressed, internal LED of Arduino board
should glow. Connected SW1 with pin 6 and SW2 with pin 7 of Arduino
board. Write program in following space.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 24


EXPERIMENT NO. 3

AIM: Generate PWM waveform and change intensity of LED connected with
Arduino board

Objectives:
 To generate PWM waveform
 Know importance of PWM for light dimmer and speed control

Program 1:
/*
Change Intensity of LED by PWM
Two LEDs of General purpose I/O board is connected to Pin 9 and
10.
Brightness of one LED will increase while other will decrease and
vice-varsa */

int maxBrightness = 254; // Highest width of pulse


int minBrightness = 1; // Minimum width of pulse
int LED1 = 9 ;// LED is conencted to pin 9 PWM channel.
int LED2 = 10; //LED is connected to pin 10
int i;
void setup()

{
pinMode(LED1, OUTPUT); // set pin 9 as output.
pinMode(LED2, OUTPUT); // set pin 10 as output.
}

void loop()
{
for (i=minBrightness; i<=maxBrightness; i=i+1)
{
analogWrite(LED1,i);
analogWrite(LED2,255-i);
delay(10);
}
delay(1000);
}

EC Department, L.D. College of Engineering, Ahmedabad Page 25


Program 2:
Change Intensity of LED by pressing different switches on DAIO
Board.
/*
Change Intensity of LED by PWM using switches
Switches are connected to Pin 2 and 3
*/
int LED = 9 ;// LED is connected to pin 9 PWM channel.
int SW1=2;
int SW2=3;
boolean sws1,sws2;
void setup()
{
pinMode(LED, OUTPUT); // set pin 9 as an output.
pinMode(SW1,INPUT); // Pin 2,3 Input
pinMode(SW2,INPUT);

void loop()
{
sws1=digitalRead(SW1);
sws2=digitalRead(SW2);

if(sws1==0)
{
analogWrite(LED,200);
}
delay(100);
if(sws2==0)
{
analogWrite(LED,100);
}

delay(100);
}
Observe waveforms on Pin 9 for by pressing SW1 and SW2 one by one by
one and then draw waveforms.
Waveform when SW1 is pressed:

Lab Manual of Microprocessor & Microcontroller (3141008) Page 26


Waveform when SW2 is pressed:

:: WORKSHEET::
Activity #1: Connect three color LED with pins 9,10 and 11. Write program
to produce 8 different colors. Write your program in following space. (If three
color LED not available then connect three separate LEDs and produce eight
different binary patterns from 000 to 111)

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 27


Activity #2: Connect three color LED with pins 8,9 and 10. Connect Switches
SW1, SW2 and SW3 with pin number 0, 1 and 2. Generate color from LED in
following sequence.

SW1 SW2 SW3 Color

ON ON ON WHITE

ON OFF OFF RED

OFF ON OFF GREEN

OFF OFF ON BLUE

ON ON OFF YELLOW

ON OFF ON MAGENTA

OFF ON ON CYAN

Write your program in following space


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 28


EXPERIMENT NO. 4

AIM: Write and execute Arduino program for serial communication.


Transmit temperature value through serial communication and store
it in spreadsheet (MS-Excel) or Text file.

Objectives:
 To measure temperature using LM35
 Understand specifications of LM35
 Transmit values using serial communication

LM35 Temperature sensor:


The LM35 is integrated-circuit temperature devices with an output voltage
linearly-proportional to the Centigrade temperature.

Specifications of LM35:

 Calibrated Directly in Celsius (Centigrade)


 Linear + 10-mV/°C Scale Factor
 0.5°C Ensured Accuracy (at 25°C)
 Rated for Full −55°C to 150°C Range
 Suitable for Remote Applications
 Low-Cost Due to Wafer-Level Trimming
 Operates from 4 V to 30 V
 Less than 60-μA Current Drain
 Low Self-Heating, 0.08°C in Still Air
 Non-Linearity Only ±¼°C Typical
 Low-Impedance Output, 0.1 Ω for 1-mA Load

Temperature sensor LM35 is available on DAIO Board. Connect LM35 pin to


A0 pin of Arduino board. Compile and upload following program in Arduino
card. See value of temperature on serial monitor.

It is better to take average of readings and display it as an actual


temperature value to suppress effect of spikes/noise. Average value gives
overall integration and provides smooth output.

EC Department, L.D. College of Engineering, Ahmedabad Page 29


Program 1:
/*
Measure Temperature and Transmit value serially
*/
int sensorValue = 0;
int alarm = 13;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(alarm,OUTPUT);
}

void loop() {
int sum = 0; // Variable to store sum of temperature readings
float temperature;
for (int i = 0; i<100; i++)
{
sum = sum + (long)(analogRead(sensorValue));// read one value and add
it to var sum.
delay(10);// wait for some time before taking next sample.
}
// sum now contains total of 100 readings. Divide it by 100 to get average
value.
sum = sum /100L;
temperature = (((float)(sum) )* 5000.0 /1023.00 )/10.0; // convert int temp
into voltage in mV. Then divide it by 10 to get temperature in degree Celcius.

if(temperature>30)
digitalWrite(alarm, HIGH);
else
digitalWrite(alarm, LOW);
// print temperature value to the serial monitor:
Serial.println(temperature);
delay(1000); // Take reading at every 1 second
}

ATMega328 microcontroller used in Arduino board has 10 bit ADC, reading


is divided by 1024 (210=1024). Value is again divided by 10 to convert
readings from mV to 0C (because sensitivity of LM 35 is 10-mV/°C)

If temperature is above 300C, LED will become ON (Student may connect


Buzzer in place of LED).

Lab Manual of Microprocessor & Microcontroller (3141008) Page 30


Program 2:
Check status of Door Switch and send message “Door is Open” if switch is
open and “Door is Closed” if switch is closed through serial communication.
/* Program to read status of Door lock switch and transmit
* status of Door serially
*/
int LED=13;
int SW=12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(SW,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int SWSTATUS;
SWSTATUS=digitalRead(SW);
Serial.println(SWSTATUS);
if(SWSTATUS==1)
{
Serial.println("DOOR IS OPEN");
digitalWrite(LED,HIGH);
}
else if(SWSTATUS==0)
{
Serial.println("DOOR IS CLOSE");
digitalWrite(LED,LOW);
}
delay(100);
Serial.flush();
}

Program 3: Measure Temperature, transmit values serially and write in


spreadsheet or text file.

#include <SPI.h>
#include <SD.h>
/*
Analog input, analog output, serial output
Connect Analog Input at pin 0

EC Department, L.D. College of Engineering, Ahmedabad Page 31


*/
File myFile;
const int analogInPin = A0;
const int analogOutPin = 13;
int sensorValue = 0; // value read from the pot
int alarm = 13;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(alarm,OUTPUT);
myFile = SD.open("d:/tempdata.txt", FILE_WRITE);
}

void loop() {
int count=0;
while(count<100)
{
int sum = 0; // Variable to store sum of temperature readings
float temperature;
for (int i = 0; i<100; i++)
{
sum = sum + (long)(analogRead(sensorValue
delay(10);// wait for some time before taking next sample.
}
sum = sum /100L;
temperature = (((float)(sum) )* 5000.0 /1023.00 )/10.0;
if(temperature>30)
digitalWrite(alarm, HIGH);
else
digitalWrite(alarm, LOW);
// print the results to the serial monitor:
Serial.print("TEMPERATURE = " );
Serial.println(temperature);
myFile.write(temperature); //Write temperature data in file
delay(1000);
count++;
}
myFile.close();

Lab Manual of Microprocessor & Microcontroller (3141008) Page 32


:: WORKSHEET::
Activity #1: Connect LDR in place of LM35 and measure value of light.
Transmit amount of light intensity through serial communication. If Light
intensity is low, transmit “DARK” and if light intensity is high, transmit
“BRIGHT” through serial communication. Write your code and execute.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 33


EXPERIMENT NO. 5

AIM: Write and execute Arduino program to display message on LCD &
GLCD
Objectives:
 Understand interfacing of LCD & GLCD with Arduino UNO Board.
 Write Arduino program to display message on LCD & GLCD
 Write Arduino program to display counter on LCD & GLCD
Pin connections:

LCD MODULE TO ARDUIONO BOARD CONNECTIONS

LCD ARDUINO

PIN NO. SIGNAL NAME PIN NO SIGNAL NAME

1 Ground GND Ground

2 +5V +5V Power

3 Contrast (GND)

4 Register Select RS 11 Digital 11

5 Read/write (GND)

6 Enable 10 Digital 10

7 D0 (Keep open)

8 D1 (Keep open)

9 D2 (Keep open)

10 D3 (Keep open)

11 D4 5 Digital 5

12 D5 4 Digital 4

13 D6 3 Digital 3

14 D7 2 Digital 2

15 LED +ve (+5V)

16 LED -ve (GND)

Note: 1. Connect only 8 wires shown above.

Short circuit LCD Pin 1,3,5,16 and connect to Ground (GND).


Short circuit Pin 2,15 and connect it to +5V.

Lab Manual of Microprocessor & Microcontroller (3141008) Page 34


Connection using breadboard:

Pin connections:

 Data lines D7-D4 of LCD are connected with Digital pin 2 to 5

 RS (Register Select) line is connected with digital pin 11

 R/W pin is directly connected to ground.

 Enable line is connected with digital pin 10.

 +5V and Ground of LCD to +5V and GND of Arduino board

Program:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("GEC RAJKOT!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}

EC Department, L.D. College of Engineering, Ahmedabad Page 35


:: WORKSHEET::
Activity: In the LCD Connection setup of this experiment, connect variable
resistor output VR1 of DAIO Board with A0 pin of Arduino board. Connect
+Vcc and Ground of DAIO board with Arduino board. Write and upload
following program

int AnalogPin = 0;
int POTValue1 = 0;
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);// LCD in 4 bit mode

void setup() {
lcd.begin(16, 2);
lcd.print("EC Department MI LAB");
for(i=0;i<10;i++)
{
lcd.scrollDisplayLeft();
delay(500);
}
lcd.clear();
delay(1000);
}
void loop() {
lcd.setCursor(0,1);
lcd.print("Value:");
lcd.setCursor(6, 1);
POTValue= analogRead(AnalogPin));
lcd.print(POTValue);
delay(100);
}
Observation:

_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________

Explain program:

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 36


Activity 2 : Search Graphical LCD interface on internet and draw
connection diagram in following space.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
___________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 37


EXPERIMENT NO. 6

AIM: Sense temperature using LM35 sensor and display temperature value
on LCD

Objectives:
 Write program to read temperature using LM35 and display it on LCD
for Arduino.

Connections:
 Connect LCD as per experiment No. 5
 Connect Temperature sensor LM35 output of DAIO board to A0 pin of
Arduino
Program:
#include <LiquidCrystal.h>
int sensorPin1 = 0; // select the input pin for the potentiometer
int count,i,count_char=0;
char message[16];
float temperature;
long sum;
const int ledPin = 13;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);// LCD in 4 bit mode

void setup() {
// set up the LCD's number of columns and rows:
i=0;
temperature=0;
sum=0;
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);

lcd.print("EC Department MI LAB");


for(i=0;i<10;i++)
{
lcd.scrollDisplayLeft();
delay(500);
}
lcd.clear();
delay(1000);
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):

Lab Manual of Microprocessor & Microcontroller (3141008) Page 38


sum = 0; // initialize sum before starting to 0.
for (i = 0; i<100; i++)
{
sum = sum + (long)(analogRead(sensorPin1));
delay(10); // wait for some time before taking next sample.
}
sum = sum /100L;
temperature = ( ((float)(sum) )* 5000.0 /1023.00 )/10.0;
/* convert int temp into voltage in mV. then divide it by 10 to get temperature in
degree Celsius.*/

if(temperature>70)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.setCursor(6, 1);
lcd.print(temperature);
lcd.setCursor(13,1);
lcd.print("Deg.C.:");
delay(100);
}
:: WORKSHEET::
Activity #1: In this experiment set up remove LM35 output connection with
Arduino board and connect LDR with A0 pin of Arduino board.

Observation:

Display on LCD when full light on LDR: ____________

Display in LCD in dark condition: ________________

Activity #2: Write Arduino program such that LED connected with Pin 13
(On board LED) should turn OFF when light falls on LDR and it should turn
ON in dark condition.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 39
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #3: Connect LCD Data lines D7 to D4 with pins 2 to 5. RS with pin
11 and Enable with pin 10. (As per earlier experiment) Connect Switch with
pin number 7. Write program to display number on LCD depending on
number of times switch is pressed. It should display “count value”
corresponding to number of times switch is pressed.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 40


EXPERIMENT NO. 7

AIM: Write assembly language programs for ATMega32 Microcontroller and


simulate using ATMEL Studio

Objectives:
 To install ATMEL Studio
 To learn assembly language instructions for AVR Microcontroller

Download ATMEL Studio from the website http://www.atmel.com or obtain


setup from the faculty in DVD or Pen drive. Install ATMEL Studio in your
desktop PC or laptop.

Program 1: Write and assemble a program to load a value into each location
of R20 – R23. Complement the value in each register. Use the simulator to
single-step and examine the flags and register content after the execution of
each instruction

.INCLUDE "M32DEF.INC"

LDI R20,0 ;Transfer (Copy) Value 0 to register R20


LDI R21,0xFF ;Transfer (Copy) Value FFh to register R21
LDI R22,0x11 ;Transfer (Copy) Value 11h to register R22
LDI R23,0x55 ;Transfer (Copy) Value 55h to register R23

COM R20 ;Complement content of register R20


COM R21 ;Complement content of register R21
COM R22 ;Complement content of register R22
COM R23 ;Complement content of register R23

L1: RJMP L1 ;Infinite Loop

Note:
 LDI Instruction is used to load immediate 8 bit data to general
purpose registers.
 COM Instruction is used to complement content of registers
 There are 32 general purpose registers in AVR microcontroller known
as R0,R1,R2 …. R31.
 LDI instruction cannot be used to load data in registers R0 to R15. It
can be used for registers R16 to R31
 Semicolon (;) can be used to write comments (Anything written after ;
will be ignored by compiler). It is necessary for readability of programs
 0x sign before the number is used to represent hexadecimal number
i.e. 0xFF is hexadecimal number whose decimal equivalent is 255.
Alternately $ sign can be used to represent hexadecimal number.

EC Department, L.D. College of Engineering, Ahmedabad Page 41


Program 2: Write and assemble a program to load a value 0x55 to register
R16, 0x33 to register R17, Transfer content of R16 to R0 and R17 to R1.
Add value of registers R0 and R1 and store result in register R0. Transfer
value of register R16 to R2 and R17 to R3. Subtract R3 from R2 and store
result in register R2.

.INCLUDE "M32DEF.INC"

LDI R16,$55 ;Transfer hexadecimal value 0x55 to register R16


LDI R17,$33 ;Transfer hexadecimal value 0x33 to register R17
MOV R0,R16 ; Move (copy) content of register R16 into R0
MOV R1,R17 ; Move (copy) content of register R17 into R1
ADD R0,R1 ; Add content of register R0 and R1 and store result in R0
MOV R2,R16 ; Move (copy) content of register R16 into R2
MOV R3,R17 ; Move (copy) content of register R16 into R17
SUB R2,R3 ; Subtract R3 from R2 and store result in R2
LOOP: RJMP LOOP ; Infinite loop

Note:
 $ sign is used to represent hexadecimal value (Option of 0x )
 MOV instruction is used to copy one register value into other …
(Source and destination can be any register R0 to R31)
 ADD instruction used to add content of specified operands
 SUB instruction used to subtract content of specified operands
 RJMP is unconditional relative jump instruction used to create infinite
loop at the end of the program.

Program 3: Write and assemble a program to flash LEDs connected to Port


B. Simulate program to observe flashing on Port B.

.INCLUDE "M32DEF.INC"
LDI R16, 0xFF ; Set Port B as an output Port
OUT DDRB, R16 ;
LDI R16, 0x08 ; Define Stack pointer 0x085F
OUT SPH,R16
LDI R16, 0x5F
OUT SPL,R16

AGAIN:
LDI R16, 0x55 ;Copy data 0x55 to register R16
OUT PORTB, R16 ; Turn alternate LEDs ON
RCALL DELAY
LDI R16, 0xAA ;Copy data 0x55 to register R16
OUT PORTB, R16 ; Make ON LED OFF and OFF LED ON
RCALL DELAY
RJMP AGAIN
DELAY:
LDI R20,0x05
L3: LDI R21,0x01 ;Load 0xFF for hardware
L2: LDI R22,0x01 ;Load 0xFF for hardware
L1: DEC R22
BRNE L1 ; Brach if Z=0 i.e. if register content=0
DEC R21

Lab Manual of Microprocessor & Microcontroller (3141008) Page 42


BRNE L2
DEC R20
BRNE L3
RET

Note:
 DDRB defines direction of PORT B. If we transfer 0x00 to DDRB, port
becomes input port and if we transfer 0xFF to DDRB, it becomes
output port.
 We are connecting 8 LEDs with port B so 0xFF is first loaded into R16
and then R16 is copied to DDRB to initialize PORT B as an output
port.
 This program uses “DELAY” subroutine
 When “DELAY” subroutine is called from Main program using RCALL
(Relative call) instruction, value of Program counter is stored in stack
memory pointed by SP(Stack pointer) at 0x085F
 DELAY routine uses three counters using registers R20, R21 and R22,
it decrements these registers until it becomes ZERO. For simulation
use 0x01 for R21 and R22 (Less delay) and for hardware use 0xFF to
get enough observation period for flashing of LEDs

Program 4: Write assembly language program to monitor switch connected


at port Pin PB2, When Switch is not pressed (logic HIGH), send value 0x55
to port C and high to low pulse at port pin PD3

.INCLUDE "M32DEF.INC"
CBI DDRB, 2 ;Set PB2 pin as an input pin
LDI R16, 0xFF ;Set Port C as an output Port
OUT DDRC, R16 ;
SBI DDRD, 3 ; Make PD3 pin output
AGAIN:
SBIS PINB, 2 ; SKIP next instruction (i.e. exit loop) if
PB2 is high (SET)
RJMP AGAIN
LDI R16, 0x45
OUT PORTC, R16
SBI PORTD, 3
CBI PORTD, 3
HERE: RJMP HERE

Note:
 CBI instruction used to clear bit immediately CBI DDRB,2 clears PB2
bit to use it input pin (Because switch is connected)
 SBI instruction is used to set bit immediately SBI DDRD,3 set pin PD3
to make it output pin (because we want to generate pulse at PD3)

EC Department, L.D. College of Engineering, Ahmedabad Page 43


Use ATMEL Studio or AVR Studio for following Assembly language program
for Arduino board.

Assembly language program to blink LED:


SBI DDRB,5 ;In-built Arduino LED connected to PB5 pin
LDI R16, 0x08 ; Define Stack pointer
OUT SPH,R16
LDI R16, 0x5F
OUT SPL,R16
AGAIN:

SBI PORTB,5 ;In built Arduino LED


RCALL DELAY
CBI PORTB,5
RCALL DELAY
RJMP AGAIN

DELAY:
LDI R20,0x05
L3: LDI R21,0xFF
L2: LDI R22,0xFF
L1: DEC R22
BRNE L1
DEC R21
BRNE L2
DEC R20
BRNE L3
RET

:: WORKSHEET::
Activity #1:Write and assemble the following program. Use the simulator
to single-step and examine the flags and register content after the
execution of each instruction.

.INCLUDE "M32DEF.INC"

LDI R20,$27
LDI R21,$15
SUB R20, R21

LDI R20,$20
LDI R21,$15
SUB R20, R21

LDI R24,95
LDI R25,95
SUB R24, R25

LDI R22,50
LDI R23,70
SUB R22, R23

L1: RJMP L1

What is purpose of $ sign?

Lab Manual of Microprocessor & Microcontroller (3141008) Page 44


------------------------------------------------------------------------------------------

Activity #2: Examine status of carry (C) flag after execution of following
instructions in single stepping mode.

LDI R20, $85


LDI R21, $92
ADD R20, R21
LOOP: RJMP LOOP

Activity #3: Write assembly language program to make LEDs connected


at port B ON one by one at certain specified delay

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #4: Write assembly language program to read switches connected


at PORT D and display status of these switches on LEDs connected to
PORTB. Simulate this program using ATMEL studio.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 45
Activity #5 : W.A.P. to find the number of 1’s in a given byte stored at
location 0x300.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #6: W.A.P. to convert packed BCD no. to two ASCII no. and place
them in R21 and R22.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 46


EXPERIMENT NO. 8

AIM: Understand hardware of ATMega32 Kit. Write and execute following


programs …
 Program to flash LEDs
 Program to read status of switch display it on LEDs.
 Program to display count value 0 to 9 on seven segment display at the
interval of approximately 1 second.
 Program to read status of switches connected at port A and display its
number on seven segment display.
Objectives:
 Understand hardware components of ATMega32 kit
 Write and execute program in C language to flash (Blink) LEDs
 Write program to read switches and take action based on status of
switches
 Interfacing of seven segment display with ATMega32 microcontroller
 Interfacing of switches with ATMega32 microcontroller
 Write program to display numbers on seven segment display using
look up table method.

AVRDude:
AVRdude is command line tool to program HEX (Binary) file from PC to AVR
microcontroller.

Command line for avrdude:


avrdude -p atmega32 -P COM1 -c siprog -U flash:w:main.hex -U
hfuse:w:0xD9:m -U lfuse:w:0xEE:m

-p Indicates part number we are using ATMega32 so atmega32 is written


-P indicates com port (serial port) used for the programming
(If you are using USB to RS232 converter cable, change it depending on
which COM port is assigned to the cable)
-c indicates type of programmer ATMega32 kit uses serial programming so
siprog is written
-U indicates memory type. We are writing program in flash memory so flash
is specified.
w indicates hex file which will be written in flash memory

HFUSE and LFUSE is for AVR fuse bits. Fuse bits are used to choose
features of AVR microcontroller. ATMega32 has two fuse bytes: hfuse and
lfuse. (Refer Chapter 8 of text book for more details)

EC Department, L.D. College of Engineering, Ahmedabad Page 47


All the commands related to programming of AVR is stored in the makefile
We can also program HEX file in the chip by giving command avrdude from
the command prompt. Students can prepare batch file so that it is easy to
give command from the command prompt.

Another way is configure ATMEL Studio to Program ATMega32 Kit using


ToolsExternal tool option. Specify path of avrdude and give above
parameters as arguments to program chip using this short-cut.

ATMega32 Kit Hardware Connection Details:

Sr. No. Hardware Connected with DIP Switch Settings

1 8 RED LEDs PORT D DIP SW1.8 ON other OFF

2 LCD Data PORT D DIP SW1.1,SW1.2,SW1.3 ON

RS PB0 To connect RS,E and RW

E PB2

RWPB1

3 Switches PORT A DIP SW1.1,SW1.2,SW1.3 and


SW1.4 ON, Others OFF

4 Temperature sensor SCL-PC0 DIP SW1.1, 1.2,1.3 ON


LM35
SDA-PC1 Others OFF

(Through
PCF8591
ADC/DAC)

5 Seven Segment Display PD0-PD7 Data DIP SW1.5,SW1.6,SW1.7 ON

PBO-PB3 Decoder Others OFF


input

Switch setting on CPU card:

Sr. No. Interfacing Hardware Position of Switch

1 LCD, Seven Segment, 8 Input SW, SW3 and SW4 OFF


ADC/DAC, LM35 Temp Sensor
SW5,SW6,SW7 ON

2 8 Output LEDs All SW3 to SW7 OFF

Lab Manual of Microprocessor & Microcontroller (3141008) Page 48


DIP Switch settings on ATMega32 Kit for LED program:
DIP SW1.8 ON other OFF

Circuit diagram for LED Interfacing:

1
RN1
470

9
8
7
6
5
4
3
2
10
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

Sample program to FLASH (Blink) LEDs :


Let us understand first program of blinking (Flashing) LEDs. This program
blinks LEDs connected at Port D of ATMega32 Kit.

Program 1 Flash LEDs:

# include <avr/io.h>
void mydelay(int count)
{
int i,j;
for(int i=0;i<=count;i++)
{
for(int j=0;j<=100;j++);
}
}

int main(void)
{
DDRD=0xFF; //Port D Output Port
PORTD=0xFF;
EC Department, L.D. College of Engineering, Ahmedabad Page 49
while(1)
{
PORTD=0xFF;
mydelay(1000);
PORTD=0x00;
mydelay(1000);
}
return 0;
}

Assembly Language Program:


LDI R16, 0xFF ; Set Port B as an output Port
OUT DDRD, R16 ; Set Port D as an output Port
LDI R16, 0x08 ; Define Stack pointer
OUT SPH,R16
LDI R16, 0x5F
OUT SPL,R16
AGAIN:
LDI R16,0x55 ;Alternate LED ON
OUT PORTD,R16
RCALL DELAY ;Call delay subroutine
LDI R16, 0xaa ; Complement sequence of LEDs
OUT PORTD,R16
RCALL DELAY
RJMP AGAIN
DELAY:
LDI R20,0x05
L3: LDI R21,0xFF
L2: LDI R22,0xFF
L1: DEC R22
BRNE L1
DEC R21
BRNE L2
DEC R20
BRNE L3
RET

Sample program read switches and glow LEDs based on status of


switches.
This program reads status of switch continuously and glow LED if switch is
ON.

Lab Manual of Microprocessor & Microcontroller (3141008) Page 50


Program 2: Read status of switches and display status on LEDs
# include <avr/io.h>
int main(void)
{
unsigned char switchdata;
DDRD=0xFF; //Port D Output Port, LEDs are connected with Port D
DDRA=0x00; //Port A Input Switches are connected with Port A
while(1)
{
switchdata=PINA;
PORTD= switchdata;
}
}

Interfacing diagram of Seven segment display and switches:

9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
OFF ON PC4/TDO
16 1 40 27
PA0/ADC0 PC5/TDI
15 2 39 28
PA1/ADC1 PC6/TOSC1
14 3 38 29
PA2/ADC2 PC7/TOSC2
13 4 37
PA3/ADC3
12 5 36 14
PA4/ADC4 PD0/RXD
11 6 35 15
PA5/ADC5 PD1/TXD
10 7 34 16
PA6/ADC6 PD2/INT0
9 8 33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

Sample program to read switches and display its hexadecimal number


of seven segment display.

DIP Switch settings:


DIP SW1.5,SW1.6,SW1.7 ON, Others OFF

Program 3: Read status of switches and display its hexadecimal number on


seven segment display.

# include <avr/io.h>
void mydelay(int count)

EC Department, L.D. College of Engineering, Ahmedabad Page 51


{
int i,j;
for(int i=0;i<=count;i++)
{
for(int j=0;j<=100;j++);
}
}

int main(void)
{
DDRD=0xFF; //Port D Output Port
DDRA=0x00; //Port A Input
unsigned char ssdata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,
0xF8,0x80,0x90,0xC8,0x83,0xC6,0xA1,0x86,
0x8E,0x00};
unsigned char switchdata;
while(1)
{
switchdata=PINA;
switchdata=switchdata&0x0F;
PORTD=ssdata[switchdata];
}
}

Program 4: Display count values on Seven Segment display:

# include <avr/io.h>
void mydelay(int count)
{
int i,j;
for(int i=0;i<=count;i++)
{
for(int j=0;j<=100;j++);
}
}
int main(void)
{
unsigned char count i=0;
DDRD=0xFF; //Port D Output Port
DDRA=0x00; //Port A Input
unsigned char ssdata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,

Lab Manual of Microprocessor & Microcontroller (3141008) Page 52


0xF8,0x80,0x90,0xC8,0x83,0xC6,0xA1,0x86,
0x8E,0x00};
while(1)
{
PORTD=ssdata[count];
count++;
if(count==10) count=0;
}
}
Use ATMEL Studio or AVR Studio for following C language and Assembly
language program for Arduino board.

Program 5: Use following C language program to blink internal LED of


Arduino board:

# include <avr/io.h>
void delay_ms(int count)
{
for(int i=0;i<=count;i++)
{
for(int j=0;j<=1000;j++);
}
}

int main(void)
{
DDRD=0xFF; //Port D Output Port
DDRB=DDRB|(1<<5); //Inbuilt Arduino LED connected to PB5 pin
DDRD=0xFF;
while(1)
{
PORTB=PORTB|(1<<5);
delay_ms(5000);
PORTB=PORTB&~(1<<5);
delay_ms(5000);
}
return 0;
}

EC Department, L.D. College of Engineering, Ahmedabad Page 53


:: WORKSHEET::
Activity #1:

Modify sample program 1 of blink LED so that Alternate LED blinks in


following manner. Write program again.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #2:

Modify program given in Activity #1 in such a way that LED should glow one
by one from left to right and than from right to left continuously…

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 54


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #3:

Write assembly language program to glow LEDs one by one at the interval of
1 second.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #4: Write program for down-counter from 9 to 0 and display it on
seven segment display.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 55


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #5:

Write program to display number 1234 on four seven segment display one
by one at the interval of 1 second.
(Hint: Send seven segment hex code corresponds to 1 on PORTD, make PB0
pin high, after 1 second send seven segment hex code of number 2 on
PORTD and make PB1 high and so on for number 3 (PB2 high) and 4 (PB3
high).
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 56


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #6:

Read switch positions connected at PORT A pins PA4 to PA7. Display its 1’s
complemented value on seven segment display.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 57


EXPERIMENT NO. 9
AIM: Write program to display message “EC Department” on first line of
LCD & GLCD. Simulate circuit in PROTEUS software
Objectives:
 Understand Interfacing of LCD & GLCD with microcontroller
 Write program to display messages and numbers on LCD & GLCD

Interfacing diagram:

LCD1
LM016L

VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
9 22
RESET PC0/SCL

10

12
13
14
1
2
3

4
5
6

7
8
9

11
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

DIP Switch settings:


DIP SW1.1,SW1.2,SW1.3 ON

C Program:
# include <avr/io.h>
# include <util/delay.h>
#define LCD_DATA PORTD
#define LCD_DDR DDRD
#define LCD_CMD PORTB
#define LCD_CDDR DDRB
#define RS 0
#define RW 1
#define EN 2

void mydelay(int count)


{

Lab Manual of Microprocessor & Microcontroller (3141008) Page 58


int i,j;
for(int i=0;i<=count;i++)
{
for(int j=0;j<=100;j++);
}
}
void lcdCommand(unsigned char);
void lcdData(unsigned char);
void lcdMessage(char *);
void lcdInit();
int main(void)
{
lcdInit();
lcdCommand(0x80);
lcdMessage("LDCE Ahmedabad");
lcdCommand(0xC0);
lcdMessage("EC Department");
while(1);
return 0;
}
void lcdInit()
{
LCD_DDR=0xFF;
LCD_CDDR=0xFF;
lcdCommand(0x38); //Initialise LCD for 5x7 dots/char, 2 lines
lcdCommand(0x0E); //Display ON and Cursor ON
lcdCommand(0x01); //Clear LCD
mydelay(10);
lcdCommand(0x06); //Shift cursorn right
}
void lcdCommand(unsigned char cmd)
{
LCD_DATA=cmd;
LCD_CMD=LCD_CMD & ~(1<<RS); //Make RS=0 to send command to LCD
LCD_CMD=LCD_CMD & ~(1<<RW); //Make RW=0 to for Write operation
LCD_CMD=LCD_CMD | (1<<EN); //Make EN=1 to generate pulse
mydelay(1);
LCD_CMD=LCD_CMD & ~(1<<EN); //Make EN=0 to complete pulse
mydelay(10);
}
void lcdData(unsigned char mydata)
{
LCD_DATA=mydata;
LCD_CMD=LCD_CMD | (1<<RS); //Make RS=1 to send Data to LCD
LCD_CMD=LCD_CMD & ~(1<<RW); //Make RW=0 to for Write operation

EC Department, L.D. College of Engineering, Ahmedabad Page 59


LCD_CMD=LCD_CMD | (1<<EN); //Make EN=1 to generate pulse
mydelay(1);
LCD_CMD=LCD_CMD & ~(1<<EN); //Make EN=0 to complete pulse
mydelay(10);
}
void lcdMessage(char *msg)
{
unsigned char i=0;
while(msg[i]!=0)
{
lcdData(msg[i]);
i++;
}
}
Assembly Language Program for LCD:
.EQU LCD_DATA=PORTD ;Port D drives data lines
.EQU LCD_DDR =DDRD
.EQU LCD_CMD = PORTB ;Port B drives control signals RS,E and RW
.EQU LCD_CDDR = DDRB
.EQU RS=0
.EQU RW=1
.EQU EN=2
LDI R16, 0xFF ; Set Port D as an output Port
OUT LCD_DDR, R16 ; we can also write OUT DDRD,R16
OUT LCD_CDDR,R16 ; We can also write OUT DDRB,R16
LDI R16, 0x08 ; Define Stack pointer
OUT SPH,R16 ;Stack pointer initialisation
LDI R16, 0x5F
OUT SPL,R16
CBI LCD_CMD,EN ;Make EN=0 Initially disable LCD
CALL LCD_INIT
LDI R31,HIGH(msg1<<1) ;Pointer for LCD Message
LDI R30,LOW(msg1<<1)
LOOP:
LPM R16,Z+ ;Get data from memory and increment memory pointer
CPI R16,0
BREQ SecondLine
CALL LCDData
RJMP LOOP
SecondLine:
LDI R16,0xC0 ; Move cursor to second line, first column
CALL LCDCommand
LDI R31,HIGH(msg2<<1) ;Pointer for LCD Message
LDI R30,LOW(msg2<<1)
LOOP1:
LPM R16,Z+ ;Get data from location defined by memory pointer and increment
; memory pointer
CPI R16,0
BREQ EXIT

Lab Manual of Microprocessor & Microcontroller (3141008) Page 60


CALL LCDData
RJMP LOOP1
EXIT:
JMP EXIT
LCD_INIT:
LDI R16,0x38 ;Initialise LCD for 5x7 dots/char, two lines
CALL LCDCommand
LDI R16,0x0E ;Display ON, Cursor ON
CALL LCDCommand
LDI R16,0x01 ; Clear LCD
CALL LCDCommand
LDI R16,0x06 ; Shift cursor right
CALL LCDCommand
LDI R16,0x80 ; Move cursor to first line, first column
CALL LCDCommand
RET
LCDCommand:
OUT LCD_DATA,R16
CBI LCD_CMD,RS ;RS=0 to select command register
CBI LCD_CMD,RW ;RW=0 for write operation
SBI LCD_CMD,EN ;EN=1 to enable LCD
NOP ;Short delay with NOP
NOP
NOP
CBI LCD_CMD,EN ; EN=0
CALL DELAY ;Call delay
RET
LCDData:
OUT LCD_DATA,R16
SBI LCD_CMD,RS ;RS=0 to select command register
CBI LCD_CMD,RW ;RW=0 for write operation
SBI LCD_CMD,EN ;EN=1 to enable LCD
NOP ; short delay with NOP
NOP
NOP
CBI LCD_CMD,EN ; EN=0
CALL DELAY ;Call delay
RET
DELAY:
LDI R21,0x05
L2: LDI R22,0xFF
L1: DEC R22
BRNE L1
DEC R21
BRNE L2
RET
msg1: .db "GEC Rajkot",0
msg2: .db "EC DEPARTMENT ",0

EC Department, L.D. College of Engineering, Ahmedabad Page 61


Interfacing diagram:

C Program:
#define F_CPU 8000000UL /* Define CPU clock Frequency 8MHz */

#include <avr/io.h> /* Include AVR std. library file */

#include <util/delay.h> /* Include defined delay header file */

#include <stdio.h> /* Include standard i/o library file */

#include "Font_Header.h"

#define Data_Port PORTD /* Define data port for GLCD */


#define Command_Port PORTC /* Define command port for GLCD */
#define Data_Port_Dir DDRD /* Define data port for GLCD */
#define Command_Port_Dir DDRC /* Define command port for GLCD */
#define RS PC0 /* Define control pins */
#define RW PC1
#define EN PC2
#define CS1 PC3
#define CS2 PC4
#define RST PC5
#define TotalPage 8
void GLCD_Command(char Command) /* GLCD command function */
{

Data_Port = Command; /* Copy command on data pin */


Command_Port &= ~(1 << RS); /* Make RS LOW to select command register */
Command_Port &= ~(1 << RW); /* Make RW LOW to select write operation */
Command_Port |= (1 << EN); /* Make HIGH to LOW transition on Enable pin */
_delay_us(5);
Command_Port &= ~(1 << EN);
_delay_us(5);
}

Lab Manual of Microprocessor & Microcontroller (3141008) Page 62


void GLCD_Data(char Data) /* GLCD data function */
{
Data_Port = Data; /* Copy data on data pin */
Command_Port |= (1 << RS); /* Make RS HIGH to select data register */
Command_Port &= ~(1 << RW); /* Make RW LOW to select write operation */
Command_Port |= (1 << EN); /* Make HIGH to LOW transition on Enable pin */
_delay_us(5);
Command_Port &= ~(1 << EN);
_delay_us(5);
}

void GLCD_Init() /* GLCD initialize function */


{
Data_Port_Dir = 0xFF;
Command_Port_Dir = 0xFF;
/* Select both left & right half of display & Keep reset pin high */
Command_Port |= (1 << CS1) | (1 << CS2) | (1 << RST);
_delay_ms(20);
GLCD_Command(0x3E); /* Display OFF */
GLCD_Command(0x40); /* Set Y address (column=0) */
GLCD_Command(0xB8); /* Set x address (page=0) */
GLCD_Command(0xC0); /* Set z address (start line=0) */
GLCD_Command(0x3F); /* Display ON */
}

void GLCD_ClearAll() /* GLCD all display clear function */


{
int i,j;
/* Select both left & right half of display */
Command_Port |= (1 << CS1) | (1 << CS2);
for(i = 0; i < TotalPage; i++)
{
GLCD_Command((0xB8) + i); /* Increment page each time after 64 column */
for(j = 0; j < 64; j++)
{
GLCD_Data(0); /* Write zeros to all 64 column */
}
}
GLCD_Command(0x40); /* Set Y address (column=0) */
GLCD_Command(0xB8); /* Set x address (page=0) */
}

void my_GLCD_String(char page_no, char *str) /* GLCD string write function */


{
unsigned int i, column;
unsigned int Page = ((0xB8) + page_no);
unsigned int Y_address = 0;
float Page_inc = 0.5;
Command_Port |= (1 << CS1); /* Select first Left half of display */
Command_Port &= ~(1 << CS2);
GLCD_Command(Page);
for(i = 0; str[i] != 0; i++) /* Print each char in string till null */
{
for(column=0; column<FontWidth; column++)
{
GLCD_Data(font[str[i]-32][column]); /* Then continue to print hat char */
if((Y_address+1)%64==0) /* check whether it gets overflowed from either half of side */
{
Command_Port ^= (1 << CS1); /* If yes then change segment controller to display
on other half of display */
Command_Port ^= (1 << CS2);
GLCD_Command((Page+Page_inc)); /* Execute command for page change */
Page_inc = Page_inc + 0.5; /* Increment Page No. by half */
}
Y_address++; /* Increment Y_address count per column */
}
GLCD_Data(0); /* Add one column of zero to print next character next of zero */
Y_address++; /* Increment Y_address count for last added zero */
if((Y_address)%64 == 0) /* check whether it gets overflowed from either half of side */
{

EC Department, L.D. College of Engineering, Ahmedabad Page 63


Command_Port ^= (1 << CS1); /* If yes then change segment controller to display on other
half of display */
Command_Port ^= (1 << CS2);
GLCD_Command((Page+Page_inc)); /* Execute command for page change */
Page_inc = Page_inc + 0.5; /* Increment Page No. by half */
}
}
GLCD_Command(0x40); /* Set Y address (column=0) */
}

void GLCD_String(char page_no, char *str) /* GLCD string write function */


{
unsigned int i, column;
unsigned int Page = ((0xB8) + page_no);
unsigned int Y_address = 0;
float Page_inc = 0.5;
Command_Port |= (1 << CS1); /* Select first Left half of display */
Command_Port &= ~(1 << CS2);
GLCD_Command(Page);
for(i = 0; str[i] != 0; i++) /* Print each char in string till null */
{
if (Y_address > (1024-(((page_no)*128)+FontWidth))) /* Check Whether Total Display get
overflowed */
break; /* If yes then break writing */
if (str[i]!=32) /* Check whether character is not a SPACE */
{
for (column=1; column<=FontWidth; column++)
{
if ((Y_address+column)==(128*((int)(Page_inc+0.5)))) /* If yes then check whether it
overflow from right side of display */
{
if (column == FontWidth) /* Also check and break if it overflow after 5th column */
break;
GLCD_Command(0x40); /* If not 5th and get overflowed then change Y
address to START column */
Y_address = Y_address + column; /* Increment Y address count by column no. */
Command_Port ^= (1 << CS1); /* If yes then change segment controller
to display on other half of display */
Command_Port ^= (1 << CS2);
GLCD_Command(Page + Page_inc); /* Execute command for page change */
Page_inc = Page_inc + 0.5; /* Increment Page No. by half */
}
}
}
if (Y_address>(1024-(((page_no)*128)+FontWidth))) /* Check Whether Total Display get
overflowed */
break; /* If yes then break writing */
if((font[((str[i]-32)*FontWidth)+4])==0 || str[i]==32) /* Check whether character is SPACE or
character last column is zero */
{
for(column=0; column<FontWidth; column++)
{
GLCD_Data(font[str[i]-32][column]); /* If yes then then print character */
if((Y_address+1)%64==0) /* check whether it gets overflowed from either half of side */
{
Command_Port ^= (1 << CS1); /* If yes then change segment controller
to display on other half of display */
Command_Port ^= (1 << CS2);
GLCD_Command((Page+Page_inc)); /* Execute command for page change */
Page_inc = Page_inc + 0.5; /* Increment Page No. by half */
}
Y_address++; /* Increment Y_address count per column */
}
}
else /* If character is not SPACE or character last column is not zero */
{
for(column=0; column<FontWidth; column++)
{
GLCD_Data(font[str[i]-32][column]); /* Then continue to print hat char */
if((Y_address+1)%64==0) /* check whether it gets overflowed from either half of side */
{

Lab Manual of Microprocessor & Microcontroller (3141008) Page 64


Command_Port ^= (1 << CS1); /* If yes then change segment controller
to display on other half of display */
Command_Port ^= (1 << CS2);
GLCD_Command((Page+Page_inc)); /* Execute command for page change */
Page_inc = Page_inc + 0.5; /* Increment Page No. by half */
}
Y_address++; /* Increment Y_address count per column */
}
GLCD_Data(0); /* Add one column of zero to print next character next of zero */
Y_address++; /* Increment Y_address count for last added zero */
if((Y_address)%64 == 0) /* check whether it gets overflowed from either half of side */
{
Command_Port ^= (1 << CS1); /* If yes then change segment
controller to display on other half of display */
Command_Port ^= (1 << CS2);
GLCD_Command((Page+Page_inc)); /* Execute command for page change */
Page_inc = Page_inc + 0.5; /* Increment Page No. by half */
}
}
}
GLCD_Command(0x40); /* Set Y address (column=0) */
}

int main(void)
{

GLCD_Init(); /* Initialize GLCD */

GLCD_ClearAll(); /* Clear all GLCD display */

my_GLCD_String(0,"Microprocessor");

my_GLCD_String(1," and ");

my_GLCD_String(2,"Microcontroller"); /* Print String on 0th page of display */

while(1);

Activity #1: Write program in C language to display switch number on LCD.


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 65


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #2: Write program in C language to display different message on the LCD
depending on position of switches. If SW1 ON: First Floor SW2 ON: Second Floor SW3 ON:
Third Floor and so on….
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Microprocessor & Microcontroller (3141008) Page 66
EXPERIMENT NO. 10

AIM: Write and execute program to rotate stepper motor clockwise and
anticlockwise with half step and full step mode

Objectives:
 Understand Interfacing stepper motor with microcontroller
 Write program to rotate stepper motor in anticlockwise and clockwise
direction.
 Write program to rotate stepper motor at different speeds.

Introduction:
Stepper motor is brushless synchronous motor which runs in discrete
steps. It divides full rotation into discrete steps. Stepper motor is
manufactured with steps per revolution of 12, 24, 72, 144, 180, 200 and
400 which results into step angle of 300, 150, 50, 2.50, 20, 1.80, and 0.90 per
step. Stepper motor can be controlled precisely with or without feedback. It
converts electrical pulses into mechanical rotation in steps.

EC Department, L.D. College of Engineering, Ahmedabad Page 67


Stepper motor is widely used in Robots, Machine tools, CNC
machines, floppy drives, semiconductor wafer cutting machines and many
other industrial control applications.
Stepping motors come in three varieties:
 Permanent magnet type
 Variable reluctance type
 Hybrid type (Combination of above two)
Lacking a label on the motor, we can generally tell the two apart by
feeling when no power is applied. Permanent magnet motors tend to “cog” as
we twist the rotor with our fingers, while variable reluctance motors almost
spin freely (although they may cog slightly because of residual magnetization
in the rotor). we can also distinguish between the two varieties with an
ohmmeter. Variable reluctance motors usually have three (sometimes four)
windings, with a common return, while permanent magnet motors usually
have two independent windings, with or without center taps. Center-tapped
windings are used in uni-polar permanent magnet motors.

Driving Uni-polar motor with ULN2803 (8 Darlington pair transistors):

A
U1
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
40
PA0/ADC0
PC4/TDO
PC5/TDI
26
27 D B
39 28
38
PA1/ADC1 PC6/TOSC1
29 U2
PA2/ADC2 PC7/TOSC2
37 10
PA3/ADC3 COM
36
35
PA4/ADC4
PA5/ADC5
PD0/RXD
PD1/TXD
14
15
1
2
1B
2B
1C
2C
18
17 C
34 16 3 16
PA6/ADC6 PD2/INT0 3B 3C
33 17 4 15
PA7/ADC7 PD3/INT1 4B 4C
18 5 14
PD4/OC1B 5B 5C
1 19 6 13
PB0/T0/XCK PD5/OC1A 6B 6C
2 20 7 12
PB1/T1 PD6/ICP1 7B 7C
3 21 8 11
PB2/AIN0/INT2 PD7/OC2 8B 8C
4
PB3/AIN1/OC0
5 ULN2803
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

Lab Manual of Microprocessor & Microcontroller (3141008) Page 68


In bipolar stepper Motor there is single winding per pole. Total there are two
coils. Direction of current for stepper motor needs to be changed by the
driving circuit. So the driving circuit of the bipolar stepper motor becomes
complex. Bipolar motor comes with four terminals (Two terminals per coil as
shown in following figure. Each coil requires H-bridge to drive. Total two H-
bridge requires driving one stepper motor.

Interfacing Diagram for Bipolar Mode:


+12V +12V

470 470
PD0 Q5 PD3 Q5
BC557 BC557

470 470
Q1 Q3 Q1 Q3
BC547 BC547 BC547 BC547
PD1 Winding
PD4 Winding
Red Black
Yellow Brown/
470 470 Orange
Q2 Q4 Q2 Q4
BC547 BC547 BC547 BC547
PD2 PD5

IC L293D has inbuilt two H-bridge so it can be used to drive bi-polar stepper
motor.

Arduino Program:
[1] Rotate motor in full step uni-polar mode
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
speed = 500;

EC Department, L.D. College of Engineering, Ahmedabad Page 69


void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop() {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(speed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(speed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(speed);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(speed);
}
C Program for ATMega32 microcontroller to rotate motor in clockwise
direction in full-step uni-polar mode:
(Stepper Motor is connected to Port D PD0-PD3 pins via ULN2803)

#include <avr/io.h>
#include <avr/delay.h>

int main(void)
{
unsigned char stepdata=0x01;
DDRD=0x0F; //PD0-PD3 output to drive stepper motor
while(1)
{
PORTD=stepdata;
_delay_ms(1000);
stepdata<<=1;
if(stepdata==0x10) stepdata=0x01;
}
}

Lab Manual of Microprocessor & Microcontroller (3141008) Page 70


C Program for ATMega32 Microcontroller:
Write program to rotate stepper motor in clockwise direction if interrupt 0
occurs and in anticlockwise direction if interrupt 1 occurs. Stepper motor is
connected to Port C PC0-PC3 pins and interrupts are generated with limit
switches.

Circuit Diagram:

U1 10
COM
9 22 1 18
RESET PC0/SCL 1B 1C
23 2 17
PC1/SDA 2B 2C
13 24 3 16
XTAL1 PC2/TCK 3B 3C
12 25 4 15
XTAL2 PC3/TMS 4B 4C
26 5 14
PC4/TDO 5B 5C
40 27 6 13
PA0/ADC0 PC5/TDI 6B 6C
39 28 7 12
PA1/ADC1 PC6/TOSC1 7B 7C
38 29 8 11 +88.8
PA2/ADC2 PC7/TOSC2 8B 8C
37
PA3/ADC3
36 14 ULN2803
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5 Anti-clockwise Clockwise
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
unsigned char stepdata=0x01;
int main(void)
{
DDRC=0x0F; //PC0-PC3 output to drive stepper motor
PORTD=1<<2; //Pull up of PD2 activated
PORTD=1<<3;
MCUCR=0x02; //Negative Edge Trigerred
GICR=0xE0;
sei(); //SET interrupt flag in SREG
while(1);
}
ISR(INT0_vect)
{
sei();
while(1)
{
PORTC=stepdata;
_delay_ms(200);
stepdata<<=1;
if(stepdata==0x10) stepdata=0x01;
EC Department, L.D. College of Engineering, Ahmedabad Page 71
}
}
ISR(INT1_vect)
{
sei();
while(1)
{
PORTC=stepdata;
_delay_ms(200);
stepdata>>=1;
if(stepdata==0x00) stepdata=0x08;
}
}

Assembly language program to rotate motor in clockwise direction in


full step mode:
(Considering stepper motor is connected to Port C PC0-PC3 pins using ULN
2803 driver IC)

LDI R16,0x0F ;PC0-PC3 Output pins


OUT DDRC,R16
LDI R16, 0x08 ; Define Stack pointer
OUT SPH,R16
LDI R16, 0x5F
OUT SPL,R16
LDI R16,0x01 ;Initial Data for stepper motor
LOOP:
OUT PORTC,R16
RCALL DELAY
LSL R16
CPI R16,0x10
BRNE LOOP
LDI R16,0x01
RJMP LOOP
DELAY:
LDI R20,0x05
L3: LDI R21,0xFF
L2: LDI R22,0xFF
L1: DEC R22
BRNE L1
DEC R21
BRNE L2
DEC R20
BRNE L3
RET

Lab Manual of Microprocessor & Microcontroller (3141008) Page 72


C Program for Half Step Mode:
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
unsigned char stepdata[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09,0x00};
unsigned char i=0;
DDRC=0x0F; //PD0-PD3 output to drive stepper motor
while(1)
{
PORTC=stepdata[i];
_delay_ms(1000);
i++;
if(stepdata[i]==0) i=0;
}
}

:: WORKSHEET::
Activity #1:Draw interfacing diagram to drive stepper motor with PC0-PC3 pins of
ATMega 32 microcontroller. Connect switch with INT0 pin. Write and execute C
language programme to rotate stepper motor in full step mode continuously. Speed
of the motor should increase when switch connected with INT0 is pressed.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 73
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #2:

Write assembly language program for activity number 1.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 74


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #3:

Stepper motor is connected with shaft of CNC machine. One revolution of


stepper motor displace shaft by 10 cm. Write C or Assembly language
program to displace shaft by 5 cm.

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 75


EXPERIMENT NO. 11

AIM: Write and execute program to rotate DC motor of Robot for forward,
reverse, left and right motion. Simulate program using PROTEUS
Software

Objectives:
 Understand Interfacing DC motor with microcontroller
 Understand use of DC motor for robot motion control
 Write program to rotate DC motor in anticlockwise and clockwise
direction to move robot in forward and reverse direction
 Write program to rotate DC motor at different speeds using PWM.

Interfacing Details:
DC Motors are driven by L293D (Two H-bridge driver circuit).

U1
30 97
RESET PF0/ADC0
96
PF1/ADC1
34 95
XTAL1 PF2/ADC2
33 94
XTAL2 PF3/ADC3
93
U2 PF4/ADC4/TCK
8 16 78 92
PA0/AD0 PF5/ADC5/TMS
77 91
PA1/AD1 PF6/ADC6/TDO
3 2 76 90
OUT1 VS VSS IN1 PA2/AD2 PF7/ADC7/TDI
6 7 75
OUT2 IN2 PA3/AD3
+88.8 1 74 51
EN1 PA4/AD4 PG0/WR
73 52
PA5/AD5 PG1/RD
72 70
PA6/AD6 PG2/ALE
9 71 28
EN2 PA7/AD7 PG3/TOSC2
11 10 29
OUT3 IN3 PG4/TOSC1
14 15 19 1
OUT4 GND GND IN4 PB0/SS/PCINT0 PG5/OC0B
20
PB1/SCK/PCINT1
+88.8 21 12
PB2/MOSI/PCINT2 PH0/RXD2
L293D 22 13
PB3/MISO/PCINT3 PH1/TXD2
23 14
PB4/OC2A/PCINT4 PH2/XCK2
24 15
PB5/OC1A/PCINT5 PH3/OC4A
25 16
PB6/OC1B/PCINT6 PH4/OC4B
26 17
PB7/OC0A/OC1C/PCINT7 PH5/OC4C
18
PH6/OC2B
53 27
PC0/A8 PH7/T4
54
PC1/A9
55 63
PC2/A10 PJ0/RXD3/PCINT9
56 64
PC3/A11 PJ1/TXD3/PCINT10
57 65
PC4/A12 PJ2/XCK3/PCINT11
58 66
PC5/A13 PJ3/PCINT12
59 67
PC6/A14 PJ4/PCINT13
60 68
PC7/A15 PJ5/PCINT14
69
PJ6/PCINT15
43 79
PD0/SCL/INT0 PJ7
44
PD1/SDA/INT1
45 89
PD2/RXD1/INT2 PK0/ADC8/PCINT16
46 88
PD3/TXD1/INT3 PK1/ADC9/PCINT17
47 87
PD4/ICP1 PK2/ADC10/PCINT18
48 86
PD5/XCK1 PK3/ADC11/PCINT19
49 85
PD6/T1 PK4/ADC12/PCINT20
50 84
PD7/T0 PK5/ADC13/PCINT21
83
PK6/ADC14/PCINT22
2 82
PE0/RXD0/PCINT8/PDI PK7/ADC15/PCINT23
3
PE1/TXD0/PDO
4 35
PE2/XCK0/AIN0 PL0/ICP4
5 36
PE3/OC3A/AIN1 PL1/ICP5
6 37
PE4/OC3B/INT4 PL2/T5
7 38
PE5/OC3C/INT5 PL3/OC5A
8 39
PE6/T3/INT6 PL4/OC5B
9 40
PE7/ICP3/CLKO/INT7 PL5/OC5C
41
PL6
98 42
AREF PL7
100
AVCC
ATMEGA2560

* Control pin for Right Motor: PA0,PA1,OC5A


* Control pin for Left Motor: PA2,PA3,OC5B

Lab Manual of Microprocessor & Microcontroller (3141008) Page 76


Program:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
int main()
{
DDRA = DDRA | 0x0F; //set direction of the PA3 to PA0 pins as o/p
PORTA = PORTA & 0xF0; // PA3 to PA0 pins to logic 0
DDRL = DDRL | 0x18; //Setting PL3 and PL4 pins as output for PWM
// generation on OC5A and OC5B
PORTL = PORTL | 0x18; //PL3 and PL4 pins are for velocity control
// using PWM
while(1)
{
PORTA=0x06; //Move in forward direction PA0=0 PA1=1 and PA2=1 PA3=0
_delay_ms(10000);

PORTA=0x00; //Stop motors


_delay_ms(2000);

PORTA=0x09; //Move in reverse direction PA0=1 PA1=0 PA2=0 PA3=1


_delay_ms(10000);

PORTA=0x00; //Stop motors


_delay_ms(2000);

}
}

Activity #1: Write Assembly language program to move robot in forward


directi Continuously.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 77


Activity #2:

Write C language program to move robot in forward direction continuously


up to distance of 1 meter in straight line. Take left turn again move up to
distance of 1 meter. Take right turn and move up to distance of 1 meter.

(To take right turn, stop right motor and rotate left motor. Similarly to take
left turn, stop left motor and rotate right motor)

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 78


Activity #3: Connect Arduino card with motor driver module. FRC
connector details of motor driver module for DC motor is as under:

• Pin1&2 : Enable
• Pin3&4 : Input1
• Pin5&6 : Input2
• Pin7&8 : Direction Switch
• Pin19&20 is GND
Write Arduino program to rotate motor in clockwise direction and
anticlockwise direction. Draw connection diagram and run program.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

EC Department, L.D. College of Engineering, Ahmedabad Page 79


Activity #4: Connect arduino card with motor driver module to drive servo
motor. FRC connector details of motor driver module for Servo motor is as
under. Draw Connection diagram and run the program.

Servo Motor:

• Pin1&2 : Servo Pin


• Pin3&4 : Switch
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 80


EXPERIMENT NO. 12

Aim: Observe waveforms of I2C and SPI communication and understand


I2C and SPI protocol

Introduction of I2C:
I2C is two wire serial data and control bus

 Implemented with one serial data (SDA) and one clock (SCL) line.

In ATMega328 IC PC4ADC4SDA & PC5ADC5SCL

Arduino UNO/Nano Card: A4 (SDA), A5 (SCL)

 It has unique start and stop conditions.

 Slave selection protocol uses a 7-Bit slave address.

 It provides bi-directional data transfer.

 Acknowledgement after each byte transferred.

 No limit on the number of bytes transferred.

 Real multi-master capability.

General block diagram of I2C:

EC Department, L.D. College of Engineering, Ahmedabad Page 81


Arduino board connection for I2C Communication:

Code for Master (Receive data from Slave):

#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}

void loop() {
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}

Code for slave (Transmit data to master):

#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}

Lab Manual of Microprocessor & Microcontroller (3141008) Page 82


Transmit data from slave and receive it on master. Observe I2C waveforms
on Storage oscilloscope.

Introduction of SPI:
 SPI is system for serial communication.

 There are four wires: one for transmission, second for reception, third
for clock (synchronization) and fourth for selecting device for
communication.

 SPI uses 4-wire interface (SDI,SDO,SCLK,CE)

 SPI is used in many devices such as ADC, DAC, EEPROM, RTC etc.

 Many other hardware devices like (MAX7221) supports SPI. It receives


SPI signals from AVR or other microcontroller and drives multiple
seven segment displays

SPI pins in Arduino

SPI Pins of ATMega328

PB5  SCK

PB4MISO

PB3MOSI

PB2SS

 MOSI: Master Output Slave Input


 MISO: Master Input Slave Output
 SCLK: System clock from master to slave
 SS: Slave select input

EC Department, L.D. College of Engineering, Ahmedabad Page 83


Run following program and observe waveforms on SPI pins

#include <SPI.h>

void setup (void)


{
digitalWrite(SS, HIGH); // ensure SS stays high
SPI.begin ();
} // end of setup

void loop (void)


{
byte c;
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
// send test string
for (const char * p = "EC" ; c = *p; p++)
SPI.transfer (c);

// disable Slave Select


digitalWrite(SS, HIGH);
delay (100);
} // end of loop

Activity #1: Find out any one application of interfacing IO device with Arduino
using SPI protocol. Draw its circuit diagram (Connection diagram) and write code
for it.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Lab Manual of Microprocessor & Microcontroller (3141008) Page 84


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

Activity #2: Find out any one application of interfacing sensor with Arduino
using I2C protocol. Draw its circuit diagram (Connection diagram) and write code
for it.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 85
EXPERIMENT NO. 13
AIM: Write Arduino program to receive IR Signal from IR remote and
operate Electrical device based on switch pressed.
Objectives:
 Write program to receive IR code transmitted by IR Remote
 Understand TSOP 1738 sensor
 Understand how to operate electrical device with Arduino board using relay.
Program:
/* This program detects types of IR Remote
* Tuning is required for different remote.
* Assuming that devices are connected at PD4,PD5,PD6 and PD7
* IR Sensor connected at PD2*/
#include <IRremote.h>
const int RECV_PIN = 2;
const int DEVICE1=4;
const int DEVICE2=5;
const int DEVICE3=6;
const int DEVICE4=7;
const int POWERON=8;
int myflag=0;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
pinMode(DEVICE1,OUTPUT);
pinMode(DEVICE2,OUTPUT);
pinMode(DEVICE3,OUTPUT);
pinMode(DEVICE4,OUTPUT);
pinMode(POWERON,OUTPUT);
digitalWrite(DEVICE1,LOW); /* If common anode then HIGH */
digitalWrite(DEVICE2,LOW);
digitalWrite(DEVICE3,LOW);
digitalWrite(DEVICE4,HIGH);
digitalWrite(POWERON,HIGH);
Serial.println("IR Remote testing Program: ");
}

void loop() {
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print("NEC: ");
} else if (results.decode_type == SONY) {
Serial.print("SONY: ");
} else if (results.decode_type == RC5) {
Serial.print("RC5: ");
} else if (results.decode_type == RC6) {
Serial.print("RC6: ");
} else if (results.decode_type == UNKNOWN) {
Serial.print("UNKNOWN: ");
}

Lab Manual of Microprocessor & Microcontroller (3141008) Page 86


Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
/* This code is for Philips Remote
if(results.value==0x2FD48B7)
Serial.println("POWER ON BUTTON PRESSED");
if(results.value==0x2FD807F)
Serial.println("1 is pressed");
if(results.value==0x2FD40BF)
Serial.println("2 is pressed");
if(results.value==0x2FDC03F)
Serial.println("3 is pressed");
if(results.value==0x2FD20DF)
Serial.println("4 is pressed");*/
/* This code is for remote provided in Workshop*/

if(results.value==0x80C)
{
Serial.println("POWER ON BUTTON PRESSED");
digitalWrite(POWERON,LOW);
myflag=~myflag;
if(!myflag)
{
digitalWrite(DEVICE1,HIGH);
digitalWrite(DEVICE2,HIGH);
digitalWrite(DEVICE3,HIGH);
digitalWrite(DEVICE4,LOW);
digitalWrite(POWERON,LOW);
}
}
if(results.value==0x801)
{
Serial.println("1 is pressed");
digitalWrite(DEVICE1,HIGH);
digitalWrite(DEVICE2,LOW);
digitalWrite(DEVICE3,LOW);
}
if(results.value==0x802)
{
Serial.println("2 is pressed");
digitalWrite(DEVICE1,LOW);
digitalWrite(DEVICE2,HIGH);
digitalWrite(DEVICE3,LOW);
}
if(results.value==0x803)
{
Serial.println("3 is pressed");
digitalWrite(DEVICE1,LOW);
digitalWrite(DEVICE2,LOW);
digitalWrite(DEVICE3,HIGH);
}
if(results.value==0x804)
{
Serial.println("4 is pressed");
digitalWrite(DEVICE4,LOW);
}
}
}

EC Department, L.D. College of Engineering, Ahmedabad Page 87


EXPERIMENT NO. 14
AIM : Students will construct Mini Project circuit on AVR Microcontroller using
Arduino Developement Board or General Purpose developement Board
and write report about it.

Suggested List of Mini Projects


[1] Home appliance control using Television Remote
[2] Arduino Nano based robot
[3] Arduino Nano based liquid level control (multi-level).
[4] Automatic street light control
[5] Alcohol sensor to prevent driving for safety
[6] Wireless Doorbell using RF Module
[7] Device Control with smart-phone using Wi-Fi
[8] Device Control with smart-phone using Blue-tooth module
[9] Smart stick to help blind person
[10] Moisture measurement and sprinking
[11] Fancy light with Three color LEDs
[12] Automatic water level controller
[13] Bank token display
[14] Wireless control of ROBOT using PC (with ZigBee module)
[15] Interface 3-axis accelerometer with Arduino & develop vibration sensor
[16] Electronic “Dip Pragtya” for inauguration events
[17] Automatic curtain control
[18] Automatic Door Control
[19] Temperature controlled automatic regulator for FAN
[20] Security systems using Arduino nano board and GSM Module
[21] Data acquisition system using Arduino nano board
[22] Object counter to count no. of bottles passing through conveyer belt.
[23] Bottle filling machine.
[24] Distance measurement using ultrasonic transducers.
[25] Line Following Robot
[26] Color sensor detector
[27] Digital Clock using Arduino showing Minute,Hour & seconds
[28] Smoke Detector
[29] Digital dice
[30] Dancing LED using Arduino
[31] Arduino Based Voting Machine
[32] Arduino based smart stick for blind person
[33] Heartbeat Monitoring system
[34] RGB coloe Pattern generator
[35] Voice Controlled Robot

Lab Manual of Microprocessor & Microcontroller (3141008) Page 88


Title:____________________________________________
Components : _______________________
_______________________
_______________________
_______________________
_______________________
_______________________
_______________________

Brief Idea of Project :

EC Department, L.D. College of Engineering, Ahmedabad Page 89


Circuit Diagram :

Lab Manual of Microprocessor & Microcontroller (3141008) Page 90


Program Code :

EC Department, L.D. College of Engineering, Ahmedabad Page 91


Possibility of future expansion :

Lab Manual of Microprocessor & Microcontroller (3141008) Page 92

You might also like