NEWBIE: Some basic questions using interrupts


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2006
    Posts
    21

    Default NEWBIE: Some basic questions using interrupts

    I am trying to play with the interrupt function on the 16F88. I have read Section 15 of the data sheet, but most of it is above my skill level. I understand the basic concept of the interrupt, but don’t understand the notations INTCON<7>, PORTB<:4>.

    I wrote a simple interrupt routine (rising edge, I think) below, but have been unable to reset the interrupt flag as discussed in several articles with statements like INTCON0.0, INTCON1.0. I don’t understand these 2 statements, but they were suggested in other posts.

    I was able to reset the flag with INTCON = %10010000, but one post suggests that this is not the correct way, and it can lead to problems.

    Also, in all of the posts (and the help file) there is the interrupt routine, the RESUME statement, then the ENABLE INTERRUPT statement. How does the enable statement ever fire if the resume comes first? Shouldn’t the interrupt be enables before the resume?

    Can someone give me a simple explanation of working with the interrupts, a way to reset the flag and where to put the enable statement?

    Thanks

    --------------------------------------------------------------
    This code works, but not sure if it is the best way to do it

    OSCCON = $60

    @ DEVICE INTRC_OSC_NOCLKOUT, MCLR_OFF, PROTECT_OFF, WDT_OFF
    @ DEVICE PIC16F88


    CMCON = 7 ' Comparators OFF
    ANSEL = 0 ' A/D OFF -- Port pins all digital
    INTCON = %10010000 ' Enable RB0 interrupt

    ENABLE INTERRUPT

    Output PORTA.0
    Output PORTA.1


    MainLED VAR PORTA.3
    InterruptLED VAR PORTA.4
    cntr Var Word

    Low MainLED
    Low InterruptLED

    Loop:
    On Interrupt goto Int

    If cntr=2000 then
    Toggle MainLED
    cntr=0
    EndIF

    cntr=cntr+1

    Goto Loop

    Int:
    Disable INTERRUPT

    Toggle InterruptLED
    PORTB=%00000000 ' Clear interrupt flag
    Enable INTERRUPT

    RESUME

  2. #2
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Hope this helps.

    Hi,

    As far as my skill goes, I would like to explain the basics of interrupt. If I go wrong somewhere I would like the gurus to correct me.I am a 100% self-taught so do not expect much.

    Basics of the basics :

    If global interrupt is enable and the processor is interrupted (by any external or internal interrupt sources) it jumps to a particular program memory address called the interrupt vector and starts executing the code thereafter. This code is called ISR (interrupt service routine). Please note there can be multiple interrupts enabled so it is the duty of the programmer to take necessary actions by polling the hardware interrupt flags and resetting them too.After you have done your ISR job you can fire the retfie (return from interrupt) instruction. It turns on your Global Interrupt Flag again and throws you back to where you were executing your normal code. Now the normal code was between some task and probably was using the W (working), S (Status) registers. These do get change change in your ISR if you did something more than just set/clear a port. So it is a good idea to save these just as you enter the ISR and restore them before you quit. Other important registers are the bank select / FSR and the PCLATH (sometimes). One important thing to note is that if the global interrupt bit is not turned on, the program does not jump to the ISR but the hardware interrupt flags remain keep setting up. So if you have plenty of time you can just poll this in your regular code and take necessary action. Now which interrupts you want to use and how are setup by the INTCON (Interrupt Control) and other peripheral interrupt registers.

    PBP and interrupt :

    Now PBP handles the interrupt a little lazily. Whenever an interrupt occurs it just flags it in software and continues (till done) whatever it was doing and then goes to the basic interrupt handler (setup by the define). It then starts executing the basic code from there and when it finds a resume goes back to where it came from. Note the PBP interrupt handler is not at the processor interrupt vector. This means if you were in middle of a pause 2000 statement and an interrupt occured, PBP would complete the pause 2000 statement and then go to the basic interrupt handler. Thus it is unpredictable and slow. However it does make life easy by taking care of the context saving (sensitive registers) automatically. Now PBP itself checks the interrupt status by placing a call to its internal interrupt check routine. This call does take code space and time (however small). It flags the interrupt in software (its own working variable) and turns back the Global bit to 1 again. If you turn-off this bit in your main program PBP would falsely enter into an interrupt condition. So it is recommended in the manual to set INTCON = $80 while entering ISR. That is GIE enable all other reset.
    Now the "Enable and Disable" commands are just compiler directives. It instructs the compiler where to put these calls and where not. For example would not like PBP to check for interrupt when you are already inside you basic interrupt routine. So a disable just before this routine would direct the compiler not to place the call. After you have finsihed the ISR you would like to enable it so the enable after the basic ISR. This also mean that you can control your program flow when and when not to be interrupted.

    So while choosing interrupt in my progs (BTW this is not my hobby I do it for my living) , I consider the following.

    1. Do I really need interrupts ? Is it possible to poll the hardware flags and manage it ?

    2. Is the latency of PBP interrupt acceptable, (no fast action than my slowest bit of code) ?

    If no.2 is no, I always write my ISR in assembly.

    Things to keep in mind and what I practice.

    1. Keep the PBP ISR on top of the main program loop.(Just after initiating variables and ports) and use a goto just before it to skip around it.

    2. Make sure that the interrupts are re-enabled when existing the PBP ISR by a resume.

    3. Keep tasks in little chunks. For eg. doing a repeat/until increment style pause with minimum chunks. (Rather than pausing 2000mS do it 100 times in steps of 20mS)

    This post would be incomplete without a mention of Darel Taylor's instant interrupt in basic. Search for it in this forum and appreciate the nice and hard work Darel has done to make our life easy.

    I personally handle all my interrupts in asm but PBP gave me the courage and insight to use interrupts effectively.So no big deal.

    Looking forward to critics comments..

    Regards

    Sougata

  3. #3
    Join Date
    Feb 2006
    Posts
    21


    Did you find this post helpful? Yes | No

    Default

    Thanks for your post. I found the information interesting. I did have a look at Darel Taylor's instant interrupt, but think I need to get more basics worked out before progressing to his code.

    I still am unclear about my original questions:
    1. How do you read this type of notation: INTCON<7>, PORTB<:4>?
    2. How do I reset the interrupt flag for RB0?
    3. If the program is executed sequentially, how does an 'Enable Interrupt' AFTER a resume statement ever get executed? Shouldn't the Disable be the first line of the ISR, and the Enable be the last line BEFORE the resume?

    Thanks again.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    sougata,

    Thanks for all the advertising of Instant Interrupts. I hope I don't have to pay you.



    Jack,

    The Instant Interrupts are really pretty easy, but you are correct. Understanding how interrupts work first is a good idea.

    1. INTCON<7>, PORTB<:4>?
    This is just a way of showing the BIT's used in a register. INTCON<7> just means BIT number 7 within the INTCON register. It's the same thing as INTCON.7 in PBP.

    PORTB<:4> probably had another number before the colon, like PORTB<7:4>. This shows a range of bits from PORTB.4 to PORTB.7 including 5 and 6.
    2. How do I reset the interrupt flag for RB0?
    INTCON.1 = 0
    3. If the program is executed sequentially, how does an 'Enable Interrupt' AFTER a resume statement ever get executed? Shouldn't the Disable be the first line of the ISR, and the Enable be the last line BEFORE the resume?
    The Enables and Disables are handled at Compile Time, and aren't executed at Run Time.

    From the beginning of the program, or at the point of an ENABLE, PBP starts putting additional code in-between Each and Every line of code in your program that checks to see if an interrupt has occured. Then when it encounters a DISABLE, it's stops putting the checks in. There can be many ENABLES/DISABLES in the program, and only the areas that are ENABLEd can respond to an interrupt.
    Last edited by Darrel Taylor; - 7th March 2006 at 21:01. Reason: beginning of the program
    DT

  5. #5
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Darrel I always mispell your name

    Hi Darrel,

    I am a fan of your instant interrupts myself and found it at a later stage when I have wasted so much energy in writing ISR codes in asm. So letting people know your good work becomes a duty. Most of my developments recently are centered around PIC18F452 and I use both low and high priority in my designs. Hope to accumulate knowledge to be able to contribute to your work. I have walked the path and this forum is a boon for newbies and pros. It is me (us) who should thank you for the great job that you have done and carrying on.

    Jack don't forget to visit http://www.pbpgroup.com

    Regards

    Sougata

  6. #6
    Join Date
    Feb 2006
    Posts
    21


    Did you find this post helpful? Yes | No

    Default

    Thank you. Now I understand. Just got my code working great.

    Thanks again for the education.

Similar Threads

  1. Quadrature encoder and ASM Interrupts. questions..
    By godfodder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th March 2013, 14:45
  2. interrupts - basic info needed
    By malc-c in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th December 2009, 23:06
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Instant Interrupts Questions
    By JosueCas in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 4th April 2006, 15:54
  5. Instant interrupts in Basic?
    By Bulman in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 6th January 2006, 11:53

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