Beruflich Dokumente
Kultur Dokumente
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 receive 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.
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.
TIMER 1 TIMER 0
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 M2 OPERATING MODE
0 1 1 16-bit timer/counter
The 8051 has also a 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:
SM2 SCON.5 Enables the multiprocessor communication feature in modes 2 & 3. In mode
2 or 3, if SM2 is set to 1 then R1 will not be activated if the received 9th
data bit (RB8) is 0. In mode 1, if SM2=1 then R1 will not be activated if a
valid stop bit was not received. In mode 0, SM2 should be 0. (See table 6).
TB8 SCON.3 The 9th bit that will be transmitted in modes 2 & 3. Set/Cleared by
software.
RB8 SCON.2 In modes 2 & 3, is the 9th data that was received. In mode 1, if SM2=0,
RB8 is the stop bit that was received. In mode 0, RB8 is not used.
T1 SCON.1 Transmit interrupt flag. Set by hardware at the end of the 8th bit time in
mode 0, or at the beginning of the stop bit in the other modes. Must be
cleared by software.
R1 SCON.0 Receive interrupt flag. Set by hardware at the end of the 8th bit time in
mode 0, or halfway through the stop bit time in the other modes (except
see SM2). Must be cleared by software.
NOTE 1:
0 0 0 Shift Register
0 1 1 8-bit UART
1 0 2 9-bit UART
1 1 3 9-bit UART
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:
4. Add MOV SBUF , #”E” and MOV SBUF, #”C” after MOV SBUF , #”E” then rebuild debug and run
again the program. Observe the output.
7. Rebuild ( i.e., Rebuild All Target Files) and debug the program. Run the program and observe
the output of serial#1.
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 happen?
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.
END
2. Rebuild (i.e., Rebuild All Target Files), debug the program. Select port2 and then unchecked bit
position 7,5,4 and 1. Rub the program and observe the output obtained in serialno.1. Is the
output evident from the value set by port2?
3. Stop the program. Select the 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?
4. Try another ASCII code if it will be displayed on the serialno.1 by configuring the settings of
port2.
INTERRUPTS:
To use any of the interrupts in the 80C51 Family, 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.
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 pon is connected to a switch that is normally HIGH. Whenever it goes
low, it should turn-on and LED (or toggle in this experiment). The LED is connected to port1
and is normally OFF. When it it’s 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).
ORG 0013H
MOV P1, #109
MOV R3, #77
MOV SBUF, R3
BACK : DJNZ R3, BACK
CLR P1.0
CLR P1.1
CLR P1.2
CLR P1.3
CLR P1.4
CLR P1.5
CLR P1.6
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 port3 and observe what happen. Record your result.
5. Toggle again bit position 3 of port3 and observe what happen. Then continuously toggle
P3.3 and record your result.
6. Toggle other bit position of port3 and observe what happen. Record your result.
Return to the “main program proper” and change MOV R3, #77 to and MOV R3,#115 then rebuild
(i.e., Rebuild All Target Files), debug and run again the program. Observe what happen and record
your result.
QUESTION:
1. What is the baud rate of the first program in procedure A of Experiment 4-A? In the second
program?
2. What is the importance of TI flag in procedure A of Experiment 4-A?
3. What is the importance of RI flag in procedure B of Experiment 4-A?
4. How does the byte of data received and outputted by the serial port in procedure B of
Experiment 4-A?
5. What is the effect every time P3.3 is toggled on serial port and port1 of Experiment 4-B?
6. What portion of the program represent (or refers to) the interrupt of Experiment 4-B?
7. What is the use of CLR P1.0 to CLR P1.7 in the program of Experiment 4-B?
8. How does interrupt in the program for P3.3 is set of Experiment 4-B?