Write an 8051 assembly program to transfer data serially at baud rate 9600 with 8 bit data,
one stop bit and observe the transmitted data in the serial window of the simulator.
org 0000H
XX: MOV DPTR,#MYDATA
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
MOV R1,#14
AGAIN:CLR A
MOVC A,@A+DPTR
MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
INC DPTR
DJNZ R1,AGAIN
SJMP XX
MYDATA: DB 'VIT UNIVERSITY'
END
Program 2
Write a 8051 Assembly Language program to get data from the PC and display it on P1.
Assume 8051 is connected to PC and observe the incoming characters. As you press a key on the PC's
keyboard,
the character is sent to the 8051 serially at 4800 baud rate and is displayed on LEDs.
The characters displayed on LEDs are in ASCII (binary).
ORG 0000H
MOV TMOD,#20H
MOV TH1,#-6
MOV SCON,#50H
SETB TR1
HERE:JNB RI,HERE
MOV A,SBUF
MOV P1,A
CLR RI
SJMP HERE
END
Assume that the 8051 serial port is connected to the COM port of
IBM PC, P1 and P2 of the 8051 are connected to LEDs and switches, respectively.
Write an 8051 assembly program to
(a) send to PC the message We Are Ready,
(b) receive any data send by PC and put it on LEDs connected to P1, and
(c) get data on
switches connected to P2 and send it to PC serially.
The program should perform part
(a) once, but parts
(b) and (c) continuously, use 9600 baud rate.
ORG 0000H
MOV DPTR,#SEND
MOV R0,#12
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
LL:CLR A
MOVC A,@A+DPTR
MOV SBUF,A
HERE:JNB TI,HERE
CLR TI
INC DPTR
DJNZ R0,LL
MOV P2,#0FFH
ZZ:CLR A
NO:MOV A,P2
CJNE A,#0FFH,TT
SJMP NO
TT:MOV SBUF,A
HHE:JNB TI,HHE
CLR TI
JNB RI,ZZ
MOV A,SBUF
MOV P1,A
CLR RI
SJMP ZZ
SEND:DB 'WE ARE READY'
END
Program 1
Write a program using timer 0 to generate a 500 Hz square wave frequency on one of the pins of P1.0
Then examine the frequency using the KEIL IDE inbuilt Logic Analyzer.
ORG 0000H
MOV TMOD,#01H
HERE:MOV TL0,#66H
MOV TH0,#0FCH
CPL P1.0
ACALL DELAY
SJMP HERE
DELAY:SETB TR0
AGAIN:JNB TF0,AGAIN
CLR TR0
CLR TF0
RET
END
Program 2
Assembly Language
Write a program using timer 1 to generate a 1 kHz square wave frequency on one of the pins of P1. Then
examine the frequency using the oscilloscope.
ORG 0000H
MOV TMOD,#10H
HERE:MOV TL1,#33H
MOV TH1,#0FEH
CPL P1.0
ACALL DELAY
SJMP HERE
DELAY:SETB TR1
AGAIN:JNB TF1,AGAIN
CLR TR1
CLR TF1
RET
END
KEYPAD:
ORG 0H
START:
ACALL LCDINIT
ACALL GETKEY
AGAIN: SJMP AGAIN
LCDINIT: MOV A,#38H ;INIT. LCD 2 LINES, 5X7 MATRIX
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#01 ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#80H ;cursor at line 1, pos. 4
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#'K' ;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#'E' ;display letter O
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#'Y' ;display letter O
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#':' ;display letter O
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
RET
COMNWRT:MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
DATAWRT:MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
DELAY:MOV R3,#50 ;50 or higher for fast CPUs
HERE2:MOV R4,#255 ;R4 = 255
HERE:DJNZ R4,HERE ;stay until R4 becomes 0
DJNZ R3,HERE2
RET
;Keyboard subroutine. This program sends the ASCII
;Code for pressed key to P0.1
;P0.4-P0.7 connected to rows, P0.0-P0.3 to column
GETKEY: MOV P0,#0FH
K1:MOV P0,#0FH
MOV A,P0
ANL A,#0FH
MOV P0,A
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,K1
K2:ACALL DELAY
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,OVER
SJMP K2
OVER:ACALL DELAY
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,OVER1
SJMP K2
OVER1:CLR P0.4 ;ROW1 SELECTED
SETB P0.5
SETB P0.6
SETB P0.7
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW0
CLR P0.5 ;ROW2 SELECTED
SETB P0.7
SETB P0.6
SETB P0.4
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW1
CLR P0.6 ;ROW3 SELECTED
SETB P0.7
SETB P0.5
SETB P0.4
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW2
CLR P0.7 ;ROW4 SELECTED
SETB P0.4
SETB P0.6
SETB P0.5
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW3
SJMP K2
MOV R0,#04H
ROW0:MOV DPTR,#KCODE0
SJMP FIND
ROW1:MOV DPTR,#KCODE1
SJMP FIND
ROW2:MOV DPTR,#KCODE2
SJMP FIND
ROW3:MOV DPTR,#KCODE3
FIND:RRC A
JNC MATCH
INC DPTR
DJNZ R0,FIND
MATCH:MOV A,#84H ;display pressed key
ACALL COMNWRT ;
ACALL DELAY
CLR A ;set A=0 (match is found)
MOVC A,@A+DPTR ;get ASCII from table
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
LJMP K1 ;ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB 'F','B','8','4' ;ROW 0
KCODE1: DB 'E','A','7','3' ;ROW 1
KCODE2: DB 'D','0','6','2' ;ROW 2
KCODE3: DB 'C','9','5','1' ;ROW 3
END
Write an 8051 program to get data from port P0 and send it to port P1continuously while an interrupt
will do the following: Timer 0 will toggle the P2.1 bit every 100 microseconds.
Program number: 1
ORG 0000H
LJMP MAIN // Jump to main.
ORG 000BH // timer 0 Interrupt vector label
CPL P2.1 // Toggle P2.1 pin
RETI // Return from ISR
ORG 0030H // After Vector Table Space
MAIN:MOV TMOD,#02H //set Timer 0 in mode 2
MOV P0,#0FFH // Set P0 an I/P port
MOV TH0,#-92 //
MOV IE,#10000010B // enable Timer 0
SETB TR0 // Start the Timer
BACK:MOV A,P0 // Get data from P0.
MOV P1,A // move data P0 to P1
SJMP BACK //Loop it Unless interrupted
END
Program 2 Assembly Language
Write an 8051 program to get data from a single bit of P1.2 and send it to P1.7 continuously while an
interrupt will do the following:
A serial interrupt service routine will receive data from a PC and display it on P2 ports.
ORG 0000H
LJMP MAIN
ORG 0023H ; ----serial interrupt vector table
LJMP SERIAL
ORG 0030H ;-- after vector table space
MAIN:SETB P1.2 ; -- P1.2 made as input pin
MOV TMOD,#20H ; -- timer 1 mode 2
MOV TH1,#-3 ;-- set baud rate 9600
MOV SCON ,#50H ; -- one stop bit
MOV IE,#10010000B ; -- serial int. enabled.
SETB TR1 ;-- Timer 1 stared.
BACK:MOV C,P1.2
MOV P1.7,C
SJMP BACK
SERIAL:JB TI,TRANS
MOV A,SBUF
MOV P2,A
CLR RI
RETI
TRANS:CLR TI
RETI
END
Program - 1
Write and assemble a program to add the following data and then use the simulator to examine the CY
flag.
92H, 23H, 66H, 87H, F5H
Org 0000h
MOV A,#92H; 23H, 66H, 87H, F5H
MOV B,#23H
ADD A,B
JNC L1
INC R0
L1: MOV B,A
MOV A,#66H
ADD A,B
JNC L3
INC R0
L2: MOV B,A
MOV A,#87H
ADD A,B
JNC L3
INC R0
L3: MOV B,A
MOV A,#0F5H
ADD A,B
JNC L4
INC R0
L4:
END
lP in 8051 to sort a block of data in ascending or descending order
; PROGRAM STARTS HERE
MOV R6, #07H ; EXTERNAL COUNTER
START : MOV R7, #07H ; INTERNAL COUNTER
MOV R0, #30H ; INTERNAL MEMORY ADDRESS
MOV A, #00H ; CLEAR ACCUMULATOR
; CY = 0 A>VAL
; CY = 1 A<VAL
BACK : MOV A, @R0 ;
INC R0 ;
CJNE A, @R0, CARRY ;
SJMP DECREMENTC ;
CARRY : JC DECREMENTC ;
MOV B, @R0 ;
MOV @R0, A ;
DEC R0 ;
MOV A, B ;
MOV @R0, A ;
DECREMENTC : INC R0 ;
DJNZ R7, BACK ;
DJNZ R6, START ;
END
rogram 3
Write a program to add two multi-byte BCD numbers together and store the result in
RAM locations 40H - 44H. The two multi-byte items are stored in the ROM space starting
at 120H and 150H. See the following example data.
ORG 120H
DATA_1: DB 54H,76H,65H,98H ;number 98657654H
DATA_2DB 93H,56H,77H,38H ;number 38775693H
Pick your own data for your program. Notice that you must first bring the data
from ROM space into the CPU's RAM and then add them together. Use a simulator
to single-step the program and examine the data.
ORG 120H
DB 54H,76H,65H,98H
ORG 150H
DB 93H,56H,97H,38H
ORG 00H
MOV DPTR,#120H
MOV R4,#05H
MOV R0,#40H
MOV R3,#00H
LOOP: CLR A
MOVC A,@A+DPTR
MOV R3,A
MOV A,#30H
MOVC A,@A+DPTR
ADDC A,R3
DA A
MOV @R0,A
INC R0
INC DPTR
DJNZ R4,LOOP
HERE:SJMP HERE
END
Program 2
Write a program to convert 4 bytes of hex data to ASCII and place the result in RAM locations starting at
50H. The hex data is stored in ROM starting at 150H. The data is stored as follows:
ORG 150H
MYDATA: DB 7FH, 3CH, 54H, 2AH ;pick your own data
Using a simulator, single-step the program and examine the data.
ORG 150H
DB 7FH,3CH,54H,2AH
ORG 0H
MOV DPTR,#150H
MOV R5,#4H
MOV R0,#50H
L2:
MOVC A,@A+DPTR
MOV R3,#03H
L1:
MOV B,#10D
DIV AB
MOV R4,A
MOV A,#30H
ADD A,B
MOV @R0,A
MOV A,R4
INC R0
DJNZ R3,L1
INC DPTR
DJNZ R5,L2
HERE:SJMP HERE
END
Program 3
Write a program to find the average age of a group of children in a class. There are 12 children, ranging
in age from 5 to 10 years old. Pick your own data. Notice that you must first bring the data from ROM
space into the CPU's RAM and then add them together. Using a simulator, single-step the program and
examine the data.
ORG 0000H
MOV DPTR,#120H
MOV R2,#12
MOV B,#12
LOOP: CLR A
MOVC A,@A+DPTR
ADD A,R5
MOV R5,A
INC DPTR
DJNZ R2,LOOP
DIV AB
ORG 120H
DB 6,7,8,9,7,8,9,6,6,7,8,9
END
Program 1
Assembly Language
Connect the LCD to your 8051 Kit. Then write and run a program to display your name on line 1 of the
LCD (first name followed by last name with a space in between).
Note: If you are not monitoring the busy flag of the LCD, put a few milliseconds delay in your program.
See the discussion in Chapter 12.
;P1.0-P1.7 are connected to LCD data pins D0-D7
;P2.0 is connected to RS pin of LCD
;P2.1 is connected to R/W pin of LCD
;P2.2 is connected to E pin of LCD
ORG 0000H
MOV A,#38H
ACALL CMNWRT
ACALL DELAY
MOV A,#0EH
ACALL CMNWRT
ACALL DELAY
MOV A,#01H
ACALL CMNWRT
ACALL DELAY
MOV A,#06H
ACALL CMNWRT
ACALL DELAY
MOV A,#80H
ACALL CMNWRT
ACALL DELAY
MOV A,#'V'
ACALL DATWRT
ACALL DELAY
MOV A,#'I'
ACALL DATWRT
ACALL DELAY
MOV A,#'T'
ACALL DATWRT
ACALL DELAY
AGAIN: SJMP AGAIN
CMNWRT:
MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DATWRT:
MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DELAY: MOV R3,#0FFH
MOV R4,#0FFH
MOV R5,#55H
HERE: DJNZ R5,HERE
HERE1: DJNZ R4, HERE1
HERE2: DJNZ R3,HERE2
RET
END
TAsk
Write an 8051 C program to transfer the message YES serially at 9600 baud,
8-bit data, 1 stop bit. Do this continuously.
#include <reg51.h>
void SerTx(unsigned char);
void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFD; //9600 baud rate
SCON=0x50;
TR1=1; //start timer
while (1) {
SerTx('Y');
SerTx('E');
SerTx('S');
}
}
void SerTx(unsigned char x){
SBUF=x; //place value in buffer
while (TI==0); //wait until transmitted
TI=0;
}
Program 1
C program:
Write an 8051 C program to toggle all the bits of port P1 continuously with
some delay in between.
Use Timer 0, 16-bit mode to generate the delay.
#include <reg51.h>
void T0Delay(void);
void main(void)
{
while (1)
{
P1=0x55;
T0Delay();
P1=0xAA;
T0Delay();
}
}
void T0Delay()
{
TMOD=0x01;
TL0=0x00;
TH0=0x35;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
Program 2
C program:
Write an 8051 C program to get a byte of data from P0. If it is less than
100, send it to P1; otherwise, send it to P2.
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
P0=0xFF; //make P0 input port
while (1)
{
mybyte=P0; //get a byte from P0
if (mybyte<100)
P1=mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}