• AVR Freaks

Hot!PIC18F47K42 IOC keeps interrupting

Author
JustRob
Super Member
  • Total Posts : 492
  • Reward points : 0
  • Joined: 2008/09/04 12:49:27
  • Location: 0
  • Status: offline
2020/02/25 12:56:16 (permalink)
0

PIC18F47K42 IOC keeps interrupting

I'm using the pic18f47k42 with MPLAB X IDE V5.15 and the XC8 v2.05 complier.
 
I'm using a 3.5" touch screen in my application.  I have configured RA3 to be my pen_down interrupt and trigger on a high-to-low transition (see code)
void cf_display_init(){

    LCD_RESETn_LATCH = 1; // preset lcd resetn to 1
    LCD_BL_LATCH = 0; // preset lcd back light off
    LCD_SPI_CS_LATCH = 1; // preset lcd chip select to high
    TOUCH_SPI_CS_LATCH = 1; // preset touch cs to high

    LCD_SPI_CS_TRIS = 0; // SPI LCD chip select set to output
    LCD_RS_DC_TRIS = 0; // SPI LCD data/command pin set as output
    LCD_BL_TRIS = 0; // SPI LCD back light pin set to output
    LCD_RESETn_TRIS = 0; // SPI LCD resetn set to output
    TOUCH_SPI_CS_TRIS = 0; // SPI TOUCH chip select set to output
    PEN_IRQ_TRIS = 1; // touch screen interrupt set to input
    WPUAbits.WPUA3 = 1; // enable weak pullup on pen_irq
    ANSELAbits.ANSELA3 = 0; // make sure touch_irq is digital
    IOCANbits.IOCAN3 = 1; // enable RA3 to interrupt on a high to low transition
    IOCAFbits.IOCAF3 = 0; // clear RA3 interrupt on change
    PIR0bits.IOCIF = 0; // clear interrupt on change bit

 
My code paints the screen then runs till I get a touch.  My problem is that my code gets stuck in an interrupt loop even though the pen_irq is high.  I'm not sure I'm clearing the ioc interrupt but I can't figure out why.  See main code:
    //fills the lcd with a red top gradient to a white middle gradient to a blue bottom
    Fill_mkr_screen();

    // wries a text string to the lcd
    post_temp(95, p_backrest_temp_value);
    post_temp(55, p_desired_temp_value);
    
    // gfx_write_text(const font_t temp_font, uint16_t pos_x, uint16_t pos_y, const char *backrest_temp_string)
    gfx_write_text(p_temp_font, pos_x_br_temp, pos_y_br_temp, p_backrest_temp, 0x7A, 0x0E, 0xCF);
    gfx_write_text(p_temp_font, pos_x_br_temp, br_temp_value_ypos, p_backrest_temp_value, 0x7A, 0x0E, 0xCF);
    
    gfx_write_text(p_temp_font, pos_x_dr_temp, pos_y_dr_temp, p_desired_temp, 0x7A, 0x0E, 0xCF);
    gfx_write_text(p_temp_font, pos_x_dr_temp, dr_temp_value_ypos, p_desired_temp_value, 0x7A, 0x0E, 0xCF);
    
    for(x=55; x<=60; x++){
        __delay_ms(500);
        gfx_write_text(p_temp_font, pos_x_dr_temp, dr_temp_value_ypos, p_temp_blockout, 0xFF, 0xFF, 0xFF);
        post_temp(x, p_desired_temp_value);
        gfx_write_text(p_temp_font, pos_x_dr_temp, dr_temp_value_ypos, p_desired_temp_value, 0x7A, 0x0E, 0xCF);
    }

    temp = temp_lookup(0xA87);
    
    ei();
    
// SPI_touch_config();
// touch_y_position = touch_get_y();
// IOCAFbits.IOCAF3 = 0; // clear RA3 interrupt on change
// PIR0bits.IOCIF = 0; // clear interrupt on change bit
    
    PIE0bits.IOCIE = 1; // enable interrupt on change
    

    while(1){
        
        if(pen_irq){
            SPI_touch_config();
            touch_y_position = touch_get_y();
            pen_irq = 0;
            PIE0bits.IOCIE = 1; // enable interrupt on change

        }

    }
}

/********** INTERRUPT SERVICE ROUTINE **********/

void __interrupt(irq(IOC), high_priority) MyIOCISR(void)
{
    // interrupt on change handler
    if (IOCAFbits.IOCAF3){
        
        PIE0bits.IOCIE = 0; // disable interrupt on change
        pen_irq = 1;
        //PIE
        IOCAFbits.IOCAF3 = 0; // reset the ioc interrupt
        IOCAF = 0;
        PIR0bits.IOCIF = 0; // clear the peripheral interrupt on change bit
        PIR0 = 0;
        //INTCONbits.IOCIF = 0;
    } // interrupt on change handler

} // isr



 
pen_irq definition:
volatile static uint8_t pen_irq = 0;

void main(void) {

 
Am I not clearing the interrupt because I don't see RA3 going low again?
 
#1

16 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3686
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/25 18:11:44 (permalink)
    +1 (1)
    Not sure but don;t forget that the xxIF bit can be set even if the xxIE bit is clear - this is a general situation with most (all?) Microchip MCUs.
    Therefore if ANY other interrupt source triggers the ISR, because you only check for the xxxIF bit, it could well be set.
    With these processors (which share an ISR across multiple interrupt sources), the best rule is to check for both the xxxIF and the xxxIE bits.
    Might not be your problem but worth a check.
    (I don't think it occurs with that MCU, but some of the older MCUs require that you read the port to reset the latch that detects the 'change').
    Susan
    #2
    mpgmike
    Super Member
    • Total Posts : 389
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/25 21:03:18 (permalink)
    +2 (2)
    If in doubt, try using the Vectored Interrupts to eliminate that possibility.

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11681
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/25 21:58:01 (permalink)
    +1 (1)
    It doesn't appear you're clearing the interrupt in the way recommended in the data sheet.
    #4
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/26 06:49:45 (permalink)
    0
    I will recheck the data sheet but I thought I was.  It must be listed in the interrupt controller section?
    #5
    ric
    Super Member
    • Total Posts : 26159
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/26 12:30:04 (permalink)
    +1 (1)
    I think jtemples is referring to EXAMPLE 18-1:CLEARING INTERRUPT FLAGS(PORTA EXAMPLE)
    however, not following that procedure could cause you to miss IOC events, not get extra ones.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    1and0
    Access is Denied
    • Total Posts : 10588
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/27 02:28:28 (permalink)
    0
    ric
    I think jtemples is referring to EXAMPLE 18-1:CLEARING INTERRUPT FLAGS(PORTA EXAMPLE)

    That example in the datasheet shows this
            movlw   0xFF
            xorwf   IOCAF,W
            andwf   IOCAF,F

    and I'm curious why it's not this
            comf    IOCAF,W
            andwf   IOCAF,F

    #7
    ric
    Super Member
    • Total Posts : 26159
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/27 02:53:20 (permalink)
    +2 (2)
    Because it was written by a datasheet donkey, not a forum expert. ;)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 11:40:03 (permalink)
    0
    At the cost of self-deprecation I'm realizing I'm a donkey who is not getting the datasheet.
     
    If in doubt, try using the Vectored Interrupts to eliminate that possibility.

     
    Am I not doing that?  Here's the excerpt of my main code involving the IOC and interrupt handler:
     
    It doesn't appear you're clearing the interrupt in the way recommended in the data sheet.

     
    I feel I'm following Section 18.4
     

        temp = temp_lookup(0xA87);
        IOCIP = 1;
        ei();
        
        
        PIE0bits.IOCIE = 1; // enable interrupt on change
        

        while(1){
            
            if(pen_irq){
                SPI_touch_config();
                touch_y_position = touch_get_y();
                pen_irq = 0;
                PIE0bits.IOCIE = 1; // enable interrupt on change

            }

        }
    }

    /********** INTERRUPT SERVICE ROUTINE **********/

    void __interrupt(irq(IOC), high_priority) MyIOCISR(void)
    {
        // interrupt on change handler
        if (IOCIE && IOCIF){
            
            PIE0bits.IOCIE = 0; // disable interrupt on change
            pen_irq = 1;
            IOCIF = 0;
            IOCAF = 0;
            
        } // interrupt on change handler

    } // isr

     
     
    #9
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 11:56:59 (permalink)
    +1 (1)
    You could start a simple MCC app for this PIC
    Select Vectored Interrupt Enable
    Select Interrupt on change enable
    pin module gpio RA3 input, IOC negative
    Generate the code and have a look at Source files, MCC Generated Files, interrupt_manager.c,   INTERRUPT_Initialize 
    It might provide some clues.
    #10
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 12:02:07 (permalink)
    0
    You could start a simple MCC app for this PIC

    I will try that.  Now to use MCC for the first time...
    Thanks
    #11
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 14:31:14 (permalink)
    0
    Not sure I should start another topic but I can't see the pin configurator in mcc.  I've googled for tutorials and looked up several videos but can't seem to find anything to show how to bring up the pin configuration but can't find anything.
    See screenshot...
     

    Attached Image(s)

    #12
    ric
    Super Member
    • Total Posts : 26159
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 14:36:46 (permalink)
    0
    You are looking at the the "System Module" tab. The "Pin module" tab is next to the left.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 14:58:18 (permalink)
    0
    Yes, so I did click on that but it doesn't show anything. (No Configurable Pins Selected) but I can't seem to show the device nor can I change the package to TQFPP44 package.
    Attached screenshot:
     

    Attached Image(s)

    #14
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 15:30:53 (permalink)
    0
    Time out!
    I just clicked on Pin Management Package View on the right upper side.
    #15
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/02/28 15:36:07 (permalink)
    0
    I spoke too soon.  That didn't help however, after clicking on Pin Manager Grid View at the bottom left I am able to configure the RA3 pin.
     
    #16
    JustRob
    Super Member
    • Total Posts : 492
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 IOC keeps interrupting 2020/03/02 14:56:27 (permalink)
    0
    I found after using the MCC to generate code to interrupt on a high to low signal the only way it resets the IOC flag on RA3 is:
            IOCAFbits.IOCAF3 = 0;
     
     
    Which I have been doing.
     
    I decided to step through my code showing the PIR0 SFR (see jpg attached).  I found that when I set the SPI chip select (TOUCH_SPI_CS_LATCH) low is when the IOCIF bit in PIR0 is set.  See attached schematic.  I don't see why this is happening.  I can include the signal initialization code but it is the same as the MCC code.
     

    Attached Image(s)

    #17
    Jump to:
    © 2020 APG vNext Commercial Version 4.5