PDA

View Full Version : How to use an Interrupt to drive a menu on 16f88



Tom Gonser
- 13th March 2005, 19:02
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

Tissy
- 13th March 2005, 20:45
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.



' 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

Tom Gonser
- 14th March 2005, 04:30
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

Tissy
- 14th March 2005, 08:03
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

mister_e
- 14th March 2005, 08:10
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

Tom Gonser
- 14th March 2005, 12:43
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

Tissy
- 14th March 2005, 16:07
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

Tom Gonser
- 15th March 2005, 01:16
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