Interrupts Revisited


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Mar 2004
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Bruce

    Let me take one step back.
    Here is the code I used originally to test my PCB
    You said I was clearing the RB0 flag which I agree with but unless I clear this the program doesn't work!!
    If I use the INTCON.0 = 0 the code doesn't work but using INTCON.1=0 makes the program work.
    I'm very confused and going round in circles as what I would expect to work doesn't. Do I need to enable the Global interrupt GIE INTCON.7=1 before exiting the ISR?

    I've reread the 16f877 data sheet and what I'm doing shouldn't work, but it does.

    Rob



    ***************************************

    DEFINE LOADER_USED 1 'Boot Loader
    'Define inputs held low by external resistors
    sw1 VAR PORTB.6
    sw2 VAR PORTB.4
    SW3 VAR PORTB.0
    ' Define the pins that are connected to LEDs
    led1 VAR PORTC.0
    led2 VAR PORTC.1

    INTCON.3 = 1 ' Enable the RB port change interrupt
    OPTION_REG = $7f ' Enable PORTB pull-ups
    TRISC = %00000011 ' Set PORTB.0-2 (LEDs) to output, 3-7 to input
    on interrupt goto ISR

    main: ' main program begins here


    ' Check any button pressed to toggle on LED


    NAP 7 ' Go to sleep. When the watchdog is
    ' disabled, NAP won't wake up until
    ' an interrupt occurs.

    GoTo main ' Do it again upon waking
    DISABLE
    :ISR
    IF sw1 = 1 Then
    high LED1
    else
    low led1
    endif

    IF sw2 = 1 Then
    HIGH led2
    else
    low led2
    endif

    IF sw3 = 1 Then
    HIGH led1
    HIGH led2
    else
    low led1
    low led2
    endif
    '***HERE*************
    'INTCON.0 = 0 ' Clear the RB port change flag bit( this doesn't)
    INTCON.1 = 0 ' Clear interrupt flag (this works)

    RESUME
    ENABLE

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    INTCON.3 = 1 turns ON RB Port Change Interrupt Enable bit. This means any change on RB7, 6, 5 or 4 will generate the interrupt.

    There are a few things to watch out for when using this.

    1. Set the upper 4-bits up as inputs. TRISB = %11110000 (assumes only top 4 are used as inputs) change as required. And make sure these inputs are not floating by using internal or external pull-ups - or external pull-downs depending on your switch open/close logic.

    2. Do a "read" of the port before turning ON RB int change. Dummy = PORTB.
    Just take a snap-shot of portb inputs before turning the option on.

    3. INTCON.3 = 1 Now any change from the last value read on RB7,6,5 or 4 will cause the interrupt flag bit INTCON.0 to be set (RBIF: RB Port Change Interrupt Flag bit)

    Now, before exiting your ISR, you need to read portb again, then clear the flag bit associated with RB int change.

    Dummy = portb ' end missmatch
    INTCON.0 = 0 ' clear interrupt flag bit before re-enabling interrupts
    RESUME
    ENABLE

    Some other things to consider.

    You're testing upper portb pins in your ISR. This actually reads the port, and will end the missmatch, BUT, your program is going to run much faster than than anyone can operate a push-button switch, and you will have switch bounce.

    What can happen is you enable RB int on change, someone releases the switch, and you have the interrupt "missmatch" condition all over again.

    You might want to include some code that recognizes the keypress, and then waits for the user to release the key before reading the port, and re-enabling the interrupt on change. You can turn ON the LED immediately, but wait for the key to be released before reading the port & re-enabling int on change.

    Also, you're turning ON internal pull-ups, and you're testing input pins for logic 1 with IF SW1 = 1 THEN do something.

    What logic level does a switch press apply to your switch inputs?

    Do I need to enable the Global interrupt GIE INTCON.7=1 before exiting the ISR?
    Definitely not. Exiting the interrupt with RESUME, ENABLE handles everything for you. Using ON INTERRUPT you never need to mess with GIE unless you want to totally disable interrupts. In that case write INTCON = $80 like shown in your manual.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Jul 2005
    Posts
    65


    Did you find this post helpful? Yes | No

    Smile Here is what you can do...

    Hello

    Good answer bruce, really good one

    Any way, to solve your problem, here is what you have to do, as it worked for me:

    1. Read portb.
    2. Clear the GIE and portb on change IE bit.
    3. Pause for, say 100 to 150 ms, to ensure that the user has releasd the button.
    4. Do what you want to do. Use the first value you read for portb.
    5. Now set RB on change IE bit.
    6. Then read portb again, to clear any "may be" pending interrupts, don't try to read portb before enabling RB IE bit, as this is certainly will not be sufficent.
    7. Resume will enable GIE bit.

    I wish I had that code, I formatted the C partition, and forgot to move some files, that was one of them

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Download AN552 (Implementing Wake-up on Key Stroke) from Microchip, and ye will see the light....;o}
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Mar 2004
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    The inputs are from brake, left and right indicators etc. so the inputs can be very short pulses if someone touches the brake for a fraction of a second hence why I was using the on change interrupts to catch all the inputs.
    The inputs are all held low by resistors (not high as i put before).

    I'll try the answer that bruce & Co have offered and see if i can find the answer to my problems.

    Thanks for the support guys it's kept me sane


    Rob

  6. #6
    Join Date
    Jul 2005
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    Hello Rob

    Its recommended to have the port pins pulled up, as this will save you long hours of code debuging to find out why your PIC is seeing a brake pushed, while its not indeed.


    Regards

Similar Threads

  1. Instant Interrupts - Revisited
    By Darrel Taylor in forum Code Examples
    Replies: 772
    Last Post: - 17th February 2016, 22:14
  2. Clock using Instant Interrupts
    By PICpocket in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th February 2009, 21:43
  3. Microcode studio - PIC16F877A Interrupts
    By mcbeasleyjr in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th January 2009, 06:10
  4. DT's Instant Interrupts trouble
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th November 2008, 20:48
  5. help: TMR0 interrupts disabling PORTAchange interrupts???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th August 2008, 15:10

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts