I need help with Interrupt


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30

    Default I need help with Interrupt

    Hello to all. I have taken the plunge and tried interrupt for the first time and I thought I understood it but I have stumbled in this simple code and would like someone to show me where I am going wrong. The interrupt works but when I added the LCDOUT command in the main loop things go wrong. I want to see how many times the button is pressed. The first time I press the button connected on RB0 the led on RB7 flashes. The second time it does not work. My understanding of interrupt is that it jumps to the ISR and then jumps back to the main loop and continues. Why would adding the LCDOUT command in the main loop cause the interrupt not work after the first time an interrupt occurs? When the button is pressed the LED does not flash indicating that it did not leave the ISR or that it is not seeing the interrupt occur. Any help would appreciated.

    16F877A
    MicroCode Studio Plus


    ' External Interrupt example
    ' PortB pin 7 Turn LED on
    ' Interrupt on Portb.0 (INTE) turns LED off
    ' Program waits .3 secs and turns LED back on


    'LCD Parameters
    '==============
    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTD
    Define LCD_RSBIT 2
    Define LCD_EREG PORTD
    Define LCD_EBIT 3
    DEFINE LCD_LINES 2 'Number lines on LCD

    define OSC 20



    led var PORTB.7 'Establish name for portb.7
    counter var byte

    OPTION_REG = %01111111 'Enable portb pullups and
    'rising edge on B0 interrupt
    On interrupt goto myint 'Define interrupt handler
    INTCON = %10010000 'Enable INTE interrupt
    counter = 0
    '**** Main Loop ***
    loop:
    lcdout $fe,2 'return to beginning of line
    lcdout "Count = ",# counter 'display count value
    high led 'turn led on
    goto loop 'Do it forever

    '*****Interrupt Handler *****
    Disable 'No interrupts pass this point
    myint:
    low led 'if we get here turn led off
    Pause 300 'wait .3 seconds
    counter = counter + 1
    INTCON.1 = 0 'clear interrupt flag
    resume 'return to main program
    enable

    end

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    This line is wrong.
    lcdout "Count = ",# counter 'display count value
    Should be.
    Code:
    lcdout $FE,"Count = ",# counter 'display count value
    Might cause the program to hang?
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    Thanks for the correction. I was hoping that was the culprit but it has not corrected the problem. Strange!!
    Thanks again

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hello Wildbilly,
    Data sheet says Port B 4-7, interrupt on change. Port B0
    should interrupt when taken low assuming pullups on ( I think).
    Even though data sheet says POR and BOR TrisB will reset as 11111111
    I think I would add tris statement early in code to advise PIC it is an input.
    Have you tried interrupt on any of ports b 4-7 ?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I just tested you code and every time PORTB.0 is connected to 0 volts(ground) the LED blinks and the LCD counts.

    Here is what I have with a couple small changes. Look at comments.
    Code:
    DEFINE OSC 20
    
    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTD
    Define LCD_RSBIT 2
    Define LCD_EREG PORTD
    Define LCD_EBIT 3
    DEFINE LCD_LINES 2 'Number lines on LCD
    
    TRISB.0 = 1
    
    led var PORTB.5 'CHANGED BECAUSE ICSP
    counter var byte
    
    OPTION_REG = %01111111 'Enable portb pullups and
    'rising edge on B0 interrupt
    On interrupt goto myint 'Define interrupt handler
    INTCON = %10010000 'Enable INTE interrupt
    counter = 0
    '**** Main Loop ***
    loop:
    lcdout $fe,1 'CLEAR SCREEN
    lcdout "Count=",# counter 'display count value
    pause 50  'TIME TO DISPLAY
    high led 'turn led on
    goto loop 'Do it forever
    
    '*****Interrupt Handler *****
    Disable 'No interrupts pass this point
    myint:
    low led 'if we get here turn led off
    Pause 300 'wait .3 seconds
    counter = counter + 1
    INTCON.1 = 0 'clear interrupt flag
    resume 'return to main program
    enable
    
    end
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    Hi Dave thanks for your help. I have tried your suggestions and still have the same problem. I even copied your code directly and still no fix. You said you have it counting. I can only get it to count to one and then it stops. You said when you pushed the button to ground, it would count. Did you have it counting pass one? Another thing is the Option_Reg bit 6 is set for "Rising edge". You said you pushed the button low. It should not work if the bit is set for rising edge.
    I guess I should also disclose that I am using the "EasyPic4 Development Board". I wonder if that is my problem.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    If you want it to interrupt when RB0 goes high, comment out the OPTION_REG line and use external resistor from RB0 to ground.

    With the OPTION_REG set as you have it "pull-up""set" the pin will need to go LOW to trigger the interrupt. No external resistor.
    From data sheet.
    External interrupt on the RB0/INT pin is edge triggered,
    either rising, if bit INTEDG (OPTION_REG<6>) is set,
    or falling, if the INTEDG bit is clear.
    I counted to 255 then of course it rolls back to 0 and starts over.
    I guess I should also disclose that I am using the "EasyPic4 Development Board". I wonder if that is my problem.
    Not sure, never used the "EasyPic".

    I am using Ubuntu (linux) for an OS, running MPLAB with PBP as the language set via Wine hooked to a PICSTART PLUS rigged as an ICSP.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    Thank for you suggestion JOE.S I will have to try the other Inputs as well, 4-7. Thank you to mackrackit. I have found the problem. There is a jumper on the board that was set for "Pull down" and I had the "Pull up" set in Option_Reg. They were fighting each other. I disabled the Option_Reg and it works very well. Thanks very much for your input. All that you said was spot on.

    This is fun.
    Wildbilly.

  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wildbilly View Post
    This is fun.
    Wildbilly.
    That is all that matters!
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 18:14
  4. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 02:59
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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