Interrupts on PIC18F252 Part 2. Interrupts Programming in C Language

Similar documents
Interrupts on PIC18F252 Part 2

Capture Mode of Pic18F252

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING. BEng(Hons) Electrical and Electronics Engineering SEMESTER 1 EXAMINATION 2016/2017

Interrupts. ELEC 330 Digital Systems Engineering Dr. Ron Hayne. Images Courtesy of Ramesh Gaonkar and Delmar Learning

Using Timers of Microchip PIC18F Microcontrollers

These 3 registers contain enable, priority,

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

ELCT 912: Advanced Embedded Systems

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING B.ENG (HONS) ELECTRICAL AND ELECTRONIC ENGINEERING EXAMINATION SEMESTER /2016

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

Topic 10 10/24/2010. C with Embedded Extension

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

The University of Texas at Arlington Lecture 21_Review

/*Algorithm: This code display a centrifuge with five variable speed RPM by increaseing */

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

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Micro-Controller: PIC16C74 < Part 5: Interrupt >

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

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

Microprocessors B (17.384) Spring Lecture Outline

Interrupts and Serial Communication on the PIC18F8520

Analog Output with a Digital to Analog Converter

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING MSC SYSTEMS ENGINEERING AND ENGINEERING MANAGEMENT SEMESTER 2 EXAMINATION 2016/2017

evsjv `k cigvyy kw³ Kwgkb BANGLADESH ATOMIC ENERGY COMMISSION

EEE394 Microprocessor and Microcontroller Laboratory Lab #6

Mod-5: PIC 18 Introduction 1. Module 5

Chapter 13. PIC Family Microcontroller

Speed Control of a DC Motor using Digital Control

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

Human Response Timer

More Fun with Timer Interrupts

Chapter 11: Interrupt On Change

Embedded systems. Exercise session 3. Microcontroller Programming Lab Preparation

CENG-336 Introduction to Embedded Systems Development. Timers

C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem1\problem1Cunningham.c

MPLAB SIM. MPLAB IDE Software Simulation Engine Microchip Technology Incorporated MPLAB SIM Software Simulation Engine

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

Interrupts. Embedded Systems Interfacing. 08 September 2011

Timer1 Capture Mode:

PIC Discussion By Eng. Tamar Jomaa

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

dspic Interrupts The Interrupt Control and Staus Registers are :- ENG721-S2 Mixed Signal Processing : Hassan Parchizadeh Page 1

BME 4900 Page 1 of 2. Meeting 2: Personal Progress Report 12/2/09 Team 12 with Drew Seils. Semester One Week Two

Embedded Systems Programming and Architectures

ME 6405 Introduction to Mechatronics

Department of Electronics and Instrumentation Engineering Question Bank

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

ECE Homework #10

Chapter 09. Programming in Assembly

Chapter 09. Programming in Assembly

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet Example Application...

How to use MPLABX to program and debug PICsimLab

Section 13. Timer0 HIGHLIGHTS. Timer0. This section of the manual contains the following major topics:

Application Note One Wire Digital Output. 1 Introduction. 2 Electrical Parameters for One Wire Interface. 3 Start and Data Transmission

Accurate Time and Interrupts

MTRX3700 Mechatronics

ECE 354 Introduction to Lab 2. February 23 rd, 2003

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

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet... 4

8051 Interrupt Organization

TKT-3500 Microcontroller systems

CHAPTER 11 INTERRUPTS PROGRAMMING

Section 14. Timer1 HIGHLIGHTS. Timer1. This section of the manual contains the following major topics:

EE 361L Digital Systems and Computer Design Laboratory

UNCA CSCI 255 Exam 3 Fall 2011

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

8051 Microcontroller Interrupts

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

Laboratory Exercise 7 - Extended I/O & Parallel Processing

Embedded Systems Module. 6EJ505. C Tutorial 3: using the ICD3 rev tjw

Chapter 2 Sections 1 8 Dr. Iyad Jafar

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

Microprocessors and Microcontrollers (EE-231)

Introduction. Prerequisites: Procedure Steps. Step1: Downloading and Adding the MCC Plugin

Introduction to Embedded Systems

Programming Embedded Systems

Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

Laboratory: Introduction to Mechatronics

Course Introduction. Purpose: Objectives: Content: 27 pages 4 questions. Learning Time: 20 minutes

Embedded System Design

PIC 16F84A programming (II)

LAB WORK 2. 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button. Lab Work

Laboratory 9. Programming a PIC Microcontroller - Part I

Chapter 2. Overview of Architecture and Microcontroller-Resources

Lecture (04) PIC16F84A (3)

Release Bugs solved

Interrupts, timers and counters

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 1.

Lecture (04) PIC 16F84A programming I

Microcontroller systems Lec 2 PIC18LF8722 Microcontroller s s core

Section 16. Basic Sychronous Serial Port (BSSP)

Timers and interrupts

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Section 9. Watchdog Timer (WDT)

CPEG300 Embedded System Design. Lecture 6 Interrupt System

Laboratory 10. Programming a PIC Microcontroller - Part II

Physics 335 Intro to MicroControllers and the PIC Microcontroller

Transcription:

Interrupts on PIC18F252 Part 2 Interrupts Programming in C Language Programming interrupts in C language using XC8 compiler is significantly simplified compared to C18 compiler. This note explains the process. Use the old Lecture 5 note in case you are using C18 compiler instead of the XC8 currently available on the machines in the laboratory. In its most basic form setting up the interrupts under XC8 compiler is reduced to the use of the keyword interrupt before the function which should be called when an interrupt happens. This function is usually referred to as Interrupt Service Routine (ISR). In addition to this, interrupt needs to be configured (enabled) at the start of the program. Thus the three steps necessary when writing the interrupt based program outlined at the end of the previous lecture note are now reduced to two: 1. Writing the ISR. 2. Enabling desired interrupt(s) (by setting the appropriate interrupt enabling bits in various microcontroller registers). In the following example, simple, Timer 2, C program discussed in the previous lectures is modified to include the use of interrupts instead of explicit calls to delay() function. Thus, we will turn this ordinary C function into Timer 2 interrupt service routine (ISR) in two easy steps. As a reminder, here is the original program, discussed in the previous lectures.

// File Name: PICprog2.c // Purpose: // Second PIC program. // This version uses Timer 2 to generate a // square wave signal on the pin 0 of port B. // The frequency of this signal is determined // by the loading of Timer 2, i.e. by duration // of delay() function // B. Vuksanovic 01/09/2013 #include <p18f252.h> void delay(void) // set the Timer 2 to produce certain delay // by loading T2CON and PR2 registers T2CON = 0x49; PR2 = 0x7C; T2CONbits.TMR2ON = 1; // start the Timer 2 while(!pir1bits.tmr2if); // wait for timer flag T2CONbits.TMR2ON = 0; // stop the Timer 2 PIR1bits.TMR2IF = 0; // clear timer flag void main (void) TRISB = 0x00; // configure portb as output while(1) PORTBbits.RB0 = 1; // set RB0 to 1 delay(); // wait PORTBbits.RB0 = 0; // set RB0 to 0 delay(); // wait again and loop back Let us rewrite this function by making just one modification to it moving the task of toggling the RB0 pin from the main program to delay() function.

// File Name: PICprog3_1.c // Purpose: // Third PIC program. // This version demonstrates the first step of // writing an interrupt driven program. // Intended interrupt task - in this case toggling // the pin 0 of port B is moved from the main function // to Timer 2 function. // Note - this is not an interrupt driven program yet. // It's just a first step towards it. // B. Vuksanovic 01/09/2013 #include <p18f252.h> void delay(void) // set the Timer 2 to produce certain delay // by loading T2CON and PR2 registers T2CON = 0x49; PR2 = 0x7C; T2CONbits.TMR2ON = 1; // start the Timer 2 while(!pir1bits.tmr2if); // wait for timer flag if(portbbits.rb0 == 1) PORTBbits.RB0 = 0; else PORTBbits.RB0 = 1; // i.e. future interrupt task T2CONbits.TMR2ON = 0; // stop the Timer 2 PIR1bits.TMR2IF = 0; // clear timer flag void main (void) TRISB = 0x00; // configure portb as output while(1) delay(); // just wait

Before introducing the interrupts in this program, we will rewrite this program in order to include the future interrupt task (toggling pin 0 of port B) in the delay() function. Thus, lines from the above program PORTBbits.RB0 = 1; and PORTBbits.RB0 = 0; are replaced with the line PORTBbits.RB0 = ~PORTBbits.RB0; which has the same effect. This line is then moved from main() into delay() function (future ISR). Here is the modified version of the program. (Note there are no interrupts in the program yet.)

// File Name: PICprog3_2.c // Purpose: // Third PIC program. // This version demonstrates the first step of // writing an interrupt driven program. // Intended interrupt task - in this case toggling // the pin 0 of port B is moved from the main function // to Timer 2 function and a small modification to this task // introduced in order to simplify the overall code. // Note - this is not an interrupt driven program yet. // It's just a first step towards it. // B. Vuksanovic 01/09/2013 #include <p18f252.h> void delay(void) // set the Timer 2 to produce certain delay // by loading T2CON and PR2 registers T2CON = 0x49; PR2 = 0x7C; T2CONbits.TMR2ON = 1; // start the Timer 2 while(!pir1bits.tmr2if); PORTBbits.RB0 = ~PORTBbits.RB0; // do the "task" // i.e. future interrupt task // wait for timer flag T2CONbits.TMR2ON = 0; // stop the Timer 2 PIR1bits.TMR2IF = 0; // clear timer flag void main (void) TRISB = 0x00; // configure portb as output while(1) delay(); // just wait

Finally, word interrupt is inserted in front of the name of the function delay() to turn it into ISR. Name of the function is also appropriately changed to timer2_isr(). The actual ISR name makes no difference to program operation (common misconception amongst many students) but it is good to give the function some meaningful name. Timer 2 interrupt configuration lines are also added at the start of the program to obtain the complete, interrupt driven program. Here is the final version of this program. Explanations for interrupt configuration lines are provided in the code and the registers containing relevant bits are given at the end of this note.

// File Name: PICprog3_3.c // Purpose: // Third PIC program. // This is the interrupt driven "toggle RB0" program. // PICprog3_1 has been modified, by including all necessary // Timer 2 interrupt configurations. // B. Vuksanovic 01/09/2013 #include <p18f252.h> // Timer 2 Interrupt Service Routine void interrupt timer2_isr(void) T2CONbits.TMR2ON = 0; // stop the Timer 2 // do the interrupt "task" // i.e. change the state of RB0 pin // to generate the square wave on RB0 PORTBbits.RB0 = ~PORTBbits.RB0; T2CON = 0x49; PR2 = 0x7C; PIR1bits.TMR2IF = 0; T2CONbits.TMR2ON = 1; // set Timer 2 to produce // 1 ms delay @ 20 MHz // clear Timer 2 interrupt flag // start Timer 2 again void main (void) TRISB = 0x00; // configure Port B pins for output PORTB = 0; // initialise RB0 to 0 RCONbits.IPEN = 1; // enable interrupt priority scheme IPR1bits.TMR2IP = 1; // configure TMR2 interrupt to high priority PIR1bits.TMR2IF = 0; // clear TMR2 interrupt flag INTCONbits.GIEH = 1; // enable high-priority interrupts PIE1bits.TMR2IE = 1; // enable TMR2 overflow interrupt T2CON = 0x49; // set Timer 2 to produce PR2 = 0x7C; // 1 ms delay @ 20 MHz T2CONbits.TMR2ON = 1; // start Timer while(1) // wait for interrupts in infinite loop // some other useful work can now be done here :-)

Following some suggestions from students during the lecture, improved version of this program is given below. Try to understand and check those modifications. // File Name: PICprog3_4.c // Purpose: // Third PIC program. // This is the interrupt driven "toggle RB0" program. // PICprog3_1 has been modified, by including all necessary // Timer 2 interrupt configurations. // B. Vuksanovic 01/09/2013 #include <p18f252.h> // Timer 2 Interrupt Service Routine void interrupt timer2_isr(void) T2CONbits.TMR2ON = 0; // stop the Timer 2 // do the interrupt "task" // i.e. change the state of RB0 pin // to generate the square wave on RB0 PORTBbits.RB0 = ~PORTBbits.RB0; PIR1bits.TMR2IF = 0; T2CON = 0x4D; // clear Timer 2 interrupt flag // set Timer 2 to produce // 1 ms delay @ 20 MHz // and restart Timer 2 at the same time void main (void) TRISB = 0x00; // configure Port B pins for output PORTB = 0; // initialise RB0 to 0 RCONbits.IPEN = 1; // enable interrupt priority scheme IPR1bits.TMR2IP = 1; // configure TMR2 interrupt to high priority PIR1bits.TMR2IF = 0; // clear TMR2 interrupt flag INTCONbits.GIEH = 1; // enable high-priority interrupts PIE1bits.TMR2IE = 1; // enable TMR2 overflow interrupt T2CON = 0x4D; // set Timer 2 to produce PR2 = 0x7C; // 1 ms delay @ 20 MHz // and start the Timer 2 at the same time while(1) // wait for interrupts in infinite loop // some other useful work can now be done here :-)

To do the task of configuring interrupts properly, we also need to have some understanding and knowledge of SFRs related to interrupts operation on PIC18F252 microcontroller. Following few pages list majority of the SFRs (Special Function Registers) involved in interrupt configuration and operation on PIC18F252 microcontroller with some explanations of relevant bits. (Copied from Microchip s PIC18Fxx2 Data Sheet). There are even more registers related to interrupts on PIC18F252, but we will not need them to get our first (Timer 2) interrupt driven program running. More explanations are provided in the Lecture note no 6. T2CON: (TIMER2 CONTROL REGISTER) This register just configures the Timer 2 operation and has been discussed in earlier lectures. Timer 2 needs to be configured in the same way again so T2CON is listed here again for your reference. PR2: (TIMER 2 PERIOD REGISTER) Nothing too interesting here (Microchip did not even produce a detailed figure of this register). PR2 is just an 8-bit readable and writable register. It is the second register used for Timer 2 configuration. It is called Timer2 period register because Timer2 (when switched on) increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle. So Timer 2 does not really overflow. More accurately, as explained in PIC18Fxx2 Data Sheet when Timer 2 reaches the count stored in PR2 TMR2 to PR2 match occurres. Result is still the same Timer 2 interrupt flag (TMR2IF bit from PIR1 register) is set.

PIR1: (PERIPHERAL INTERRUPT REQUEST (FLAG) REGISTER 1)

RCON: IPR1: (PERIPHERAL INTERRUPT PRIORITY REGISTER 1)

INTCON:

PIE1: