Hot!A question about interrupts.

Page: 12345.. > >> Showing page 1 of 7
Author
CVRIV
Junior Member
  • Total Posts : 71
  • Reward points : 0
  • Joined: 2017/09/06 13:49:25
  • Location: 0
  • Status: offline
2017/10/06 02:27:13 (permalink)
0

A question about interrupts.

So I have this assignment to do that was given to me by my professor. I read the assignment and then went to the datasheet for the PIC16F883, which is the MCU we're using. Reading his assignment, he said that the TMR0 interrupt is linked to program memory location 0x04 and that I have assign a GOTO for that location. Fine, but where in the data sheet is this mentioned? Also, what if I want to use another interrupt, maybe the PORTB interrupt? Where do I place a GOTO for that interrupt?
 
Im thinking maybe I'm to place a GOTO, to some general interrupt subroutine, at 0x04 and in that subroutine I'm to check for all the interrupt flags? 
 
Where in the datasheet do it mention this? I checked the TMR0 section and the PORTB section and it doesn't mention any of that. 
#1

125 Replies Related Threads

    vloki
    always on da run
    • Total Posts : 6645
    • Reward points : 0
    • Joined: 2007/10/15 00:51:49
    • Location: Germany
    • Status: offline
    Re: A question about interrupts. 2017/10/06 02:42:34 (permalink)
    +1 (1)
    CVRIV
    Im thinking maybe I'm to place a GOTO, to some general interrupt subroutine, at 0x04 and in that subroutine I'm to check for all the interrupt flags? 
     
    Where in the datasheet do it mention this? I checked the TMR0 section and the PORTB section and it doesn't mention any of that. 

    Yes, there is only one interrupt vector. View FIGURE 2-1: PROGRAM MEMORY MAP.
    Also view Chapter 14.3 Interrupts  with  FIGURE 14-7: INTERRUPT LOGIC to see what bits you have to set in addition to enable a special interrupt (T0IE, GIE).
     
    I don't understand why to place a GOTO at the interrupt location. Usually you place a GOTO at the reset vector to jump over the interrupt code. Maybe it helps to view the templates for your PIC coming with MPLAB (../MPLABX/v4.01/mpasmx/templates)
    post edited by vloki - 2017/10/06 02:50:00

    Posting images, links and code - workaround for restrictions.
    #2
    qhb
    Superb Member
    • Total Posts : 5819
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: A question about interrupts. 2017/10/06 04:03:42 (permalink)
    +2 (2)
    Did your professor really tell you to put a GOTO at location 0004?
    That thinking is from other processors which have multiple interrupt vectors.
    It is actually a very BAD thing to do in a PIC16 device, because a GOTO should NOT be performed before the "context save" code documented in the datasheet.
     
     
    #3
    mbrowning
    Caffeine Caffeine
    • Total Posts : 533
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: A question about interrupts. 2017/10/06 04:12:06 (permalink)
    +4 (4)
    This is not in the datasheet because it isn't a requirement, and actually adds latency to the interrupt response. The ISR (interrupt service routine) can reside at address 0x04, and then a GOTO is not needed. If you choose to put your ISR somewhere else (and the C compiler generally will) then obviously a GOTO at the interrupt vector is needed to jump immediately to the ISR, but this wastes a program word and 2 instruction clocks in servicing the interrupt. But context saving must be done before the GOTO.
     
    If interrupts are used, then obviously a GOTO must be at the reset vector (0x00) to jump over the ISR (or ISR GOTO) - unless your main program is only 4 words :)
     
    In the (sole) ISR you service all enabled interrupts by testing their interrupt flag==1 AND interrupt enable bit==1 (because if you disable the interrupt, the flag still can go high). If an interrupt is sure to be enabled all the time, then you need not test the enable, but a lot of people do it anyway for safety.
     
     
    post edited by mbrowning - 2017/10/06 04:13:23

    Mark
    #4
    1and0
    Access is Denied
    • Total Posts : 6958
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: A question about interrupts. 2017/10/06 06:24:08 (permalink)
    +1 (1)
    mbrowning
    In the (sole) ISR you service all enabled interrupts by testing their interrupt flag==1 AND interrupt enable bit==1 (because if you disable the interrupt, the flag still can go high). If an interrupt is sure to be enabled all the time, then you need not test the enable, but a lot of people do it anyway for safety.

    ... and testing the enable bit, when it's enabled all the time, wastes a program word and an instruction clock that adds latency.
     
    #5
    Jim Nickerson
    User 452 _
    • Total Posts : 4107
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: A question about interrupts. 2017/10/06 06:44:04 (permalink)
    0
    Right or wrong do exactly what this "professor" said to get full marks.
    As the assignment was written maybe you can share the exact text.
    #6
    1and0
    Access is Denied
    • Total Posts : 6958
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: A question about interrupts. 2017/10/06 06:51:18 (permalink)
    +1 (1)
    JANickerson
    Right or wrong do exactly what this "professor" said to get full marks.

    Caveat emptor ... when more than one page of program memory is used, as the 16F883 has two pages.
    #7
    Jim Nickerson
    User 452 _
    • Total Posts : 4107
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: A question about interrupts. 2017/10/06 07:02:04 (permalink)
    0
    It is quite possible the instructions left room for more than one choice as part of the assignment was to research and choose the best path.
    #8
    1and0
    Access is Denied
    • Total Posts : 6958
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: A question about interrupts. 2017/10/06 07:09:45 (permalink)
    +1 (1)
    JANickerson
    It is quite possible the instructions left room for more than one choice as part of the assignment was to research and choose the best path.

    ... or the "professor" does not know what's he doing. ;)
     
    <edit> I've been asked to grade engineering lab finals before. Long story short, we have to fix the circuits and/or code that were given to the students for their final exams, in order for it to work.
     
     
    post edited by 1and0 - 2017/10/06 07:14:27
    #9
    Jim Nickerson
    User 452 _
    • Total Posts : 4107
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: A question about interrupts. 2017/10/06 07:12:52 (permalink)
    0
    Smile: Smile I had not considered that there were professors who did not know what they were doing.
    #10
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1355
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: A question about interrupts. 2017/10/06 07:42:51 (permalink)
    0
    Professors know their sh it.
    Were you playing with your phone while the professor was talking?

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #11
    Jim Nickerson
    User 452 _
    • Total Posts : 4107
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: A question about interrupts. 2017/10/06 07:59:32 (permalink)
    +1 (1)
    1and0
    JANickerson
    It is quite possible the instructions left room for more than one choice as part of the assignment was to research and choose the best path.

    ... or the "professor" does not know what's he doing. ;)
     
    <edit> I've been asked to grade engineering lab finals before. Long story short, we have to fix the circuits and/or code that were given to the students for their final exams, in order for it to work.

    I had to switch to edge from chrome to "quote" ...
    And what school might this have happened at ?
    edit: I pressed the quote button and thought nothing had happened, the message was quoted but the window did not scroll to the edit area at the bottom of the page though it worked as expected and quoted the post. As asp playground is designed to work on a Microsoft back end it looks like some new gremlins have found yet another way to mess up chrome. With the release of Windows 10 that was compatible with so much more I had high hopes this petty squabbling was over...
    post edited by Jim Nickerson - 2017/10/06 08:08:03
    #12
    1and0
    Access is Denied
    • Total Posts : 6958
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: A question about interrupts. 2017/10/06 08:08:04 (permalink)
    0
    JANickerson
    And what school might this have happened at ?

    It was a community college located near my former former workplace. The wife of that "professor" worked for my boss at the time, and we were asked to help grade the lab finals.
     
    #13
    Jim Nickerson
    User 452 _
    • Total Posts : 4107
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: A question about interrupts. 2017/10/06 08:09:51 (permalink)
    0
    I was curious what school I should watch out for on resumes/CVs I might run into.
    #14
    1and0
    Access is Denied
    • Total Posts : 6958
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: A question about interrupts. 2017/10/06 08:10:40 (permalink)
    0
    JANickerson
    edit: I pressed the quote button and thought nothing had happened, the message was quoted but the window did not scroll to the edit area at the bottom of the page though it worked as expected and quoted the post. As asp playground is designed to work on a Microsoft back end it looks like some new gremlins have found yet another way to mess up chrome. With the release of Windows 10 that was compatible with so much more I had high hopes this petty squabbling was over...

    I have noticed this too. :(  At first, I thought it was fubar.
     
    #15
    1and0
    Access is Denied
    • Total Posts : 6958
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: A question about interrupts. 2017/10/06 08:13:01 (permalink)
    0
    JANickerson
    I was curious what school I should watch out for on resumes/CVs I might run into.

    ;)  I rather not name it, but it was over a decade ago.
     
    #16
    CVRIV
    Junior Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2017/09/06 13:49:25
    • Location: 0
    • Status: offline
    Re: A question about interrupts. 2017/10/06 18:01:23 (permalink)
    0
    Ok. Let me get this straight.
     
    The reset vector is 0x00 right?
     
    The interrupt location in program memory is at 0x04 right? I should put ALL my interrupt code right under a ORG 0x04? This is where the interrupt code starts? I was under the impression there was only one line in program memory for the service interrupts and that's why he had a GOTO there.
     
    I place a GOTO before the interrupt code to jump over it if there are no interrupts?
     
    What is context saving? I haven't read up on interrupts yet. In my professors example code, he saves the value of the WREG before the service interrupt code and restores it when it's done. Is that context saving?
     
    Someone here asked if I was on my phone in class while the professor was talking... the answer is no. All of our lab assignments are PDF's. I'm not posting the PDF online. The professor was very specific in the PDF about placing a GOTO at that point. There was some example code as well.
     
    This definitely clears things up a bit.  
    #17
    qhb
    Superb Member
    • Total Posts : 5819
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: A question about interrupts. 2017/10/06 18:29:12 (permalink)
    +1 (1)
    CVRIV
    Ok. Let me get this straight.
     
    The reset vector is 0x00 right?

    Yes. I'm not sure I'd even use the word "vector".
    The PIC does jump to address 0000 when it is reset, and start executing code from that address.
     

    The interrupt location in program memory is at 0x04 right? I should put ALL my interrupt code right under a ORG 0x04? This is where the interrupt code starts? I was under the impression there was only one line in program memory for the service interrupts and that's why he had a GOTO there.

    That would be a bad impression.
    You have code memory from address 0000 up to the top of FLASH memory. It is continuous.
     

    I place a GOTO before the interrupt code to jump over it if there are no interrupts?

    That is back to front.
    If you are using interrupts (and therefor have an interrupt service routine at location 0004), then your reset code has to jump over location 0004. You therefore have room for three other instructions before the GOTO, if needed.
     
    If you are not using interrupts, then there is no need to jump over location 0004. Your startup code can just start at address 0000, and continue through location 0004.
     

    What is context saving? I haven't read up on interrupts yet. In my professors example code, he saves the value of the WREG before the service interrupt code and restores it when it's done. Is that context saving?

    That is one part of it. You also have to save the STATUS register, and any other registers that your interrupt could change. You also need to select bank 0 (or any other known bank) before you access any banked memory locations.
    You also need to clear the PCLATH register before you execute any GOTO or CALL instructions. That means PCLATH has to be saved also.
    This is all documented in the interrupt section of the datasheet. You really should read that FIRST, before asking questions here.
     
     
    Someone here asked if I was on my phone in class while the professor was talking... the answer is no. All of our lab assignments are PDF's. I'm not posting the PDF online. The professor was very specific in the PDF about placing a GOTO at that point. There was some example code as well.
     
    This definitely clears things up a bit.  

    Your professor must not have had to code a real world application on a PIC16 device then, or at least not one that required using more than one page of code memory.
    For small programs, it is possible to ignore PCLATH, as it will always be zero.
    If you document what you are doing with PCLATH, or why you can safely ignore it, that should be worth some bonus points.
     
    #18
    CVRIV
    Junior Member
    • Total Posts : 71
    • Reward points : 0
    • Joined: 2017/09/06 13:49:25
    • Location: 0
    • Status: offline
    Re: A question about interrupts. 2017/10/06 18:51:41 (permalink)
    0
    I didn't read up on interrupts before asking questions here because I didn't think I needed to read it right now. I was just going forth with what I was reading in my lab assignment. It's not just a lab assignment, it's a lesson. So yea. 
     
    I'm going to read up on interrupts before I even get started with this assignment.
     
    Also... I think someone said that checking for the interrupt enable bits is a waste of time. I was thinking the same thing when I was reading through my lab assignment. Is this true that checking for the enable bits is a waste of time? I'm thinking yes. I just need to check/ clear the flags. Why do you think he would want us to check for the enable bits?  
    #19
    dan1138
    Super Member
    • Total Posts : 2583
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: A question about interrupts. 2017/10/06 19:34:17 (permalink)
    +1 (1)
    CVRIV
    Also... I think someone said that checking for the interrupt enable bits is a waste of time.
     
    I was thinking the same thing when I was reading through my lab assignment.
     
    Is this true that checking for the enable bits is a waste of time? I'm thinking yes.
     
    I just need to check/ clear the flags. Why do you think he would want us to check for the enable bits?

    I suspect that there is no way to teach this concept to someone that has not spent several days looking for bugs caused when interrupt service routine execute when you do not expect them to, or when your system live-locks due to interrupt thrashing.
     
    The assignments that most students get have interrupt service requirements so simple that testing the interrupt enable flags appears to be a waste of time.
     
    It is better style to do this test, also it can make finding bugs in other parts of your application easier when you can disable individual interrupt sources.
     
    If these reasons make no sense to you at present perhaps when you gain some experience the understanding will come.
    #20
    Page: 12345.. > >> Showing page 1 of 7
    Jump to:
    © 2017 APG vNext Commercial Version 4.5