ES LAB Programs
ES LAB Programs
4. Create a source file (using Filenew), type in the assembly or C program and save this
(filename.asm/ filename.c) and add this source file to the project by right clicking on the
Source Group in the Project Window and Add Files to Groupoption.
5. Set the Target options using Project Options for Target ‘Target 1’configuration
dialog. Set the Xtal frequency as 11.0592 MHz, and also the Options for Target
Debug use either Simulator / KeilMonitor- 51 driver.
1
If Keil Monitor- 51 driver is used click on Settings COM Port settings select the
COM Port to which the board is connected and select the baud rate as 19200 or 9600
(recommended). Enable Serial Interrupt option if the user application is not using on-
chip UART, to stop program execution.
6. Build the project; using Project Build Project. Any errors in the code are indicated
by – “Target not created” in the Build window, along with the error line. Debug the
errors. After an error free build, goto Debug mode.
7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog.
Or by clicking the icon.
8. Run the program using the Debug-Run command & halted using Debug-Stop
Running. Also the (reset, run, halt) icons can be used. Additional icons are
9. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status,
etc. If it is a part-A program, the appropriate memory window is opened using View
memory window (for data RAM & XRAM locations), Watch window (for timer
program), serial window, etc.
Note: To access internal RAM area type address say i:20h.
Similarly to access the DPTR region (XRAM-present on chip in AT89C51ED2) say
9000h location type in X:9000H.
ATMEL chip > Device > Settings > RS232 > 9600 > Connect
For interfacing programs:Ensure the following supporting files are present in the
working directory
lcd.h , LCD.LIB, REG51F.H, REG51XD2.H
Source group1: For the step 4 above, add all the supporting files and application program
Similar steps are followed as same as simulation programs
2
PART- A
8051 Assembly Language Programs using Keil simulator
Warm-Up cycle:
Example programs:
#1 To add two data bytes in External data memories
ORG 00
mov DPTR,#1000h; initialize Ext MEMORY
movx A, @DPTR; Get the First data
mov R0,A; Copy D1 to R0
inc DPTR; To point to next memory
movx A, @DPTR; Get the Second data
add A,R0; Add nums
inc DPTR; To point to sum mem
movx @DPTR, A; To Store the sum
here: sjmp here
org 00
mov r0,#25h;
inc @r0;
inc r0;
inc @r0;
here: sjmp here
end
# 3 To perform logical AND of data internal data memories
org 00
mov r0,#25h;
mov a, @r0 ;
inc r0;
anl a,@r0;
inc r0;
mov @r0,a
here: sjmp here
end
3
First cycle programs:
org 00h
sjmp 30h
org 30h
mov dptr,#9000h
mov r0,#10
mov r1,#90h
mov r2,#91h
nxt: movx a,@dptr
mov dph,r2
movx @dptr,a
mov dph,r1
incdptr
djnz r0,nxt
hr: sjmphr
end
4
mov r4,#00
nxt_bt:movx a,@dptr
add a,r4
mov r4,a
jnc skip
inc r3
skip: inc dptr
djnz r2,nxt_bt
movx @dptr,a
inc dptr
mov a,r3
movx @dptr,a
hr: sjmp hr
end
org 30h
mov dptr,#2000h
mov r2,#11
mov r3,#00
mov r4,#00
nxt_bt:movx a,@dptr
add a,r4
da a
mov r4,a
jnc skip
clr a
mov a,r3
add a,#01
da a
mov r3,a
skip: inc dptr
djnz r2,nxt_bt
mov a,r4
movx @dptr,a
inc dptr
mov a,r3
movx @dptr,a
hr: sjmp hr
end
5
5. Program for 16-bit addition
org 00h
sjmp 30h
org 30h
result equ 40h
mov dptr,#0ffabh
mov a, #0cdh
mov b,#0abh
mov r0,#result
add a,dpl
mov @r0,a
inc r0
mova,b
addc a,dph
mov @r0,a
inc r0
mov @r0,#00
jnc hr
inc @r0
hr: sjmphr
end
Second cycle programs:
6
2. Program to subtract two multibyte numbers
org 00h
sjmp 30h
org 30h
mov dptr,#9000h
mov r1,#5
mov r2,#90h
mov r3,#91h
mov r4,#92h
clr c
nxt_bt: mov dph,r2
movx a,@dptr
mov r5,a
mov dph,r3
movx a,@dptr
subb a,r5
mov dph,r4
movx @dptr,a
inc dptr
djnz r1,nxt_bt
mov a,r1
jnc skip
inc a
skip: movx @dptr,a
hr: sjmp hr
end
org 00h
sjmp 30h
org 30h
mov r0,#4
nxt_ps: mov dptr,#2000h
mov a,r0
mov r1,a
nxt_cmp:mov r2,dpl
movx a,@dptr
mov 0f0h,a
inc dptr
movx a,@dptr
cjne a,0f0h,not_eq
ajmp skip
not_eq: jnc skip
mov dpl,r2
movx @dptr,a
inc dptr
mov a,0f0h
movx @dptr,a
7
skip: djnz r1,nxt_cmp
djnz r0,nxt_ps
hr:sjmp hr
end
4. Program to XOR two bits stored in locations 30h and 31h. Store the result in 32h.
( Modify this program for XNOR operation)
; Flip Flops A = 30h, B = 31h,Y= 32h have byte address 26h
; Y=AB’ + BA’ ( XOR operation)
; Assuming initially output of XOR gate (Y) = 0
Input at 26h Output at 26h
00h 00h
01h 05h
02h 06h
03h 03h
org 00h
sjmp 30h
org 30h
mov c,30h ; Cy=A
anl c,/31h ; Cy=A.B’
mov 32h,c ; Y=A.B’
mov c,31h ; Cy= B
anl c,/30h ; Cy= B.A’
orl c,32h ; Cy=Y+BA’ = AB’ + BA’
mov 32h,c ; Y= Cy = AB’ + BA’
hr: sjmphr
end
5. Program for multiplication of 16 bit number with 8 bit number (eg: ABCD X EF )
(1000h)=EFh, (1001h) =CDh, (1002h)=ABh
;(eg1: ABCDh X EFh ) (1000h)=EFh, (1001h) =CDh, (1002h)=ABh
; ABCDh x EFh = A06463h
; eg2: ABCDh X AAh = 721622h
org 00h
mov dptr,#1000h
movx a,@dptr ; (a) = (1000h) = EFh
mov r0,a ; (r0) = (a) = EFh
inc dptr ;
movx a,@dptr ; (a) = (1001h) = CDh
mov r1,a ; (r1) = (a) = CDh
8
inc dptr
movx a,@dptr ; (a) = (1002h) = ABh
mov r2,a ;(r2) = (a) = ABh
mov b,01 ;(b) = (r1) = CDh
mov a,r0 ;(a) = (r0) = EFh
mul ab ;(b) = Higher byte of product (CDh X EFh)
;(a) = Lower byte of product (CDh X EFh)
mov r3,a ;(r3) = (a) = Lower byte of product (CDh X EFh)
mov r4,0f0h ;(r4) = (b) = Higher byte of product (CDh X EFh)
mov b,02 ; (b) = (r2)= ABh
mov a,r0 ; (a) = (r0) = EFh
mul ab; ; (b) = Higher byte of product (ABh X EFh)
; (a) = Lower byte of product (ABh X EFh)
mov r5,a ; (r5) = (a) = Lower byte of product (ABh X EFh)
mov r6,b ; (r6) = (b) = Higher byte of product (ABh X EFh)
mov a,r3 ; (a) = (r3) = Lower byte of product (CDh X EFh)
inc dptr ;
movx @dptr,a ;(1003h) = (a) = First byte of product (CDh X EFh)
mov a,r4 ;(a) = (r4) = Higher byte of product (CDh X EFh)
add a,r5 ;(a) = Second byte of the product
; = Higher byte of (CDh X EFh) + Lower byte of (ABh X EFh)
inc dptr
movx @dptr,a ; (1004h) = (a) = Second byte of product
clr a
addc a ,r6 ; (a) = Third byte of the product
; = Higher byte of (ABh X EFh)+ possible carry from Second
; byte of product
inc dptr ;
movx @dptr,a ; (1005h)= Third byte of the product
hr: sjmp hr ; ABCD x EF = A06463h
end
9
Third cycle programs
1. Program to generate 10 Fibonacci numbers with first two stored in
locations 1000h and 1001h
; Fibonacci series .... 10 Nos
; If (1000h)=2 and (1001h)= 3 then the Result = 2,3,5,8,0dh,15h,22h,37h,59h,90h
org 00h
sjmp 30h
org 30h
mov dptr,#1000h
mov r3,#08
movx a,@dptr
mov r0,a
incdptr
movx a,@dptr
nxt: xch a,r0
add a,r0
inc dptr
movx @dptr,a
djnz r3,nxt
hr: sjmp hr
end
10
gcd: incdptr
movx @dptr,a ; (1002h) = GCD
inc dptr
mov 0f0h,a ; (b) = GCD
mov 82h,#00
movx a,@dptr ; (a)=N1
div ab ; (a)/(b) q=(a) and r=(b) => (a) = (N1/GCD) (b)= 0
mov 0f0h,a ; (b)= (a) = (N1/GCD)
inc dptr
movx a,@dptr ; (a) = N2
mul ab ; (a) = (N1/GCD) x N2 =LCM
mov 82h,#03
movx @dptr,a ; (1003h) = LCM
hr:sjmp hr
end
11
4. Program to find the square and cube of a number.
org 00h
Sjmp 30h
org 30h
12
sjmp back
end_proc: mov 31h,r0 ; Store the Square root in 31h
hr: sjmphr
end
org 00h
sjmp 30h
org 30h
clr a
back: mov p1,a
acall delay
inc a
sjmp back
delay: mov r3,#0ffh
loop3: mov r1,#0ffh
loop2: mov r2,#0ffh
loop1: djnz r2,loop1
djnz r1,loop2
djnz r3,loop3
ret
end
13
3. Program to convert Binary number to ASCII
{ Modify this program to demonstrate ASCII number to Binary }
org 00h
Sjmp 30h
org 30h
mov dptr,#1000H
movx a,@dptr
mov r0,a
anl a,#0fh
acall binary2ascii
mov a,r0
anl a,#0f0h
swap a
acall binary2ascii
hr:sjmp hr
binary2ascii:cjne a,#0ah,ner
ajmp nxt
ner: jc skip
nxt:add a,#07h
skip:add a,#30h
inc dptr
movx @dptr,a
ret
end
org 00h
sjmp 30h
org 30h
mov dptr,#1000h
movx a,@dptr
mov b,#100
div ab
inc dptr
mov r0,a
mov a,b
mov b,#10
div ab
swap a
orla,b
movx @dptr,a
inc dptr
mov a,r0
movx @dptr,a
hr: sjmphr
end
14
5. Program to generate a square wave of 2 KHz of 30%duty cycle. Use Timer 0 in
Mode 1
org 00h
sjmp 30h
org 30h
mov tmod,#01h
loop:mov th0,#0FFh
mov tl0,#76h
setb p1.5
acall delay
clr p1.5
mov tl0,#0BDh
mov th0,#0FEh
acall delay
sjmp loop
delay: setb tr0
bk:jnb tf0,bk
clr tf0
clr tr0
ret
6. Program to send the message “Serial Communication” to the serial port. Assume
XTAL = 11.0592 MHz, 8-bit data, and 1 stop bit. Baud rate=4800
15
; subroutine for transmitting Character
16
PART- B
17
Keyboard Interfacing Program
/***************************Keyboard Interface ***********************
Output : Displays the pressed key's key code on the on-board LCD of the ESAMCB 51
It uses the LCD library to write on the LCD.
**********************************************************************/
#include <REG51xD2.H>
#include "lcd.h"
unsigned char getkey();
void delay(unsigned int);
main()
{
unsigned char key,tmp;
InitLcd(); /* Initialise LCD */
WriteString("Key Pressed="); /* Display msg on LCD */
while(1)
{
GotoXY(12,0); /*Set Cursor Position */
key = getkey(); /* Call Getkey method */
}
}
unsigned char getkey()
{
unsigned char i,j,k,indx,t;
P2 = 0x00; /* P2 as Output port */
P0 = 0xFF;
indx = 0x00; /* Index for storing the first value of scanline */
for(i=1;i<=4;i<<=1) /* for 4 scanlines */
{
P2 = 0x0f & ~i; /* write data to scanline */
t = P0; /* Read readlines connected to P0*/
t = ~t;
if(t>0) /* If key press is true */
{
delay(6000); /* Delay for bouncing */
for(j=0;j<=7;j++) /* Check for 8 lines */
{
18
t >>=1;
if(t==0) /* if get pressed key*/
{
k = indx+j; /* Display that by converting to Ascii
*/
t = k>>4;
t +=0x30;
WriteChar(t); /* Write upper nibble */
t = k & 0x0f;
if(t > 9)
t+=0x37;
else
t+=0x30;
WriteChar(t); /* write lower nibble */
return(indx+j); /* Return index of the key pressed */
}
}
}
indx += 8; /* If no key pressed increment index
*/
}
}
19
2. Develop a C program to generate square and triangular wave using
DAC
Dual DAC Interface Circuit Diagram
20
Dual DAC interfacing program
/*********************************** Squre wave************************
Object : To demonstrate how to generate square wave using ESA Dual DAC interface.
Connection: Connect the interface module to 26pin FRC connector J7 ofESAMCB51.
Output: Generates the square wave with 2.5v amplitude.
user can change the amplitude and freaquency by presseing the following keys.
To change Amplitude press P3.3/INT1 on ESAMCB51.
To change frequency press P3.2/INT0 on ESAMCB51.
**********************************************************************/
#include <REG51xD2.H>
#include "lcd.h"
sbit Amp = P3^3; /* Port line to change amplitude */
sbitFre = P3^2; /* Port line to change frequency */
void delay(unsigned int x) /* delay routine */
{
for(;x>0;x--);
}
main()
{
unsigned char on = 0x7f,off=0x00;
unsignedintfre = 100;
InitLcd(); /* Initialize LCD */
WriteString("Squarewave"); /* Write to LCD */
while(1)
{
if(!Amp) /* if user choice is to change amplitude */
{
while(!Amp); /* wait for key release */
on+=0x08; /* Increase the amplitude */
}
if(!Fre) /* if user choice is to change frequency */
{
if(fre> 1000) /* if frequency exceeds 1000 reset to default */
fre = 100;
while(!Fre); /* wait for key release */
fre += 50; /* Increase the frequency */
}
P0=on; /* write apmlitude to port */
delay(fre);
21
P0 = off; /* clear port */
delay(fre);
}
}
Object : To demonstrate how to generate a triangular wave formusing ESA Dual DAC I/F.
Connection: Connect the Interface to 26pin FRC connector J7 ofESAMCB51.
Output : Generates a Triangular waveform with 5v Amp.
*******************************************************************/
#include <REG51xD2.H>
#include "lcd.h"
main()
{
unsigned char i=0;
InitLcd(); /* Initialise LCD */
WriteString("Triangular Wave"); /* Display on LCD */
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++) /* GenerateON pulse */
P0 = i;
for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */
P0 = i;
}
}
Hardware details:
Vref 10.5 V , R f 2.7 K , R ref 5.1 K
Calculation:
D D D D D D D D
I out I ref 7 6 5 4 3 2 1 0
2 4 8 16 32 64 128 256
Vref
I ref
Rref
Vo R f I out
22
Exercise:
23
Stepper Motor Interface Circuit diagram
24
Stepper Motor Interfacing program
/**************************** Stepper Motor **************************
**********************************************************************/
#include <REG51xD2.H>
#include "lcd.h"
static bit Dir=0;
voidChangeDir(void) interrupt 0 /* Int Vector at 000BH, Reg Bank 1
*/
{
Dir = ~Dir; /* Complement the Direction flag */
ClrLcd();
if(Dir)
WriteString("Clockwise");
else
WriteString("Anti Clockwise");
}
void delay(unsigned int x) /* Delay Routine */
{
for(;x>0;x--);
}
main()
{
unsigned char Val,i;
EA=0x1; /* Enable Interrupt flag and Interrupt 0 & Serial Interrupt */
EX0=0x1;
ES=0x0; /*since the monitor is using the serial interrupt it has to be enabled*/
InitLcd(); /* Initialise LCD */
WriteString("Anti Clockwise"); /* Write to LCD */
while(1)
{
if(Dir) /* If Dir Clockwise */
25
{
Val = 0x88;
for(i=0;i<4;i++)
{
P0 = Val; /* Write data for clock wise
direction*/
Val = Val>>1;
delay(575);
}
}
else /* AntiClockwise Direction */
{
Val = 0x11;
for(i=0;i<4;i++)
{
P0 = Val; /* Write data for anticlockwise direction*/
Val = Val<<1;
delay(575);
}
}
}
}
26
PART- C
IoT Experiments using Arduino uno
IoT Board with Arduino UNO R3
5. → (upload)
27
/* 1. Program to test Buzzer */ // RM17 - RM9 connected and RM25-RM26
int buzzer_pin = 9; // Arduino pin #23/D9 or Board pin P38
void setup(){
pinMode(buzzer_pin, OUTPUT);
Serial.begin(9600);
digitalWrite(buzzer_pin,HIGH);
}
void loop() {
digitalWrite(buzzer_pin, LOW);
delay(5000);
digitalWrite(buzzer_pin, HIGH);
delay(2000);
}
/* 2. Digital light sensor : Connect RM3 to RM20 */
int light_pin = 5; //Arduino board pin #19 / D5
void setup() {
pinMode(light_pin, INPUT);
Serial.begin(9600);
}
void loop() {
int light_data = digitalRead(light_pin);
if (light_data)
Serial.println("Light Not Detected!");
else
Serial.println("Light Detected!");
delay(1000);
}
28
Open Ended Experiments:
1. Design a Calculator to perform arithmetic functions.
2. Perform Serial communication to transmit a multimedia file / Image / Audio using
C language.
3. Using ALP, design a temperature controller to meet the design specifications.
4. Generate a given wave pattern using the DAC interface.
29