Day 2 AM Interrupts

Embed Size (px)

Citation preview

  • 7/30/2019 Day 2 AM Interrupts

    1/12

    20-Aug-2010 Paranz 1

    Introduction to PICMicrocontroller Programming

    & Interfacing

    August 19-21, 2010Franz Duran

    20-Aug-2010 Paranz 2

    Interrupts

    DAY 2 Morning Session

    August 20, 2010

    20-Aug-2010 Paranz 3

    OVERVIEW

    Interrupt BasicsInterrupt sources

    Interrupt service routine (ISR)

    Interrupts in PIC16F84A, PIC16F877A

    Associated SFRs

    RB0 Interrupt

    PORTB Interrupt on-change

  • 7/30/2019 Day 2 AM Interrupts

    2/12

    20-Aug-2010 Paranz 4

    BASICS OF INTERRUPTS

    Very important microcontroller featureAllows internal & external events to

    trigger an interruptnotify the CPU

    CPU temporarily halt program

    execution and respond to the interruptExecute an interrupt service routine

    20-Aug-2010 Paranz 5

    BASICS OF INTERRUPTS

    void main()

    {

    InitializeSystem();

    while(1)

    {

    Task1();

    Task2();

    Task3();

    Task4();

    Task5();

    }

    }

    void interrupt isr(void)

    {

    //Execute relevant instructions.

    //Clear interrupt flag.

    }

    an interrupt is tri ggered

    - CPU saves context (Program Counter, STATUS, W)

    CPU exits from interrupt

    service routine; previouscontext is restored, continue

    program execution.

    CPU executes theinterrupt service routine

    20-Aug-2010 Paranz 6

    INTERRUPTS: PIC16F84A

    4 Interrupt sourcesRB0 interrupt

    PORTB-on-change interruptTMR0 interrupt

    Data EEPROM Write Complete Interrupt

    SFR: INTCON

  • 7/30/2019 Day 2 AM Interrupts

    3/12

    20-Aug-2010 Paranz 7

    INTERRUPTS: PIC16F877A

    15 interrupt sourcesRB0 interrupt

    PORTB-on-change interrupt

    TMR0 interrupt

    Data EEPROMWrite Complete Interrupt

    UART TX, UART RX

    ADC, TMR1, TMR2

    SSP, CCP, etc..

    SFRs: INTCON, PIR1, PIR2, PIE1, PIE2

    20-Aug-2010 Paranz 8

    INTERRUPTS: INTCON

    PIC16F84A

    PIC16F877A

    20-Aug-2010 Paranz 9

    INTERRUPTS: PIC16F84A

    Interrupt Logic

  • 7/30/2019 Day 2 AM Interrupts

    4/12

    20-Aug-2010 Paranz 10

    INTERRUPTS: PIC16F877A

    Interrupt Logic

    20-Aug-2010 Paranz 11

    RB0/INT INTERRUPT

    RB0 pin is an interrupt source

    Generate an interrupt on the ff. event:falling-edge signal transition

    +5v 0v

    rising-edge signal transition

    0v +5v

    20-Aug-2010 Paranz 12

    RB0/INT INTERRUPT

    1 interrupt on rising edge

    0 interrupt on falling edge

    Set automatically by hardwareon interrupt; cleared via software

    Set to enableRB0/INT interrupt

    Set to enable interrupt

  • 7/30/2019 Day 2 AM Interrupts

    5/12

    20-Aug-2010 Paranz 13

    RB0/INT INTERRUPT

    1

    1

    01

    ISR is executed

    20-Aug-2010 Paranz 14

    RB0/INT INTERRUPT#include

    __CONFIG(HS & WDTDIS & LVPDIS & PWRTDIS & UNPROTECT);

    void main()

    {

    //Button at RB0.

    INTEDG = 0; //Interrupt on falling edge.

    INTF = 0; //Clear RB0 interrupt flag.

    INTE = 1; //Enable RB0 interrupt.

    GIE = 1; //Enable interrupt.

    TRISC1 = 0; //LED at RC1

    RC1 = 0;

    while(1); //Do nothing but wait for RB0 interrupt,

    // to occur then execute the ISR.

    }

    Example#1

    20-Aug-2010 Paranz 15

    RB0/INT INTERRUPT

    //Interrupt Service Routine

    void interrupt isr_RB0(void)

    {

    RC1 = 1; //Toggle LED.INTF = 0; //Clear RB0 interrupt flag.

    }

    Example#1

  • 7/30/2019 Day 2 AM Interrupts

    6/12

    20-Aug-2010 Paranz 16

    RB0/INT INTERRUPTEXERCISE:

    Change to INTEDG=1 rising edge

    Observe behaviour

    20-Aug-2010 Paranz 17

    RB0/INT INTERRUPT

    EXERCISE:Modify Example#1

    Toggle another LED in program loopLED1 toggled when RB0/INT triggers

    LED2 blink in program loop

    toggle LED2 every 500 msec

    use delay.c and delay.h

    Example#2

    20-Aug-2010 Paranz 18

    BASICS OF INTERRUPTS

    Other interrupt INFOs:GIE is automatically cleared during ISR

    execution; GIE is set on ISR exit

    prevent the ISR from being interrupted

    Interrupt latency time between interrupt & execution of ISR

    CPU complete current instruction, do context save

    3-4 instruction cycles for external interrupts

    2-3 instruction cycles for internal interrupts

  • 7/30/2019 Day 2 AM Interrupts

    7/12

    20-Aug-2010 Paranz 19

    BASICS OF INTERRUPTS

    other INFOs: (cont)ISR should be as short as possible set event flags inside the ISR &

    do associated tasks in program loop

    20-Aug-2010 Paranz 20

    RB0/INT INTERRUPT#include

    __CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);

    #define LED RC1

    #define TRUE 1

    #define FALSE 0

    #define TOGGLE 1

    volatile bit rb0_int_flag = FALSE; //event flag variable

    void interrupt ISR(void)

    {

    rb0_int_flag = TRUE;

    INTF = 0; //clear RB0 interrupt flag

    }

    Example#3

    20-Aug-2010 Paranz 21

    RB0/INT INTERRUPT

    void main()

    {

    InitSystem();

    while(1){

    if(rb0_int_flag == TRUE) //Poll flag.

    {

    rb0_int_flag = FALSE; //Clear flag.

    LED = 1; //Do task.

    }

    }

    }

    Example#3

  • 7/30/2019 Day 2 AM Interrupts

    8/12

    20-Aug-2010 Paranz 22

    BASICS OF INTERRUPTS

    other INFOs: (cont)ISR should be as short as possible set event flags inside the ISR &

    do associated task in program loop

    Global variables modified inside the ISR AND

    program loop should be declared asvolatile

    20-Aug-2010 Paranz 23

    BASICS OF INTERRUPTS

    volatile keyword

    Variable type qualifier

    inform the compiler that the value of a

    variable may be modified by the hardware inways that are not explicitly specified duringnormal program operation.

    Compiler should not optimize the variable inmain (or in other functions)

    20-Aug-2010 Paranz 24

    RB0/INT INTERRUPT#include

    void main()

    {

    INTEDG = 0; //Falling edge

    INTF = 0; //Clear RB0 interrupt flag.

    INTE = 0; //Disable RB0 interrupt.

    GIE = 0; //Disable all interrupt.

    TRISC1 = 0; //LED

    RC1 = 0;

    while(1)

    {

    if(INTF==1) //poll INTF if it is set

    {

    RC1 = 1; // then toggle LED.

    INTF = 0; //Clear RB0 interrupt flag.

    }

    }

    }

    Example#4

  • 7/30/2019 Day 2 AM Interrupts

    9/12

    20-Aug-2010 Paranz 25

    PORTB INTERRUPT ON CHANGE

    PORTB pins can be configuredas an interrupt source

    Generate an interrupt

    when the logic signal

    level on any pin

    changes

    20-Aug-2010 Paranz 26

    PORTB INTERRUPT ON CHANGE

    Generate an interrupt on both risingedgeand falling edge

    Operation:old value of PORTB during last read is

    stored

    if current value not equal to old value,

    mismatch condition

    RBIF=1

    20-Aug-2010 Paranz 27

    PORTB INTERRUPT ON CHANGE

    1

    1

    01ISR is executed

  • 7/30/2019 Day 2 AM Interrupts

    10/12

    20-Aug-2010 Paranz 28

    PORTB INTERRUPT ON CHANGE

    To clear PORTB interrupt:read value of PORTB AND

    clear RBIF

    20-Aug-2010 Paranz 29

    PORTB INTERRUPT ON CHANGE

    Set by hardware automaticallyon interrupt; cleared via software

    Set to enablePORTB interrupt

    Set to enableinterrupt

    20-Aug-2010 Paranz 30

    PORTB INTERRUPT ON CHANGE#include

    void interrupt isr(void)

    {

    unsigned char portb_temp;

    RC1 = 1; //Toggle LED

    portb_temp = PORTB & 0xF0; //Read to clear mismatch

    RBIF = 0; //Clear interrupt flag

    }

    void main()

    {

    TRISB = 0b00010000; //RB4 is input, rest are output & unused.

    RBIF = 0; //Clear interrupt flag.

    RBIE = 1; //Enable interrupt.

    GIE = 1; //Enable all (global) interrupt.

    TRISC1 = 0; //LED

    RC1 = 0;

    while(1); //do nothing, simply wait for ISR

    }

    The ISR is

    executed twice

    during a buttonpress

    Example#5

  • 7/30/2019 Day 2 AM Interrupts

    11/12

    20-Aug-2010 Paranz 31

    PORTB INTERRUPT ON CHANGE If using multiple PORTB pins, use switch() and masks

    Example: RB4 and RB5 are interrupt source

    void interrupt isr(void)

    {

    unsigned char portb_temp;

    portb_temp = PORTB & 0b00110000; //read PORTB

    switch(portb_temp)

    {

    case 0b00100000: //if Pushbutton1 is pressed,

    LED1 = TOGGLE; //toggle LED1

    break;

    case 0b00010000: //if Pushbutton2 is pressed,

    LED2 ^= TOGGLE; //toggle LED2

    break;

    }

    RBIF = 0; //clear interrupt flag

    }

    20-Aug-2010 Paranz 32

    Multiple Interrupts

    PIC16F has several interrupts

    But only one ISR

    If using multiple interrupts:Poll the interrupt enable bit and interrupt flag

    bit

    Example: RB0 int & PORTB Int.Poll INTE and INTF

    Poll RBIE and RBIF

    20-Aug-2010 Paranz 33

    Multiple Interruptsvoid interrupt isr(void)

    {

    unsigned char portb_temp;

    //Check if PORTB Int. triggered the interrupt

    if(RBIE && RBIF)

    {

    //codes here

    RBIF = 0; //clear interrupt flag

    }

    //Check if RB0 triggered the interrupt

    if(INTE && INTF)

    {

    //codes here

    INTF = 0; //clear interrupt flag

    }

    }

  • 7/30/2019 Day 2 AM Interrupts

    12/12

    20-Aug-2010 Paranz 34

    Multiple Interrupts

    EXERCISE: using multiple interruptsToggle LED1 with RB0 interrupt

    Toggle LED2 and LED3 with PORTB interrupt

    Toggle LED2 with RB4

    Toggle LED3 with RB5

    20-Aug-2010 Paranz 35

    BASICS OF INTERRUPTS

    other INFOs: (cont)ISR should be as short as possible

    Global variables modified inside the ISR AND

    program loop should be declared asvolatile

    interrupt flags are set even if interrupt isdisabled INTF is set by hardware, even if INTE=0 & GIE=0

    20-Aug-2010 Paranz 36

    INTERRUPTS: Summary

    Interrupts expands MCU functionality

    Main benefit:speed of response to internal/external events max. 4 TOSC interrupt latency

    reduced software overhead

    program efficiency greatly increased

    facilitates multitasking