• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

interrupt help urgent!

Status
Not open for further replies.

electricano

New Member
i am building a system for my project, which has 4 switches attached to it. these switches are connected to different pins.when these switches is pressed, a signal (a high pulse) is sent to the pin, the PIC (i use PIC16F877) must perform specific task immediately. what and how should i do with the software part?
i am thinking to use the interrupt but the PIC has only one pin for external interrupt (RB0), and i am now got stuck in this part.

if u know, plz post ur suggestions here.it is really urgent (for my projet).plz help me !
 

Exo

Active Member
Connect your 4 switches to their respective pins. Add a 4 input OR-gate wich gets an input from each switch and outputs to RB0.

IF you press one or more switches, RB0 will get a signal from the OR-gate, wich calls your interrupt on RB0 routine. The routine can then check the input pins to see wich buttons were pressed
 

electricano

New Member
can i connect the 4 switches to any 4 pins such as RB1-RB4?and then parallel-connect it to a OR gate and feed it to the RB0?

how does it really work?can the PIC detect which button is pressed with the connection?

i am using PIC basic pro since i do not know how to program in assembly language. i just learnt it few weeks ago. so how should i write the code of interrupt handler for all the swithces?

i know that there is a command in PIC basic pro called 'ON INTERRUPT GOTO label'.how should i make use of the command to write the code for them?each switch will perform diferent task.can u show me example?plz.

or using other commands?
 

ivancho

New Member
What exo said was to OR the signals.... so if you have 4 switches OR together then when ever any of the 4 switched is pressed then you are going to have your interrupt signal..... but this way you will not be able to know what signal was pressed.

To solve it occurs to me 2 things:
1. Connect the switches to the OR chips and OR them together, and at the same time connect them into 4 pins on the PIC. So that when you have a interrupt signal, on your interrupt handler you check the port where the Swithes are connected to.

2. The other option is to use the Change in portB interrupt. According to the datasheet:
An input change on PORTB<7:4> sets flag bit RBIF
(INTCON<0>). The interrupt can be enabled/disabled
by setting/clearing enable bit RBIE (INTCON<4>)
(Section 3.2).
But this will allow you only 3 switches to cause an interrupt so you still will need to OR 2 swithces. [CORRECTION: THAT IS 4 PINS SO NO NEED FOR OR CHIP]

This is a proven working code. Study it and make it work for your application

Code:
' On Interrupt - Interrupts in BASIC
'  Turn LED on.  Interrupt on PORTB.0 (INTE) turns LED off.
'  Program waits .5 seconds and turns LED back on.

led     var     PORTB.7


        OPTION_REG = $7f        ' Enable PORTB pullups

        On Interrupt GoTo myint ' Define interrupt handler
        INTCON = $90            ' Enable INTE interrupt

Loop:   High led                ' Turn LED on
        Goto loop               ' Do it forever


' Interrupt handler
        Disable                 ' No interrupts past this point
myint:  Low led                 ' If we get here, turn LED off
        Pause 500               ' Wait .5 seconds
        INTCON 0.1 = 0          ' Clear interrupt flag
        Resume                  ' Return to main program
        Enable
Huge problem with the ON interrupt command is that it puts an instruction after every command to chek the flag. THis might cause a problem with long codes. You may want to do a mix of assembler and PBP commands if this affects you.

Good Luck

Ivancho
 

electricano

New Member
(1)i am using push buttons as a switch.when i press and release a button, the signal will go into the PIC pin.is it possible for the PIC to detect the 'pulse'?

(2)i think your 1st solution is quite similar to exo's. but how does actually the interrupt handler determine which button was pressed.does it requires any command? or it does it automatically?for exxample if i press button A to light the LED, how can it determine that button A is pressed?

(3)i understand the program.but it is only for one interrupt.since i have multiple interrupts (i have 4 swithces,each switch with different functions), how do i actually write the code so that when i press switch A,it does the task A, and when i press switch B,it does the task B.
 

ivancho

New Member
IF you OR them together you will not know wht switch was pressed you will only know that one switch was pressed.

That is why I said that you would connect the signal to the OR chip and at the same time to the PORTB7-4 and when an interrupt occurs you check the status of the PortB7-4 pins and do accordingly.

I would like to correct myself... there is an interrupt that will be generated when a change in status occurs in any of the pins in PortB.7 to Portb4..... that is 4 pins, where you can connect you swithes. If you pull them high and when presses they connect low, then that change will generate an interrupt.

For the actual code, I would leave it to you to figure it out, once you have soemthing post it and we can work around it. :roll:

Good Luck

Ivancho
 

electricano

New Member
thank u ivancho. i think i start to understand wat u tried to tell me.
i still ahve doubt:
the port b has weak internal pulllup when configured as input, do i still need an external pullup?
 

blibala

New Member
i have question
[' On Interrupt - Interrupts in BASIC
' Turn LED on. Interrupt on PORTB.0 (INTE) turns LED off.
' Program waits .5 seconds and turns LED back on.

led var PORTB.7


OPTION_REG = $7f ' Enable PORTB pullups

On Interrupt GoTo myint ' Define interrupt handler
INTCON = $90 ' Enable INTE interrupt

Loop: High led ' Turn LED on
Goto loop ' Do it forever


' Interrupt handler
Disable ' No interrupts past this point
myint: Low led ' If we get here, turn LED off
Pause 500 ' Wait .5 seconds
INTCON 0.1 = 0 ' Clear interrupt flag
Resume ' Return to main program
Enable]

what is the meaning of 'INTCON 0.1 = 0 '? is it used to clear the bit0 and bit1 in the INTCON resigster?[/quote]
 

Exo

Active Member
I meant something like on the attached scematic...
When one or more buttons are pressed, RB0 will get a signal trough the OR-gate and the interrupt routine will be called.

The interrupt routine must then check the 4 I/O pins where the switches are connected to determine wich ones were pressed.

This scematic is not correct (something i had already drawn) but you'll get the picture of what i mean
 

crust

Member
electricano said:
the port b has weak internal pulllup when configured as input, do i still need an external pullup?
You can certainly add an external pull-up, but I doubt that you need it. I generally do not use any external pull-ups except on the reset line of my micro circuits.
 

electricano

New Member
[Four of PORTB’s pins, RB7:RB4, have an interrupt on change feature. Only pins configured as
inputs can cause this interrupt to occur (i.e. any RB7:RB4 pin configured as an output is excluded
from the interrupt on change comparison). The input pins (of RB7:RB4) are compared with the
old value latched on the last read of PORTB. The “mismatch” outputs of RB7:RB4 are OR’ed
together to generate the RB Port Change Interrupt with flag bit RBIF (INTCON<0>).
This interrupt can wake the device from SLEEP. The user, in the interrupt service routine, can
clear the interrupt in the following manner:
a) Any read or write of PORTB. This will end the mismatch condition.
b) Clear flag bit RBIF.
A mismatch condition will continue to set flag bit RBIF. Reading PORTB will end the mismatch
condition, and allow flag bit RBIF to be cleared.]

can anyone explain to me the meaning of the following sentence?
(1)The input pins (of RB7:RB4) are compared with the
old value latched on the last read of PORTB. what is the meaning of the 'Last Read'?

(2)'Any read or write of PORTB. This will end the mismatch condition'.
what is 'read or write' mean?is it when i configure the pin as an input (using TRIS register) by writing the register a binary 1,that is called write?
and when i use the command (PORT<x>=<?>, that is called read?[/quote]
 

ivancho

New Member
Let me see if I can address some of your questions
the port b has weak internal pulllup when configured as input, do i still need an external pullup?
You aren't going to need a pullup resistor if they are enable.
(1)The input pins (of RB7:RB4) are compared with the
old value latched on the last read of PORTB. what is the meaning of the 'Last Read'?
For this part you will need to follow me to Fig 3-4 on the PIC16F877 datasheet CK DIAGRAM OF
RB7:RB4 PINS
You can follow what happens at each pin in RB7-RB4. To read is to move data into the PortB register. Unless you make a command to read the PortB you will never know what is there. You can read ports by assiging them to a variable or by reading the actual port like this

Switch1 = PortB.1
If PortB4 = 1 then ....
Switches = PortB

What ever it was that you read stays latched "remembered" to compare against the chages in port. That is why the XOR gate with the two D-flip flops as inputs. (Fig 3-4)
2)'Any read or write of PORTB. This will end the mismatch condition'.
what is 'read or write' mean?is it when i configure the pin as an input (using TRIS register) by writing the register a binary 1,that is called write?
and when i use the command (PORT<x>=<?>, that is called read?
You read an Input..... and write to an output. You use the TRIS register to make a pin an input or an output. And you use the PortB to either read the pins or write to the pins. So if once you get the interrupt you read the PortB register that will clear the mismatch (since the pins read became latch) and therefore clears the interrupt.


Blibala: The 'INTCON 0.1 is so you choose which one bit you want clear. :wink:

Check the attached files.

Hope it helps some...
Ivancho
 

Attachments

Louis13

New Member
Could anyone shed some light on the following idea of linking each interrupt source to RB0 via a diode? Is there a way to achieve this? It's just that I need to link 20 interrupt inputs to RB0 and do not want to have to install many or gate chips to achieve this. Any suggestions welcome. Regards
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top