1 / 18

Meeting Timing Constraints

Meeting Timing Constraints. Certain tasks must be performed on time for correct operation Sampling audio/video anti-lock braking avionics Wait for time (I.e. sampling) or events (I.e. ABS) May require an understanding of the HW/SW specifics Clock frequency of microcontroller

irisa
Télécharger la présentation

Meeting Timing Constraints

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Slides created by: Professor Ian G. Harris Meeting Timing Constraints • Certain tasks must be performed on time for correct operation • Sampling audio/video • anti-lock braking • avionics • Wait for time (I.e. sampling) or events (I.e. ABS) • May require an understanding of the HW/SW specifics • Clock frequency of microcontroller • Understand the assembly code • Understand the microcontroller architecture (I.e. pipelining)

  2. Slides created by: Professor Ian G. Harris Busy-Wait Loops • Write a loop which just waits for fixed time, or for event • for (I=0; I<25000; I++); • while (!GODONE); • Wait loops waste processor resources • Doing nothing useful while waiting • This is not reasonable for complicated systems • Timing loops are sensitive to compiler and to HW parameters • What if clock frequency is changed? • What about compiler optimizations?

  3. Slides created by: Professor Ian G. Harris Interrupts • An interrupt is an interesting event which can occur and needs to be handled by a program • Interrupt service routine (ISR) is a function which is automatically executed when an event occurs • Timer expires, input set to 1, ADC completes, etc. • Main program can continue to execute until the event occurs • No time is wasted waiting • Main program is not aware of the interrupt • Interrupt is invoked by the HW, not the main program • Programmer does not need to worry about triggering the interrupt

  4. Slides created by: Professor Ian G. Harris Interrupt Sources • Interrupts can be triggered by many soruces • External Interrupt - RA2 assigned to 1 • PORTChange Interrupt - A change on any PORTA pin • Timer0 Overflow - Timer 0 expires • A/D Converter - Conversion completes • Comparators - Comparators return 1 • Oscillator fail - System oscillator no longer detected • EEPROM - EEPROM write operation completes • Timers 1 and 2 - Timers expire • Interrupts are grouped into primary and peripheral • Primary interrupt sources are External, PORTChange, and Timer0

  5. Slides created by: Professor Ian G. Harris Interrupt Masks and Flags • Interrupts are often disabled so that uninteresting events can be ignored • Disable timer interrupt when the timer is not needed • Disable external interrupt when RA2 is not used for interrupts • An interrupt is masked if it is disabled • Each interrupt source is associated with an enable bit and a flag bit • Flag bit is set to 1 when the event occurs • Ex. When Timer0 overflows the Timer0 flag bit is automatically set • Enable bit must be set by your code on order to have the interrupt service routine (ISR) invoked when the interrupt event occurs • Ex. If Timer0 enable bit is set then ISR will start when Timer0 overflows

  6. Slides created by: Professor Ian G. Harris Interrupt Registers INTCON register contains enable bits and flag bits for the primary interrupts Bit Name Description • Peripheral interrupts are controlled by other registers

  7. Slides created by: Professor Ian G. Harris More Interrupt Register Bits INTCON register contains enable bits and flag bits for the primary interrupts Bit Name Description • Interrupt flags must be cleared in software (your C code) • Flags are set when condition occurs, regardless of enable

  8. Slides created by: Professor Ian G. Harris Using Interrupts in C Decide which interrupts you want to enable (timer, external, etc.) Clear interrupt flags by setting flag bits in the INTCON register Enable the interrupts by setting appropriate bits in the INTCON register Write code for your interrupt service routine

  9. Slides created by: Professor Ian G. Harris Vcc RA0 PIC LED1 RA1 RA2 RA3 LED2 RA4 Interrupt Example • Problem: Make LED1 blink, but when a button is pressed light LED2 immediately • RA2 = 1 when button is pressed, 0 otherwise

  10. Slides created by: Professor Ian G. Harris Code to Blink LED1 • Some lines are omitted for simplicity (ANSEL, CMCON, #include) int i; main () { TRISA = 0b000100; // RA2 is an input, all // others are outputs PORTA = 0; // All outputs are 0 while (1 == 1) { for (i=0; i<25000; i++); RA0 = RA0 ^ 1; } }

  11. Slides created by: Professor Ian G. Harris Additions to Enable Interrupts • Interrupt must occur when RA2 is set to 1 • Use the RA2/INT External Interrupt Add the following code in the main, before the while loop INTCON = 0; // Disable all interrupts, clear flags GIE = 1; // Enable interrupts INTE = 1; // Enable RA2/INAT interrupt

  12. Slides created by: Professor Ian G. Harris Interrupt Service Routine • ISR is function called when ANY interrupt occurs • ISR must check the interrupt flag bits to see which interrupt occurred • Takes no arguments, returns no values void interrupt button_int(void) { int j; if (INTF && INTE) { INTF = 0; RA3 = 1; for (j=0; j<25000; j++); RA3 = 0; } return; }

  13. Slides created by: Professor Ian G. Harris Impact of Interrupts • Interrupts allow high priority tasks to be serviced quickly • Interrupts make timing difficult to guarantee • - Can happen at any time, in the middle of a program • for(i=0; i<25000; i++); • This code is usually ~half second delay • Interrupts can occur in the middle of loop, making it longer • Interrupts can interrupt interrupts • - Should disable interrupts at appropriate times

  14. Slides created by: Professor Ian G. Harris Using a Timer (TMR0) • TMR0 can be used as an interrupt source • TMR0 is an 8-bit up counter • T0IF (INTCON bit 2) is set when TMR0 overflows (FF to 00) • Need to relate the clock to real time • System Clock - Clock used to drive all operations • 4MHz is the default value • Instruction Clock - Clock used to drive the counter • 1/4 the system clock frequency (1MHz) • Instruction clock period = 1us

  15. Slides created by: Professor Ian G. Harris Timer Control • Three ways to control timer behavior: • 1. Prescaler value - the number of instruction clocks required to increment the timer • Ex. Prescaler = 1, timer increments after 1us • Prescaler = 4, timer increments after 4us • 2. Initialization - Timer value can be initialized in your code • Ex. TMR0 = 22; • 3. Clock Source - The internal (4MHz) clock can be used, or an external clock

  16. Slides created by: Professor Ian G. Harris OPTION Register • T0CS - 1: RA2 pin, 0: Internal instruction clock • T0SE - 1: falling edge, 0: rising edge • PSA - 1: Watchdog timer (not TMR0), 0: TMR0 • PS2:PS0 - Prescalar = 2 (PS2:PS0) + 1 • Ex. PS2:PS0 = 001, Prescalar = 4

  17. Slides created by: Professor Ian G. Harris Prescalar/Init. Examples • Example 1: Interrupt should be triggered after 30 us • Use internal clock - instruction clock period = 1us • Timer must overflow every 30 instruction clocks • Use no prescalar (PSA = 1;) • Initialize counter to 256 - 30 = 226 (TMR0 = 226;) • Example 2: Interrupt should be triggered after 1000us • Use internal clock with prescalar 4 in order to count to 1000 • PSA = 0 (use prescalar) and PS2:PS0 = 011 • Timer must overflow every 1000 instr. clocks, 250 with prescalar • Initialize counter to 256 - 250 = 6

  18. Slides created by: Professor Ian G. Harris TMR0 = 226; // Initialize timer value T0IF = 0; // Clear existing interrupt flag T0IE = 1; // Enable timer0 interrupt GIE = 1; // Enable interrupts OPTION = 0b 0 1 0 0 1 000 Prescalar value T0CS = 0: Use internal clock No prescalar Timer Code Example • Interrupt after 30 us

More Related