Interrupt on PORTB.....


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005
    Posts
    23

    Default Interrupt on PORTB.....

    Please,
    in interrupt handler how do you understand which pin of PORTB (RB7:RB4)changed its state and caused the interrupt ?

    Int_Handler:
    If INTCON.0=1 then

    ' here i must understand which one..

    INTCON.0=0
    resume.....

    Thanks

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


    Did you find this post helpful? Yes | No

    Default

    Basically you DON'T KNOW which one caused the interrupt.

    The only way is to first read the state of RB7:RB4 bits into a variable, then Reset the RB Port Change Interrupt Flag Bit in the INTCON register. You may need to do this at the start of your program, and thereafter each time you reset the RBIF Flag in your Interrupt Service Routine. The next time when the RBIF flag trips, compare the current state of RB7:RB4 against what you had previously read.

  3. #3
    Join Date
    Oct 2004
    Location
    Fayetteville Arkansas
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    I know this is a very basic question and I have gone thru the data sheet for a 16f676 trying to figure out how to do what you were telling robert to do. could you give a code example? I actually found this thread looking for how to do it.

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


    Did you find this post helpful? Yes | No

    Default

    Well the 16F676 hasn't got a PortB, so you're going to be looking for a long time for an Interrupt on that Port. But this chip has an Interrupt on PortA (Datasheet section 2.2.2.3, 9.4.1 and 9.4.3).

    Let's assume in this instance PortA is configured for Input... somewhere at the start when we initialise our interrupts, we first have to clear the interrupt Flag RAIF, so at that time read the contents of PortA to be used for later comparative purposes...

    OldPortA=PortA
    INTCON=10001000
    On Interrupt goto InterruptServiceRoutine
    ENABLE

    Compare my Register settings above with 2.2.2.3 and with the Interrupt Logic Diagram at Figure 9-10.

    Now we go and do whatever we're going to do in our code... when an interrupt happens, in our Interrupt Service Routine (ISR) we first need to know if PortA caused it... if it's the ONLY interrupt configured in our PIC (ie other peripherals like the Timer, Comparators etc are not configured for interrupt), this IF INTCON.0 THEN (below) can be omitted...

    DISABLE
    If INTCON.0=1 then

    We now need to read the content of PortA and determine which pin caused the interrupt...

    NewPortA=PortA
    If NewPortA.0<>OldPortA.0 then goto PortA0CausedInterrupt
    If NewPortA.1<>OldPortA.1 then goto PortA1CausedInterrupt
    If etc etc...

    and finally when all is done, just prior to exiting our ISR, remember to reset your PortA Interrupt Flag and Read the Port once more...

    OldPortA=PortA
    INTCON.0=0
    RESUME
    ENABLE

    The description is a little crude just as an example, but you'll get the idea of the kind of things you need to do. If further Interrupts could happen whilst you are in your ISR, then your code needs to be a little more recursive, but that's a topic for another time...

    Example in a typical Rover Robot application... when sensors hit an object, the Rover performs an opposite action to clear from the obstruction...

    Code:
    	DISABLE
    InterruptHandler:
    	If PIR1.3=1 then 		
    		gosub LowBatteryWarning
    		PIR1.3=0
    		endif
    	If INTCON.0=1 then
    		NewPortA=PortA
    		If NewPortA.2<>OldPortA.2 then gosub MotorForward
    		If NewPortA.3<>OldPortA.3 then gosub MotorBackward
    		If NewPortA.4<>OldPortA.4 then gosub TurnLeft
    		If NewPortA.5<>OldPortA.5 then gosub TurnRight
    		OldPortA=PortA
    		INTCON.0=0
    		endif
    	RESUME
    	ENABLE

  5. #5
    Join Date
    Oct 2004
    Location
    Fayetteville Arkansas
    Posts
    44


    Did you find this post helpful? Yes | No

    Default Thank You

    Melanie, thank you for the reply. I did understand the 676 just had Port A & C. Sorry for not clarifying the port. This was exactly what I needed and want to thank you for the assistance. I had gotten confused on wheather I had to addres it like you did or in some type of register configuration. Guess I'm just getting old and some of my reasoning has deteriorated. Than you again.

  6. #6
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default Thank you

    Thank you Melanie. Your explaination helped me also because , even i extensively use the F676 in my project work and now shall use your idea for trapping INT's.

Similar Threads

  1. PICs can do more if use others than delays instructions
    By hardcore in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th February 2010, 19:52
  2. shifting problem
    By helmut in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 31st August 2007, 06:11
  3. Output PIC module
    By freelancebee in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th September 2005, 20:10
  4. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29
  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 : 1

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