Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

Similar documents
Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

Timer1 Capture Mode:

More Fun with Timer Interrupts

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

ECE Homework #10

Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

Flow Charts and Assembler Programs

ECE Test #1: Name

Binary Outputs and Timing

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

Assembly Language Instructions

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

Chapter 11: Interrupt On Change

These 3 registers contain enable, priority,

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

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

Introduction. Embedded system functionality aspects. Processing. Storage. Communication. Transformation of data Implemented using processors

ECE Homework #3

Embedded Systems Programming and Architectures

Lecture (04) PIC16F84A (3)

D:\PICstuff\PartCounter\PartCounter.asm

Speed Control of a DC Motor using Digital Control

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

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

Embedded System Design

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

PIC PROGRAMMING START. The next stage is always the setting up of the PORTS, the symbol used to indicate this and all Processes is a Rectangle.

The University of Texas at Arlington Lecture 5

Performance & Applications

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

PIC 16F84A programming (II)

Laboratory Exercise 5 - Analog to Digital Conversion

CENG-336 Introduction to Embedded Systems Development. Timers

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

Accurate Time and Interrupts

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

Interfacing PIC Microcontrollers. ADC8BIT2 Schematic. This application demonstrates analogue input sampling

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

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

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

movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

Analog Output with a Digital to Analog Converter

SRF08 Ultra sonic range finder

CONNECT TO THE PIC. A Simple Development Board

Chapter 3: Further Microcontrollers

EECE.3170: Microprocessor Systems Design I Summer 2017

PIC Discussion. By Eng. Tamar Jomaa

LPTCOM. Bruce Misner Lakehead University h d3 RD2 pin 21. RD3 pin h d4. RD4 pin 27 RD5 pin h d5. RD6 pin 29 RD7 pin H d6

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.

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

EEE111A/B Microprocessors

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

Simon. Final Project Report December 11, 2009 E155. Julia Karl and Kirsten McAfee

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

Instuction set

Chapter 5 Sections 1 6 Dr. Iyad Jafar

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

Chapter 10 Sections 1,2,9,10 Dr. Iyad Jafar

Human Response Timer

Application Note - PIC Source Code v1.1.doc

Week1. EEE305 Microcontroller Key Points

Interrupts on PIC18F252 Part 2. Interrupts Programming in C Language

Example of Asyncronous Serial Comms on a PIC16F877

ELCT 912: Advanced Embedded Systems

C and Embedded Systems. So Why Learn Assembly Language? C Compilation. PICC Lite C Compiler. PICC Lite C Optimization Results (Lab #13)

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

Chapter 13. PIC Family Microcontroller

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

ME 515 Mechatronics. A microprocessor

Weekly Report: Interactive Wheel of Fortune Week 4 02/014/07-02/22/07 Written by: Yadverinder Singh

EECE.3170: Microprocessor Systems Design I Spring 2016

CHAPTER 6 CONCLUSION AND SCOPE FOR FUTURE WORK

SOLUTIONS!! DO NOT DISTRIBUTE!!

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

ME 6405 Introduction to Mechatronics

Interrupts on PIC18F252 Part 2

Input/Output Ports and Interfacing

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

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

ﻢﯿﺣﺮﻟا ﻦﻤﺣﺮﻟا ﷲا ﻢﺴﺑ

Physics 335 Intro to MicroControllers and the PIC Microcontroller

PIC Discussion By Eng. Tamar Jomaa

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

EE 361L Digital Systems and Computer Design Laboratory

Learning Objectives:

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

Experiment 7:The USART

Jordan University of Science and Technology Electrical Engineering Department Microcontrollers and Embedded Systems Spring 2011

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 5 Solution

President Alan VK6ZWZ Acting Sec. Don VK6HK Vice President Terry VK6ZLT Treasurer Ces VK6AO

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

Capture Mode of Pic18F252

Model Answer Microcontrollers. MCQ Problems Total Q1 Q2

Transcription:

Background: Timer2 Interrupts The execution time for routines sometimes needs to be set. This chapter loops at several ways to set the sampling rate. Example: Write a routine which increments an 8-bit counter every 10 ms and sends this to PORTC org goto Assembler Solution 0x300 Start ;********************************* ; 10ms wait routine ;********************************* Wait movlw 60 movwf CNT2 Loop1 movlw 138 movwf CNT3 C Solution // Subroutine Definitions void Wait(void) unsigned int X; for (X=0; X<5000; X++) Loop2 nop nop nop decfsz CNT3 goto Loop2 decfsz CNT2 goto Loop1 return ;********************************* ; Main Routine ;******************************* Start bcf bsf clrf bcf clrf STATUS,RP1 STATUS,RP0 TRISC STATUS,RP0 COUTNER Main incf COUNTER,F movf COUNTER,W movwf PORTC call Wait goto Main // Main Routine void Main(void) unsigned char COUNTER; COUNTER = 0; TRISC = 0; do COUNTER += 1; PORTC = COUNTER; Wait(); while (1>0); JSG - 1 -

In assembler, you can compute the exact time the wait routine takes: #clocks = ((6 138 + 5) 60 + 5) = 49, 985 time =(49, 985 clocks) 100ns clock = 4.9985ms In C, you aren't sure exactly how long the routine takes since you don't know exactly how the code compiles. Trial and error can be used to tweek the number 5,000 Note that The timing is slightly off. This clock will be off by 26 seconds per day The routine is rather inefficient. 99.95% of the time is spent in a wait loop. Only 0.05% of the time is spent in the main routine. This is much less accurate in C since you aren't sure exactly how your C code compiles. TIMER Interrupts One way to improve the efficiency of this program is to use interrupts. Interrupts are similar to subroutines except that Subroutines are routines called by software (such as the 10ms wait loop from before) Interrupts are routines called by hardware (such as a certain time elapses) Timers are useful, so four are available on the PIC18F4626: TIMER0: Interrupt after N events (or N clocks). N = 1 to 2 24 (1.67 seconds) TIMER1: Interrupt after N events (or N clocks). N = 1 to 2 19 (52 milliseconds) TIMER2: Interrupt every N clocks. N = 1 to 2 16 (6.5 millisecond) TIMER3: Interrupt after N events (or N clocks). N = 1 to 2 19 (52 milliseconds) Default for the PIC is to disable interrupts. You must set up the interrupt (enable and conditions for the interrupt) if you want to use them. If an interrupt occurs, The present instruction is completed The processor inserts a call 0x08 into the program At address 0x08, the interrupt service routine must be placed (or a goto InteruptService needs to be placed. This routine must Save the W and STATUS register. Since you don't know where in the program the interrupt will be called, W and STATUS may be important. Clear TMR2IF. This tells the PIC that the present interrupt has been serviced. If you don't, the interrupt will be called immediately upon return, essentially halting the processor. (optional) Do something Restore the W and STATUS registers, and Terminate with retfie for a return from interrupt. JSG - 2 -

TIMER2 INITIALIZATION Suppose you'd like to keep track of time. To do this, set up the interrupt so that it's called every 1 millisecond (10,000 clocks: 10000 * 100ns = 1ms). For every interrupt you want to use, you need to initialize them by: Enable the interrupt Set up the conditions for the interrupt (10,000 clocks) In the interrupt service routine (which is called every 10,000 clocks in this case), you need to Do something (such as increment a counter, which is how the main program keeps track of time), Set up the next interrupt (10,000 clocks from now), and Acknowledge the interrupt (clear the interrupt flag) The hoops you have to jump through for TIMER0 to TIMER3 are summarized in the following table: TIMER2 N = 1..65,535 200ns to 13.1ms Clock Source N Enable Bits Flag N = A*B*C A = 1..16 B = 1..256 C = 1, 4, 16 N = A*B*C PR2 = B-1 T2CON = xaaaa1cc aaaa = 0000: A=1 aaaa = 0001: A=2 ::: aaaa = 1110: A=15 aaaa = 1111: A=16 cc = 00: C = 1 cc = 01: C = 4 cc = 10: C = 16 cc = 11: C = 16 TMR2ON = 1 TMR2IE = 1 TMR2IP = 1 PEIE = 1 TMR2IF To enable a Timer2 interupt, you need to turn it on four times TMR2ON = 1; 0 turns off Timer2 and interrupts won't happen TMR2IE = 1; 0 disables interrupts PEIE = 1; 0 disables several interrupts, including TIMER2 GIE = 1; 0 disables all interrupts TMR2IP = 1; Timer2 is a high priority interrupt The rate at which the TIMER2 interrupts happen is Time = (A B C) 100ns A, B, and C are defined by registers T2CON and PR2: T2CON 7 6 5 4 3 2 1 0 - A3 A2 A1 A0 TMR2ON C1 C0 PR2 7 6 5 4 3 2 1 0 B7 B6 B5 B4 B3 B2 B1 B0 JSG - 3 -

The scalar values are PostScalar A Main Scalar B Prescalar C A3:A2:A1:A0 A B7:B0 B C1:C0 C 0000 1 0000 0000 1 00 1 0001 2 0000 0001 2 01 4 10 16 1110 15 1111 1110 255 11 16 1111 16 1111 1111 256 The maximum time you can set for a Timer2 interrupt is A x B x C = (16) x (256) x (16) = 65,536 clocks = 6.5536 ms Example: Toggle RC0 every 6.5536 ms (65,536 clocks) #include <pic18.h> // Global Variables // Subroutine Declarations void interrupt timer2(void) RC0 =!RC0; TMR2IF = 0; void main(void) TRISA = 0; TRISB = 0; TRISC = 0; TRISD = 0; ADCON1 = 15; // initialize Timer2 T2CON = 0xFF; PR2 = 255; TMR2IE = 1; PEIE = 1; TMR2ON = 1; TMR2IP = 1; // Turn on all interrupts GIE = 1; while(1) PORTB = PORTB + 1; // Count for no reason other than to count The resulting signal on RC0 is as follows: JSG - 4 -

RC0 Toggles Every Timer2 Interrupt: Timer2 Set Up for N = 65,536 Clocks (6.5536 ms) For a 1ms interrupt rate, A B C = (1ms) (10, 000, 000 clock/second) A x B x C = 10,000 One combination which works is C = 01 (x 4) B = 249 (x 250) A = 9 (x 10) or PR2 = 249 T2CON = 0x4D T2CON 7 6 5 4 3 2 1 0 - A3 A2 A1 A0 T2E C1 C0 (A=9, C=1) 0 1 0 0 1 1 0 1 Example: Toggle RC0 every 1.00ms #include <pic18.h> // Global Variables // Subroutine Declarations void interrupt timer2(void) RC0 =!RC0; TMR2IF = 0; JSG - 5 -

void main(void) TRISA = 0; TRISB = 0; TRISC = 0; TRISD = 0; ADCON1 = 15; // initialize Timer2 T2CON = 0x4D; PR2 = 249; TMR2IE = 1; PEIE = 1; TMR2ON = 1; TMR2IP = 1; // Turn on all interrupts GIE = 1; while(1) PORTB = PORTB + 1; // Count for no reason other than to count RC0 Toggles Every Timer2 Interrupt: Timer2 Set Up for N = 10,000 Clocks (1.000 ms) JSG - 6 -

The flow chart for this program is a little difficult to draw since two routines are running in parallel: The main routine which sends COUNTER to PORTC and repeats The Timer2 routine which increments COUNTER every 1ms. Two parallel flow charts may be the best way to represent this: Main Routine Interrupt Service Routine Start Start Every 2ms Set PortC to output Save W & STATUS Initialize Timer2 Interrupts for 12ms 1 Turn On Interrupts Increment COUNT (do stuff) Clear Interrupt Flag Restore W & STATUS Copy COUNT to PORTC Return from Interrupt Note that The main routine simply watches COUNTER and sends it to PORTC. The Interrupt routine is responsible for changing COUNTER every 1ms Moroover: The shaded parts of the interupt routine are common to any interrupt service routine. Only the conditions under which this routine is called (set up in the main routine) and what you do when it is called change. JSG - 7 -

Interrupt Constraints Background: Timer2 interrupts are a way to keep track of time. The PIC is running at 10 million instructions / second (10MHz) Every N clocks, a Timer2 interrupt is triggered N = A * B * C A, C are from T2CON B is from PR2 When the interrupt is triggered, the main routine stops and you run the interrupt service routine. If you plot time on the X axis, the processor is then running as follows: Main routine halted Running the interrupt service routine In Main Routine In Main Routine Interrupt In Main Routine Interrupt In Main Routine Timer2 Interrupt is triggered every N clocks Once set up, the main routine has no control over the timing: hardware triggers the interrupt every N clocks. Note however, that the interrupt service routine is stealing clocks from the main routine. You can't steal more than 100%. You probably don't want to steal more than 50%. With interrupts turned on, you essentially have 2 (or more) programs running in parallel: The main routine which supposedly does stuff, The interrupt routines which handle administration every time an event occurs. This creates several possible constraints: JSG - 8 -

Timing Constraints: This interrupt service routine takes about 50 clocks to execute. This is much more efficient than the wait loop which wastes 10,000 clocks to wait 1ms. Instead, the interrupt 'steals' 50 clocks from the main routine every 10,000 clocks. If you call the interrupt more often (because you want a timer with a better resolution than 1ms) this slows down the processor. If you go too fast, the main routine shuts down and all the time is spent in the interrupt service routine: Interrupt Time (clock resolution) Clocks / Interrupt (N) # Clocks Spent in the Interrupt # Clocks Left for the Main Routine Processor 'Speed' 1ms 10,000 50 9,950 99.5% 100 us 1,000 50 950 95% 10 us 100 50 50 50% 1 us 10 50-50 0% The faster you interrupt, the more accurate your clock. The faster you interrupt, however, the slower the main routine appears to run. Moreover, if you interrupt too frequently, you spend all of the time servicing the interrupt and never get to the main routine. This results in the following rule: Keep interrupt service routines short. The interrupt service routine should take much less time to execute than the rate at which it is called. With Timer2 interrupts, you can keep track of time to 100us (slowing down the main routine by 50%). There are ways to measure time to 100ns - but this comes up later with Timer1 interrupts. JSG - 9 -