You are on page 1of 11

EXPERIMENT NO.

4
SERIAL PORT AND INTERRUPT PROGRAMMING OF 8051

OBJECTIVE:
 To be able to program the 8051 to transfer character bytes serially.
 To be able to program the 8051 to received a character of data serially.
 To be able to program the 8051 for interrupt routine.
 To be able to determine the effect and use of interrupt routines.

EXPERIMENT 4-A: SERIAL PROGRAMMING


INTRODUCTION:
The 8051 transfers and receives data serially at many different baud rates. The baud rate in
8051 is programmable. This is done with the help of TIMER1. The 8051 divides the crystal
frequency by 12 to get the machine cycle frequency. In the case of XTAL = 11.0592MHz, the
machine cycle frequency is 921.6kHz. The 8051’s serial communication UART circuitry divides
the machine cycle frequency by 32 once more before it is used by TIMER1 to set the baud rate
(i.e., 28,800Hz). This is the value used in this experiment to find the TIMER1 value to set the
baud rate.

When TIMER1 is used to set the baud rate it must be programmed in mode 2, that 8-bit, auto
reload. To get the baud rate compatible with PC’s, we must load TH1 with values shown in
TABLE1.

BAUD RATE TH1(Decimal) TH1(Hexadecimal)


9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8

Two timers are found in 8051 MCU. Both timers use the same register called TMOD, to set the
various timer operation modes. TMOD is an 8-bit register in which the lower 4 bits are set aside
for TIMER0 and the upper for bits for TIMER1. In each case, the lower two bits are used to set
the timer mode and the upper 2 bits to specify the operation as shown below.

TMOD: TIMER/COUNTER MODE CONTROL REGISTER. NOT BIT ADDRESSABLE

GATE When TRx (in TCON) is set and GATE = 1,TIMER/COUNTERx will run only while
INTx pin is high (hardware control). When GATE=0, TIMER/COUNTERx will run only
while TRx=1(software control).

C/T Timer or Counter selector. Cleared for Timer operation (input from internal system
clock). Set for Counter operation (input from Tx input pin).

M1 Mode selector bit. (NOTE 1)

M0 Mode selector bit. (NOTE 1)


The 8051 has also an SCON (serial control) register. The SCON register is an 8-bit register used
to program the start bit, stop bit, and the data framing, among other things. The following
describes the bits of the SCON register.

PROCEDURE A: PROGRAMMING 8051 TO TRANSFER DATA SERIALLY

1. Make a new Project and .a51 file with a name expt5 and expt5a.a51 respectively.
(Just repeat procedure A of experiment No. 1 in creating a New Project and .a51 file).

2. Encode the following commands:


MOV TMOD,#20H
MOV TH1,#-6
MOV SCON,#50H
SETB TR1
AGAIN: MOV SBUF,#”E”
HERE: JNB T1,HERE
CLR T1
SJMP AGAIN
END
3. Rebuild (i.e., Rebuild All Target Files) and debug the program. (NOTE: Select the
“serial window no.1” icon found in the second line of menu bar (at the right of the
icon “code” with a check) if not yet displayed). Run the program and observe the
output of serial #1.

After running the program, a continuous E was debugged.

4. Add MOV SBUF,#”E”and MOV SBUF,#”C” after MOV SBUF,#”E” then rebuild,
debug and run again the program. Observe the output.

After adding MOV SBUF, #”E” ans MOV SBUF, #”C”, the output become a continuous C.
5. Return to the “main program proper”.

6. Encode the following program.

MOV TMOD,#20H
MOV TH1,#-3
MOV SCON,#50H
SETB TR1
AGAIN: MOV A,#”E”
ACALL TRANS
MOV A,#”C”
ACALL TRANS
MOV A,#”E”
ACALL TRANS
SJMP AGAIN
TRANS: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET

END
7. Rebuild (i.e., Rebuild All Target Files) and debug the program. Run the program and
observe the output of serial #1.

After running the program, a continuous A was debugged.


8. Replace -3 in the instruction MOV TH1,#-3 by -24 and then rebuild,debug and run
again the program. Observe the output of serial no.1. What will happen?

The output in the serial no.1 is the same with procedure 7.

PROCEDURE B: PROGRAMMING 8051 TO RECEIVED DATA SERIALLY


Under the same project, create another text file, save, and label it as expt5b.a51. Close the
expt5a.a51 text file.

1. The following programs the 8051 to receive byte of data serially from P2. It uses a
baud rate of about 4800 and 1 stop bit. Encode, rebuild, debug and run the following
program.
MOV TMOD,#20H
MOV TH1,#0FAH
MOV SCON,#50H
SETB TRI
MOV A,P2
ACALL SEND
ACALL RECV
SEND: MOV SBUF,A
H_1: JNB TI,H_1
CLR TI
RET

RECV: JNB RI,RECV


MOV A,SBUF
CLR RI
RET

END
2. Rebuild (i.e., Rebuild All Target Files), debug the program. Select port2 and then
unchecked bit positions 7,5,4 and 1. Run the program and observe the output
obtained in serial no.1. Is the output evident from the value set by port2?

The serial port produces M as we run the program.

3. Stop the program. Select RST icon (means RESET CPU) found in the left most part
of the second line of menu bar. Checked bit position 5 and then run again the
program and observe the output obtained in serialno.1. Is the output evident from the
value set by port2?

 Yes. It has M and Beta.

4. Try another ASCII code if it will be displayed on the serialno.1 by configuring the
settings of port2.
We unchecked bit position 7, 4, and 1 it produces an output of degree
symbol added to M and Beta.

EXPERIMENT 4-B: INTERRUPT PROGRAMMING


INTRODUCTION:
There are only two external hardware interrupts in the 8051: INT0 and INT1. They are
located on pins P3.2 and P3.3 of port 3, respectively. The interrupt vector table locations
0003H and 0013H are set aside for INT0 and INT1, respectively.

INTERRUPTS:
To use any of the interrupts in the 80C51Family, the following three steps must be taken.
1. Set the EA (enable all) bit in the IE register to 1.
2. Set the corresponding individual interrupt enable bit in the IE register to 1.
3. Begin the interrupt service routine at the corresponding Vector Address of that interrupt.
See Table below.

INTERRUPT SOURCE VECTOR ADDRESS


IE0 0003H
TF0 000BH
IE1 0013H
TF1 001BH
RI & TI 0023H

In addition, for external interrupts, pins INT0 and INT1 (P3.2 and P3.3) must be set to 1, and
depending on whether the interrupt is to be level or transition activated, bits IT0 or IT1 in
the TCON register may need to be set to 1.
ITx = 0 level activated

ITx = 1 transition activated


IE: INTERRUPT ENABLE REGISTER. BIT ADDRESSABLE

User software should not write 1s to reserved bits. These bits may be used in future 80C51
products to invoke new features.

PROCEDURE:
1. Make a new Project and .a51 file with a name expt6 and expt6.a51 respectively.
(Just repeat procedure A of experiment No. 1 in creating a New Project and .a51
file).

2. Assume that the INT1 pin is connected to a switch that is normally HIGH. Whenever
it goes low, it should turn-on an LED (or toggle in this experiment). The LED is
connected to port 1 and is normally OFF. When it is turned-on it should stay ON for a
fraction of a second. As long as the switch is press low, LED should stay ON (or the
port should stay toggled).

Encode the following commands:

ORG 000H
LJMP MAIN

ORG 0013H
MOV P1,#109
MOV R3,#77
MOV SBUF,R3
BACK: DJNZ R3,BACK
CLR P1.0
CLR P1.1
CLR P1.1
CLR P1.1
CLR P1.1
CLR P1.1
CLR P1.1
CLR P1.1
RETI

ORG 30H
MAIN: MOV IE,#10000100B
HERE: SJMP HERE
END
3. Rebuild (i.e., Rebuild All Target Files) and debug the program. Select the “serial
window no.1” icon and display port1 and port3. Run the program.

4. Toggle bit position 3 of port 3 and observe what happen. Record your result.

When you toggle the bit position 3 to port 3, letter M was displayed in the serial window.

5. Toggle again bit position 3 of port3 and observe what happen. Then continuously
toggle P3.3 and record your result.

Serial No. 1 has stopped.


6. Toggle other bit position of port3 and observe what happen. Record your result.

Ports 1,7, and 3 are blinking and there’s a continuously and a continuous letter “M” in the
serial port.

Return to the “main program proper” and change MOV R3,#77 to MOV R3,#122 and then rebuild
(i.e., Rebuild All Target Files), debug and run again the program. Observe what happen and
record your result.

When we toggled the bit position 3 of port 3, the letter “z” was displayed in the serial window.
QUESTION:
1. What is the baud rate of the first program in procedure A of Experiment 4-A? In
the second program?
-The baud rate of the first program is 4,800 while in the second
program our baud rate is 9,600.

2. What is the importance of TI flag in procedure A of Experiment 4-A?


-When 8051 finishes the transfer of 8-bit character it raise the TI flag
to indicate that it is ready to transfer another byte. By monitoring the TI flag,
we make sure that we are not overloading the SBUF.

3. What is the importance of RI flag in procedure A of Experiment 4-A?


-By checking the RI flag bit when it raised, we know that a character
has been received and is sitting in the SBUF register. After the SBUF
contents are copied into a safe place, the RI flag bit must be forced to zero by
CLR RI in order to allow the next received character byte to be placed is
SBUF.

4. How does the byte of data received and outputted by the serial port in
procedure B of Experiment 4-A?
- Generally, whenever a byte has been received the 8051 will set the "RI"
(Receive Interrupt) flag. This lets the program know that a byte has been
received and that it needs to be processed. However, when SM2 is set the "RI"
flag will only be triggered if the 9th bit received was a "1". That is to say, if SM2
is set and a byte is received whose 9th bit is clear, the RI flag will never be set.

5. What is the effect every time P3.3 is toggled on serial port and port1 of
Experiment 4-B?
-If we toggle p3.3, the value of register 3 becomes “00” and letter m
continuously displayed at the serial window, when p3.3 is at off state it has a
value “4a” and letter m was not transferred to the serial window.

6. What portion of the program represent (or refers to) the interrupt of
Experiment 4-B?
-The instruction JNB TI, H1 represent or refers to the interrupt in the
experiment.

7. What is the use of CLR P1.0 to CLR P1.7 in the program of Experiment 4-B?
-The use of the CLR P1.0 to CLR P1.7 in the program is to disable the
interrupts.

8. How does interrupt in the program for P3.3 is set of Experiment 4-B?
The interrupt in the program is used to wait for the entire byte to be
sent. The RI flag bit can be checked by the JNB T1, H1.

You might also like