University Of Duhok College Of Engineering ECE Department Year 2020--2021 3rd year/S1 Microcontroller Sys LAB EXPERMINT #5 Interrupt of Microcontroller 8051 References: 1- Tutorial for digital design through Proteus 2- The 8051 Microcontroller Notes by M. Ali majidi ( CH 12) I-Objective:The objective of this experiment is to program, and simulate on PROTEUS7 an interrupt system of microcontroller based on 8051 . II- Theory: An interrupt is the occurrence of a condition—an event—that causes a temporary suspension of a program while the condition is serviced by another program. Interrupts play an important role in the design and implementation of microcontroller applications. They allow a system to respond asynchronously to an event and deal with the event while another program is executing. An interrupt-driven system gives the illusion of doing many things simultaneously. Of course, the CPU cannot execute more than one instruction at a time; but it can temporarily suspend execution of one program, execute another, and then return to the first program. In a way, this is like a subroutine. The CPU executes another program— the subroutine—and then returns to the original program. The difference is that in an interrupt-driven system, the interruption is a response to an “event" that occurs asynchronously with the main program. It is not known when the main program will be interrupted. The program that deals with an interrupt is called an interrupt service routine (ISR) or interrupt handler. The ISR executes in response to the interrupt and generally performs an input or output operation to a device. When an interrupt occurs, the main program temporarily suspends execution and branches to the ISR; the ISR executes, performs the operation, and terminates with a “return from interrupt” instruction; the main program continues where it left off. It is common to refer to the main program as executing at baselevel and the ISRs as executing at interrupt-level. The terms foreground (base-level) and background (interrupt-level) are also used. This brief view of interrupts is depicted in Figure 6-1, showing (a) the execution of a program without interrupts and (b) execution at base-level with occasional interrupts and ISRs executing at interrupt-level. A typical example of interrupts is manual input using a keyboard. Consider an application for a microwave oven. The main program (foreground) might control a microwave power element for cooking; yet, while cooking, the system must respond to manual input on the oven’s door, such as a request to shorten or lengthen the cooking time. When the user depresses a key an interrupt is generated (a signal goes from high to low, perhaps) and the main program is interrupted. The ISR takes over in the background, reads the keyboard code(s) and changes the cooking conditions accordingly, and finishes by passing control back to the main program. The main program carries on where it left off. The important point in this example is that manual input occurs “asynchronously;” that is, it occurs at intervals not predictable or controlled by the software running in the system. This is an interrupt. Fig # 5.1 8051 INTERRUPT ORGANIZATION There are five interrupt sources on the 8051: two external interrupts, two timers interrupts, and a serial port interrupt. The 8052 adds a sixth interrupt source from the extra timer. All interrupts are disabled after a system reset and are enabled individually by software. In the event of two or more simultaneous interrupts or an interrupt occurring while another interrupt is being serviced, there is both a polling sequence and a two-level priority scheme to schedule the interrupts. The polling sequence is fixed but the interrupt priority is programmable. Let’s begin by examining ways to enable and disable interrupts . Enabling and Disabling Interrupts Each of the interrupt sources is individually enabled or disabled through the bit-addressable special function register IE (interrupt enable register). As well as individual enable bits for each interrupt source, there is a global enable/disable bit that is cleared to disable all interrupts or set to turn on interrupts. Two bits must be set to enable any interrupt: the individual enable bit and the global enable bit. For example, timer 1 interrupts are enabled as follows : SETB ETl SETB EA ;ENABLE Timer 1 INTERRUPT ;SET GLOBAL ENABLE BIT This could also be coded as MOV IE,#10 0 01000B Although these two approaches have exactly the same effect following a system reset, the effect is different if IE is written “on-the-fly,” in the middle of a program. The first approach has no effect on the other five bits in the IE register, whereas the second approach explicitly clears the other bits. It is fine to initialize IE with a “move byte” instruction at the beginning of a program (i.e., following a power-up or system reset), but enabling and disabling interrupts on-the-fly within a program should use “set bit” and “clear bit” instructions to avoid side effects with other bits in the IE register. Sample Program The following assembly program is generating a square wave with 50% duty cycle and 2 KHz frequency. Note that the microcontroller is not needed to monitor TF0 flag. The flow chart of this program is and HW design is shown below in Fig#5.3 respectively. SW LED WAVE COUNT_TR0 EQU EQU EQU EQU P1.0 P3.0 P2.0 -500 ; ORG 00H ; LJMP ORG LJMP MAIN 001BH T0_ISR ;ISR vector address of timer0 ; MAIN: RPT: ORG 30H SETB SW MOV TMOD, #01H SETB TF0 MOV IE, #82H MOV LED, SW SJMP RPT ; Make switch input ; Timer0 Mode 1 ; Force timer 0 interrupt to execute ; Enable Interrupts for timer0 T0_ISR: EXIT: MOV MOV DJNZ CPL SETB RETI END TL0, #LOW COUNT_TR0 ; Load T0 low ( -500) TH0, #HIGH COUNT_TR0 ; Load T0 High( -500) R7, EXIT WAVE TR0 ; Start timer 0 ; return from interrupt START ISR _ T0 Port Assign SW ,LED, WAVE Initialize Timer0 Decrement Counter Configure timers Timer0 in mode 0 Start Timer0 Is Counter=0 ? Enable Global Interrupt Enable timer0 Interrupt RUN TIMER0 Initialize counter Complement P2.0 Reinitialize Counter LED= SW RUN Timer0 RETI Fig# 5.2 Fig# 5.3 III- Procedure 1- Edit the above program and try to run it on the design in Fig# 5.3. Observe how the system is working. 2- Modify the demo program by using timer 0 in mode 2 instead of mode. (pre lab). 3- Compare between polling and interrupt methods by implementation the job in 1 above using polling methods. Draw a flow chart and write the program Based on polling method. IV- Lab Project 1- Modify the above program to toggling the LED in main program and generating a square wave signal with frequency 10KHZ on port p2.0 when the INT1 is activated. 2- Develop the demo program to toggling the LED based on timer 1 interrupt, with 1 second period. 3- Try to design an 8051based measuring system and writing a program it in C or assembly language an 8051 microcontroller that measured the frequency of a square wave by using an interrupt technique associated with any timers and counter of 8051. Display the value of measured frequency on 2x16 LCD display using 8 bit format. Run the system on PROTEUS and verify your work to T.A.