0% found this document useful (0 votes)
4 views65 pages

Module 4

Module 4 covers 8051 serial port and interrupt programming in both assembly and C. It includes topics such as basics of serial communication, RS232 standards, and programming exercises for data transfer at various baud rates. Additionally, it discusses interrupt handling, including sources of interrupts, registers associated with interrupts, and the differences between interrupts and polling.

Uploaded by

twitterrono30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views65 pages

Module 4

Module 4 covers 8051 serial port and interrupt programming in both assembly and C. It includes topics such as basics of serial communication, RS232 standards, and programming exercises for data transfer at various baud rates. Additionally, it discusses interrupt handling, including sources of interrupts, registers associated with interrupts, and the differences between interrupts and polling.

Uploaded by

twitterrono30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 4

(a) 8051 serial port programming


in assembly and C

(b) 8051 Interrupt programming


in assembly and C
In This Module . . .
➢ In this Module

1. Basics of serial communication,


2. 8051 connection to RS232,
3. 8051 serial port programming in assembly,
4. serial port programming in 8051 C.
Basics of Serial Communication

SENDER RECEIVER

SENDER RECEIVER
Types
Simplex TRANSMITTER RECEIVER

TRANSMITTER RECEIVER
Half Duplex
RECEIVER TRANSMITTER

TRANSMITTER RECEIVER
Full Duplex
RECEIVER TRANSMITTER
Asynchronous serial communication

Each character is placed between a start and stop bits – framing

The start bit is always one bit, the stop bit can be one or two bits

The start bit is always a 0 and the stop bit is a 1


Ex: To transmit the ASCII character ‘A’ (01000001)
Start
bit
Space 0 1 0 0 0 0 0 1 mark
Stop D7 D0
bit Goes out first

Goes out last

When there is no transfer, the signal is 1(High) –


referred to as mark
The 0 (low) is referred to a space.
Rate of Transfer – bits/sec or bps
Data Transfer Rate
Another wide terminology – baud rate – term
used in modems – number of signal changes per
second

Baud rate and bps are not necessarily equal


RS232 Standards
❖To allow compatibility among
data communication equipment
made by various manufacturers
❖Most widely used serial I/O
interfacing standard
RS232 Pins
DB-25 : 25 pin connector
PIN Description
1 Protective ground
2 Transmitted data
3 Received data
4 Request to send
5 Clear to send
6 Data set ready
7 Signal ground
8 Data carrier detect
9/10 Reserved for data
testing
PIN Description PIN Description
11 Unassigned 18 Unassigned
12 Secondary data 19 Secondary request to
carrier detect send
13 Secondary clear to 20 Data terminal ready
send 21 Signal quality
14 Secondary detector
transmitted data 22 Ring indicator
15 Transmit signal
23 Data signal rate
element timing
select
16 Secondary received
24 Transmit signal
data
element timing
17 Receive signal
25 unassigned
element timing
RS232 Pins
DB-9 : 9 pin connector
PIN Description
1 Data carrier detect (DCD)
2 Received data (RxD)
3 Transmitted Data (TxD)
4 Data Terminal Ready (DTR)
5 Signal Ground (GND)
6 Data Set Ready (DSR)
7 Request to send (RTS)
8 Clear to send (CTS)
9 Ring indicator (RI)
Function of the pins of 9 pin
RS 232 connector
⚫ Refer page 241 (Mazidi)
Registers and Pins Associated with Serial
data Input/Output
SCON : Controls data communication
PCON : Controls data rate
Pins RXD and TXD connect to the serial data
network
SBUF : Holds data to be transmitted or received.
SBUF is physically two different registers. One is
write only and is used to hold data to be
transmitted out via TXD. The other is read only
and holds received data via RXD. Both mutually
exclusive registers use address 99h.
SERIAL PORT CONTROL (SCON)
7 6 5 4 3 2 1 0

SM0 SM1 SM2 REN TB8 RB8 TI RI


***BIT ADDRESSABLE
7&6: SM0 & SM1 – Serial port mode select bits
SM0 SM1 MODE DESCRIPTION
0 0 0 Shift register; Baud = f/12
0 1 1 8 – bit UART; Baud = variable
1 0 2 9 – bit UART; Baud = f/32 or f/64
1 1 3 9 – bit UART; Baud = variable
5: SM2 – Multiprocessor Communication Bit
Set/Cleared by program to enable multiprocessor
communication in modes 2 & 3.
When set to 1 an interrupt is generated if bit 9 of
the received data is a 1; no interrupt is generated
if bit 9 is a 0.
If set in mode 1, no interrupt will be generated
unless a valid stop bit is received. Clear to 0 if
mode 0 is in use.
4: REN – Receive Enable Bit
Set to 1 to enable reception; Cleared to 0 to
disable reception
3: TB8 – Transmitted bit 8. Set or cleared by program in
modes 2 &3
2: RB8 – Received bit 8. Bit 8 of received data in modes 2
& 3. Stop bit in mode 1. Not used in mode 0.
1: TI – Transmit Interrupt flag. Set to one at the end of bit 7
time in mode 0, and at the beginning of the stop bit for
other modes.
Must be cleared by the program.
0: RI – Receive Interrupt flag. Set to one at the end of bit 7
time in mode 0, and halfway through the stop bit for other
modes.
Must be cleared by the program.
POWER MODE CONTROL (PCON)
7 6 5 4 3 2 1 0

SMOD -- -- -- GF1 GF0 PD IDL


***NOT BIT ADDRESSABLE
7: SMOD – Serial Baud rate modify bit
Set to 1 by program to double baud rate using
timer 1 for modes 1, 2 and 3. Cleared to 0 by
program to use timer 1 baud rate
MODE I & 3 BAUD RATE
SMOD
2
f baud =  (Timer1Overflowfrequency)
32d
 2 SMOD OscillatorFrequency 
TH1 = 256d −   
 32d 12  f baud 

SMOD
2 Oscillatorfrequency
f baud = 
32d 12d  [256 − (TH1)]
TH1 Register values for various baud
rates
Baud rate Baud Rate TH1 TH1(Hex)
SMOD = 0 SMOD = 1 (Decimal
)
9600 19200 -3 FD

4800 9600 -6 FA

2400 4800 -12 F4

1200 2400 -24 E8


Excercise
Write a 8051 ALP to transfer letter “A”
serially at 4800 baud, continuously.
MOV TMOD,#20H
MOV TH1,#FA
MOV SCON,#50H
SETB TR1
AGAIN: MOV SBUF,#’A’
HERE: JNB TI , HERE
CLR TI
SJMP AGAIN
#include<reg51.h>
void main(void)
{
TMOD=0x20;
TH1=0xFA;
SCON=0x50;
TR1=1;
while(1)
{
SBUF=‘A’;
while(TI==0);
TI=0;
}
}
Excercise
Write a program to transfer letter the message
“YES” serially at 9600 baud, 8 bit data,
1stop bit. Do this continuously.
MOV TMOD,#20H
MOV TH1,#FD
MOV SCON,#50H
SETB TR1
AGAIN: MOV SBUF,#’Y’
WAIT1: JNB TI , WAIT1
CLR TI
MOV SBUF,#’E’
WAIT2: JNB TI , WAIT2
CLR TI
MOV SBUF,#’S’
WAIT3: JNB TI , WAIT3
CLR TI
SJMP AGAIN
#include<reg51.h>
Void trans(unsigned char)
Void main(void)
{
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
while(1)
{
SBUF=‘Y’;
while(TI==0);
TI=0;
SBUF=‘E’;
while(TI==0);
TI=0;
SBUF=‘S’;
while(TI==0);
TI=0;
}
}
Excercise
Write a program to send the message “The
Earth is but One Country” to serial port at a
baud rate of 9600.
MOV TMOD,#20H
MOV TH1,#FD
MOV SCON,#50H
SETB TR1
REPEAT: MOV DPTR,#Message
MOV R0,#28d
NEXT: CLR A
MOVC A,@A+DPTR
MOV SBUF,A
HERE: JNB TI , HERE
CLR TI
INC DPTR
DJNZ R0, NEXT
SJMP REPEAT

Message: DB “The Earth is but One Country”


#include<reg51.h>
void main(void)
{
unsigned char i;
unsigned char message[28]=“The Earth is but One Country”;
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
while(1)
{
for (i=0;i<28;i++)
{
SBUF=Message[i];
while(TI==0);
TI=0;
}
}
}
Setting/ Clearing the SMOD Bit
ALP
MOV A,PCON MOV A,PCON
SETB ACC.7 CLR ACC.7
MOV PCON,A MOV PCON,A

C Programming
PCON=PCON|0x80; PCON=PCON&0x7F;
Excercise
Write a program to send the letter “A” to
serial port. Assume a switch is connected to
P1.2. Monitor the status of the switch and
set the baud rate as follows:
SW=0, 4800 baud rate
SW=1, 9600 baud rate
MOV TMOD,#20H
MOV TH1,#FA
MOV SCON,#50H
SETB TR1
SETB P1.2
MOV A,PCON
JNB P1.2, SLOW
SETB ACC.7
SJMP OVER
SLOW: CLR ACC.7
OVER: MOV PCON,A
MOV SBUF,#”A”
HERE: JNB TI , HERE
CLR TI
SJMP OVER
#include<reg51.h>
sbit SW=P1^2
Void main(void)
{
TMOD=0x20;
TH1=0xFA;
SCON=0x50;
TR1=1;
SW=1;
while(1)
{
if (SW==1)
PCON=PCON|0x80;
else
PCON=PCON&0x7F;
SBUF=‘A’;
while(TI==0);
TI=0;
}
}
Excercise
Program the 8051 to receive bytes of data
serially, and put them in P1. Set the baud
rate at 4800, 8-bit, and one stop bit.
MOV TMOD,#20H
MOV TH1,#FA
MOV SCON,#50H
SETB TR1
HERE: JNB RI , HERE
MOV P1,SBUF
CLR RI
SJMP HERE
#include<reg51.h>
Void main(void)
{
TMOD=0x20;
TH1=0xFA;
SCON=0x50;
TR1=1;
while(1)
{
while(RI==0);
P1=SBUF;
RI=0;
}
}
In this Module

1. 8051 interrupts
2. Programming
Timer
External hardware
Serial communication interrupt
3. Interrupt priority in 8051/52
4. Interrupt programming in C
Definitions
⚫ Interrupt
– Is an input to the processor by a device or
peripheral to inform the processor that it is
ready for communication
⚫ Interrupt Service Routine (ISR)
– Is the program which the processor executes
after an interrupt is received from a peripheral
⚫ Interrupt Vector Table
⚫ The group of memory locations set aside to hold the
addresses of ISRs
Sources of interrupt
3 Internal and 2 External Interrupts
1. Timer 0 Overflow. TF0
2. Timer 1 Overflow. TF1
3. Reception/Transmission of Serial
Data. TI OR RI
4. External Event 0. INTO
5. External Event 1. INT1
REGISTERS AND PINS
ASSOCIATED WITH INTERRUPTS

❖ Interrupt Enable Register (IE)


❖ Interrupt Priority Register (IP)
❖ Timer Control Register (TCON)
❖ Pins INT0 AND INT1
❖ Pin RESET
INTERRUPT ENABLE REGISTER (IE)
7 6 5 4 3 2 1 0

EA -- ET2 ES ET1 EX1 ET0 EX0


***BIT ADDRESSABLE
1 – Enable
0 - Disable
7: EA – Enable interrupts bit.
If 0, disables all interrupts.
If 1, permits individual interrupts to
be enabled by their enable bits
6: -- - Not implemented
5: ET2 – Reserved for future use
4: ES - Enable/Disable Serial port interrupt
3: ET1 – Enable/Disable timer 1 overflow
interrupt
2: EX1 – Enable/Disable external interrupt 1
1: ET0 – Enable/Disable timer 0 overflow
interrupt
0: EX0 – Enable/Disable external interrupt 0
INTERRUPT PRIORITY REGISTER (IP)
7 6 5 4 3 2 1 0

-- -- PT2 PS PT1 PX1 PT0 PX0


***BIT ADDRESSABLE

1 – High Priority
0 – Low Priority
7,6: -- - Not implemented
5: PT2 – Reserved for future use
4: PS – Priority of Serial port interrupt
3: PT1 – Priority of timer 1 overflow
interrupt
2: PX1 – Priority of external interrupt 1
1: PT0 – Priority of timer 0 overflow
interrupt
0: PX0 – Priority of external interrupt 0
Interrupt Vs Polling
Interrupts Polling
Whenever any device needs a service it notifies Microcontroller continuously monitors the
the microcontroller by sending it an interrupt status of a given device- when the status
signal condition is met, it performs the service. After
servicing the first device it moves onto to
monitor the next device
Allows efficient usage of the microcontroller The microcontroller is not very efficiently used
since it has to continuously monitor the
condition of the devices
Allows the microcontroller to service multiple Priority assignment is not possible
devices by assigning priority
By masking the interrupt capability of a device The microcontroller does not have the capability
some devices can be disabled to ignore any device connected to it
Allows the microcontroller to perform other Leads to wastage of microcontroller time since
tasks until the interrupt request is raised the microcontroller has to monitor the devices
continuously
Microcontroller is interrupted using external No external hardware signal required
hardware signal
Order of polling of interrupts
❖ External 0 Interrupt
❖ Timer 0 Interrupt
❖ External 1 Interrupt
❖ Timer 1 Interrupt
❖ Serial Interrupt
TIMER CONTROL REGISTER (TCON)
7 6 5 4 3 2 1 0

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


***BIT ADDRESSABLE
3: IE1 – External interrupt 1 edge flag.
Set by CPU to 1 when a high-to-low edge signal is
received on INT1 pin.
Cleared when the processor vectors to ISR located at
0013h
2: IT1 - External interrupt 1 signal type control bit
Set to 1 to enable external interrupt 1 to be generated
for a falling edge signal
Set to 0 to enable a low level signal on external
interrupt 1 to generate an interrupt
1: IE0 - External interrupt 0 edge flag.
Set by CPU to 1 when a high-to-low edge signal is
received on INT0 pin.
Cleared when the processor vectors to ISR located at
0003h
0: IT0 - External interrupt 0 signal type control bit
Set to 1 to enable external interrupt 0 to be generated
for a falling edge signal
Set to 0 to enable a low level signal on external
interrupt 0 to generate an interrupt
ISR Address
INTERRUPT ISR Flag Clearing
SOURCE ADDRESS
RESET 0000h Auto
INT0 0003h Auto
TF0 000Bh Auto
INT1 0013h Auto
TF1 001Bh Auto
RI and TI 0023h By Programmer
Interrupt Numbers in C Programming

Number
Interrupt C Function Name
in C

External 0 0 void external0(void) interrupt 0

void timer0(void) interrupt 1


Timer 0 1

void external1(void) interrupt 2


External 1 2

void timer1(void) interrupt 3


Timer 1 3

Serial void serial(void) interrupt 4


4
Communication
Exercise
Write a program that displays a value of ‘Y’
at port0 and ‘N’ at port2 and also generates
a square wave of 10kHz with Timer 0 in
mode2 at port Pin P1.2. XTAL=22MHz
MOV TMOD, #02H
MOV TH0, -92
MOV IE, #82H
SETB TR0
BACK: MOV P0,#’Y’
MOV P2, #’N’
SJMP BACK

ORG 000BH
CPL P1.2
RETI
#include<reg51.h> void timer0(void) interrupt 1
void timer0(void) interrupt 1; {
void main(void) ~P1^2;
{
}
TMOD=0x02;
TH0=-92;
IE=0x82;
TR0=1;
P1^2=0;
while(1)
{
P0=‘Y’;
P2=‘N’;
}
}
Exercise
Write a program that continuously gets 8 bit
data from P0 and sends it to P1 while
simultaneously creating a square wave of
200micro sec period on P2.1. Use Timer 1
in mode 2 to create the square wave.
Assume that XTAL = 11.0592 MHz
MOV TMOD,#20h
MOV P0,#FFH
MOV TH1,#-92
MOV IE,#88H
SETB TR1
Back: MOV A,P0
MOV P1,A
SJMP Back

Org 001Bh
CPL P2.1
RETI
#include<reg51.h> void timer1(void) interrupt 3
void timer1(void) interrupt 3; {
void main(void)
~P2^1;
{
unsigned char a;
}
TMOD=0x10;
P0=0xFF;
TH1=-92;
IE=0x88;
TR1=1;
P2^1 =0;
while(1)
{
a=P0;
P1=a;
}
}
Write a program to generate a
square wave on P1.3 with a
frequency equal to half the
frequency of the signal applied
at INTO pin (P3.2)
MOV IE,#81H
SETB IT0
SETB P3.2
Back: SJMP Back

Org 0003h
CPL P1.3
RETI
#include<reg51.h>
void external0(void) interrupt 0;
void main(void)
{
IE=0x81;
IT0=1;
P3^2 =1;
P1^3=0;
while(1);
}
void external0(void) interrupt 0
{
~P1^3;
}
Write a program for 8051 to do the
following
1. Read data from P1 and writes to P2
continuously
2. Also the data in P1 is transferred
serially
Assume 11.0592 MHz crystal frequency,
9600 baud rate
MOV IE,#90H
MOV SCON,#50H
MOV TMOD,#20H
MOV TH1,#-3
SETB TR1
MOV P1,#FFH
HERE: MOV A,P1
MOV P2,A
MOV SBUF,A
SJMP HERE

Org 0023h
CLR TI
RETI
#include<reg51.h> void serial(void) interrupt 4
void serial(void) interrupt 4;
{
void main(void)
{ TI=0;
unsigned char a; }
IE=0X90;
SCON=0X50;
TMOD=0x20;
TH1=-3;
TR1=1;
P1=0xFF;
while(1)
{
a=P1;
P2=a;
SBUF=a;
}
}
Write a program to generate two square
waves of 5 kHz ad 25 kHz at pins P1.3
and P2.3 respectively, with a crystal
frequency of 22 MHz and in interrupt
mode.
MOV IE,#8Ah
MOV TMOD,#22h
MOV TH0,#-183
MOV TH1,#-37
SETB TR0
SETB TR1
Back: SJMP Back

Org 000Bh
CPL P1.3
RETI

Org 001Bh
CPL P2.3
RETI
#include<reg51.h> void timer0(void) interrupt 1
void timer0(void) interrupt 1; {
void timer1(void) interrupt 3;
~P1^3;
void main(void)
{
}
IE=0x8A;
TMOD=0x22; void timer1(void) interrupt 3
TH0=-183; {
TH1=-37; ~P2^3;
TR0=0;
}
TR1=1;
P1^3=0;
P2^3=0;
while(1);
}

You might also like