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

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

INTERRUPTS PROGRAMMING

CHAPTER 11 INTERRUPTS PROGRAMMING

Microprocessors and Microcontrollers (EE-231)

Interrupt Programming: Interrupts vs. Polling Method:

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

CPEG300 Embedded System Design. Lecture 6 Interrupt System

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

CoE3DJ4 Digital Systems Design. Chapter 6: Interrupts

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 I/O and 8051 Interrupts

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interfacing External Devices using Embedded C Module No: CS/ES/22

1. Pin diagram of 8051 and ports

8051 Interrupt Organization

8051 Microcontroller Interrupts

CPEG300 Embedded System Design. Lecture 8 Timer


These three counters can be programmed for either binary or BCD count.

ELEG3923 Microprocessor Ch.9 Timer Programming

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

Chapter 09. Programming in Assembly

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

8051 Timers and Serial Port

8051 Microcontroller memory Organization and its Applications

8051 Serial Communication

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

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

80C51 Block Diagram. CSE Overview 1

8051 Microcontroller

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

CHAPTER TIMER PROGRAMMING

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

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

Microcontroller and Embedded Systems:

Department of Electronics and Instrumentation Engineering Question Bank

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

Timers and interrupts

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

Microcontroller & Interfacing

1. LCD (Liquid Crystal Display)interface

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

Embedded Controller Programming II. I/O Device Programming in C Part 1: Input and Interrupts

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

Lecture 10. Serial Communication


UNIT THE 8051 INSTRUCTION SET AND 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

Lecture 9. Timer Operations and Programming

EE 354 September 16, 2016 C Sample Programs

By the end of Class. Outline. Homework 5. C8051F020 Block Diagram (pg 18) Pseudo-code for Lab 1-2 due as part of prelab

Chapter 09. Programming in Assembly

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

EE6502- MICROPROCESSOR AND MICROCONTROLLER

1. Attempt any three of the following: 15

Interrupts, timers and counters

WINTER 14 EXAMINATION Subject Code: Model Answer Page No: 1/ 26

CoE3DJ4 Digital Systems Design. Chapter 5: Serial Port Operation

Three criteria in Choosing a Microcontroller

AGH University of Science and Technology Cracow Department of Electronics

8051 Microcontroller

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

CS 320. Computer Architecture Core Architecture

Microprocessors B (17.384) Spring Lecture Outline

Rev. No. History Issue Date Remark

ELCT 912: Advanced Embedded Systems

AGH University of Science and Technology Cracow Department of Electronics

How to use the PSoC based 16C450 Replacement

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

Measuring Duty Cycles with an Intel MCS-51 Microcontroller

Embedded Systems and Software

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

Maxim > Design Support > Technical Documents > Application Notes > Microcontrollers > APP 4465

Interrupts & Interrupt Service Routines (ISRs)

Microcontrollers. Fig. 1 gives a comparison of a microprocessor system and a microcontroller system.

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

Timer Counter and Interrupt. Equation (16 bits counter, Mode 1, 16MHz):

Introduction To MCS-51

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

The University of Texas at Arlington Lecture 21_Review


Module 3. Embedded Systems I/O. Version 2 EE IIT, Kharagpur 1

8086 Interrupts and Interrupt Responses:

4) In response to the the 8259A sets the highest priority ISR, bit and reset the corresponding IRR bit. The 8259A also places

GUJARAT TECHNOLOGICAL UNIVERSITY

ELEG3923 Microprocessor Ch.10 Serial Port Programming

Chapter 2. Overview of Architecture and Microcontroller-Resources

8051 MICROCONTROLLER

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

3. (a) Explain the steps involved in the Interfacing of an I/O device (b) Explain various methods of interfacing of I/O devices.

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

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

The Final Word on 8051 Microcontroller

8051 Microcontrollers

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

C Language Programming, Interrupts and Timer Hardware

Timer programming

MODEL ANSWER WINTER 17 EXAMINATION Subject Title: Microcontroller and applications

MLR INSTITUTE OF TECHNOLOGY DUNDIGAL , HYDERABAD

General Purpose Programmable Peripheral Devices. Assistant Professor, EC Dept., Sankalchand Patel College of Engg.,Visnagar

Unit-I. 1. INTRODUCTION TO MICROCONTROLLERS. Micro controller, types, selection of a microcontroller and applications

Transcription:

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text In this lecture embedded C program for interrupt handling will be discussed. Timer interrupt programming in Embedded C will be visited. Embedded C program for external hardware interrupt will be discussed in detail with examples. 1. Interrupt Vs. Polling External or internal event interrupts the microcontroller to inform it that a device needs its service. There are two ways to serving the device, they are interrupt and polling. Interrupt service begins when the device notifies the microcontroller by sending it an interrupt signal. After receiving an interrupt signal, the microcontroller stops whatever it is doing and serves the device. Program associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler. Polling happens while the microcontroller continuously monitors the status of a given device. When the condition is met, it performs the service. Next, it moves on to monitor the next device until everyone is serviced. Polling is not efficient, because it wastes much of the microcontroller s time by checking devices that do not need service. Interrupt can serve many devices, each device can get the service of microcontroller based on priority; but in polling, there is no priority, only on round robin they can get the attention of microcontroller. Microcontroller can also ignore a device request for service, because each interrupt is having an interrupt service routine (ISR), or interrupt handler. This is not possible in the polling method 1.1 Interrupt Service Routine(ISR): Each interrupt has an interrupt service routine (ISR), or interrupt handler. When an interrupt is called, the microcontroller runs the interrupt service routine. Every interrupt has a fixed location in memory that holds the address of the ISR. The group of memory locations that hold the addresses of ISRs is called interrupt vector table. Interrupt vector table for 8051 is shown in Table 1 below. Table 1. Interrupt Vector Table for the 8051.

1.2 Six interrupts in the 8051. Six interrupts in the 8051 are allocated as follows: Reset When the reset pin is activated, the 8051 jumps into memory location 0000. This is the power-up reset. Two interrupts are set aside for the timers:one for timer 0 and one for timer 1. Two interrupts are set aside for hardware external interrupts. Pin numbers 12 (P3.2) and 13 (P3.3) in port 3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2). Serial communication has a single interrupt that belongs to both receive and transmit. The interrupt vector table location 0023H belongs to this interrupt. 1.3 Steps in enabling the interrupt The following are the steps to enable the interrupt in 8051: 1. Bit D7 of the IE register (EA) must be set to high to allow the rest of the register to take effect. 2. If EA=1 enable all interrupts, EA=0 disable all interrupts even if the associated bit in the IE register is high. The figure 1 shown below gives the details about IE (Interrupt Enable) registers. Figure 1 Interrupt Enable Register. 1.4 8051 C interrupt numbers The 8051 C compilers have extensive support for 8051 interrupt with two features. They are Assign a unique number to each of the 8051 interrupts. It is shown in Table 2 shown below. It can also assign a register bank to ISR. This avoids code overhead due to push and pop operation of the R0-R7 registers.

Table 2 8051 Interrupt numbers in C 1.5 Programmable Timer Interrupt The timer flag (TF) is raised when the timer rolls over. In polling mode, we have to wait until the TF is raised. The microcontroller is tied down while waiting for TF to be raised, and cannot do anything else. If the timer interrupt in the IE register is enabled, whenever the timer rolls over, the Timer Flag is raised. After that the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR. In this way, the microcontroller can do other work until it is notified that the timer has rolled over. The figure 2 shown below gives the details about TF interrupt. If the timer interrupt is enabled, whenever TF=1, the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR. Figure 2. TF interrupt The Example 1 gives details about generating a square wave using Timer 0 assuming that XTAL value is 11.0592 MHz Example 1 Write a C program that continuously gets a single bit of data from P1.7 and sends it to P1.0, while simultaneously creating a square wave of 200 μs period on pin P2.5. Use Timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz Solution: We will use timer 0 in mode 2 (auto-reload). One half of the period is100 μs. 100/1.085 μs = 92, and TH0 = 256-92 = 164 or A4H. #include <reg51.h> sbit SW =P1^7;

sbit IND =P1^0; sbit WAVE =P2^5; void timer0(void) interrupt 1 WAVE=~WAVE; //toggle pin void main() SW=1; //make switch input TMOD=0x02; TH0=0xA4; //TH0=-92 IE=0x82; //enable interrupt for timer 0 while (1) IND=SW; //send switch to LED 1.6 Programming the Serial Communication Interrupt There is only one interrupt for serial communication. It is used to both send and receive the data. If the interrupt bit in the IE register (IE.4) is enabled, when RI (Received interrupt) or TI (transfer interrupt) is raised, the 8051 gets interrupted and jumps to memory location 0023H to execute the ISR. In ISR, the programmer has to examine the TI and RI flags to see which one has caused the interrupt and take action accordingly. Serial interrupt mainly used for receiving and not for sending the data. The following figure 3 es Single interrupt for both TI and RI flag. Figure 3. Single interrupt for both TI and RI. Example 2 described below is used for creating a square wave of 200 us period on pin P2.5 and sending letter A to the serial port. It uses Timer0 to create the square wave. Example 2: Write a C program that continuously gets a single bit of data from P1.7 and sends it to P1.0, while simultaneously (a) creating a square wave of 200 us period on pin P2.5, and (b) sending letter A to the serial port. Use Timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz. Use the 9600 baud rate. Solution:

2. Programming the External hardware interrupt Two external hardware interrupts are available in 8051.They are Level triggered and Edge triggered interrupt. The interrupt vector table locations 0003H and 0013H are set aside for INT0 and INT1. Level Triggered Interrupt: The level-triggered or level-activated interrupt is the default mode upon reset of the 8051. INT0 and INT1 pins are normally high in the level trigger mode, while the low-level signal on INT0 or INT1 triggers the interrupt. Once the interrupt occurs, the microcontroller stops whatever it is doing and jumps to the interrupt vector table to service that interrupt. The low-level interrupt signal is to be removed before the execution of the last instruction of the ISR, RETI. If low level signal is not removed it results in another interrupt being generated. Edge Triggered Interrupt: To make INT0 and INT1 edge triggered interrupts, we must program the bits of the TCON register. The details of TCON register is given in Table 3.

2.1 Sampling the edge-triggered interrupt Table 3 TCON Register. The external source must be held high for at least one machine cycle, and then held low for at least one machine cycle. The falling edge of pins INT0 and INT1 are latched by the 8051 and are held by the TCON.1 and TCON.3 bits of TCON register. These function as interrupt-inservice flags. It indicates that the interrupt is being serviced now. On this INTn pin, no new interrupt will be responded to until this service is finished. When the ISRs are finished, TCON.1 and TCON.3 are cleared. The interrupt is finished and the 8051 is ready to respond to another interrupt on that pin. During the time that the interrupt service routine is being executed, the INTn pin is ignored, no matter how many times it makes a high-to-low transition. RETI clears the corresponding bit in TCON register (TCON.1 or TCON.3). There is no need for instruction CLR TCON.1 before RETI in the ISR associated with INT0. Example 3 described below shows a C program to Light all LEDS connected to Port 0 if the switch is pressed and display y at port2. Example 3: A switch is connected to pin P3.2. When switch is pressed the corresponding line goes low. Write a C program to Light all LEDS connected to Port 0 if the switch is pressed. Display y at port2. Solution: # include <reg51.h> Sbit switch = P3^2; void extint0() // interrupt 0

P0=0xFF; void main() Switch=1; IE=0x81; While(1) P2= y ; 2.2 Interrupt Flag Bits for 8051 The TCON register holds four of the interrupt flags in the 8051. The interrupt flag bits for 8051 are shown in Table 4. The SCON register has the RI and TI flags. Table 4 Interrupt flag bits for the 8051. Example 4 shown below gives the C program to receive data serially and send it to P0. Example 4 Write a C program using interrupts to do the following: (a) Receive data serially and send it to P0 (b) Read port P1, transmit data serially, and give a copy to P2 (c) Make timer 0 generate a square wave of 5 khz frequency on P0.1. Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800. Solution: void main() unsigned char x; P1=0xFF; //make P1 an input TMOD=0x22; TH1=0xF6; //4800 baud rate SCON=0x50; TH0=0xA4; //5 khz has T=200us

IE=0x92; //enable interrupts TR1=1; //start timer 1 TR0=1; //start timer 0 while (1) x=p1; //read value from pins SBUF=x; //put value in buffer P2=x; //write value to pins Example 5 gives the details about how to write an Embedded C program to generate 10 KHz frequency wave and use timer 1 as an event counter to count up to 1-Hz pulse. Example 5 Write a C program using interrupts to do the following: (a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-reload (b) Use timer 1 as an event counter to count up a 1-Hz pulse and display it on P0. The pulse is connected to EX1. Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600. Solution: #include <reg51.h> sbit WAVE =P2^1; Unsigned char cnt; void timer0() interrupt 1 WAVE=~WAVE; //toggle pin void timer1() interrupt 3 cnt++; //increment counter P0=cnt; //display value on pins void main() cnt=0; //set counter to 0 TMOD=0x42; TH0=0x-46; //10 KHz IE=0x86; //enable interrupts TR0=1; //start timer 0 while (1); //wait until interrupted 3. Summary In this lecture interrupt handling in 8051 has been discussed. Timer interrupt programming in 8051 is discussed with examples. External interrupt programming in embedded C is discussed in detail.

4. References 1. The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie, Mazidi and Rolin D. McKinlay.