Mod-3: Interrupts,Timer operation,serial communication 1

Similar documents
Interrupt Programming: Interrupts vs. Polling Method:


CPEG300 Embedded System Design. Lecture 8 Timer

MCS-51 Serial Port A T 8 9 C 5 2 1

The 8051 microcontroller has two 16-bit timers/counters called T0 and T1.

INTERRUPTS PROGRAMMING

CoE3DJ4 Digital Systems Design. Chapter 5: Serial Port Operation

Interrupts. EE4380 Fall 2001 Class 9. Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

8051 Timers and Serial Port

EE6502- MICROPROCESSOR AND MICROCONTROLLER

Serial I-O for Dinesh K. Sharma Electrical Engineering Department I.I.T. Bombay Mumbai (version 14/10/07)

8051 Timers. Class 7 EE4380 Fall Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

Department of EIE / Pondicherry Engineering College. Timer/Counters. Department of EIE / Pondicherry Engineering College 1

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Serial Port Programming in Assembly Module No: CS/ES/12 Quadrant 1 e-text

8051 Serial Communication

Chapter 6 Interrupts. (I. Scott Mackenzie) By: Masud-ul-Hasan

ELEG3923 Microprocessor Ch.9 Timer Programming

Microcontroller and Embedded Systems:

Introduction To MCS-51

CHAPTER TIMER PROGRAMMING

CHAPTER 11 INTERRUPTS PROGRAMMING

CoE3DJ4 Digital Systems Design. Chapter 6: Interrupts

CPEG300 Embedded System Design. Lecture 6 Interrupt System

8051 MICROCONTROLLER


CS 320. Computer Architecture Core Architecture

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

Embedded Controller Programming

8051SERIAL PORT PROGRAMMING

The Timers/Counters The Serial Interface The Interrupt System Reset P0.0-P0.7 P2.0-P2.7. Port 2 Drivers. Port 2 Latch

8051 Microcontroller. Ali Ziya Alkar 1

Timers and interrupts

Timer-1 can be run using the internal clock, fosc/12 (timer mode) or from any external source via pin T1 (P3.5) (Counter mode).

MODULE-1. Short Answer Questions

Rev. No. History Issue Date Remark

8051 Serial Port. EE4380 Fall02 Class 10. Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

8051 Microcontroller memory Organization and its Applications

ISSI. IS89C51 CMOS SINGLE CHIP 8-BIT MICROCONTROLLER with 4-Kbytes of FLASH ISSI IS89C51 NOVEMBER 1998 FEATURES GENERAL DESCRIPTION

ELEG3923 Microprocessor Ch.10 Serial Port Programming

Chapter 09. Programming in Assembly

8051 Microcontroller

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

WINTER 14 EXAMINATION

UNIT IV MICROCONTROLLER

Experiment# 8: Photo-Interrupter Control

Department of Electronics and Instrumentation Engineering Question Bank

8051 Microcontroller Interrupts

MODEL ANSWER SUBJECT- MICROCONTROLLER(12187) CLASS-EJ5E CLASS TEST-02 Q1.)Attempt any THREE of the following.

8XC51RA RB RC Hardware Description

8051 Peripherals. On-Chip Memory Timers Serial Port Interrupts. Computer Engineering Timers

Vidyalankar T.E. Sem. V [ETRX] Microprocessors and Microcontrollers I Prelim Question Paper Solution

8051 Microcontroller

FACULTY OF ENGINEERING LAB SHEET

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 4 The 8051 Architecture

Chapter C2051 Architecture and Serial Communication Link

Understanding the basic building blocks of a microcontroller device in general. Knows the terminologies like embedded and external memory devices,

Microcontroller and Applications

Q 1 a) Attempt any THREE of the following: 12 TMOD.7 TMOD.6 TMOD.5 TMOD.4 TMOD.3 TMOD.2 TMOD.1 TMOD.0 GATE C/T M1 M0 GATE C/T M1 M0

Serial communication

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT89S52

VRS540-4kB Flash, 128B RAM, 25~40MHz, 8-Bit MCU

Fig 1. Block diagram of a microcomputer

UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI UNIT- IV

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Summer 2016 EXAMINATIONS.

The Final Word on 8051 Microcontroller

VRS550-8kB Flash, 256B RAM, 25~40MHz, 8-Bit MCU VRS560-16kB Flash, 256B RAM, 40MHz, 8-Bit MCU

SUMMER 13 EXAMINATION

Memory organization Programming model - Program status word - register banks - Addressing modes - instruction set Programming examples.

8051 Interrupt Organization

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

8-bit Microcontroller with 2/4-Kbyte Flash AT89LP2052 AT89LP4052

8051 I/O and 8051 Interrupts

THE 8051 MICROCONTROLLER Simple comparison: Pentium vs. 8051

Timer programming

8051 Core Specification

MCS -51 Programmer s Guide and Instruction Set

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: 8051 Architecture Module No: CS/ES/5 Quadrant 1 e-text

Q.1. A) Attempt any THREE of the following:

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

UNIT 5. Microcontrollers. Syllabus

2. Write an 8051 program to generate a square wave of 25 khz at pin P2.3 using XTAL = 12 MHz. Solution:

Three criteria in Choosing a Microcontroller

Programming Book Microcontroller Kit. Rev 3.0 January, Wichit Sirichote

How to use the PSoC based 16C450 Replacement

C51 Family. C51 Family Programmer s Guide and Instruction Set. Summary

8051 MICROCONTROLLER

80C51 family programmer s guide and instruction set. 80C51 Family. PROGRAMMER S GUIDE AND INSTRUCTION SET Memory Organization. Philips Semiconductors

8051 Microcontrollers

INTEGRATED CIRCUITS DATA SHEET. P89C738; P89C739 8-bit microcontrollers Dec 15. Product specification File under Integrated Circuits, IC20

UNIT MICROCONTROLLER AND ITS PROGRAMMING

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR. ELECTRONICS & COMMUNICATION DEPARTMENT Question Bank- 1

Module I. Microcontroller can be classified on the basis of their bits processed like 8bit MC, 16bit MC.


MICROPROCESSOR & MICROCONTROLLER

8051 Single Board Monitor Programming. Minmon - Yeralan & Ahluwalia. PaulMon1 & PaulMon2 - Paul Stoffregen

MODEL ANSWER SUMMER 17 EXAMINATION Subject Title: Microcontroller Subject Code:

Question Bank Microprocessor and Microcontroller

Transcription:

Mod-3: Interrupts,Timer operation,serial communication 1 Module-3 Contents: Interrupts - interrupt sources - interrupt handling programming examples. Timers operation different modes waveform generation- programming examples - Serial communication different modes - programming examples. Interrupts There are five interrupt sources for the 8051. Since the main RESET input can also be considered as an interrupt, six interrupts can be listed as follows: External Interrupts Port P3 of 8051 is a multi-function port. Different lines of this port carry out functions which are additional to data input-output on the port. Lines P3.2 and P3.3 can be used as interrupt inputs. Interrupts will be caused by a LOW level, or a negative edge on these lines. Half of the special function register TCON is used for setting the conditions for causing interrupts from external sources. This register is bit addressable. IT1 and IT0 are the Interrupt Type flags for external sources 1 and 0 respectively. These decide whether a negative going edge or a LOW level will cause an interrupt. If the bit is set, the corresponding interrupt is edge sensitive. If it is cleared, the interrupt is level sensitive. IE1

Mod-3: Interrupts,Timer operation,serial communication 2 and IE0 are the status flags for the two external interrupt lines. If the flag is 1, the selected type of event (edge or level) has occurred on the corresponding interrupt line. Internal Interrupts Internally generated interrupts can be from either timer, or from the serial interface. The serial interface causes interrupts due to a receive event (RI) or due to a transmit event (TI). The receive event occurs when the input buffer of the serial line (SBUF in) is full and a byte needs to be read from it. The transmit event indicates that a byte has been sent a new byte can be written to output buffer of the serial line (SBUF out). 8051 timers always count up. When their count rolls over from the maximum count to 0000, they set the corresponding timer flag TF1 or TF0 in TCON. Counters run only while their run flag (TR1 or TR0) is set by the user program. When the run flag is cleared, the count stops incrementing. The 8051 can be setup so that an interrupt occurs whenever TF1 or TF0 is set. Interrupts Enabling Interrupts are enabled in a manner which is quite similar to the 8085. There is an interrupt enable special function register IE at byte address A8H. This register is bit addressable. (The assembler gives special mnemonics to each bit address.) The most significant bit of the register is a global interrupt enable flag. This bit must be set in order to enable any interrupt. Bits 6 and 5 are undefined for 8051. Bit 4, when set, enables interrupts from the serial port. Bit 3 should be set to enable interrupts from Timer 1 overflow. Bit 2 is set to enable interrupts from external interrupt 1 (pin P3.3 on Port 3). Bit 1 enables interrupts from Timer 0 when it overflows. Bit 0, when set, will enable interrupts from external interrupt 0 (pin P3.2 on Port 3). Interrupt Priorities 8051 has two levels of interrupt priorities: high or low. By assigning priorities, we can control the order in which multiple interrupts will be serviced. Priorities are set by bits in a special function register called IP, which is at the byte address B8H. This register is also bit addressable.

Mod-3: Interrupts,Timer operation,serial communication 3 Notice that the bits are in the polling order of interrupts. A 1 in a bit position assigns a high priority to the corresponding source of interrupts a 0 gives it a low priority. In case of multiple interrupts, the following rules apply: While a low priority interrupt handler is running, if a high priority interrupt arrives, the handler will be interrupted and the high priority handler will run. When the high priority handler does RETI, the low priority handler will resume. When this handler does RETI, control is passed back to the main program. If a high priority interrupt is running, it cannot be interrupted by any other source even if it is a high priority interrupt which is higher in polling order. A low-priority interrupt handler will be invoked only if no other interrupt is already executing. Again, the low priority interrupt cannot preempt another low priority interrupt, even if the later one is higher in polling order. If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both interrupts are of the same priority, the interrupt which is higher in polling sequence will be executed first. This is the only context in which the polling sequence matters. Serial Interrupts Serial interrupts are handled somewhat differently from the timers. There are independent interrupt flags for reception and transmission of serial data, called RI and TI. RI indicates that a byte has been received and is available for reading in the input buffer. TI indicates that the previous byte has been sent serially and a new byte can be written to the serial port. A serial interrupt occurs if either of these flags is set. (of course the serial interrupt must be enabled for this to occur). The interrupt service routine should check which of these events caused the interrupt. This can be done by examining the flags. Either or both of the flag might be set, requiring a read from or write to the serial buffer SBUF (or both). Recall that the input and output buffers are distinct but are located at the same address. A read from this address reads the input buffer while a write to the same address writes to the output buffer. The RI and TI flags are not automatically cleared when an interrupt is serviced. Therefore, the interrupt service routine must clear them before returning. Sources of Interrupts Figure shows the set of 8051 interrupt sources. If we follow the external interrupt INT0, for example, we see that this external interrupt connects to the processor at the P3.2 pin. Note Port 3 can be used as a standard input/output port as shown earlier but various Port 3 pins have alternative functionality. When INT0 is activated (negative edge usually), internally within the 8051 the EX0 request is raised. This flags an interrupt request but the relevant interrupt bit within the IE register must be set, along with the EA bit if this interrupt request is to raise an interrupt flag. The interrupt flag IE0 is then raised and causes the program counter (PC) to vector

Mod-3: Interrupts,Timer operation,serial communication 4 to vector location 0003h, as discussed earlier. Note, the Timer/Counter interrupt flags can be software polled even if the ETx bits are not enabled. Interrupts can also be software generated by setting the interrupt flags in software. The interrupt flags are accessible as flags on the TCON and SCON registers as follows: Interrupt Handling Fig: Interrupt sources Upon activation of an interrupt, the microcontroller goes through the following steps; 1. It finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack. 2. It also saves the current status of all the inrterrupts internally.

Mod-3: Interrupts,Timer operation,serial communication 5 3. It jumps to a fixed location in the memory called the interrupt vector table that holds the address of the interrupt service routine. 4. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it. It starts to execute the interrupt service routine until it reaches the last instruction of the subroutine, which is RETI (return from interrupt). 5. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted. First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack in to the PC. Then it starts to execute from that address. The following figure shows the flow of operation when a system is interrupted.

Mod-3: Interrupts,Timer operation,serial communication 6 Fig: Interrupt Operation Example. In this example it is assumed that some program, say the main program, is executing when the external interrupt INT0 occurs. The 8051 hardware will automatically complete the current machine level (assembler level) instruction and save the Program Counter to the stack. The IE register is also saved to the stack. The IE0 flag is disabled (cleared) so that another INT0 interrupt will be inhibited while the current interrupt is being serviced. The Program Counter is now loaded with the vector location 0003h. This vector address is a predefined address for

Mod-3: Interrupts,Timer operation,serial communication 7 interrupt INT0 so that program execution will always trap to this address when an INT0 interrupt occurs. Other interrupt sources have uniquely defined vector addresses for this purpose. The set of these vector addresses is referred to as the interrupt vector table. Program execution is now transferred to address location 0003h. In the example a LJMP instruction is programmed at this address to cause the program to jump to a predefined start address location for the relevant ISR (Interrupt Service Routine) routine. The ISR routine is a user written routine, which defines what action is to occur following the interrupt event. It is good practice to save (PUSH) to the stack any registers used during the ISR routine and to restore (POP) these registers at the end of the ISR routine, thus preserving the registers contents, just like a register is preserved within a subroutine program. The last instruction in the ISR routine is a RETI (RETurn from Interrupt) instruction and this instruction causes the 8051 to restore the IE register values, enable the INT0 flag, and restore the Program Counter contents from the stack. Since the Program Counter now contains the address of the next instruction which was to be executed before the INT0 interrupt occurred, the main program continues as if it had never being interrupted. Thus only the temporal behaviour of the interrupted program has been affected by the interrupt; the logic of the program has not been otherwise affected. Programming examples: 1. Assume that the INT1 pin is connected to a switch that is normally high. Whenever it goes low, it should turn on an LED. The LED is connected to P1.3 and is normally off. When it is turned on, it should stay on for a fraction of a second. As long as the switch is pressed low, the LED should stay on. Solution: ORG 0000H Pressing the switch will cause the LED to be turned on. If it is kept activated,the LED stays on. LJMP MAIN ;--ISR for hardware interrupt INT1 to turn on the LED ORG 0013H SETB P1.3 MOV R3,#255 BACK: DJNZ R3,BACK CLR P1.3 RETI

Mod-3: Interrupts,Timer operation,serial communication 8 ;--main program for initialization ORG 30H MAIN: MOV IE,#10000100B ;enable INT1 HERE: SJMP HERE ;stay here until get interrupt END 2. Assuming that pin 3.3 (INT1) is connected to a pulse generator, write a program in which the falling edge of the pulse will send a high to P1.3 which is connected to an LED (or buzzer). In other words, the LED is turned on and off at the same rate as the pulses are applied to the INT1 pin. In this example, to turn on the LED again, the INT1 pulse must be brought back high and then forced low to create a falling edge to activate the interrupt. ORG 0000H LJMP MAIN ;--ISR for hardware interrupt INT1 to turn on the LED ORG 0013H SETB P1.3 MOV R3,#255 BACK: DJNZ R3,BACK ;keep the buzzer on for a while CLR P1.3 RETI ;--MAIN program for initialization ORG 30H MAIN: SETB TCON 0.2 ;make INT1 edge-trigger interrupt MOV IE,#10000100B ;enable External INT 1 HERE: SJMP HERE END Timers Timer is a clock that controls the sequence of an event while counting in fixed intervals of time. A Timer is used for producing precise time delay. Secondly, it can be used to repeat or initiate an action after/at a known period of time. This feature is very commonly used in several

Mod-3: Interrupts,Timer operation,serial communication 9 applications. An example could be setting up an alarm which triggers at a point of time or after a period of time. Most of the microcontrollers have inbuilt Timers. Timers in a controller not only generate time delays but they can also be used as counters. They are used to count an action or event. The value of counter increases by one, every time its corresponding action or event occurs. Timers in a controller are inbuilt chips that are controlled by special function registers (SFRs) assigned for Timer operations. These SFRs are used to configure Timers in different modes of operations. While working with microcontrollers, it is more than often required to generate time delays. There are two possible ways of generating time delays. First is by using the code, like using for or while loops in a C program. However, the delays provided by the software are not very precise. The other method is to use Timers. Timers provide time delays that are very precise and accurate. 8051 microcontroller has two Timers designated as Timer0 and Timer1. Each of these timers is assigned a 16-bit register. The value of a Timer register increases by one every time a timer counts. Timer takes a time period of one machine cycle to count one. (Machine cycle is a unit that refers to the time required by the microcontroller to execute instructions.) This means that the maximum number of times a timer can count without repeating is 2 16, i.e.,65536. So the maximum allowed counts in value of Timer registers can be from 0000H to FFFFH. Since 8051 is an 8 bit controller, the registers of 8051 Timers are accessed as two different registers; one for lower byte and other for higher byte. For example, register of Timer0 is accessed as TL0 for lower byte and TH0 for higher byte.similarly TL1 and TH1 are registers assigned to Timer 1.

Mod-3: Interrupts,Timer operation,serial communication 10 Timer/counter control logic: If we want to timer as a timer, it will count the on chip clock frequency of 8051 oscillator divided by 12. e.g if the crystal frequency is 6.0 megahertz, then we will gate timer clock frequency of 500 kilohertz. The resultant timer clock is gated to the timer by means of the circuit shown in Figure. In order to oscillator clock pulses to reach the timer we have to clear the C/T bit in the TMOD register. We must set Bit TRX in the TCON register to '1' to run the timer run, and we should clear Gate bit in the TMOD register, or external pin (INTX) must be a 1. In short, the timer is configured as a timer, then the timer pulses are gated to the timer by the run bit and the gate bit or the external input bits (INTX). Timer operation While using 8051 Timers certain factors need to be considered, like whether the Timer is to be used for time keeping or for counting; whether the source for time generation is external clock or the controller itself; how many bits of Timer register are to be used or left unused. The registers of Timers are loaded with some initial value. The value of a Timer register increases by one after every machine cycle. One machine cycle duration is the 1/12th of the frequency of the crystal attached to the controller. For example, if the frequency of the crystal is 12 MHz, then the frequency for Timer will be 1MHz (1/12 of crystal frequency) and hence the time (T = 1/f) taken by the Timer to count by one is 1μs (1/1MHz). Similarly if an 11.0592 MHz crystal is used, operating frequency of Timer is 921.6 KHz and the time period is 1.085 μs. If no value is loaded into the Timer, it starts counting from 0000H. When the Timer reaches FFFFH, it reloads to 0000H. This roll over is communicated to the controller by raising a flag corresponding to that Timer, i.e., a flag bit is raised (set high) when the timer starts

Mod-3: Interrupts,Timer operation,serial communication 11 counting from 0000H again. TF0 and TF1 are the Timer flags corresponding to Timers 0 and 1. These flags must be cleared (set low) by software every time they are raised. The Timer may terminate updating register values after a roll over or continue with its operation. Starting or stopping a Timer For every Timer, there is a corresponding Timer control bit which can be set or cleared by the program to start or stop the Timer. TR0 and TR1 are the control bits for Timers 0 and 1 respectively. Setting the control bit would start the Timer. TR0 = 1; starts Timer 0 TR1 = 1; starts Timer 1 Clearing the control bit would stop the Timer. TR0 = 0; stops Timer 0 TR1 = 0; stops Timer1 Designing a delay program using 8051 timers. Time Delay = No. of counts X one clock duration of timer

Mod-3: Interrupts,Timer operation,serial communication 12 While designing delay programs in 8051, calculating the initial value that has to be loaded in to TH and TL registers forms a very important thing. Let us see how it is done. Assume the processor is clocked by a 12MHz crystal. That means, the timer clock input will be 12MHz/12 = 1MHz That means, the time taken for the timer to make one increment = 1/1MHz = 1 S Devide the desired time delay by 1 S. 2^16 = 65536 is the maximim number of counts possible for a 16 bit timer. Perform 65,536-n, where n is the no. of counts. Then, THTL = Hexadecimal equivalent of (65536-n) where (65536-n) is considered in decimal. Example. Let the required delay be 1000 S (ie; 1mS). That means n = 1000 (1000 S/1 S) 65536 n = 65536 1000 = 64536. 64536 is considered in decimal and converting it to hexadecimal gives FC18 That means THTL = FC18 Therefore TH=FC and TL=18 Program for generating 1mS delay using 8051 timer. The program shown below can be used for generating 1mS delay and it is written as a subroutine so that you can call it anywhere in the program. Also you can put this in a loop for creating longer time delays (multiples of 1mS). Here Timer 0 of 8051 is used and it is operating in MODE1 (16 bit timer). DELAY: MOV TMOD,#00000001B // Sets Timer 0 to MODE1 (16 bit timer). Timer 1 is not used MOV TH0,#0FCH // Loads TH0 register with FCH MOV TL0,#018H // Loads TL0 register with 18H SETB TR0 // Starts the Timer 0 HERE: JNB TF0,HERE // Loops here until TF0 is set (ie;until roll over) CLR TR0 // Stops Timer 0 CLR TF0 // Clears TF0 flag RET Different modes of a Timer There are four Timer modes designated as Modes 0, 1, 2 and 3. A particular mode is selected by configuring the M1 & M0 bits of TMOD register.

Mod-3: Interrupts,Timer operation,serial communication 13 13-bit Time Mode (mode 0) Mode 0 is a 13 bit Timer mode and uses 8 bits of high byte and 5 bit prescaler of low byte. The value that the Timer can update in mode0 is from 0000H to 1FFFH. The 5 bits of lower byte append with the bits of higher byte. The Timer rolls over from 1FFFH to 0000H to raise the Timer flag. When the timer is in 13-bit mode, TLx will count from 0 to 31. When TLx is incremented from 31, it will "reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two timer bytes are being used: bits 0-4 of TLx and bits 0-7 of THx. 16-bit Time Mode (mode 1) Mode1 is one of the most commonly used Timer modes. It allows all 16 bits to be used for the Timer and so it allows values to vary from 0000H to FFFFH. TLx is incremented from 0 to 255. When TLx is incremented from 255, it resets to 0 and causes THx to be incremented by 1. Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. If you set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles. Mode 1 Programming: The following are the characteristics and operations of mode 1: 1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer s register TL and TH.

Mod-3: Interrupts,Timer operation,serial communication 14 2. After TH and TL are loaded with a 16-bit initial value, the timer must be started. This is done by SETB TR0 for timer 0 and SETB TR1 for timer 1. 3. After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH. When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag). Each timer has its own timer flag: TF0 for timer 0 and TF1 for timer 1. This timer flag can be monitored. When this timer flag is raised, one option would be to stop the timer with the instructions CLR TR0 or CLR TR1, for timer 0 and timer 1, respectively. 4. After the timer reaches its limit and rolls over, in order to repeat the process. TH and TL must be reloaded with the original value, and TF must be reloaded to 0. Steps to program in mode 1: To generate a time delay, using timer in mode 1, following are the steps: 1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or 1) is selected. 2. Load registers TL and TH with initial count value. 3. Start the timer. 4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see if it is raised. Get out of the loop when TF becomes high. 5. Stop the timer. 6. Clear the TF flag for the next round. 7. Go back to Step 2 to load TH and TL again. Programming Examples : 1. Assume that XTAL = 11.0592 MHz. What value do we need to load the timer s register if we want to have a time delay of 5 ms? Show the program for timer 0 to create a pulse width of 5 ms on P2.3. Solution: Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us. This means that out of many 1.085 us intervals we must make a 5 ms pulse. To get that, we divide one by the other. We need 5 ms / 1.085μs = 4608 clocks. To Achieve that we need to load into TL and TH the value 65536 4608 = EE00H. Therefore, we have TH = EE and TL = 00. Program: CLR P2.3 ;Clear P2.3 MOV TMOD,#01H HERE: MOV TL0,#00H MOV TH0,#0EEH SETB P2.3 ;SET high P2.3 ;Timer 0, 16-bitmode ;TL0=0, the low byte ;TH0=EE, the high byte

Mod-3: Interrupts,Timer operation,serial communication 15 SETB TR0 ;Start timer 0 AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0 CLR TR0 ;Stop the timer 0 CLR TF0 ;Clear timer 0 flag 3. Assume that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 khz frequency on pin P1.5. Solution: Look at the following steps. (a) T = 1 / f = 1 / 2 khz = 500 us the period of square wave. (b) 1 / 2 of it for the high and low portion of the pulse is 250 us. (c) 250 us / 1.085 us = 230 and 65536 230 = 65306 which in hex is FF1AH. (d) TL = 1A and TH = FF, all in hex. Program MOV TMOD,#01 ;Timer 0, 16-bitmode AGAIN: MOV TL1,#1AH ;TL1=1A, low byte of timer MOV TH1,#0FFH ;TH1=FF, the high byte SETB TR0 ;Start timer 1 BACK: JNB TF0,BACK ;until timer rolls over CLR TR0 ;Stop the timer 1 CPL P1.5 ; Comp. p1.5 to get high and low CLR TF0 ;Clear timer 1 flag SJMP AGAIN ;Reload timer 4. Write a sub routine to create a time delay of 20 ms by assuming an oscillator running at 12 MHZ controls an Intel 8051 micro controller. Solution: Look at the following steps. (a) If C/T =0, then clock source to the timer 1 = osc freq/12 = 12 MHZ/12 = 1 MHZ (b)for 20 ms delay the Timer register (TH1, TL1) value = 1/1us * 20 ms = 20,000 (c)timer register (TH1 &TL1) value =65,536-20,000=45,536(d) =B1E0 (H) (d)configure timer 1 to operate in mode 1 and choose the oscillator /12 as clock i/p. C/T=0, GATE =0, TF1=0 (e)place value B1E0h into the timer 1 register and wait until the overflow flag is set to 1 Subroutine: DELAY: MOV TMOD, #10H //MODE 1, 16BIT TIMER CLR TF1 // CLEAR TIMER 1 OVERFLOW FLAG

Mod-3: Interrupts,Timer operation,serial communication 16 CLR ET1 //CLEAR TIMER 1 INTERRUPT FLAG LCALL DELAY: MOV TH1, #B1H // STORE UPPER BYTE OF COUNT MOV TL1, #E0H // STORE LOWER BYTE OF COUNT SETB TR1 // ENABLE TIMER 1 WAIT: JNB TF1, WAIT // 8-bit Time Mode (mode 2) Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself. Thus, TLx starts counting up. When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value stored in THx. For example, let us say TH0 holds the value FDh and TL0 holds the value FEh. If we were to watch the values of TH0 and TL0 for a few machine cycles this is what we see: As you can see, the value of TH0 never changed. In fact, when you use mode 2 you almost always set THx to a known value and TLx is the SFR that is constantly incremented. The following are the characteristics and operations of mode 2: 1. It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer s register TH 2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL Then the timer must be started This is done by the instruction SETB TR0 for timer 0 and SETB TR1 for timer 1 3. After the timer is started, it starts to count up by incrementing the TL register

Mod-3: Interrupts,Timer operation,serial communication 17 It counts up until it reaches its limit of FFH When it rolls over from FFH to 00, it sets high the TF (timer flag) 4. When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded automatically with the original value kept by the TH register To repeat the process, we must simply clear TF and let it go without any need by the programmer to reload the original value This makes mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reload TH and TL. Steps to program in mode 2: To generate a time delay 1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used, and the timer mode (mode 2) is selected. 2. Load the TH registers with the initial count value. 3. Start timer. 4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see whether it is raised. Get out of the loop when TF goes high. 5. Clear the TF flag and 6. Go back to Step 4, since mode 2 is auto reload. Programming examples : 1. Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0 in the following program. Program: Solution: MOV TMOD, #20H ; T1/8-bit/auto reload MOV TH1, #5 ; TH1 = 5 SETB TR1 ; start the timer 1 BACK: JNB TF1, BACK ; till timer rolls over CPL P1.0 ; P1.0 to high, low CLR TF1 ; clear Timer 1 flag SJMP BACK ; mode 2 is auto-reload First notice the target address of SJMP. In mode 2 we do not need to reload TH since it is autoreload. Now (256-05) 1.085 us = 251 1.085 us = 272.33 us is the high portion of the pulse. Since it is a 50% duty cycle square wave, the period T is twice that; as a result T = 2 272.33 us = 544.67 us and the frequency = 1.83597 khz. 2. Assuming that clock pulses are fed into pin T1, write a program for counter 1 in mode 2 to count the pulses and display the state of TL1, count on P2, which connects to 8 LEDs.

Mod-3: Interrupts,Timer operation,serial communication 18 AGAIN: BACK: MOV TMOD,#60H ;counter1,mode2 MOV TH1,#00H SETB P3.5 ;make T1 input SETB TR1 MOV A, TL1 MOV P2,A JNB TF1,BACK CLR TR1 CLR TF1 SJMP AGAIN 3. Generate a square wave of 2 KHz on P1.2 using Timer 1 in mode 2.Assume XTAL=12MHz. Look at the following steps. (a) T = 1 / f = 1 / 2 khz = 500 us the period of square wave. (b) 1 / 2 of it for the high and low portion of the pulse is 250 us. (c) 250 us / 1 us = 250 and 256 250 = 6 d which in hex is 06H. (d) TH = 06H. Program MOVE TMOD,#20H MOV TH1,#06H SETB TR1 BACK: JNB TF1,BACK CLR TR1 CLR TF1 SJMP BACK 4. Assuming that clock pulses are fed in to the pin T1,write a program for counter 1 in mode 2 to count the pulses and display the state of TL1 count on P2 which connects to 8 LEDs. Solution: Here we set a counter to count the pulses from T1 Program MOV TMOD,#60H ;Counter 1 mode 2 MOV TH1,#00H SETB P3.5 ;Make T1 as input pin AGAIN: SETB TR1 BACK: MOV A,TL1 MOV P2,A JNB TF1,BACK

Mod-3: Interrupts,Timer operation,serial communication 19 CLR TR1 CLR TF1 SJMP AGAIN Split Timer Mode (mode 3) Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0.

Mod-3: Interrupts,Timer operation,serial communication 20 Note: Timers of 8051 do starting and stopping by either software or hardware control. In using software to start and stop the timer where GATE=0. The start and stop of the timer are controlled by way of software by the TR (timer start) bits TR0 and TR1. The SETB instruction starts it, and it is stopped by the CLR instruction. These instructions start and stop the timers as long as GATE=0 in the TMOD register. The hardware way of starting and stopping the timer by an external source is achieved by making GATE=1 in the TMOD register. 8051 Serial Communication One of the 8051 s many powerful features is it s integrated UART, otherwise known as a serial port. The fact that the 8051 has an integrated serial port means that you may very easily read and write values to the serial port. If it were not for the integrated serial port, writing a byte to a serial line would be a rather tedious process requiring turning on and off one of the I/O lines in rapid succession to properly "clock out" each individual bit, including start bits, stop bits, and parity bits. The 8051 includes a hardware UART to support serial asynchronous communications so that, typically, the product can support RS-232 standard communication. The (Universal

Mod-3: Interrupts,Timer operation,serial communication 21 Asynchronous Reveiver and Transmitter) block diagram is shown in figure. In our examples the BAUD clocks are, in fact, a single clock source provided by Timer/Counter 1. Fig: UART Block Diagram The UART can be configured for 9-bit data transmission and reception. Here 8 bits represent the data byte (or character) and the ninth bit is the parity bit. The Following figure shows a block diagram for the UART transmitter, where the ninth bit is used as the parity bit. Fig: block Diagram of UART Receiver using 9 th bit

Mod-3: Interrupts,Timer operation,serial communication 22 SBUF is an SFR register which can be written to, so as to hold the next data byte to be transmitted. Also it can be read from to get the latest data byte received by the serial port. SBUF is thus effectively two registers: one for transmitting and one for receiving. The SCON (Serial Control) register is an SFR register, used for configuring and monitoring the serial port status. This lets us tell the 8051 how many data bits we want, the baud rate we will be using, and how the baud rate will be determined. First, let s present the "Serial Control" (SCON) SFR and define what each bit of the SFR represents: Bit Name Bit Addres Explanation of Function 7 SM0 9Fh Serial port mode bit 0 6 SM1 9Eh Serial port mode bit 1. 5 SM2 9Dh Mutliprocessor Communications Enable (explained later) 4 REN 9Ch Receiver Enable. This bit must be set in order to receive characters. 3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3. 2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3. 1 TI 99h Transmit Flag. Set when a byte has been completely transmitted. 0 RI 98h Receive Flag. Set when a byte has been completely received. Additionally, it is necessary to define the function of SM0 and SM1 by an additional table: SM0 SM1 Serial Mode Explanation Baud Rate 0 0 0 8-bit Shift Register Oscillator / 12 0 1 1 8-bit UART Set by Timer 1 (*) 1 0 2 9-bit UART Oscillator / 64 (*) 1 1 3 9-bit UART Set by Timer 1 (*) (*) Note: The baud rate indicated in this table is doubled if PCON.7 (SMOD) is set. SM0 - Serial port mode bit 0 is used for serial port mode selection. SM1 - Serial port mode bit 1. SM2 - Serial port mode 2 bit, also known as multiprocessor communication enable bit. When set, it enables multiprocessor communication in mode 2 and 3, and eventually mode 1. It should be cleared in mode 0.

Mod-3: Interrupts,Timer operation,serial communication 23 REN - Reception Enable bit enables serial reception when set. When cleared, serial reception is disabled. TB8 - Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem of transmiting the 9th bit in modes 2 and 3. It is set to transmit a logic 1 in the 9th bit. RB8 - Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if 9th bit received is a logic 0. Set by hardware if 9th bit received is a logic 1. TI - Transmit Interrupt flag is automatically set at the moment the last bit of one byte is sent. It's a signal to the processor that the line is available for a new byte transmite. It must be cleared from within the software. RI - Receive Interrupt flag is automatically set upon one byte receive. It signals that byte is received and should be read quickly prior to being replaced by a new data. This bit is also cleared from within the software. Serial Communication: Different modes o Mode - 0 Shift register mode. Serial data enters and exists through RXD. 8-bits are transmitted/recieved. Pin TXD is connected to the internal shift frequency pulse source to supply shift pulses to external circuits. The shift frequency or baud rate is fixed at 1/2 of the oscillator frequency. o Mode - 1 Standard UART 10 bits are transmitted (through TXD) or recieved through (RXD), a start bit(0), 8 data bits (LSB first), and a stop bit(1). Once recieved, the stop bits goes into RB8 in special function register SCON.The baud rate is variable. o Mode - 2 Multiprocessor Mode. 11 bits are transmitted through TXD or recieved through RXD, a start bit (0), 8 data bits (LSB first), a programmable 9th bit and a stop bit(1). On transmission, the 9th data bit (TB8 in SCON)

Mod-3: Interrupts,Timer operation,serial communication 24 can be assigned the value 0 or 1. Or, for example, the parity bit (P in the PSN) could be moved into TB8. On receive, the 9th bit goes into RB8 in SFR SCON, which the stop bit is ignored. The bandwidth is programmable to either 1/32 or 1/64 of oscillator frequency. o Mode - 3 11 bits are transmitted through TXD or received through RXD: a start bit, 8 data bits (LSB first), a programmable 9th bit, and a stop bit (1). In fact, Mode 3 is same as Mode 2 in all respects except the baud rate. The baud rate in Mode 3 is variable. The SCON SFR allows us to configure the Serial Port. Thus, well go through each bit and review its function. The first four bits (bits 4 through 7) are configuration bits. Baud rate in the 8051 The 8051 transfers and receives data serially at many different baud rates. The baud rate in the 8051 is programmable. This is done with the help of Timer 1. Before we discuss how to do that, we will look at the relationship between the crystal frequency and the baud rate in the 8051. As discussed in earlier, the 8051 divides the crystal frequency by 12 to get the machine cycle frequency. In the case of XTAL = 11.0592 MHz, the machine cycle frequency is 921.6 khz (11.0592 MHz / 12 = 921.6 khz). The 8051 s serial communication UART circuitry divides the machine cycle frequency of 921.6 khz by 32 once more before it is used by Timer 1 to set the baud rate. Therefore, 921.6 khz divided by 32 gives 28,800 Hz. When Timer 1 is used to set the baud rate it must be programmed in mode 2,that is 8-bit, auto-reload. The data rate is generated by timer-1 using the following formula. Where, SMOD is the 7th bit of PCON register fosc is the crystal oscillator frequency of the microcontroller It can be noted that fosc/ (12 X [256- (TH1)]) is the timer overflow frequency in timer mode-2, which is the auto-reload mode. If timer-1 is not run in mode-2, then the baud rate is,

Mod-3: Interrupts,Timer operation,serial communication 25 Timer-1 can be run using the internal clock, fosc/12 (timer mode) or from any external source via pin T1 (P3.5) (Counter mode). Example: If standard baud rate is desired, then 11.0592 MHz crystal could be selected. To get a standard 9600 baud rate, the setting of TH1 is calculated as follows. Assuming SMOD to be 0' or or In mode-1, if SM2 is set to 1, no receive interrupt (RI) is generated unless a valid stop bit is received. To get baud rates compatible with the PC, we must load TH1 with the values shown in Table. SBUF register SBUF is an 8-bit register used solely for serial communication in the 8051. For a byte of data to be transferred via the TxD line, it must be placed in the SBUF register. Similarly, SBUF holds the byte of data when it is received by the 8051 s RxD line. SBUF can be accessed like any other register in the 8051. Look at the following examples of how this register is accessed: The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD pin. Similarly, when the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data received, and then placing it in the SBUF.

Mod-3: Interrupts,Timer operation,serial communication 26 Writing to the Serial Port Once the Serial Port has been properly configured as explained above, the serial port is ready to be used to send data and receive data. If you thought that configuring the serial port was simple, using the serial port will be a breeze. To write a byte to the serial port one must simply write the value to the SBUF (99h) SFR. For example, if you wanted to send the letter "A" to the serial port, it could be accomplished as easily as: MOV SBUF,#A Upon execution of the above instruction the 8051 will begin transmitting the character via the serial port. Obviously transmission is not instantaneous--it takes a measureable amount of time to transmit. And since the 8051 does not have a serial output buffer we need to be sure that a character is completely transmitted before we try to transmit the next character. The 8051 lets us know when it is done transmitting a character by setting the TI bit in SCON. When this bit is set we know that the last character has been transmitted and that we may send the next character, if any. Consider the following code segment: CLR TI ; Be sure the bit is initially clear MOV SBUF,#A ;Send the letter A to the serial port JNB TI,$ ;Pause until the TI bit is set. The above three instructions will successfully transmit a character and wait for the TI bit to be set before continuing. The last instruction says "Jump if the TI bit is not set to $"--$, in most assemblers, means "the same address of the current instruction." Thus the 8051 will pause on the JNB instruction until the TI bit is set by the 8051 upon successful transmission of the character. Reading the Serial Port Reading data received by the serial port is equally easy. To read a byte from the serial port one just needs to read the value stored in the SBUF (99h) SFR after the 8051 has automatically set the RI flag in SCON. For example, if your program wants to wait for a character to be received and subsequently read it into the Accumulator, the following code segment may be used: JNB RI,$ ;Wait for the 8051 to set the RI flag MOV A,SBUF ;Read the character from the serial port The first line of the above code segment waits for the 8051 to set the RI flag; again, the 8051 sets the RI flag automatically when it receives a character via the serial port. So as long as the bit is not set the program repeats the "JNB" instruction continuously. Once the RI bit is set upon character reception the above condition automatically fails and program flow falls through to the "MOV" instruction which reads the value.

Mod-3: Interrupts,Timer operation,serial communication 27 Programming the 8051 to transfer data serially In programming the 8051 to transfer character bytes serially, the following steps must be taken. 1. The TMOD register is loaded with the value 20H, indicating the use of Timer 1 in mode 2 (8- bit auto-reload) to set the baud rate. 2. The TH1 is loaded with one of the values in Table to set the baud rate for serial data transfer (assuming XTAL = 11.0592 MHz). 3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8-bit data is framed with start and stop bits. 1. TR1 is set to 1 to start Timer 1. 2. TI is cleared by the CLR TI instruction. 3. The character byte to be transferred serially is written into the SBUF register. a. The TI flag bit is monitored with the use of the instruction JNB TI, xx to see if the character has been transferred completely. 4. To transfer the next character, go to Step 5. Write a program to transfer the message YES serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.

Mod-3: Interrupts,Timer operation,serial communication 28 Write a program to continually transmit the character A out the serial port. ORG 0H MOV SCON, #40H ;serial port mode 1 ;configure timer 1 in auto-reload mode for 9600 baud MOV TMOD, #20H ;timer mode 2 MOV TH1, #0FDH ;reload value for 9600 baud MOV TL1, #0FDH SETB TR1 ;start timer LOOP: MOV SBUF, #'A' ;transmit character WAIT4TI: JNB TI, WAIT4TI ;wait for end of transmission CLR TI JMP LOOP ;re-transmit END Importance of the Tl flag To understand the importance of the role of TI, look at the following sequence of steps that the 8051 goes through in transmitting a character via TxD. 1. The byte character to be transmitted is written into the SBUF register. 2. The start bit is transferred. 3. The 8-bit character is transferred one bit at a time.

Mod-3: Interrupts,Timer operation,serial communication 29 a. The stop bit is transferred. It is during the transfer of the stop bit that the 8051 raises the TI flag (TI =1), indicating that the last character was transmitted and it is ready to transfer the next character. b. By monitoring the TI flag, we make sure that we are not overloading the SBUF register.if we write another byte into the SBUF register before TI is raised, the untransmitted portion of the previous byte will be lost. In other words, when the 8051 finishes transferring a byte, it raises the TI flag to indicate it is ready for the next character. 4. After SBUF is loaded with a new byte, the TI flag bit must be forced to 0 by the CLR TI instruction in order for this new byte to be transferred. From the above discussion we conclude that by checking the TI flag bit, we know whether or not the 8051 is ready to transfer another byte. More importantly, it must be noted that the TI flag bit is raised by the 8051 itself when it finishes the transfer of data, whereas it must be cleared by the programmer with an instruction such as CLR TI. It also must be noted that if we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a portion of the byte being transferred. The TI flag bit can be checked by the instruction JNB TI,... or we can use an interrupt. Programming the 8051 to receive data serially In the programming of the 8051 to receive character bytes serially, the following steps must be taken. 1. The TMOD register is loaded with the value 20H, indicating the use of Timer 1 in mode 2 (8-bit auto-reload) to set the baud rate. 2. TH1 is loaded with one of the values in Table 10-4 to set the baud rate (assum ing XTAL = 11.0592MHz). 3. The SCON register is loaded with the value 50H, indicating serial mode 1, where 8-bit data is framed with start and stop bits and receive enable is turned on. 4. TR1 is set to 1 to start Timer 1. 5. RI is cleared with the CLR RI instruction. a. The RI flag bit is monitored with the use of the instruction JNB RI, xx to see if an entire character has been received yet. 6. When RI is raised, SBUF has the byte. Its contents are moved into a safe place. 7. To receive the next character, go to Step 5. Program the 8051 to receive bytes of data serially, and put them in PI. Set the baud rate at 4800, 8-bit data, and 1 stop bit.

Mod-3: Interrupts,Timer operation,serial communication 30 Importance of the Rl flag bit In receiving bits via its RxD pin, the 8051 goes through the following steps. 1. It receives the start bit indicating that the next bit is the first bit of the character byte it is about to receive. 2. The 8-bit character is received one bit at time. When the last bit is received, a byte is formed and placed in SBUF. 3. The stop bit is received. When receiving the stop bit the 8051 makes RI = 1, indicating that an entire character byte has been received and must be picked up before it gets overwritten by an incoming character. 4. By checking the RI flag bit when it is raised, we know that a character has been received and is sitting in the SBUF register. We copy the SBUF contents to a safe place in some other register or memory before it is lost. 5. After the SBUF contents are copied into a safe place, the RI flag bit must be forced to 0 by the CLR RI instruction in order to allow the next received character byte to be placed in SBUF. Failure to do this causes loss of the received character. From the above discussion we conclude that by checking the RI flag bit we know whether or not the 8051 has received a character byte. If we fail to copy SBUF into a safe place, we risk the loss of the received byte. More importantly, it must be noted that the RI flag bit is raised by the 8051, but it must be cleared by the programmer with an instruction such as CLR RI. It also must be noted that if we copy SBUF into a safe place before the RI flag bit is raised, we risk copying garbage. The RI flag bit can be checked by the instruction JNB RI, xx or by using an interrupt. 1. Write a program in which the 8051 reads data from P1 and writes it to P2 continuously while giving a copy of it to the serial COM port to be transferred serially. Assume that XTAL=11.0592. Set the baud rate at 9600.

Mod-3: Interrupts,Timer operation,serial communication 31 Solution: ORG 0 LJMP MAIN ; ------ main program, initialization ------ MAIN: MOV P1,#OFFH ;make P1 an input port MOV TMOD,#20H ;timer 1,mode 2 (auto reload) MOV TH1,#OFDH ;9600 baud rate MOV SCON,#50H ;8-bit, 1 stop, REN enabled SETB TR1 ;start timer 1 BACK: MOV A,P1 MOV P2,A MOV SBUF,A ;A has a copy of data HERE: JNB TI,HERE ;jump if TI is high CLR TI SJMP BACK END 2. 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 = 11.0592. Set the baud rate at 9600. Solution ORG 00H ; ------ main program, initialization ------ MAIN: MOV P1,#0FFH ;make P1 an input port MOV TMOD,#20H ;timer 1, mode 2 (auto reload) MOV TH1,#OFDH ;9600 baud rate MOV SCON,#50H ;8-bit,1 stop, REN enabled SETB TR1 ;start timer 1 BACK: MOV A,P1 MOV P2,A HERE: JNB RI,HERE MOV A,SBUF MOV P0,A CLR RI SJMP BACK END

Mod-3: Interrupts,Timer operation,serial communication 32 REVIEW QUESTIONS 1. Explain the different serial communication modes in 8051. 2. Explain the function and operating modes of timer in 8051. 3. Explain the interrupt structure of 8051 microcontroller Explain how interrupts are prioritized.