Programs
Programs
Programs
one stop bit and observe the transmitted data in the serial window of the simulator.
org 0000H
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
MOV R1,#14
AGAIN:CLR A
MOVC A,@A+DPTR
MOV SBUF,A
CLR TI
INC DPTR
DJNZ R1,AGAIN
SJMP XX
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.
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.
(b) receive any data send by PC and put it on LEDs connected to P1, and
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
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
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
DJNZ R3,HERE2
RET
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
SETB P0.5
SETB P0.6
SETB P0.7
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW0
SETB P0.7
SETB P0.6
SETB P0.4
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW1
SETB P0.7
SETB P0.5
SETB P0.4
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW2
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
ACALL DELAY
ORG 300H
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
MOV TH0,#-92 //
END
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
LJMP SERIAL
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.
Org 0000h
MOV B,#23H
ADD A,B
JNC L1
INC R0
MOV A,#66H
ADD A,B
JNC L3
INC R0
MOV A,#87H
ADD A,B
JNC L3
INC R0
MOV A,#0F5H
ADD A,B
JNC L4
INC R0
L4:
END
; CY = 0 A>VAL
; CY = 1 A<VAL
INC R0 ;
SJMP DECREMENTC ;
CARRY : JC DECREMENTC ;
MOV B, @R0 ;
MOV @R0, A ;
DEC R0 ;
MOV A, B ;
MOV @R0, A ;
DECREMENTC : INC R0 ;
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
ORG 120H
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
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
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.
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
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
MOV R4,#0FFH
MOV R5,#55H
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
}
}