Internal Interface Programming

42
Programming internal Programming internal compoenents compoenents

description

Internal Interface Programming

Transcript of Internal Interface Programming

Page 1: Internal Interface Programming

Programming internal Programming internal compoenentscompoenents

ContentsContents

Timer amp countersTimer amp countersSerial communicationSerial communication Interrupts Interrupts

Timer amp CounterTimer amp Counter bull Timers

ndash 8051 has 2 built-in timers timer 0 and timer 1- They can be used to generate a specific time delay

(more accurate and easier than loops) Or - can be used as counters to count events happening

outside the uCndash Both timers are 16 bits wide

Three important register associated withTimer register - (TH0-TL0) (TH1-TL1)Timer mode register - TMODTimer control register -TCON

Timer registers Timer registers (2)

ndash The contents of each timer is stored in two 8-bit registers

ndash Timer 0 TH0 (higher byte) TL0 (lower byte)

ndash Timer 1 TH1 (higher byte) TL1 (lower byte)

TMOD (timer mode) register

ndash TMOD 8-bit register used to set the operation modes of timer - higher nibble timer 1 lower nibble timer 0

ndash M1 M0 the combination of the two bits specify the operation modes of the timer

(M1M0)= 00 mode 0 13 bit timer 01 mode 1 16 bit timer

10 mode 2 auto reload 8bit 11 mode 3ndash CT (countertimer) bull Whether the timer will be used as a delay generator (timer) or

event counter bull CT = 0 used as timer for time delay generation bull CT = 1 event counterndash Gate bull Gate = 0 the timer is started or stopped by software bull Gate = 1 the timer is started or stopped by external hardware

TCON register

8-bit registerndash TR0 TF0 TR1 TF1

TR0 amp TR1 - Start timer 0 or 1 - SETB TR0 is the same as SETB TCON4

TF0 amp TF1 - Timer flag will be 1 when timer 0 or 1 overflows

bullndash IE1 IT1 IE0 IT0 are used by interrupt

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 2: Internal Interface Programming

ContentsContents

Timer amp countersTimer amp countersSerial communicationSerial communication Interrupts Interrupts

Timer amp CounterTimer amp Counter bull Timers

ndash 8051 has 2 built-in timers timer 0 and timer 1- They can be used to generate a specific time delay

(more accurate and easier than loops) Or - can be used as counters to count events happening

outside the uCndash Both timers are 16 bits wide

Three important register associated withTimer register - (TH0-TL0) (TH1-TL1)Timer mode register - TMODTimer control register -TCON

Timer registers Timer registers (2)

ndash The contents of each timer is stored in two 8-bit registers

ndash Timer 0 TH0 (higher byte) TL0 (lower byte)

ndash Timer 1 TH1 (higher byte) TL1 (lower byte)

TMOD (timer mode) register

ndash TMOD 8-bit register used to set the operation modes of timer - higher nibble timer 1 lower nibble timer 0

ndash M1 M0 the combination of the two bits specify the operation modes of the timer

(M1M0)= 00 mode 0 13 bit timer 01 mode 1 16 bit timer

10 mode 2 auto reload 8bit 11 mode 3ndash CT (countertimer) bull Whether the timer will be used as a delay generator (timer) or

event counter bull CT = 0 used as timer for time delay generation bull CT = 1 event counterndash Gate bull Gate = 0 the timer is started or stopped by software bull Gate = 1 the timer is started or stopped by external hardware

TCON register

8-bit registerndash TR0 TF0 TR1 TF1

TR0 amp TR1 - Start timer 0 or 1 - SETB TR0 is the same as SETB TCON4

TF0 amp TF1 - Timer flag will be 1 when timer 0 or 1 overflows

bullndash IE1 IT1 IE0 IT0 are used by interrupt

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 3: Internal Interface Programming

Timer amp CounterTimer amp Counter bull Timers

ndash 8051 has 2 built-in timers timer 0 and timer 1- They can be used to generate a specific time delay

(more accurate and easier than loops) Or - can be used as counters to count events happening

outside the uCndash Both timers are 16 bits wide

Three important register associated withTimer register - (TH0-TL0) (TH1-TL1)Timer mode register - TMODTimer control register -TCON

Timer registers Timer registers (2)

ndash The contents of each timer is stored in two 8-bit registers

ndash Timer 0 TH0 (higher byte) TL0 (lower byte)

ndash Timer 1 TH1 (higher byte) TL1 (lower byte)

TMOD (timer mode) register

ndash TMOD 8-bit register used to set the operation modes of timer - higher nibble timer 1 lower nibble timer 0

ndash M1 M0 the combination of the two bits specify the operation modes of the timer

(M1M0)= 00 mode 0 13 bit timer 01 mode 1 16 bit timer

10 mode 2 auto reload 8bit 11 mode 3ndash CT (countertimer) bull Whether the timer will be used as a delay generator (timer) or

event counter bull CT = 0 used as timer for time delay generation bull CT = 1 event counterndash Gate bull Gate = 0 the timer is started or stopped by software bull Gate = 1 the timer is started or stopped by external hardware

TCON register

8-bit registerndash TR0 TF0 TR1 TF1

TR0 amp TR1 - Start timer 0 or 1 - SETB TR0 is the same as SETB TCON4

TF0 amp TF1 - Timer flag will be 1 when timer 0 or 1 overflows

bullndash IE1 IT1 IE0 IT0 are used by interrupt

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 4: Internal Interface Programming

Timer registers Timer registers (2)

ndash The contents of each timer is stored in two 8-bit registers

ndash Timer 0 TH0 (higher byte) TL0 (lower byte)

ndash Timer 1 TH1 (higher byte) TL1 (lower byte)

TMOD (timer mode) register

ndash TMOD 8-bit register used to set the operation modes of timer - higher nibble timer 1 lower nibble timer 0

ndash M1 M0 the combination of the two bits specify the operation modes of the timer

(M1M0)= 00 mode 0 13 bit timer 01 mode 1 16 bit timer

10 mode 2 auto reload 8bit 11 mode 3ndash CT (countertimer) bull Whether the timer will be used as a delay generator (timer) or

event counter bull CT = 0 used as timer for time delay generation bull CT = 1 event counterndash Gate bull Gate = 0 the timer is started or stopped by software bull Gate = 1 the timer is started or stopped by external hardware

TCON register

8-bit registerndash TR0 TF0 TR1 TF1

TR0 amp TR1 - Start timer 0 or 1 - SETB TR0 is the same as SETB TCON4

TF0 amp TF1 - Timer flag will be 1 when timer 0 or 1 overflows

bullndash IE1 IT1 IE0 IT0 are used by interrupt

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 5: Internal Interface Programming

TMOD (timer mode) register

ndash TMOD 8-bit register used to set the operation modes of timer - higher nibble timer 1 lower nibble timer 0

ndash M1 M0 the combination of the two bits specify the operation modes of the timer

(M1M0)= 00 mode 0 13 bit timer 01 mode 1 16 bit timer

10 mode 2 auto reload 8bit 11 mode 3ndash CT (countertimer) bull Whether the timer will be used as a delay generator (timer) or

event counter bull CT = 0 used as timer for time delay generation bull CT = 1 event counterndash Gate bull Gate = 0 the timer is started or stopped by software bull Gate = 1 the timer is started or stopped by external hardware

TCON register

8-bit registerndash TR0 TF0 TR1 TF1

TR0 amp TR1 - Start timer 0 or 1 - SETB TR0 is the same as SETB TCON4

TF0 amp TF1 - Timer flag will be 1 when timer 0 or 1 overflows

bullndash IE1 IT1 IE0 IT0 are used by interrupt

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 6: Internal Interface Programming

TCON register

8-bit registerndash TR0 TF0 TR1 TF1

TR0 amp TR1 - Start timer 0 or 1 - SETB TR0 is the same as SETB TCON4

TF0 amp TF1 - Timer flag will be 1 when timer 0 or 1 overflows

bullndash IE1 IT1 IE0 IT0 are used by interrupt

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 7: Internal Interface Programming

Mode-1 Mode-1 bull Mode 1ndash 16-bit timer all 16 bits of timer are used

ndash Steps (we use timer0 as example)

1 Load TMOD value (set the operation mode of timer)ndash Eg MOV TMOD 01H timer 0 CT=0 GATE=0 mode = 1

2 Load TL and TH with initial count values ndash Eg MOV TL0 0F2H load init value to timer 0

MOV TH0 0FFH init value FFF2 3 Start timer

ndash SETB TR0 start timer 0raquo TR0(timer start 0) when set to 1 timer 0 will start

ndash Once timer is started values in (TH0 TL0) will automatically increase by 1for every timer tick (every 1085us

ndash when (TH0 TL0) = FFFFH it will overflow in the next clock period FFFFH will become 0000H

- When overflow TF0 (timer flag for timer 0) will automatically et to 1

-By monitoring TF0 we will know when timer expires

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 8: Internal Interface Programming

Steps (Contrsquod)bull 4 Monitoring timer flag

- HERE JNB TF0 HERE exit loop when TF0 = 1 (timer expires)bull 5 When timer overflows stop timer ndash CLR TR0 set TR0 = 0 will stop timer 0 ndash The duration of timer is determined by the initial value of

timerbull 6 Clear timer flag for next round ndash CLR TF0

ndash The duration of timer is determined by the initial value in timer and timer clock

(FFFFH ndash YYXXH + 1)clock period

bull Eg before timer starts (TH0)=FFH (TL0)=F2H XTAL = 110592MHzWhat is the duration of the timer (how long

it akes for timer to overflow) ((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec((FFFF)H-(FFF2)H)+1 =141085 usec=1519usec

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 9: Internal Interface Programming

Examplendash Write a program to generate the delay timer 0

CLR P23 MOV TMOD 01 1 timer 0 mode 1 (16-bit)

HERE MOV TL0 3EH 2 load init value MOV TH0 0B8H SETB P23 SETB TR0 3 start timer

AGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflows

CLR TR0 5 stop timer CLR TF0 6 clear flag

CLR P23

Find the maximum delay that can be incurred by a 16-bit time To generate larger delay use loops to repeat the timer

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 10: Internal Interface Programming

Write a program generate a square waveform on P15 The waveform has 50duty cycle with period 2 ms

MOV TMOD 10 timer 1 mode 1HERE SETB P15

ACALL DELAYCLR P15ACALL DELAYSJMP HERE

-----------delay subroutine-------------------DELAY

MOV TL1 YYH load initial valueMOV TH1 XXHSETB TR1 start timer

AGAIN JNB TF1 AGAIN monitor flagCLR TR1 stop timerCLR TF1 clear flagRET

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 11: Internal Interface Programming

bull Mode 0 13-timerndash The timer will set TF = 1 when it reaches 1FFFHndash MOV TMOD 00H (mode 0)ndash The remaining operation is the same as mode 1

bull Mode 2 auto-reload 8-bit timerndash Timer range 00H - FFHndash Operations (use timer 0 as example)bull 1 Set timer 0 to mode 2 MOV TMOD 02H (timer 0 mode 2)bull 2 Load 8-bit init value to TH0 MOV TH0 32Hbull 3 8051 automatically copies the init value to TL0bull 4 Start timer SETB TR0

ndash TL0 starts counting until it reaches FFHbull 5 Monitor TF0

ndash When TL0 overflows from FFH to 00H it sets TF = 1bull 6 When TL0 overflows to 00H and TF = 1 the init value is

automaticallyreloaded from TH0 to TL0

bull 7 Clear TF The timer will continue to run (go back to step 5)

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 12: Internal Interface Programming

Counter (CT=1)

ndash Counting the events happening outside of 8051 ndash Counter vs timer

bull If CT=0 timer The clock source is XTAL12ndash The value of TH0 TL0 increases by 1 for each clockbull If CT = 1 counter The clock source is an outside

pulse to be countedndash The value of TH0 TL0 increases by 1 for each outside pulse

ndash We can use the counter to count the of pulses ndash The remaining operations are the same for

counter and timer (eg mode 0 1 2) ndash Connection bull External pulses are connected through P34 (T0) or P35 (T1)

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 13: Internal Interface Programming

Examplendash Assume a 1 Hz frequency pulse is connected to input pin P35 (T1) Use mode 2 of counter 1 to count the pulses and display results on P2

MOV TMOD 01100000B counter 1 mode 2 CT = 1

MOV TH1 0 counting from 0SETB P35 make T1 input mode

AGAIN SETB TR1 start timer 1BACK MOV A TL1 get copy of count from TL1 (mode 2)

MOV P2 A send result to P2 (LCD)JNB TF1 BACK keep doing it if TF=0 (no

overflow)CLR TR1 stop timerCLR TF1 clear TFSJMP AGAIN

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 14: Internal Interface Programming

Serial CommunicationSerial Communication Serial communication vs parallel communication

ndash Computer transfer data in two ways parallel serial

ndash Parallel communicationbull 8 or more parallel lines are used to transfer data

ndash Eg connecting 8051 to LCD data bus connecting to hard drive

bull More data can be transferred in unit period of timebull Usually used for short distance data transfer

ndash Long parallel wires function like antenna and will leak signal during Transmissionndash The leaked signals will cause mutual interference for signals in wire (cross-talk)

ndash Serial communicationbull Use 1 data line to transfer data

ndash Data is transmitted 1 bit a timebull Usually used for data transfer over longer distance

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 15: Internal Interface Programming

Simplex Half-duplex and full-duplex

ndash Simplex communication can occur in only one direction bull Eg pager broadcast radio

ndash Half-duplex communication can happen in both directions but only one at a time (A to B or B to A but not simultaneously)

bull Eg Police radio (walki-talki)bull Only 1 channel (data line) is enough

ndash Full-duplex communication can happen in both directions simultaneously

bull Eg telephone bull Two channels (data lines) are required bull Full-duplex = two simplex

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 16: Internal Interface Programming

serial communication bull Asynchronous

ndash Data is transmitted in bursts without following a specific clock

- Data can be transmitted at any time bull Synchronous

- data can only be transmitted at special instants

ndash When there is no data channel remains constant to indicate ldquoidlerdquo (no information)

bull eg some system use ldquohighrdquo voltage to indicate idlebull How does the receiver tell the difference between

ldquoidlerdquo and ldquo11111111rdquo ndash Framing

bull Data characters are placed between start and stop bitsbull Start bit 1 bit (eg low)bull Stop bit 1 bit (eg high) or 2 bits (eg high low)bull Eg 8-bit ASCII + 1 start bit + 1 stop bit = 10 bitsframe

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 17: Internal Interface Programming

SERIAL COMMUNICATION STANDARD bull Communication standardndash A set of rules that must be followed by communication devices

bull To ensure that communication devices from different manufactures can interoperate with each other

ndash Example rulesbull Which voltage used to represent lsquo0rsquo which voltage used to represent lsquo1rsquobull How many start bits how many stop bitsbull Which voltage(s) used for start bits which voltage(s) used for stop bitsbull How many bits in one frame (7 bits 8 bits 10 bits hellip)bull The format of control signals how many control pinsndash Example standardsbull RS232 IEEE 80211 (WiFi) IEEE 80216 (WiMax) CDMA helliphellip

bull RS232ndash The most popular serial communication standardndash Developed by Electronics Industries Association (EIA) in the 1960srsquondash Still widely used today

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 18: Internal Interface Programming

Signal level of RS232ndash lsquo1rsquo is represented by -3 to -25 Vndash lsquo0rsquo is represented by +3 to +25 Vndash They are not TTL (transistor-transistor logic) compatible In TTL

bull lsquo1rsquo 22 to 5 Vbull lsquo0rsquo 0 to 08 V

ndash 8051 is TTL compatible -8051 and RS232 devices cannot be directly connected togetherbull MAX232 ndash A voltage converter (line driver) that can convert RS232 signal to

TTL voltage level ndash 1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 19: Internal Interface Programming

8051 AND UART

8051ndash Most 8051 has 1 serial portbull P30 (RXD0) P31 (TXD0)

UARTndash Universal asynchronous receiver transmitterndash An integrated circuit commonly used in conjunction with RS232

bull Itrsquos built inside 8051bull The circuit can interpret communication command

ndash When an ASCII code is sent to UART it will automatically add start

and stop bits before transmit it through serial portndash When receiving data from serial port UART will automatically detect start bit and stop bit remove the start and stop bits from the

received data and send the pure data to the CPU ndash UART saves us from the details of communication standards

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 20: Internal Interface Programming

Baud rate in 8051 and timer bull Baud rate in 8051 and timer

ndash The baud rates supported by 8051 (unit bps) 9600 4800 2400

1200)ndash How to set the baud ratebull Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit auto-

reload)bull When used for serial port the frequency of timer tick is

determined by(XTAL12)32

bull 1 bit is transmitted for each timer period (the time duration from timer start to timer expires)

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 21: Internal Interface Programming

Calculation of baud rate ndash With XTAL = 110592 find the TH1 value needed to have the baud rate

9600bull Clock frequency of timer clock

f = (110592 MHz 12)32 = 28800Hzbull Time period of each clock tick

T0 = 1f = 128800bull Duration of timer (1 timer cycle)

( of clock ticks in timer)T0

bull 9600 baud duration of 1 symbol 19600 1 timer cycle 19600

19600 = 1f ( of clock ticks in timer)

bull of clock ticks in timer = f9600 = 288009600 = 3 TH1 =-3

bull Similarly for baud 2400 of clock ticks = f2400 = 12 TH1 = -12

ndash When connecting two devices through serial port both devices must have the same baud

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 22: Internal Interface Programming

Serial Serial communication registerscommunication registers

SBUF register (Serial buffer)

ndash An 8-bit register used for serial communication

ndash It holds the data to be transferred or received from serial port

ndash Eg to send lsquoDrsquo to serial port MOV SBUF rsquoDrsquobull The data in SBUF will be automatically processed by UART then sent to serial port (eg pin TXD0)

ndash Eg to receive data from serial portMOV A SBUFbull Once UART receives data from serial port (eg pin RXD0) it will strip the start and stop bits and then put the data in SBUF

ndash It serves as a buffer between CPU and serial ports

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 23: Internal Interface Programming

bull SCON register (Serial control register)

An 8-bit register used to program the start bit stop bit and data bits of data framing and some other serial related processing

- SM0 SM1 (serial port mode)bull Specify framing format how to calculate baud- (SM0 SM1) = (01) mode 1 8-bit data 1 start bit 1 stop bit

Variable baud set by timer Most commonly usedbull The other three modes are rarely used

ndash (SM0SM1) = (00) mode 0 fixed baud = XTAL12ndash (SM0 SM1) = (10) mode 2 9-bit data fixed baudndash (SM0 SM1) = (1 1) mode 3 9-bit data variable baud

ndash SM2bull SM2 = 0 single processorbull SM2 = 1 multiprocessor communication (not required for this

course)

Serial Serial communication registerscommunication registers

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 24: Internal Interface Programming

ndash REN (Receive Enable)bull Enabledisable receptionbull REN = 1 the 8051 will accept incoming data from serial portbull REN = 0 the receiver is disabledbull Eg SETB REN CLR REN SETB SCON4 CLR SCON4

ndash TB8bull Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)bull CLR TB8 when using mode 1bull - RB8bull Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)bull Used by mode 1 to store the stop bit

-TI (transmit interrupt)bull When 8051 finishes the transfer of the 8-bit character it set TI to lsquo1rsquo toindicate that it is ready to transfer the next characterbull The TI is raised at the beginning of the stop bit

ndash RI (receive interrupt)bull When 8051 receives a characterndash 1 The UART removes start bit and stop bitndash 2 The UART puts the 8-bit character in SBUFndash 3 RI is set to lsquo1rsquo to indicate that a new byte is ready to be picked upin SBUFbull RI is raised halfway through the stop bit

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 25: Internal Interface Programming

Examplendash Write a program to transfer letter ldquoArdquo serially at 4800 baud continuously

MOV TMOD 20H timer 1 mode 2 (8-bit auto-reload)MOV TH1 -6 4800 baudMOV SCON 50H 0101 0000

(mode 1SingleProcessorREN=1)SETB TR1 start timer

AGAIN MOV SBUF rsquoArsquo store lsquoArsquo in SBUFHERE JNB TI HERE wait for TI = 1 (transmission over)

CLR TI clear TI for next transmissionSJMP AGAIN repeat

ndash The data in SBUF is transmitted serially one bit at a timebull If we write a new character to SBUF before TI is raised (the

transmission of the previous character is not over yet) part of the original data will be lost

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 26: Internal Interface Programming

Examplendash Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 8-bit data 1 stop bit

MOV TMOD 20HMOV TH1 -6 4800 baudMOV SCON 50H mode 1SETB TR1

HERE JNB RI HERE wait for char to come in MOV A SBUF save incoming byte in AMOV P1 A send to port 1CLR RI clear get ready for

next byteSJMP HERE

ndash RI = 1 indicates a new byte is copied in SBUFndash We need to copy the data in SBUF to another place

immediately after RI = 1bull Otherwise the contents in SBUF will be overwritten by the

next character

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 27: Internal Interface Programming

InterruptsInterrupts What is interrupt

ndash Example timerMOV TMOD 02 1 timer 0 mode 2 (8-bit auto reload)HERE MOV TH0 3EH 2 load init valueSETB TR0 3 start timerAGAIN JNB TF0 AGAIN 4 monitor TF0 until timer overflowsCLR TR0 5 stop timerCLR TF0 6 clear flag

ndash Polling the CPU continuously monitor the status of a device (eg check the status of TF0 wait until TF0 turns to 1)

bull The status monitoring process wastes a lot of MCU resources

ndash Interruptbull Whenever any device needs service (eg timer expires) it will notify

the MCU by sending an interrupt signalbull Upon receiving the interrupt signal the CPU interrupts whatever it is doing and serve the device (eg stop timer) by executing a special subroutine Interrupt Service Routine (ISR)bull Once ISR is done CPU will go back to the location before interrupts

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 28: Internal Interface Programming

Interrupt service routine

ndash Special subroutines to be executed by CPU upon interrupt ndash Different interrupts have different subroutines

Example A program needs to (1) send the letter lsquoArsquo to P1 every 100ms (2) continuously sensing and display temperature

bull 1 start timer then display temperature on displaybull 2 when timer expires it sends an interrupt signal to CPUbull 3 CPU stops temperature sensing and jump to ISRbull 4 When ISR is over the CPU returns to where it left and

continue temperature sensing

ndash Multiple devices can be connected to MCU each of them has their unique ISRs Whenever a device needs service it will send interrupt to CPU and CPU can execute the corresponding ISR

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 29: Internal Interface Programming

Six interrupts in 8051ndash There are totally six different interrupts in 8051

bull Each interrupt has its own ISRbull Each ISR has its unique starting addressbull When a particular interrupt is detected by CPU PC will jump to thecorresponding ISR starting address to execute the ISR

ndash Timer interrupts (2 interrupts)bull 1 interrupt for timer 0 (ISR starting address 000BH)bull 1 interrupt for timer 1 (ISR starting address 001BH)

ndash Serial ports interrupt (1 interrupt)bull 1 interrupt used to indicate Tx or Rx is done (ISR starting address

0023H)

ndash External hardware interrupts (2 interrupts)bull P32 external interrupt 0 (INT0) (ISR starting address 0003H)bull P33 external interrupt 1 (INT1) (ISR starting address 0013H)

ndash Reset (1 interrupt)bull when reset is activated the 8051 jumps to address 0 (ISR address 0H)

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 30: Internal Interface Programming

Interrupt vector table

ndash The starting address for each ISRndash If interrupts are enabled ROM addresses 0003 ndash 0023 are

taken by interrupt vector table- We need to bypass interrupt vector table

ORG 0LJMP MAIN bypass interrupt vector table-------------------- ISRs can be written from 0003 ndash 0029 H--------------------

ORG 30H MAIN MOV A 23H main prgram

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 31: Internal Interface Programming

INTERRUPT ENABLE REGISTER

bull Enabledisable interruptsndash Upon reset all interrupts are disabled the CPU will not respond

to any interruptndash In order to use an interrupt it must be enabled by softwarendash Register IE (interrupt enable bit addressable)bull EA=0 disable all interrupts EA=1 each interrupt is enabled individuallybull ET2 = 1 enable interrupt for timer 2bull ES = 1 enable interrupt for serial portbull ET1 = 1 enable interrupt for timer 1bull EX1 = 1 enable external interrupt 1bull ET0 = 1 enable interrupt for timer 0bull EX0 = 1 enable external interrupt 0ndash ExampleMOV IE 10010110BMOV IE 00010110B

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 32: Internal Interface Programming

TIMER INTERRUPTS

Roll-over timer flag and interrupt

ndash Recall when timer roll-over TF0 or TF1 is set to 1ndash Polling HERE JNB TF0 HERE- The entire CPU is tied down and waiting for TF to be

set to 1 ndash Interrupt

bull If timer interrupt is enabled (SETB IE3 for timer 0 SETB IE5 for timer1)

ndash whenever TF0 or TF1 is set to 1 a timer interrupt will be automatically generated

- Program will transfer to ISR at specified location for timer01

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 33: Internal Interface Programming

Examplendash Write a program that continuously (1) gets 8-bit data from P0 and sends it to P1 while simultaneously (2) create a square wave of 200 us period on P21

ORG 0000HLJMP MAIN by pass interrupt vector

table------------------timer 0 ISR ---------------

ORG 000BH timer 0 ISRCPL P21 toggle P21RETI return from ISR

------------------ main program--------------ORG 0030H

MAIN MOV TMOD 02H timer 0 mode 2 (auto-reload)

MOV P0 0FFH input MOV TH0 -92 initial value

MOV IE 82H IE = 10000010 SETB TR0 start timer

--- read P0 send its data to P1-----BACK MOV A P0 MOV P1 A SJMP BACK END

1 Due to interrupt we donrsquot need to monitor TF02 No need to clear TF0

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 34: Internal Interface Programming

bull External interrupts INT0 and INT1

ndash INT0 P32 ISR starting address 0003Hndash INT1 P33 ISR starting address 0013H

ndash Upon activation of these pins the 8051 gets interrupted in whatever it is doing and jumps to the corresponding ISR

ndash Two types of interrupts activation signalsbull Level triggered interrupt (default interrupt mode)

ndash INT0 and INT1 is normally highndash If a low level signal is applied to them interrupt is

triggeredndash If the low level signal is not removed after the

execution of ISR it will be interpreted as a new interruptbull Edge triggered interrupt

ndash If a high-to-low signal is applied to P32 or P33 interrupt is

triggered

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 35: Internal Interface Programming

Examplendash Assume INT1 is connected to a switch that is normally high Whenever it goes low it should turn on an LED The LED should stay on for a fraction of second

ORG 0HLJMP MAIN

----------------ISR for INT1-------------------- ORG 0013H SETB P13 MOV R3 255

BACK DJNZ R3 BACK CLR P13RETI

-----------------Main Program----------------ORG 30H

MAIN MOV IE 10000100BHERE SJMP HERE

END

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 36: Internal Interface Programming

EXTERNAL INTERRUPT EDGE TRIGGERED

bull Edge triggeredndash Level triggered interrupt is the default interrupt modendash In order to change to edge triggered mode we need to set the

TCON register

bull TF1 timer over flow flag (HERE JNB TF1 HERE)bull TR1 start or stop timer (SETB TR1 CLR TR1)bull IT1 interrupt mode selection Default is 0ndash IT1 = 0 level triggered (triggered by low level)ndash IT1 = 1 edge triggered (triggered by H-to-L)bull IE1 external interrupt 1 edge flagndash Itrsquos set to 1 when H-to-L is detectedndash Itrsquos automatically cleared when the interrupt is processed

(afterRETI is executed)raquo When IE1 = 1 no more interrupt will be recognized 1048774 avoidinterrupt in an interruptndash If IT1 = 0 (level triggered) IE1 is not used at all

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 37: Internal Interface Programming

Examplendash Assuming P33 (INT1) is connected to a pulse generator Write a program in which the falling edge of the pulse will send a high pulse to P13 which is connected to an LED

ORG 0000HLJMP MAIN

--------------ISR for INT1-----------ORG 0013HSETB P13MOV R3 255

BACK DJNZ R3 BACKCLR P13RETI

---------------Main--------------------MAIN SETB TCON2 make INT1 edge-triggered

interrupt MOV IE 10000100 enable external INT1HERE SJMP HERE

END

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 38: Internal Interface Programming

Serial port interruptndash Only 1 interrupt and 1 ISR assigned to serial port communication

for both Tx and Rx

ndash ISR entrance address 0023H

ndash When TI or RI is raised an interrupt is generated and PC will jump to 0023H to execute the corresponding IRS

ndash How do we tell if the interrupt is generated by TI or RI flagbull In the ISR we must examine the RI and TI flag to see which one

triggered the interruptbull Before executing RETI we must clear RI or TI flag

ndash So in the next serial port interrupt we will still be able to distinguish

Tx interrupt from RI interrupt

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 39: Internal Interface Programming

Examplendash Write a program in which the 8051 gets data from P1 and sends it to P2 continuously while incoming data from the serial port is sent to P0 Assume that XTAL = 110592MHz Set the baud rate at 9600

ORG 0LJMP MAIN

-----------------------------------ORG 23H

SER JB TI TXMOV A SBUFMOV P0 ACLR RIRETI

TX CLR TI RETI

--------------------------------------- ORG 30H

MAIN MOV P1 0FFH make P1 an input portMOV TMOD 20H timer 1 mode 2MOV TH1 -3 9600 baudMOV SCON 50H mode1 receiver enableMOV IE 10010000B enable serial interruptSETB TR1 start timer 1

BACK MOV A P1 read data from P1MOV P2 A send it to P2SJMP BACKEND

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 40: Internal Interface Programming

INTERRUPT PRIORITYInterrupt priorityndash What will happen if two or more interrupts are

activated at the same timendash The 6 interrupts have different prioritiesndash When two or more interrupts are activated

simultaneously interrupt with higher priority will be served first

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 41: Internal Interface Programming

IP register (Interrupt Priority Register)

The interrupt priority can be changed by programming the IP register

When power up all bits in IP register are 0 1048774 default priority orderbull To assign a high priority to an interrupt set the corresponding bit to 1 1048774The interrupt with IP bit set to 1 has the highest priority among allinterruptsbull If more than one bit are set to 1 their relative priorities are determined by the default priority orderbull Interrupts with priority bits set to 1 have higher priority than interruptswith priority bits being 0

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt

Page 42: Internal Interface Programming

Interrupt inside an interrupt

ndash What happens if another interrupt happens during the execution of ISR

bull If the new interrupt has a higher priority than the interrupt that is

currently being responded the CPU willndash 1 immediately jump to the ISR of the new interruptndash 2 Upon finishing the new ISR come back to the original ISR being served

bull If the new interrupt has a lower priority than the current interrupt theCPU willndash 1 finish the current interruptndash 2 then jump to the ISR of the new interrupt