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


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    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
    ‘

  2. #2
    Join Date
    Feb 2013
    Posts
    1,132


    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?

  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?

    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.

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


    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.

  5. #5
    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

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


    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

  7. #7
    Join Date
    Feb 2013
    Posts
    1,132


    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?

  8. #8
    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

Similar Threads

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

Members who have read this thread : 0

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