Lab Manual MES Experiment 5 (1)
Lab Manual MES Experiment 5 (1)
Introduction:
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.
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.
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.
//-------------------------
// C Code for Blinking LED
//-------------------------
extern “C”
{
void start();
void led(byte);
}
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
//-----------------------------------
// C Code: RGB LED ON/OFF via Buttons
//-----------------------------------
extern "C"
{
void start();
void btnLED();
}
//-----------------------
void setup()
{
start();
}
//-----------------------
void loop()
{
btnLED();
}
;------------------------------------------
; 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
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/