Does variable value change being transferred to code running in interrupt loop?


Closed Thread
Results 1 to 28 of 28
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Does variable value change being transferred to code running in interrupt loop?

    Hello.

    Say I have some code, the "main" code and the interrupt driven looped code.

    The interrupt driven code flashes the led according to variable value changed in main code. Is this possible? will it work that way?

  2. #2
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    As long as the variable used in your INT for LED timing is the same variable as referenced and adjusted in your main code loop it should work.

    You will learn more by knocking it up on a breadboard to test your theory (At least I would learn more)
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  3. #3
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Yes it’s the same variable, things can only go wrong if you don’t do your business in enough time for the next interrupt.
    The interrupt gets called more often than your code expects, or something like that, then things can run away from you.

    Essentially though, the interrupt code isn’t special, don’t think of that part of the program in interrupt land.
    It’s just a part of the same program that gets called under certain event driven circumstances and that’s all.
    If you want to share any variable between the ISR and main program you will probably have to declare it in the include file,
    or in your main program before you inserted the include line. The only reason you have to save and restore w & status
    (assembler interrupt) is because your main program doesn’t know when the interrupt will happen.. or you wouldn’t need one

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Here is the code how I want to use it. The code shown is one included in interrupt:

    Code:
    SOFTPWM:
    IF A=1 THEN
    HIGH LED1
    PAUSE 100
    LOW LED1
    PAUSE 100
    ENDIF
    
    IF B=1 THEN
    HIGH LED2
    PAUSE 100
    LOW LED2
    PAUSE 100
    ENDIF
    
    IF C=1 THEN
    HIGH LED3
    PAUSE 100
    LOW LED3
    PAUSE 100
    ENDIF
    
    IF A>1 THEN
    HIGH LED1
    PAUSE 100
    LOW LED1
    PAUSE 100
    ENDIF
    
    IF B>1 THEN
    HIGH LED2
    PAUSE 100
    LOW LED2
    PAUSE 100
    ENDIF
    
    IF C>1 THEN
    HIGH LED3
    PAUSE 100
    LOW LED3
    PAUSE 100
    ENDIF
    
    IF A>2 THEN
    HIGH LED1
    PAUSE 100
    LOW LED1
    PAUSE 100
    ENDIF
    
    IF B>2 THEN
    HIGH LED2
    PAUSE 100
    LOW LED2
    PAUSE 100
    ENDIF
    
    IF C>2 THEN
    HIGH LED3
    PAUSE 100
    LOW LED3
    PAUSE 100
    ENDIF
    
    GOTO SOFTPWM
    The idea is to realize multichannel software PWM using interrupt. The PAUSE values are just for illustration purposes.

    This is just short code, to illustrate how I want to do it.

    If A=1 then led only will be lit 1/3 of total time, approx. 33% of duty cycle
    If A=2 then led only will be lit 2/3 of total time, approx. 66% of duty cycle

    and so on, for other leds. This code allows to have different leds at different duty cycle in one loop. Total there will be 16 leds and 16 duty cycle levels. The meaning of A/B/C variables will be changed externally, outside this loop, so that was the question, will it "sense" variable value change.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Hi,
    In PBP all variables are global, meaning they are always "in scope" and can be accessed by anything/everything at any time/all the time so the answer to your question is yes.

    Pay special attention to what Art said, the total execution time of the interrupt code can not be allowed to be MORE than the time between interrupts. If you have a timer tripping an interrupt at 100Hz then the total execution time of the code in the interrupt service routine can not be more than 10ms. If it is then your main code will not run.

    You'll see comments about never doing any "real work" in the interrupt code, quick in, quick out, set flags etc or you'll set the world on fire. These are generally good advice but there's absolutely no problems with spending 90% of your time in the ISR and 10% in the main code as long as you understand what that does to your code. A PAUSE 100 in the main code will pretty much turn into a PAUSE 900 due to the interrupt service routine "absorbing" 90% of the CPU power without the rest of the system knowing about it. The same goes for all the software times commands like SERIN, COUNT, PWM, DEBUG etc etc - as long you understand that and you're OK with it then no problem.

    /Henrik.

  6. #6
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Well, that PAUSE 100 was provided just as filler - at such rate, leds going to blink rapidly. I believe, PAUSEUS 100 would be much better, but PBP PAUSEUS accuracy is far from desired.

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    If that code is in your interrupt, the interrupt can never exit and you might as well have put this in your main program.
    You can’t run a continuous loop. The ISR is always a call i.e.. GOSUB, and you always have to return from interrupt or your main code will never run.
    What is currently triggering the interrupt?

    Also, it looks like you want PWM at 50% duty cycle, but if more than one condition is true, it will break all of them.

    There is a thread a little below about frequency counters. Darrel didn’t want to shove his own timer code down someone’s throat,
    but it was really the answer to the problem. To his merit, he gave advice about a less efficient manner the OP was already working on.
    Similarly DT’s code is the answer to your problem.
    Last edited by Art; - 7th April 2015 at 11:30.

  8. #8
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    It looks to me you want three software PWM channels, all 50% duty cycle 100ms ON/100ms OFF,
    that you switch on & off based on the values in variables A,B & C.

    Even though this code won’t do that, I don’t understand why you check X=1, X>1, X>2, all for the same result.
    Could you have just:
    Code:
    IF A != 0 THEN
    HIGH LED1
    PAUSE 100
    LOW LED1
    PAUSE 100
    ENDIF
    and then the same for B & C?

    Just want to be sure of what you want to do.

  9. #9
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Just checked, sorry, I've pasted ENDIF at wrong place. It should look like this

    Code:
    SOFTPWM:
    IF A=1 THEN
    HIGH LED1
    ENDIF
    PAUSE 100
    LOW LED1
    PAUSE 100
    GOTO SOFTPWM
    and so on, for all LEDS and cases.

    I want to run this code in background of main task, so this is why I wanted to use interrupt. Seems like it is impossible?

  10. #10
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    You mean you want to run the above lines of code as an interrupt? I don't think so.... The pauses will kill you.... What is your anticipated interrupt period?
    By the way, What processor are you using and at what frequency?
    Last edited by Dave; - 7th April 2015 at 12:53.
    Dave Purola,
    N8NTA
    EN82fn

  11. #11
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    I want to run this task in a way, that main task won't interfere with it, nor it execution will affect main task.

    Processor can be any of 16F family at any possible frequency.

  12. #12
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    It’s not the pauses, the loop never ends:
    Code:
    SOFTPWM:
    code
    GOTO SOFTPWM
    Only your main program can do that.
    This ISR doesn’t allow your main program to ever run once it’s called.

  13. #13
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    If you get this going I’ll do the rest from there.
    The code to flash one led on/off per second is only needed
    to prove the include file is being included from the right directory and everything works.

    At the speeds you’re talking about you can have up to eight software PWM channels
    who’s rising and falling edges are synchronised to the clock cycle fairly easily.
    especially easy if the frequency of the outputs are the same.

    Code:
    DEFINE OSC xx
    ‘ tell PBP the clock frequency you’re using
    DEFINE NOCLRWDT
    ‘ this only means the program has it’s own clrwdt instruction
    ‘ you can still set the watchdog timer on at programming time
    
    include “Elapsed.bas”
    ‘ DT’s Elapsed Timer code from PBP forum
    ‘ drop the Elapsed.bas file in the PBP folder
    
    ‘don’t bother declaring SecondsChanged variable,
    ‘it should already be accessible because it’s declared in the include file
    
    ‘CMCON = 7
    ‘uncomment to set analogue ports digital if the device has analogue ports
    ‘and the led is connected to one of them
    
    trisb.1 = 0
    ‘ set led output whichever pin you have the led connected
    ledstate var bit
    
    gosub ResetTime		'reset time 00:00:00
    gosub StartTimer	'start timer
    '
    cycle:
    @ clrwdt
    ‘
    if SecondsChanged = 1 then
    SecondsChanged = 0
    ledstate = ledstate + 1
    portb.1 = ledstate
    ‘ use your led pin of course
    endif
    ‘
    goto cycle
    ‘

  14. #14
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Below is the actual code which currently runs on 16F870 @ 4mhz:

    Code:
    FOR F=1 TO 8
    A=F
    C=F
    B=9-F
    FOR E=0 TO 10
    IF A=1 THEN PORTC.0=0
    IF B=1 THEN PORTC.1=0
    if c=1 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>1 THEN PORTC.0=0
    IF B>1 THEN PORTC.1=0
    if c>1 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>2 THEN PORTC.0=0
    IF B>2 THEN PORTC.1=0
    if c>2 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>3 THEN PORTC.0=0
    IF B>3 THEN PORTC.1=0
    if c>3 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>4 THEN PORTC.0=0
    IF B>4 THEN PORTC.1=0
    if c>4 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>5 THEN PORTC.0=0
    IF B>5 THEN PORTC.1=0
    if c>5 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>6 THEN PORTC.0=0
    IF B>6 THEN PORTC.1=0
    if c>6 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    IF A>7 THEN PORTC.0=0
    IF B>7 THEN PORTC.1=0
    if c>7 then PORTC.2=0
    PAUSE LODINI
    PORTC.0=1
    PORTC.1=1
    PORTC.2=1
    
    NEXT
    NEXT
    It smoothly dims in-out leds, as needed. Here I'm imitating external change of A,B,C by introducing variable F and altering it's value in loop. Imagine there's no that FOR/NEXT loop, and values of A,B,C are updated elsewhere, but this code runs in interrupt. Will it work?

  15. #15
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Depending on what your main code does, the answer might be yes.
    Just remembering that the ISR is tied up for any of those pauses that could happen.

    So maybe you’d have the first one, but I wouldn’t say you have the second one.
    The idea is to realize multichannel software PWM using interrupt.

    I want to run this code in background of main task.
    Because it’s not done in a way you could forget about it.
    If you sent a serial, LCDOUT, I2C, etc. command from your main program,
    and it got interrupted part way through for the above, the command in the main program wouldn’t work
    .. unless those pauses were extremely small maybe.

  16. #16
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Main program interacts only with user via buttons, by pressing which, user adjusts led brightness value, so a slight delay/flicker when pressing the buttons is acceptable.

  17. #17
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    CuriousOne, Here ia a small template for a program I just wrote that you can use as a starting point. It uses DT interrupts. I have them set for 1 millisecond and using 2 complete ports for the leds. Those ports are B & D. These interrupts are running in the background updating the led status from the 16 byte array. You can change the values in the array at any time in the main program. Enjoy. Let me know if it works for you.
    Attached Files Attached Files
    Dave Purola,
    N8NTA
    EN82fn

  18. #18
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Thanks a lot, I don't have 18F46K22, but I have 18F45K22, will give it a try.

    I need some time to read and interpret "foreign" code for myself

  19. #19
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Here's how I understand, how interrupt should work (for my case of course).

    There are two "independent" code blocks. The MCU works like this - 4 clock ticks (if I'm not mistaken, minimal amount needed to execute one command) are given to code block #1, next 4 ticks - to block #2, and so on. Since these "ticks" are going at high speed, from the user side, response is fluid and smooth.

    am I wrong?

  20. #20
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    CuriousOne, It sounds as if you are interpreting interrupts to mean time multiplexing the operations of 2 different code segments at 50 %. This is NOT how it works. The MAIN code seqment is interrupted for a short period of time do execute some code then execution is returned to the main seqment. You should be in the interrupt routine as short of a time as possible.
    Dave Purola,
    N8NTA
    EN82fn

  21. #21
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Yes, I might be wrong, since I approach interrupts as sort of multi-tasking. So, it might be possible to use it in they way I'd like it to be?

    Like this sequence:

    program starts, timer1 starts counting and after 4 ticks (or whatever value needed) it generates interrupt which jumps to another part of code. That another part of code, itself launches timer2, which again, after 4 ticks or whatever, generates interrupt, which jumps to 1st part of code.

    Is this possible?

    I guess, ASM will be needed.

  22. #22
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Wow, If that's all you want to do is jump between two different programs at a fixed interval then I would just start a timer interrupt at your favorite interval then TOGGLE a BIT called "PROG" each time the interrupt occures as well as set a flag bit called "Interrupt" to indicate the interrupt has occured. Then your main program would look like:
    MAIN:
    if Interrupt = 1 then
    Interrupt = 0
    if PROG = 0 then
    gosub program 1
    else
    gosub program 2
    endif
    endif
    goto main
    Dave Purola,
    N8NTA
    EN82fn

  23. #23
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Yes, but program should not be terminated, it should be "paused" while another part is executing. Let me illustrate by this simple "CODE"

    Conventional BASIC:

    DO:
    FOR A=1 TO 10
    PRINT "A=";A
    NEXT A

    FOR B=1 TO 10
    PRINT "B=";B
    NEXT B

    LOOP

    The above program will print value of A first, and then it will print value of B.

    But I need a output like this:

    A=1
    B=1
    A=2
    B=2
    A=3
    B=3

    and so on.

    I need to somehow "multiplex" two different parts of code in a such way, that they would run in "parallel" If you have heard about Parallax "propeller" chip, which has 8 "cores" but they being executed sequentally, at high speed, so user gets feeling of multi-core system. So I want basically to emulate something like that.

  24. #24
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    CuriousOne, The Parallax Propeller is truly a multicore processor. You would need multiple Pic's to do the same.
    I don't see the difference in your code next to what I am suggesting? The way your's is written it will not produce the output you require.And what do you mean terminated? How about this for an interrupt example:
    a = 1
    MAIN:
    if Interrupt = 1 then
    Interrupt = 0
    if PROG = 0 then
    gosub program 1
    else
    gosub program 2
    endif
    endif
    a = a + 1
    if a > 10 then a = 0
    goto main

    Program1:
    PRINT "A=";A
    return

    Program2:
    PRINT "B=";B
    return

    This will produce the output you require.

    Or without interrupts, Your way..
    A=1
    B=1
    DO:
    PRINT "A=";A
    PRINT "B=";B
    A=A+1
    B=B+1
    IF A > 10 THEN A = 1
    IF B > 10 THEN B = 1
    LOOP

    This will produce the same output you require.
    Last edited by Dave; - 7th April 2015 at 23:23.
    Dave Purola,
    N8NTA
    EN82fn

  25. #25
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    If you’re going that way it would make more sense to do the PWM in your main code and handle button pushes in the interrupt.
    So you’d interrupt on port change and change some variables in the ISR and don’t have to run a loop polling buttons.

    Either way unless the PWM is timer driven, it’s still not “emulating something like that”. Even though you might get the job done.

  26. #26
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Quote Originally Posted by Dave View Post
    CuriousOne, The Parallax Propeller is truly a multicore processor. You would need multiple Pic's to do the same.
    I don't see the difference in your code next to what I am suggesting? The way your's is written it will not produce the output you require.And what do you mean terminated? How about this for an interrupt example:
    a = 1
    MAIN:
    if Interrupt = 1 then
    Interrupt = 0
    if PROG = 0 then
    gosub program 1
    else
    gosub program 2
    endif
    endif
    a = a + 1
    if a > 10 then a = 0
    goto main

    Program1:
    PRINT "A=";A
    return

    Program2:
    PRINT "B=";B
    return

    This will produce the output you require.

    Or without interrupts, Your way..
    A=1
    B=1
    DO:
    PRINT "A=";A
    PRINT "B=";B
    A=A+1
    B=B+1
    IF A > 10 THEN A = 1
    IF B > 10 THEN B = 1
    LOOP

    This will produce the same output you require.
    The main difference is that I need it to work like this:

    advance code 1
    advance code 2
    loop

    The "PRINT" was just an example, of course if I want to print two variables at same time, I know how to do this

    And doing it in both way means that I have to prematurely exit from FOR/NEXT loop, and I can't return to inside it.

    Let me explain once again what I need.

    In above example, I have two for/next loops, each with 10 steps. Is there a way to execute them like one step from code a, one step from code b and so on. Execute by not modifying code as you did (because it works for this certain case, but in real life it might be totally impossible to do like you did), but just distributing processor time between two codes ?

  27. #27
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    Simply saying, windows is multi tasking OS, and can do multi-tasking even on single core CPU. How to do it similar way on PIC ?

  28. #28
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Does variable value change being transferred to code running in interrupt loop?

    With a hardware timer. ie. Time slice, time splitting.
    The same way other platforms and threading languages (C) do it.

Similar Threads

  1. Can not change variable while using DT_INT?
    By hvguy0 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 30th March 2013, 00:05
  2. Altering a variable in a loop.
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 26
    Last Post: - 18th September 2012, 07:24
  3. Change a variable to the opposite?
    By Hylan in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 21st June 2012, 08:00
  4. Change variable allocation to save code space
    By aberco in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 5th September 2011, 02:28
  5. 16f84a running interrupt PICBASIC code
    By divaker in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 31st July 2008, 16:49

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