Interrupt question

Discussion in 'Programmer's Corner' started by ke5nnt, Oct 11, 2009.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    I'm working on a project to help me learn assembly language and I'm stuck on something. Looking for suggestions:

    The program is pretty much an LED display that has several patterns it can display by scrolling through them using a push button switch interrupt.

    Program flow:

    1. Initialize
    2. Go into infinite loop displaying light pattern 1
    3. When button is pressed, first a switch bounce delay, then the program jumps to an infinite loop displaying light pattern 2, and so fourth...

    I would like to set up an interrupt so that I don't have to constantly scan my button input port, but I can't think of a good way to do it.

    Basically the program will stay in light pattern 1 forever UNTIL the push button is pressed, then I want it to go to a button bounce delay, and then go to the next pattern and stay there forever until the button is pressed again, then to pattern 3, pattern 4, etc...

    There are some things I don't understand just yet in assembly, like if I could use some kind of lookup table here, and how I'd do it. I'd love to be able to tell the program "when you detect an interrupt, go to the next pattern" but another problem is that since an interrupt can be set from anywhere in the program, the interrupt wouldn't know what pattern it left from, so no way of knowing where to go next.

    Like I said, I think I'm missing some crucial piece of information here, so help would be appreciated at this point.
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    What microcontroller are you using?

    hgmjr
     
  3. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Oh sorry. PIC 16F628A, using MPLAB IDE
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    My experience is with AVR. There are however lots of AAC members with PIC experience.

    hgmjr
     
  5. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Try something like this:

    Set State = 0 in the initialization.

    The main loop should display the pattern based upon the state. In C* it would be something like Display(Pattern[State]);

    The interrupt should increment the State variable and when State > maximum value, reset it to 0. if(++State > MAXVAL) State = 0;

    Note that, as described, this is simple enough that interrupts are not really needed. The main loop should be fast enough to allow you to use polling.

    --Rich

    *Yes, I know it should be in assembler, but I've only used PIC assembler enough to know I do not care for it. :D C allows me to describe the algorithm without using a specific assembly language. Anyway, as "they" say, "Real Programmers program in machine code!"
     
  6. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    I'm reading a lot about addresses? does each line of code have an address you can load into the W register and then go to it?

    For instance, in my problem here could I?

    start pattern 1 placing an instruction address in a file register
    begin loop
    interrupt occurs -->interrupt says movfw then goto W? (where value in W says go to next pattern)

    next pattern here (initialize before entering main loop by...)
    clearing interrupt flag
    setting new instruction address in file register so next interrupt goes to pattern 3...etc, etc.

    Is this possible?
     
  7. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    You cannot change the return address from an interrupt. The program always has to go back and continues on from where it was when the interrupt occurred.

    What you can do is set a flag in the interrupt routine then in the main routine select the LED pattern on the basis of the flag value.
     
  8. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    can you store a LABEL in EEPROM some how? anytime pattern is changed, that pattern's label is saved so after a power down and then power back up, it automatically starts in the pattern it was in when it was turned off?
     
  9. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    I would say not readily. About the best you can do is to save the state in the EEPROM, then on POR read it and do a compare-and-goto. But check to see what the EEPROM is defaulted to by the factory.

    --Rich
     
  10. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi ke5nnt,
    If you want to use Interrupt, you can use PortB status change interrupt and also External Interrupt. With little ingenuity, you can use PortB 4 pins for using for 4 separate tasks.
    Yes, you can store data in Internal EEPROM, which will be of nonvolatile type and for that you have to know the process of reading and writing of EEPROM and even you can write the EEPROM with Software Coding, not required your programmer all the time.
    Thanks.
     
Loading...