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

Lab Manual MES Experiment 5 (1)

Uploaded by

ratintv7
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)
8 views

Lab Manual MES Experiment 5 (1)

Uploaded by

ratintv7
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/ 7

Experiment 5 Lab Manual

American International University- Bangladesh


Department of Electrical and Electronic Engineering
EEE 4103: Microprocessor and Embedded Systems Laboratory

Title: Familiarization of assembly language program in a microcontroller.

Introduction:

The objectives of this experiment are to-


1. Study the assembly language program of Arduino.
2. Write assembly language programming code for Arduino.
3. Build a circuit to turn on an LED on an Arduino Microcontroller Board an input signal
detected by an input switch connected to an I/O port of the microcontroller.
4. Know the working duration of a project run by an Arduino’s built-in Timer.
5. Implement a sequential LED light pattern control system using an input switch and an
Arduino Microcontroller Board.

Theory and Methodology:

To study assembly language programming using Arduino IDE, we need an Arduino


Microcontroller Board with connecting cable, LEDs, switches, resistors, assembly language
programming knowledge, compiler, loader, etc. Assembly language is written in the Arduino
IDE and uploaded into the microcontroller via the USB cable after successfully compiling the
code. This is shown in Fig. 1.

Fig. 1 Assembly programming with Arduino IDE

For this purpose, we need to know that the codes should be written in two different files
with the file extension being .ino and .S with the same file name as shown in Fig. 2. Both
files must be in the same directory. We know that the Arduino has 32 general-purpose
registers as shown in Fig. 3 with their physical address being on the left side. Arduino has
4 I/O ports for programming, namely ports A, B, C, and D. This is shown in Fig. 4. Each
port is 8-bit wide and can be configured as an input and output port. For each port, there
is a special purpose register called the data direction register, which defines the direction
of signal flow to or from the Arduino microcontroller. These ports can be used to perform
other functions as well. This is explained in Fig. 5.

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 1


Experiment 5 Lab Manual

Fig. 2 Assembly programming via Arduino IDE

Fig. 3 General-purpose registers of the Arduino (ATMega328P)

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 2


Experiment 5 Lab Manual

Fig. 4 I/O ports of the Arduino (ATMega328P)

Fig. 5 Assembly programming of I/O ports of the Arduino (ATMega328P)

Apparatus:
1) Arduino IDE (2.0.1 or any recent version)
2) Arduino Microcontroller board
3) PC having an Intel processor
4) LED lights (Red, 1 pc)
5) One 100  resistor
6) One push switch
7) Jumper wires

Experimental Setup:

(a) (b)
Fig. 6 Experimental setup of an LED control system using an Arduino Microcontroller Board.
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 3
Experiment 5 Lab Manual
Experimental Procedure:
The main task of this experiment is to implement an LED light blinks and control system using
a push switch. Connect the circuits as per the diagram of Figs. 6 (a) and 6 (b). Then plug the
Arduino microcontroller board into the PC.

Using Arduino IDE to write code:


1. Open the Arduino Uno IDE 2.0.1 (version may have been updated automatically on your
computer) and a blank sketch will open. The window as in Fig. 2 will come up on your PC:

Fig. 2 IDE Environment (text editor)

2. Now, write the program by copying the codes given below boxes in sequences on the blank
sketch of Fig. 7 for the LED blink and control.
a. Create led.ino and led.S files using the codes given above.
b. Create a folder named LED and place the above two files in that folder.
c. Open led.ino using Arduino IDE.
d. Compile and upload to the hardware.
e. Modify the program to blink an LED at digital PIN 12 with a different delay.

Code of an LED light blink and LED control using a switch:


PART 1: Blink an LED [for Fig. 6 (a)]

The .ino file:

//-------------------------
// C Code for Blinking LED
//-------------------------
extern “C”
{
void start();
void led(byte);
}

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 4


Experiment 5 Lab Manual
//----------------------------------------------------
void setup()
{
start();
}
//----------------------------------------------------
void loop()
{
led(1);
led(0);
}

The .S file:

;---------------
; Assembly Code
;---------------
#define __SFR_OFFSET 0x00
#include “avr/io.h”
;------------------------
.global start
.global led
;------------------------
start:
SBI DDRB, 5 ; set PB5 (D13) as o/p
RET ; return to setup() function
;---------------------------------------------------------------------------
led:
CPI R24, 0x00 ; value in R24 passed by caller compared with 0
BREQ ledOFF ; jump (branch) if equal to subroutine ledOFF
SBI PORTB, 5 ; set D13 to high
RCALL myDelay ; Calling a delay function
RET ; return to loop() function
;---------------------------------------------------------------------------
ledOFF:
CBI PORTB, 5 ; set D13 to low
RCALL myDelay
RET ; return to loop() function
;---------------------------------------------------------------------------
.equ delayVal, 10000 ; initial count value for the inner loop
;---------------------------------------------------------------------------
myDelay:
LDI R20, 100 ; initial count value for the outer loop
outerLoop:
LDI R30, lo8(delayVal) ; low byte of delayVal in R30

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 5


Experiment 5 Lab Manual
LDI R31, hi8(delayVal) ; high byte of delayVal in R31
innerLoop:
SBIW R30, 1 ; subtract 1 from 16-bit value in R31, R30
BRNE innerLoop ; jump if countVal not equal to 0
;--------------
SUBI R20, 1 ; subtract 1 from R20
BRNE outerLoop ; jump if R20 not equal to 0
RET
;---------------------------------------------------------------------------

PART 2: Push button LED control [for Fig. 6 (b)]

The btnLED.ino file:

//-----------------------------------
// C Code: RGB LED ON/OFF via Buttons
//-----------------------------------
extern "C"
{
void start();
void btnLED();
}
//-----------------------
void setup()
{
start();
}
//-----------------------
void loop()
{
btnLED();
}

The btnLED.S file:

;------------------------------------------
; Assembly Code: RGB LED ON/OFF via Buttons
;------------------------------------------
#define __SFR_OFFSET 0x00
#include "avr/io.h"
;------------------------
.global start
.global btnLED
;================================================================
=============
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 6
Experiment 5 Lab Manual
start:
SBI DDRB, 4 ; set PB4 (pin D12 as o/p - red LED)
SBI DDRB, 3 ; set PB3 (pin D11 as o/p - green LED)
SBI DDRB, 2 ; set PB2 (pin D10 as o/p - blue LED)
CBI DDRD, 2 ; clear PD2 (pin D02 as i/p - red button)
CBI DDRD, 3 ; clear PD3 (pin D03 as i/p - green button)
CBI DDRD, 4 ; clear PD4 (pin D04 as i/p - blue button)
RET

btnLED:
L2: SBIS PIND, 4; ; Skips below statement if the push button is not
pressed
RJMP L1
SBI, PORTB, 2 ; Turn ON LED, PB2 if not pressed
CBI, PORTB, 3 ; Turn OFF LED, PB3 if not pressed
SBIC, PIND, 4 ; Skips below statement if the push button is
pressed
RJMP L2

L1: SBI, PORTB, 3 ; Turn ON LED, PB3 if pressed


CBI, PORTB, 2 ; Turn OFF LED, PB2 if pressed
RET

Questions for report writing:

1) Include all codes and scripts in the lab report following the lab report writing template.
2) Show the output/results in the form of images. Give their captions and descriptions.
3) Configure the port numbers for outputs and inputs according to your ID. Consider the last
six digits from your ID (if your ID is XY-PQABC-Z then consider Port B’s and Port D’s
bits of PQABC and Z as the inputs and outputs, respectively). Include all the programs and
results within your lab report.
4) Include the Proteus simulation of the LED blink program and LED control system using
push buttons. Explain the simulation methodology. You may learn the simulation from the
following video link: https://www.youtube.com/watch?v=yHB5it0s2oU

Reference(s):

[1] https://www.arduino.cc/.
[2] ATMega328 manual
[3] https://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers
[4] http://maxembedded.com/2011/06/avr-timers-timer0/

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 7

You might also like