Gooligum tutorial on interrupts now available

Author
meikled
Super Member
  • Total Posts : 546
  • Reward points : 0
  • Joined: 2006/01/11 20:12:06
  • Location: Sydney, Australia
  • Status: offline
2008/12/29 00:41:21 (permalink)
0

Gooligum tutorial on interrupts now available

For those who have been following my Gooligum tutorials (or anyone else!), I have finally completed a lesson introducing interrupts on midrange PICs.

In case the link embedding above didn't work (?), you'll find the lesson at: http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_A_6.pdf

It introduces:
  • What is an interrupt?
  • How they are implemented on midrange PICs
  • Saving and restoring context in an interrupt service routine (ISR)
  • Timer-driven interrupts (using Timer0)
  • Using timer interrupts to debounce a single switch (using counting algorithm)
    (I'll cover using vertical counters to debounce multiple switches later...)
  • Signalling between an ISR and the main program
  • External interrupts (the INT pin)
  • Handling multiple interrupt sources
As in the previous lessons in the midrange tutorial series, the PIC12F629 is used, but these techniques apply to all midrange (14-bit) PICs.

This one has taken a long time to write - a bit of a hurdle to get over (and I've been pretty busy working for a living, so finding time to write tutorials isn't easy!), but now that I've introduced interrupts, I'll be able to use them in future lessons, when discussing the other timers, comparators, multiplexing, or whatever.

I hope someone finds this lesson useful...



#1

13 Replies Related Threads

    Guest
    Super Member
    • Total Posts : 80499
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Gooligum tutorial on interrupts now available 2008/12/29 03:21:07 (permalink)
    0
    congrulations on the work.
     
    i am also working on tutorials. but fundamentally different. i use floating daily english most likely,
    no technical words, or pictures of bitfields.
     
    tutorials like yours can be useful for people with no pre-requisites, even if i also start off from the very basics.
    however, HEX, and bitfields are not explained, in the best case, a few lines to remember content that was once teached.
     
    so i think my style is for people who want to work out their own code, and not so much rely on listings.
     
    it seems to be good work for the people using this method (look at sources of other people).
     
    by the way do you know Don Lancaster's CMOS cookbook, there's also key debouncing.
    i do not have these materials right now. guess once i've used 1MOhm, not 1KOhm like in your tutorial.

    aspforum.mchp.guest
    #2
    meikled
    Super Member
    • Total Posts : 546
    • Reward points : 0
    • Joined: 2006/01/11 20:12:06
    • Location: Sydney, Australia
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2008/12/29 03:56:30 (permalink)
    0
    ORIGINAL: hybridpic

    so i think my style is for people who want to work out their own code, and not so much rely on listings.



    I'd be interested in other people's opinions.  It certainly was never my intention to create a "cookbook" of listings that people would rely on - I'm trying to use examples to demonstrate basic principles - my intent is that anyone working through my tutorials will be able to write their own code.


    by the way do you know Don Lancaster's CMOS cookbook, there's also key debouncing.


    No, I haven't seen that one, but in any case I'm not trying to cover hardware debouncing in detail - the point of the tutorials is PIC programming (in assembler and C).


    i do not have these materials right now. guess once i've used 1MOhm, not 1KOhm like in your tutorial.


    I think I used 1k between a switch and an input pin?  That's exactly what's used on Microchip's "low pin count" demo board, supplied with the PICkit 2 starter kit.  That's the development environment I use in the tutorials - cheap, but very useful, which is why I recommend it.

    Anyway - a 1M resistor in the same place wouldn't be a good idea.  The pin is being held high by a 10k resistor.  When the button is pressed, the pin is pulled low through the 1k resistor.  If a 1M resistor is used, the problem is that any leakage current supplied by the pin (GP2 or GP3 in different examples) goes through that resistor.  A leakage current of just 5 uA is enough to cause a voltage drop of 5V across that 1M resistor, so the input pin will be sitting at 5V - i.e. it won't have been pulled low.  This remains true whether you also have a capacitor between the pin and ground, as I did in the external debounce example.



    #3
    Guest
    Super Member
    • Total Posts : 80499
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Gooligum tutorial on interrupts now available 2008/12/29 04:46:49 (permalink)
    0
    i am just writing a few thoughts, have not read the complete tutorial.
    it is, for sure, qualified, and suitable to learn PIC programming.
     
    the CMOS cookbook contains a lot of topics, as the name says, about CMOS technology.
    the writing style is a bit esoteric, and it is old, from the 1970s.
    but i would still recommend it for beginners, even if some of the ICs are not available anymore for a long time now.
     
    1MOhm works together with static CMOS inputs, i think 100nF capacitors have been used.
    i know, PIC WPU equal about 500 uA, yes.
     
    also i am seeing the older TTL/CMOS technology (discrete chips) being less and less used, with a few exceptions.
    maybe some of the ICs will survive?
     
    it is important for how to start writing a tutorial.
     
    personally i require some TTL/CMOS knowledge, also BASIC.
    but yes, they now use VISUAL BASIC (in the best case).
     
    you never know the pre-requisites, might be different for older generation, and
    for students of nowadays.
     
    right now, i have headlines for 15 chapters, and have written a bit of text for one or two.
     
    well the truth is i won't write a similar tutorial than yours!
    it is good (in my opinion), if people can use 10 or 20 different materials,
    not one large book for everyone.
     
    in the CMOS cookbook also a lot of strange things are mentioned, which also can occur when using PIC.
     
    for instance, one chapter will be about "strange things", people wonder why it can not act.
    i think i have labelled it "hardware debugging", not exactly sure.
     
    i have not read all your tutorials, it is a lot of text, but will take a look now and then.

    aspforum.mchp.guest
    #4
    K8LH
    Super Member
    • Total Posts : 1871
    • Reward points : 0
    • Joined: 2004/03/26 05:12:34
    • Location: Michigan, USA
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2008/12/30 00:07:30 (permalink)
    0
    Hi David,

    I trust you and family are well and enjoying a wonderful Holiday season?

    Congrats' on another fine tutorial.  Your debounce ISR example is a good foundation for moving students towards an efficient interrupt driven multi-switch solution.  With that in mind, would you mind if I post an alternative example?  It's not better, just different, but I suspect if it were explained properly (as you do so well in your tutorials) it might be an effective bridge towards multi-switch management.

    This example, like yours, debounces both the press and release switch states but only the debounced "new press" state is passed along as a flag to Main.  The other major difference is that a debounced "new press" bit is exclusive or'ed with the corresponding switch flag bit.  This allows us to process a standard "momentary" switch by testing and clearing its switch flag bit in Main or to process an emulated "toggle" switch by simply testing its switch flag bit in Main (press the switch to toggle its switch flag bit from on-to-off or from off-to-on).  I also show code for implementing a 32 msec 500 Hz "new press" beep (requires 1 msec interrupts).

    While the example shows multiple switch use, this would only work if you could guarantee only one switch pressed at a time.  Like I said, it's simply meant as a bridge that might help explain a more advanced example using vertical counters.

    Kind regards, Mike

    ;
    ;  sample = GPIO;               // sample active hi switches
    ;  sample &= 1<<SW1 | 1<<SW2;   // filter out non-switch bits
    ;  if(sample ^= SwLatch)        // if changes (press or release)
    ;  { db_cnt--;                  // decrement debounce counter
    ;    if(db_cnt == 0)            // if timed-out (debounced)
    ;    { SwLatch ^= sample;       // update debounced state latch
    ;      sample &= SwLatch;       // filter out "new release" bits
    ;      SwFlags ^= sample;       // toggle switch flags for Main
    ;    }                          //
    ;  }                            //
    ;  else                         // else, no changes
    ;  { db_cnt = 20;               // reset db counter to 20 msecs
    ;  }
    ;
    isr_gooligum
             movf    GPIO,W          ; sample active hi switches
    ;       comf    GPIO,W          ; sample active lo switches
             andlw   1<<SW1|1<<SW2   ; filter out non-switch bits
             xorwf   SwLatch,W       ; W = changes (press or release)
             bz      isr_dbreset     ; branch if no changes, else
             decfsz  db_cnt,F        ; debounce timer timed out?
             goto    isr_beep        ; no, branch, else
             xorwf   SwLatch,F       ; update debounced state latch
             andwf   SwLatch,W       ; filter out "new release" bits
    ;       skpz                    ; if any "new press" bits
    ;       bsf     beeper,5        ; task a "new press" beep
             xorwf   SwFlags,F       ; toggle switch flags for Main
    isr_dbreset
             movlw   d'20'           ; reset db counter to 20 msecs
             movwf   db_cnt          ;
    isr_beep
    ;       movf    beeper,W        ; beep task running?
    ;       bz      isr_gxit        ; no, branch, else
    ;       movlw   1<<Speaker      ;
    ;       xorwf   sGPIO,F         ; toggle speaker pin in shadow
    ;       decf    beeper,F        ; dec beep msec counter
    isr_gxit


    <added>

    Puzzle: Do you know how you might implement repeat key capability in Main using the debounced switch state latch variable?
    post edited by K8LH - 2008/12/30 04:25:29
    #5
    meikled
    Super Member
    • Total Posts : 546
    • Reward points : 0
    • Joined: 2006/01/11 20:12:06
    • Location: Sydney, Australia
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2008/12/30 15:02:22 (permalink)
    0
    ORIGINAL: K8LH

    Congrats' on another fine tutorial.  Your debounce ISR example is a good foundation for moving students towards an efficient interrupt driven multi-switch solution.



    Thanks Mike.  It was difficult to know how far to go in discussing debouncing, in a lesson that is really about introducing the concept of interrupts and what sorts of things (in general) they can be used for - while building on the material in earlier lessons.  So I took the approach of saying - ok, we've done these familiar things before (including the debounce algorithm I'd intoduced before) - here's how you can implement those same things (e.g. flashing an LED) using an interrupt.


    With that in mind, would you mind if I post an alternative example?  It's not better, just different, but I suspect if it were explained properly (as you do so well in your tutorials) it might be an effective bridge towards multi-switch management.


    Please do!  I do intend to come back to this and dedicate a lesson to more advanced input methods, such as scanning matrix keypads and mutliple switch debouncing, at some stage - but it will be many months off, because I intend to do lessons on timer 1, timer 2, ECCP, comparators, ADC, EEPROM, sleep, watchdog timer, serial interfaces - the list goes on - and I'm doing both assembler and C versions of each!  Huge project, and I have a day job...

    So, I'm sure that everyone out there, wanting to learn, will want as many good sources as possible - so people like you, posting this sort of useful material, can only help everyone.


    #6
    Guest
    Super Member
    • Total Posts : 80499
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Gooligum tutorial on interrupts now available 2008/12/31 04:17:27 (permalink)
    0
    a note on the tutorial, writing to PORT in the ISR.
     
    i have never experienced direct problems writing to ports and then having interrupt.
    eventually i am just using the port for LED display multiplex.
    however there is one obscure issue, using LCD, very rarely there is a bug when copying data.
     
    this might be the reason for it!
     
    normally, i think, i switch off interrupts when doing I/O, maybe some older code does not switch off interrupt.
    but also i think it (doing the port based I/O) is only done after the interrupt, and before new interrupt occurs.
     
    i will investigate for this issue.
     
    what about the information to switch off interrupts before doing I/O? can this be added eventually.

    aspforum.mchp.guest
    #7
    K8LH
    Super Member
    • Total Posts : 1871
    • Reward points : 0
    • Joined: 2004/03/26 05:12:34
    • Location: Michigan, USA
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/01/03 07:38:42 (permalink)
    0
    Hi again David,

    I apologize for continually pushing you toward a multi-switch example and tutorial.

    Kind regards, Mike
    post edited by K8LH - 2009/01/03 09:46:07
    #8
    meikled
    Super Member
    • Total Posts : 546
    • Reward points : 0
    • Joined: 2006/01/11 20:12:06
    • Location: Sydney, Australia
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/01/03 19:49:00 (permalink)
    0
    ORIGINAL: K8LH

    I apologize for continually pushing you toward a multi-switch example and tutorial.



    Mike - absolutely no apologies needed, ever.  Your own multi-switch (and hold-down repeat) examples are very useful, and I'll certainly be looking at them (again) when I eventually do come to write tutorials on that.


    #9
    merser
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2008/11/21 13:40:46
    • Location: Brisbane, Australia
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/01/04 22:40:15 (permalink)
    0
    Hi David,
    Another Aussie here. Ozzie ozzie Ozzie oy oy oy!
    Your tutes are fantastic. So well written.
    If only all technical documents were written so clearly.
    I'd say if you ever get a chance to work as a technical writer do it.

    Best regards
    Mike Seiler
    #10
    meikled
    Super Member
    • Total Posts : 546
    • Reward points : 0
    • Joined: 2006/01/11 20:12:06
    • Location: Sydney, Australia
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/01/04 23:35:35 (permalink)
    0
    ORIGINAL: merser

    I'd say if you ever get a chance to work as a technical writer do it.



    Thanks Mark!

    Sounds a little boring though (with all due respect to tech writers!) - but could it be any worse than being an IT manager?
    Now, what I'd really like would be to be a hands-on engineer, actually paid to design and build things - now that would be fun!  Instead, all I can do is tinker as a hobby on the side, and write about my learnings... 


    #11
    meikled
    Super Member
    • Total Posts : 546
    • Reward points : 0
    • Joined: 2006/01/11 20:12:06
    • Location: Sydney, Australia
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/02/01 00:43:10 (permalink)
    0
    ORIGINAL: meikled

    For those who have been following my Gooligum tutorials (or anyone else!), I have finally completed a lesson introducing interrupts on midrange PICs.

    In case the link embedding above didn't work (?), you'll find the lesson at: http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_A_6.pdf

    It introduces:
    • What is an interrupt?
    • How they are implemented on midrange PICs
    • Saving and restoring context in an interrupt service routine (ISR)
    • Timer-driven interrupts (using Timer0)
    • Using timer interrupts to debounce a single switch (using counting algorithm)
      (I'll cover using vertical counters to debounce multiple switches later...)
    • Signalling between an ISR and the main program
    • External interrupts (the INT pin)
    • Handling multiple interrupt sources
    As in the previous lessons in the midrange tutorial series, the PIC12F629 is used, but these techniques apply to all midrange (14-bit) PICs.



    I have just completed the midrange C version of this lesson: http://www.gooligum.com.au/tutorials/midrange/PIC_Mid_C_3.pdf

    It covers the same material as the midrange assembler version, described above, but implemented using HI-TECH's C compilers - the old PICC-Lite (still available from here) and HI-TECH C PRO running in "Lite" mode (bundled with MPLAB 8.20).


    #12
    wael_sal
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2008/12/03 21:29:52
    • Location: 0
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/06/29 09:47:56 (permalink)
    0
    Good Day,
    Great work,but unfourtionatly I am not familiar with assembly, I will be so thankfully If you could give a help in C18, I mainly need a help to set up reading switches 3: Reading Switches " reading and debouncing simple switches", hope you could gave a help.
    Best regards
    Eng. Wael
    #13
    nur53
    Super Member
    • Total Posts : 1102
    • Reward points : 0
    • Joined: 2008/06/07 08:16:33
    • Location: 0
    • Status: offline
    RE: Gooligum tutorial on interrupts now available 2009/06/30 02:06:52 (permalink)
    0
    Congratulation for your excellent work David !

    Since I have begun to work with PIC's at the beginning of 2007 as a Hobbyist I can say your site is one of the three sites which are very helpful for the Beginners to recommend .

    The two others are "sprut"(German site) and "interq"(English version of the Japan site) .

    Regards

    Nur
    #14
    Jump to:
    © 2017 APG vNext Commercial Version 4.5