How to use an Interrupt to drive a menu on 16f88


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125

    Cool How to use an Interrupt to drive a menu on 16f88

    I am doing resarch on interrupts, and have seen several postings describing various aspects, but it is not quite clear how I add an interrupt routine to a program I have running. I actaully can't form the PBP code to really start this without some structural help on how interrupts work, and how they can branch to a routine that will allow a person to change settings by using a button to select a view, and then letting the routine time-out (ie button not pushed for 5 secs), to jump back to the main code and continue running, but now with our new settings..

    My situation is probably common - I have a program that 'does stuff', in a loop, say reading from a serial port, and writing to an LCD just fine. What I want to do is interrup this with a button, and select some display options using a pushbutton, then have the program jump back and continue running, but now with the different choices I selected in the interrupt routine..

    For example - my current main program loops through automatically displaying 3 different views of the data on the LCD. Example:
    --------------------------------------------------------------
    Loop from 1-3
    Select case displayLCD
    Case 1
    gosub display1
    Case 2
    gosub display2
    Case 3
    gosub display3
    End Select
    --------------------------------------------------------------

    This is fine, but I want to control which data set gets displayed by pressing a button.. I want to make the RB4 pin an interrupt so that If button press/interrupt then goto showmenu....

    I *think* my program kinda wants to do this:

    on interrupt on RB4 showmenu

    Main:
    gosub collect data
    gosub display
    goto main

    display:
    ' show whatever view was selected by the button interrupt
    gosub display(x) - ' somehow make this display one view as selected by the button/interrupt routine..
    return

    ---

    showmenu:
    ' loop through 3 different views, if user times out (5 secs) on one of them, this is the view we want to show in the main program.

    for t= 1 to 3 ' three times you can push the button - then it loops back

    serout to LCD "show first data"
    set display(x) to "1"
    choice=t+1
    ' if it times out while this is displayed, then this is our choice (how?)

    if button pushed again then
    serout to LCD "show second data"
    set display(x) to "2"
    choice=t+1
    ' if it times out while this is displayed, then this is our choice (how?)
    endif

    if button pressed again then
    serout to LCD "show second data"
    set display(x) to "3"
    choice=t+1
    ' if it times out while this is displayed, then this is our choice (how?)

    if choice = 3 then choice =1

    endif
    ---------------------------------------------------------------------

    Can someone help me visualize this closer to PBB code ??

    Tom

  2. #2
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    I'm still learning myself but i hope this works for your need or is a good pointer. This i have test and works on my development board. Each time you push the switch on the Interupt pin, the LEDs on PORTB light in sequence as you push the switch.

    Code:
    ' I/O definition
    '
    TRISB = %10000001   ' Set PORTB (0)INPUT (1-5)OUTPUTS
    
    ' Variable Definition
    '
    Switch              VAR PORTB.0
    PushHowManyTimes    VAR Byte
    
    ' register and interrupt definition
    '
    CMCON = 7          ' Disable analog comparator on PORTA... not needed as now
    OPTION_REG=0       ' enable pull-up resistor on PORTb
                       ' Interrupt on falling edge of RB0
    INTCON = %10010000 ' Enable RB0 interrupt
    
    On Interrupt Goto ProcedureSwitcher
    
    ' Variable initialisation
    '
    PORTB=0             ' Reset all outputs on PORTB
    PushHowManyTimes=0
    
    'Main Procedure
    '
    MainProcedure:
          Select Case PushHowManyTimes
                 case 1
                      Gosub Routine_1 
                 Case 2
                      Gosub Routine_2
                 Case 3
                      Gosub Routine_3
                 Case 4
                      Gosub Routine_4
                 Case 5
                      Gosub Routine_5
                 End Select
    Goto Mainprocedure
    
    Routine_1:
        High 1
    Return
    
    Routine_2:
        High 2
    Return
    
    Routine_3:
        High 3
    Return
    
    Routine_4:
        High 4
    Return
    
    Routine_5:
        High 5
    Return
    
    ' Interrupt handler stuff here
    '
    Disable                                     ' Disable interrupts in handler
    ProcedureSwitcher:
        PushHowManytimes=PushHowManytimes+1     ' Changing task
        If PushHowManytimes=6 Then PushHowManytimes=1
    Here:
        While SWITCH = 0                        ' waiting until
        wend                                    ' push-button is release
        pause 100                               ' debounce time
        If switch=0 then here
        INTCON.1=0                              ' reset RB0 interrupt flag
    Resume                                      ' Return to main program
    
    Enable                                      ' Enable interrupts after
                                                ' handler
    As i say, still learning myself, but willing to help where i can !!

    Regards,

    Steve

  3. #3
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Thumbs up

    This is EXACTLY what I was looking for.... I just need to know if I can use RB4 instead of RB0.. and where to find the % mapping for that!!!

    Tom

  4. #4
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Tom,

    As far as i'm aware (without looking at the Data Sheet for your PIC), this PIC has only one inturrupt line, RB0. RB4 is used for MCLR which is a RESET line.

    To be sure go to Microchips WEB site and get the latest Datasheet.

    Why don't you want to use RB0?

    Hope this helps,

    Steve

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    you probably have Interrupt on PORTB change just check the PORTB.4 status in the interrput handler and you should be in business.

    Just be careful when you program your PIC.. disable MCLR to make it RB4
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default

    Ahhh! OK! I thought interrupts were things you could assign to any pin, that is all. I was looking to closely at this to notice that RB0 had an assigned function 'INT'..

    I have a proto board made up that runs rb4 and rb5 off to a post for easily attaching pins to - the reason I wanted to use RB4.. I can tie off to RB0 but it is a little soldering now, that's all. Btw, RB0 is pin #6 on the F88. Pin 4 is MCLR.

    One other thing - 'enable pull up resistor' - do I need to tie a 1K ohm resistor from VDD to the pin also, or is this a software switch?

    Tom

  7. #7
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    Ah, now i have learnt something !!

    Steve, so in the example code i have posted, what part needs to be changed to reflect the different PORT for the Inturrupt.

    Cheers,

    Steve

  8. #8
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Question

    So.... Is there some way to listen for interrups on RB4? Or is the chip designed to have interrupts come in vai RB0?

    Thanks,
    Tom

Similar Threads

  1. 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
  2. 16f88 interrupt
    By tur_bot in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 2nd April 2008, 11:35
  3. 16F88 Interrupt
    By sskeet in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th January 2006, 15:51
  4. Interrupt Menu System Example and Help Needed
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st April 2005, 17:05
  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