Connect with us

PIC interrupt routine help

Discussion in 'Microcontrollers, Programming and IoT' started by nepow, Jan 16, 2013.

  1. nepow

    nepow

    99
    1
    Jul 18, 2011
    Hi, I need some help with an interrupt routine. I'm no programmer and use simple pic programming language with my projects. My program monitors a voltage on one of the ADC input ports, results of which btfsc goto function “a” or goto function “b”.
    However I want goto “a” to enable an interrupt routine. I have in the past used an external switch on PORT B, 0 to instigate an interrupt, but this time the interrupt has to be activated from with the program. Would appreciate any help please on how this can be achieved and with what instruction. Kind regards Nepow
     
  2. gorgon

    gorgon

    603
    24
    Jun 6, 2011
    There are some loose ends here. When you say 'simple pic programming' I assume you think of Assembly programming.

    You say pic, but not the type of chip you use.

    I'm not sure what you really mean when talking about 'enable an interrupt routine'. Can you expand on this a bit? If you want to enable an interrupt, you need to decide which inbuilt interrupt source you want to use.
    If you on the other hand want to call a routine, you really don't need an interrupt, since you are already processing the 'call'.

    So, please leave some more info on this.
     
  3. nepow

    nepow

    99
    1
    Jul 18, 2011
    Hi gorgon, thanks for you swift response. Sorry I didn’t clarify my help request, the controller is a PIC16F870 and I’m using MPLAB ide with it’s built in simulator to build and run my assembly code. My circuit calls for one of the ADC’s inputs to continually monitor an external voltage level; at some point I need to stop the program to undertake another task of which when completed will return to the main program from where it left off. However, I may rethink the way this project is to function without the need of an interrupt for simplicity reasons. Kind Regards Dupe
     
  4. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    The beauty of interrupts is that the chip will continue to do other tasked full time, it will simply pause and jump to the interrupt when triggered...

    Thus if your ADC read, is done via an interrupt say every second or so, you can have the chip do whatever in between those interrupts... Or use it in reverse, have the main program looping on an ADC read, and when it's time use an interrupt to jump to whatever, an then return to the ADC read routine seamless...

    As gorgon said you have to decide what interupt to use, an interupt is basically a full proof 'goto' that isn't locking into any location in the program, when triggered it will pause the main program wherever it is and make the interrupt 'goto' jump and return where it left off... Most of the time this is done via a change in an external port or using a timer at preset intervals...
     
  5. nepow

    nepow

    99
    1
    Jul 18, 2011
    Thanks CocaCola, what you have described is exactly what I'm trying to achieve depending on the result of an AD convertion will halt the control program to carry out another function then return to the program from where it was stopped. Most of the examples I've come across use external switches on RB0 etc, which is not what I want to do here. If any one knows of examples using assembly that I can play around with would be appreciated. Regards Nepow
     
  6. BobK

    BobK

    7,645
    1,663
    Jan 5, 2010
    nepow,

    But what you describe is done just as well by a call. An interrupt has no advantage if it is always triggered from the same spot in the program, which seems to be the end of your ADC read.

    If I understand it, your program is something like this:

    <initialization>
    main_loop:
    <do some stuff>
    <read the ADC>
    <handle the ADC results> // this seems to be what you want to make an interrupt
    <do some more stuff>
    goto main_loop

    What CC is descrbing would be more like:

    <initialization>
    <set up ADC interrupt>
    <start first ADC reading>
    main_loop:
    <do some stuff>
    goto main_loop:

    interrupt:
    <get the ADC reading>
    <do something with the ADC reading>
    <start next ADC>
    <return from interrrupt>

    With this kind of code, the ADC reading happen asychronously with the main program. This is the advantage of interrupts, they can happen at any time, and the main program doesn't even know they are happening (only that it has lost time, like people picked up by aliens)

    Bob
     
    Last edited: Jan 18, 2013
  7. nepow

    nepow

    99
    1
    Jul 18, 2011
    Thanks Bob
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-