Lab Sheet For Chapter 9
Lab Sheet For Chapter 9
Section 1: Basics
The 8051 is the original member of the MCS-51 family, and is the core for all MCS-51 devices. The
features of the 8051 core are -
◆ 8-bit CPU optimized for control applications
◆ Extensive Boolean processing (single-bit logic) capabilities
◆ 64K Program Memory address space
◆ 64K Data Memory address space
◆ 4K bytes of on-chip Program Memory
◆ 128 bytes of on-chip Data RAM
◆ 32 bidirectional and individually addressable 1/0 lines
◆ Two 16-bit timer/counters
◆ Full duplex UART
◆ 6-source/5-vector interrupt structure with two priority levels
◆ On-chip clock oscillator
1
Program Status Word
The Program Status Word (PSW) contains several status bits that reflect the current state of the CPU.
The PSW, shown in Figure below, resides in SFR space. It contains the Carry bit, the Auxiliary Carry
(for BCD operations), the two register bank select bits, the Overflow flag, a Parity bit, and two user
definable status flags.
CY AC F0 RS1 RS0 OV -- P
Arithmetic Instructions
The list of arithmetic instructions is listed in Table below. The table below indicates the addressing modes
that can be used with each instruction to access the <byte> operand. For example, the ADD A, <byte>
instruction can be written as
ADD A, 7FH (direct addressing)
ADD A, @R0 (indirect addressing)
ADD A, R7 (register addressing)
ADD A, #127 (immediate constant)
2
Logical Instructions
The table below shows the list of MCS-51 logical instructions. The instructions that perform Boolean
operations (AND, OR, Exclusive OR, NOT) on bytes perform the operation on a bit-by-bit basis. That
is, if the accumulator contains 00110101B and <byte> contains 01010011B, then ANL A, <byte> will
leave the Accumulator holding 00010001B.
Also, the ANL A, <byte> instruction may take any of the forms
ANL A, 7FH (direct addressing)
ANL A, @Rl (indirect addressing)
ANL A, R6 (register addressing)
ANL A, # 53H (immediate constant)
3
Data Transfer Instructions
The MOV < dest >, < src > instruction allows dats to be transferred between any two internal RAM or
SFR locations without going through the Accumulator. Remember the Upper 128 byes of data RAM can
be accessed only by indirect addressing, and SFR space only by direct addressing. With a 12 MHz clock,
all of these instructions execute in either 1 or 2 micro seconds.
4
Note that in all MCS-51 devices, the stack resides in on-chip RAM, and grows upwards. The PUSH
instruction first increments the Stack Pointer (SP), then copies the byte into the stack. PUSH and POP
use only direct addressing to identify the byte being saved or restored, but the stack itself is accessed by
indirect addressing using the SP register. This means the stack can go into the Upper 128, if they are
implemented, but not into SFR space.
In devices that do not implement the Upper 128, if the SP points to the Upper 128,PUSHed bytes are
lost, and POPped bytes are indeterminate.
The Data Transfer instructions include a 16 bits MOV that can be used to initialize the Data Pointer
(DPTR) for look-up tables in Program Memory, or for 16 bit external Data Memory access.
The XCH A, <byte> instruction causes the accumulator and addressed byte to exchange data. The XCHD
A, @Ri instruction is similar, but only the low nibbles are involved in the exchange.
Boolean Instructions
MCS-51 devices contain a complete Boolean (single-bit) processor. The internal RAM contains 128
addressable bits, and the SFR space can support up to 128other addressable bits. All of the port lines are
bit addressable and each one can be treated as a separate single bit port. The instructions that access these
bits are not just conditional branches, but a complete menu of move, set clear, complement, OR and AND
instructions.
5
Jump Instructions
The table above lists a single “JMP addr” instruction, but in fact there are three-SJMP, LJMP and AMP-
which differ in the format of the destination address. JMP is a generic mnemonic which can be used if
the program- mer does not care which way the jump is encoded.
The SJMP instruction encodes the destination address as a relative offset, as described above. The
instruction is 2 bytes long, consisting of the opcode and the relative offset byte. The jump distance is
limited to a range of -128 to + 127 bytes relative to the instruction following the SJMP.
The LJMP instruction encodes the destination address as a 16 bits constant. The instruction is 3 bytes
long, consisting of the opcode and two address bytes. The destination address can be anywhere in the
64K Program Memory Space.
The AJMP instruction encodes the destination address as 1l bits constant. The instruction is 2 byte long,
consisting of the opcode, which itself contains 3 of the 11 address bits, followed by another byte
containing the low 8 bits of the destination address. When the instruction is executed, these 11 bits are
simply substituted for the low 11 bits in the PC. The high 5 bits stay the same. Hence the destination has
to be within the same 2K block as the instruction following the AJMP.
6
Example Programs:
1. Write a program to clear the ACC and add value 3 to the ACC ten times.
;This program adds value 3 to the ACC ten times
ORG 00H
MOV A,#00H ;A=0, clear ACC
MOV R2,#10H
AGAIN: ADD A,#03H
DJNZ R2,AGAIN
MOV R5,A
END
2. Write a program to load the ACC with the value 55H, and complement the ACC 700 times.
ORG 00H
MOV A,#55H ;A=55H
MOV R3,#10H ;R3=10, outer loop count
NEXT: MOV R2,#70H ;R2=70, inner loop count
AGAIN: CPL A ;complement A register
DJNZ R2,AGAIN ;repeat it 70 times
DJNZ R3,NEXT
END
3. Write a program to Find the sum of the values 79H, F5H, E2H. Put the sum in registers R0
(low byte) and R5 (high byte)
ORG 00H
MOV A,#00H ;A=0
MOV R5,A ;clear R5
ADD A,#79H ;A=0+79H=79H
JNC N_1 ;if CY=0, add next number
INC R5 ;if CY=1, increment R5
N_1: ADD A,#0F5H ;A=79+F5=6E and CY=1
JNC N_2 ;jump if CY=0
INC R5 ;if CY=1,increment R5 (R5=1)
N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment R5
OVER: MOV R0,A
END
7
Time Delay For Various 8051 Chips
CPU executing an instruction takes a certain number of clock cycles which are referred as to as machine
cycles. The length of machine cycle depends on the frequency of the crystal oscillator connected to 8051.
In original 8051, one machine cycle lasts 12 oscillator periods
4. Find the period of the machine cycle for 11.0592 MHz crystal frequency .
Since, 11.0592/12 = 921.6 kHz. Hence, machine cycle is 1/921.6 kHz = 1.085μs
5. For 8051 system of 11.0592 MHz, find how long it takes to execute each instruction.
(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target (d) LJMP (e) SJMP (f) NOP
b) 1 1x1.085μs = 1.085μs
c) 2 2x1.085μs = 2.17μs
d) 2 2x1.085μs = 2.17μs
e) 2 2x1.085μs = 2.17μs
f) 1 1x1.085μs = 1.085μs
6. Find the size of the delay in following program, if the crystal frequency is 11.0592MHz.
Machine Cycle
DELAY: MOV R3, #250H 1
HERE: NOP 1
NOP 1
NOP 1
NOP 1
DJNZ R3,HERE 2
RET 2
The time delay inside HERE loop is [250(1+1+1+1+2)]x1.085μs = 1627.5μs.
Adding the two instructions outside loop we have 1627.5μs + 3 x 1.085μs = 1630.755μs
8
Section 2: 8051 I/O PROGRAMMING
In the 8051 there are a total of four ports for I/O operations.
The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins. All the ports upon RESET are configured as
input, ready to be used as input ports. When the first 0 is written to a port, it becomes an output. To
reconfigure it as an input, a 1 must be sent to the port
Port 0: It can be used for input or output, each pin must be connected externally to a 10K ohm pull-up
resistor. This is due to the fact that P0 is an open drain, unlike P1, P2, and P3. In order to make port 0 an
input, the port must be programmed by writing 1 to all the bits
7. Write a program to toggle all the bits of port 0 by sending to it the values 55H and 33H
continuously Put a time delay in between each issuing of data to port 0.
ORG 00H
BACK: MOV A,#55H ;load A with 55H
MOV P0,A ;send 55H to port 1
LCALL DELAY ;time delay
MOV A,#33H ;load A with AA
MOV P0,A ;send AAH to port 1
LCALL DELAY
SJMP BACK ;keep doing this indefinitely
9
ORG 300H ;put time delay at 300Hj
DELAY: MOV R5, #0FFH ;R5 = 255(FF in hex)
AGAIN: DJNZ R5,AGAIN ;stay here until R5 becomes 0
RET ;return to the caller
END
8. Write a program to configure Port 0 as an input port, and then send the data received from that
port to P1.
MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 an i/p port
;by writing it all 1s
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it
Port 1: It occupies a total of 8 pins (pins I through 8).It can be used as input or output. In contrast to port
0, this port does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset,
port 1 is configured as an input port. If port 1 has been configured as an output port, to make it an input
port again, it must programmed as such by writing 1 to all its bits.
Port 2: It can be used as input or output. Just like P1, port 2 does not need any pull- up resistors since it
already has pull-up resistors internally. Upon reset, port 2 is configured as an input port.To make port 2
an input port, it must be programmed as such by writing 1 to all its bits. In many 8051-based system, port
2 is used as simple I/O.
Port 3: Port 3 can be used as input or output and Port 3 also does not need any pull-up resistors. Port 3
has the additional function of providing some extremely important signals
10
Section 3: I/O Bit Manipulation PROGRAMMING
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits. A powerful feature
of 8051 I/O ports is their capability to access individual bits of the port without altering the rest of the
bits in that port. Of the four 8051 ports, we can access either the entire 8 bits or any single bit without
altering the rest.
When accessing a port in single-bit manner, we use the syntax
SETB X.Y where X is the port number 0, 1,2, or 3, and
Y is the desired bit number from 0 to 7 for data bits D0 to 07.
For example, SETB P1.5" sets high bit 5 of port 1. Remember that D0 is the LSB and D7 is the MSB
We have the following single bit instructions
Instructions Functions
SETB bit Set the bit(bit = 1)
CLR bit Clear the bit(bit = 0)
CPL bit Complement the bit(bit = NOT bit)
JB bit, target Jump to target if bit = 1(jump if bit)
JNB bit, target Jump to target if bit = 0(jump if no bit)
JBC bit, target Jump to target if bit = 1, clear bit (jump if bit, then clear)
9. Write a program to create a square wave of 50% duty cycle on bit 0 of port 1.
The 50% duty cycle means that the “on” and “off” state (or the high and low portion of the pulse)
have the same length. Therefore, we toggle P1.0 with a time delay in between each state.
HERE: SETB P1.0 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
CLR P1.0 ;P1.0 = 0
LCALL DELAY ;call the delay subroutine
SJMP HERE ;keep doing it
10. Write a program to create a square wave of 66% duty cycle on bit 3 of port 1.
The 66% duty cycle means the "on" state is twice the "off' state.
HERE: SETB P1.3 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
LCALL DELAY ;call the delay subroutine
CLR P1.3 ;P1.0 = 0
LCALL DELAY ;call the delay subroutine
11
SJMP HERE ;keep doing it
11. Using an assembly language, generate a pulse of 75% duty cycle at pin P1.7 when the switch
at 1.1 is ON.
HERE: SETB P1.1 ;make P1.1 an input
AGAIN: JNB P1.1,AGAIN ;get out when P1.1 = 1
SETB P1.7 ;set to high bit 7 of port 1
LCALL DELAY ;call the delay subroutine
LCALL DELAY ;call the delay subroutine
LCALL DELAY ;call the delay subroutine
CLR P1.7 ;P1.7 = 0
LCALL DELAY ;call the delay subroutine
SJMP HERE ;keep doing it
12. Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it
means that the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-
to-low pulse to port P1.5 to tum on a buzzer.
HERE: JNB P2.3,HERE ;keep monitoring for high
SETB Pl.5 ;setbitP1.5=1
CLR Pl. 5 ;make high-to-Iow
SJMP HERE ;keep repeating
13. A switch is connected to pin Pl.7 and an LED to pin P2.0. Write a program to get the status
of the switch and send it to the LED.
SW BIT P1. 7 ;assign bit
LED BIT P2.0 ;assign bit
HERE: MOV C,SW ;get the bit from the port
MOV LED,C ;send the bit to the port
SJMP HERE ;repeat forever
12
14. A switch is connected to pin P1.7? Write a program to check the status of the switch and
make the following decision.
(a) If SW= 0, send "NO" to P2.
(b) If SW = 1, send "YES" to P2.
SW EQU P1.7
MYDATA EQU P2
HERE: MOV C,SW
JC OVER
MOV MYDATA,#’N’
MOV MYDATA,#’O’
SJMP HERE
OVER: MOV MYDATA,#’Y’
MOV MYDATA,#’E’
MOV MYDATA,#’S’
SJMP HERE
END
15. An LED is connected to pin P1.7. Write a program to toggle the LED forever.
LED BIT P.7 ;using BIT directive
13
Section 4: Arithmetic, Logic Instructions, and Programs
16. Assume that RAM locations 40 – 44H have the following values. Write a program to find
the sum of the values. At the end of the program, register A should contain the low byte and
R7 the high byte.
40 = (7D) 41 = (EB) 42 = (C5) 43 = (5B) 44 = (30)
MOV R0,#40H ; load pointer
MOV R2,#5 ; load counter
CLR A ; clear A
MOV R7,A ; clear R7
AGAIN: ADD A,@R0 ; add the byte ptr to by R0
JNC NEXT ; if CY = 0, don’t add CY
17. Write a program to add two 16-bit numbers. Place the sum in R7 and R6; R6 should have
the lower byte.
CLR C ;make CY=0
MOV A, #0E7H ;load the low byte now A=E7H
ADD A, #8DH ;add the low byte
MOV R6, A ;save the low byte sum in R6
MOV A, #3CH ;load the high byte
ADDC A, #3BH ;add with the carry
MOV R7, A ;save the high byte sum
18. Assume that 5 BCD data items are stored in RAM locations starting at 40H, as shown below.
Write a program to find the sum of all the numbers. The result must be in BCD.
40=(71) 41=(11) 42=(65) 43=(59) 44=(37)
MOV R0,#40H ; load pointer
MOV R2,#5 ; load counter
CLR A ; clear A
MOV R7,A ; clear R7
AGAIN: ADD A,@R0 ; add the byte ptr to by R0
DA A ; adjust for BCD
JNC NEXT ; if CY = 0, don’t add CY
14
INC R7 ; keep track of carry
19. Write a program to transfer value 41H serially (one bit at a time) via pin P2.1. Put two highs
at the start and end of the data. Send the byte LSB first.
MOV A, #41H
SETB P2.1 ; high
SETB P2.1 ; high
MOV R5, #8
AGAIN: RRC A
MOV P2.1, C ; send CY to P2.1
DJNZ R5, AGAIN
SETB P2.1 ;high
SETB P2.1 ;high
20. Write a program to bring in a byte of data serially one bit at a time via pin P2.7 and save it
in register R2. The byte comes in with the LSB first.
MOV R5,#8
AGAIN: MOV C,P2.7 ;bring in bit
RRC A
DJNZ R5,AGAIN
MOV R2,A ; save it
15
Section 5: 8051 Programming in C
C data types for the 8051
Unsigned char: The character data type is the most natural choice because 8051 is an 8-bit
microcontroller. Unsigned char is an 8-bit data type in the range of 0 – 255 (00 – FFH) and is one of the
most widely used data types for the 8051. C compilers use the signed char as the default if we do not put
the keyword unsigned before char.
21. Write an 8051 C program to send values 00 – FF to port P1.
#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
22. Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C,
and D to port P1.
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=“012345ABCD”;
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}
23. Write an 8051 C program to toggle all the bits of P1 continuously.
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
p1=0x55;
p1=0xAA;
}
}
Signed char: The signed char is an 8-bit data type Use the MSB D7 to represent – or + Give us values
from –128 to +127. We should stick with the unsigned char unless the data needs to be represented as
signed numbers(e.g: temperature)
16
24. Write an 8051 C program to send values of –4 to +4 to port P1.
//Singed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
Unsigned int: The unsigned int is a 16-bit data type which takes a value in the range of 0 to 65535 (0000
– FFFFH). It can be used to define 16-bit variables such as memory addresses, setting counter values of
more than 256. Since registers and memory accesses are in 8-bit chunks, the misuse of int variables will
result in a larger hex file
Signed int: It is a 16-bit data type. We use the MSB D15 to represent – or +. We have 15 bits for the
magnitude of the number from –32768 to +32767.
Sbit (single bit): The sbit keyword is a widely used 8051 C data type designed specifically to access
single-bit addressable registers.
25. Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
26. Write an 8051 C program to toggle bits of P1 continuously forever with some delay.
//Toggle P1 forever with some delay in between //“on” and “off”
#include <reg51.h>
void main(void){
unsigned int x;
for (;;) //repeat forever
{
p1=0x55;
for (x=0;x<40000;x++); //delay size unknown
p1=0xAA;
for (x=0;x<40000;x++);}}
17
27. Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms.
#include <reg51.h>
void MSDelay(Pununsforunsigned int);
void main(void)
{
while (1)// repeat forever
{
p1 = 0x55;
MSDelay(250);
p1 = 0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for (i = 0; i < itime; i++)
for (j = 0; j < 1275; j++);
}
The program below is tested for the DS89C420 with XTAL = 11.0592 MHz.
28. LEDs are connected to bits P1 and P2. Write an 8051 C program that shows the count from
0 to FFH (0000 0000 to 1111 1111 in binary) on the LEDs
#include <reg51.h>
#define LED P2;
void main(void)
{
P1 = 00;
LED = 0;
for (;;)
{
P1++;
LED++;
}
}
29. Write an 8051 C program to get a byte of data form P1, wait 1/2 second, and then send it to
P2.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
unsigned char mybyte;
P1 = 0xFF;
while (1)
{
mybyte = P1;
18
MSDelay(500);
P2 = mybyte;
}
}
30. Write an 8051 C program to monitor bit P1.5. If it is high, send 55H to P0; otherwise, send
AAH to P2.
#include <reg51.h>
sbit mybit = P1 ^ 5;
void main(void)
{
mybit = 1;
while (1)
{
if (mybit == 1)
P0 = 0x55;
else
P2 = 0xAA;
}
}
31. The data pins of an LCD are connected to P1. The information is latched into the LCD
whenever its Enable pin goes from high to low. Write an 8051 C program to send “The
Earth is but One Country” to this LCD.
#include <reg51.h>
#define LCDData P1 //LCDData declaration
sbit En=P2^0; //the enable pin
void main(void)
{
unsigned char message[] =“The Earth is but One Country”;
unsigned char z;
for (z = 0; z < 28; z++) // send 28 characters
{
LCDData=message[z];
En=1; //a high-
En=0; //-to-low pulse to latch data
}
}
32. Write an 8051 C program to get the status of bit P1.0, save it, and send it to P2.7
continuously.
#include <reg51.h>
sbit inbit = P1 ^ 0;
sbit outbit = P2 ^ 7;
bit membit;//use bit to declare bit addressable memory
void main(void)
{
19
while (1)
{
membit = inbit;//get a bit from P1.0
outbit = membit;//send it to P2.7
}
}
33. Write a C program to send out the value 44H serially one bit at a time via P1.0. The LSB
should go out first.
#include <reg51.h>
sbit P1b0 = P1 ^ 0;
sbit regALSB = ACC ^ 0;
void main(void)
{
unsigned char conbyte = 0x44;
unsigned char x;
ACC = conbyte;
for (x = 0; x < 8; x++)
{
P1b0 = regALSB;
ACC = ACC >> 1;
}
}
34. Write a C program to bring in a byte of data serially one bit at a time via P1.0. The LSB
should come in first.
#include <reg51.h>
sbit P1b0 = P1 ^ 0;
sbit ACCMSB = ACC ^ 7;
bit membit;
void main(void)
{
unsigned char x;
for (x = 0; x < 8; x++)
{
membit = P1b0;
ACC = ACC >> 1;
ACCMSB = membit;
}
P2 = ACC;
}
20
Section 6: 8051 Timer Programming in Assembly and C
The 8051 has two timers/counters, they can be used either as timers to generate a time delay or as event
counters to count events happening outside the microcontroller.
Both Timer 0 and Timer 1 are 16 bits wide. Since 8051 has an 8-bit architecture, each 16-bits timer is
accessed as two separate registers of low byte and high byte. The low byte register is called TL0/TL1
and the high byte register is called TH0/TH1. These are accessed like any other register:
MOV TL0,#4FH
MOV R5,TH0
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various timer operation
modes. TMOD is a 8-bit register. The lower 4 bits are for Timer 0 and the upper 4 bits are for Timer 1 .
In each case, the lower 2 bits are used to set the timer mode and the upper 2 bits to specify the operation.
GATE:
Gating control when set: Timer/counter is enable only while the INTx pin is
high and the TRx control pin is set.
When cleared, the timer is enabled whenever the TRx
control bit is set.
21
35. Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H
(b) MOV TMOD, #20H
(c) MOV TMOD, #12H
We convert the value from hex to binary.
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
36. Find the value for TMOD if we want to program timer 0 in mode 2, use 8051 XTAL for the
clock source, and use instructions to start and stop the timer.
TMOD= 0000 0010 Timer 0, mode 2,
CIT = 0 to use XTAL clock source, and
gate = 0 to use internal (software) start and stop method.
Mode 1 Programming: To generate a time delay
I. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and
which timer mode (0 or 1) is selected
II. Load registers TL and TH with initial count value
III. Start the timer
IV. Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see if it is raised and
get out of the loop when TF becomes high
V. Stop the timer
VI. Clear the TF flag for the next round
22
VII. Go back to Step 2 to load TH and TL again
37. In the following program, we create a square wave of 50% duty cycle (with equal portions
high and low) on the P1.5 bit. Timer 0 is used to generate the time delay.
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ; toggle P1.5
ACALL DELAY
SJMP HERE
38. In Example 37, calculate the amount of time delay in the DELAY subroutine generated by
the timer. Assume XTAL = 11.0592 MHz.
The timer works with a clock frequency of 1/12 of the XTAL frequency; therefore, we have
11.0592 MHz / 12 = 921.6 kHz as the timer frequency. As a result, each clock has a period of
T = 1/921.6kHz = 1.085 μs
In other words, Timer 0 counts up each 1.085 us resulting in delay = no. of counts × 1.085μs.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal). However, we
add one to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raise the
TF flag.
This gives 14 × 1.085μs = 15.19μs for half the pulse. For the entire period it is
T = 2 × 15.19us = 30.38μs as the time delay generated by the timer.
39. 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();
}
23
}
void T0Delay()
{
Here, FFFFH – 3500H
TMOD=0x01; = CAFFH = 51967 + 1
TL0=0x00; = 51968
TH0=0x35; Hence,
TR0=1; 51968 × 1.085 μs
while(TF0==0); = 56.384 ms is the approximate delay
TR0=0;
TF0=0;
}
40. Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use Timer 1,
mode 1 to create the delay.
#include <reg51.h>
void T1M1Delay(void); Here, A5FEH = 42494 in decimal
void main(void) = 65536 – 42494
{ = 23042
unsigned char x; Hence, 23042 × 1.085 μs = 25 ms and
P2=0x55; 20 × 25 ms = 500 ms
while(1)
{
P2=~P2;
for(x=0;x<20;x++)
T1M1Delay();
}
}
void T1M1Delay(void)
{
TMOD=0x10;
TL1=0xFE;
TH1=0xA5;
TR1=1;
while(TF1==0);
TR1=0;
TF1=0;
}
24
Tasks:
1) For a machine cycle of 1.085 μs,find the time delay in the following subroutine.
Machine Cycle
DELAY: MOV R2,#200 1
AGAIN: MOV R3,#250 1
HERE: NOP 1
NOP 1
DJNZ R3,HERE 2
DJNZ R2,AGAIN 2
RET 2
2) Write a program to toggle all the bits of port 1 by sending to it the values 55H and AAH
continuously.Put a time delay in between each issuing of data to port 1.
3) Write a program to configure port 1 as an input port by writing 1s to it, then save the data received
from that port in R7, R6, and R5.
4) A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status of the
switch and send it to the LED .
5) Write an assembly language program to blink the 8 LED connected at port 2 of the 8051
microcontroller.
6) Explain the addressing modes used in 8051 microcontroller with examples. Write a program that finds
the number of 1s in a given byte.
7) Write a program that finds the position of the first high in an 8-bit data item. The data is scanned from
DO to D7. Give the result for 68H.
8) Write an 8051 C program to get a byte of data form P0. If it is less than 100, send it to P1; otherwise,
send it to P2.
9) Write a C program to send out the value 44H serially one bit at a time via P1.0. The MSB should go
out first.
10) Draw the circuit diagram of the minimum configuration for the 8051 microcontroller to operate. Also
show the connnection of LED at P1.7 and switch at P1.1 in the same circuit.
11) Using an assembly language, generate a pulse of 75% duty cycle at pin P1.7 when the switch at 1.1
is ON.
12) Write an assembly language programming for 8051 microcontroller to read the data from switches
connected at port 1 and send it to port 2 for display in LED.
25
13) Write a program using C-programming language to find the sum between two 8-bit BCD data stored
in RAM locations 50H and 5lH and store the BCD sum at RAM locations 52H and 53H.
14) Write 8051 program and draw circuit diagram to display number from 99 to 00 in a seven segment
display. The program should written in both assembly and C.
15) Why is 8051 microcontroller used? Wtite an assembly program to get data from P0 and send it to P1
and compare it with corresponding C program.
16) Show the internal structure of the 8051 microcontroller. Provide a comparison chart of the 8051
family members.
17) Draw the pin diagram of 8051 microcontoller and explain ports 1 and 2 only.
18) Explain about port 3 of 8051 microcontoller. Using 8051 instructions, control the rate of blink of
LED ar pin P1.1 by two switches at P2.1 and P2.2.(Use one switch to increase the rate of blink and
another switch to decrease the rate of blink)
19) Generate a periodic square wave having period of 15 ms and a duty cycle of 20% in 8051 using
assembly programming. The waveform should be produced at pin zero of port two(P 2.0), the XTAL
frequency is 11.0592 MHz and use Timer 1 in mode 0(13-bit timer mode)
20) What is seven segment display and write its types. Design a circuit with 7 segments display which is
used as a counter watch which displays seconds and minutes.
26