Menu ▾ ▴

Fixed Time Processing ( or some other name you define! )

Anobium
14 hours ago
4 hours ago
  • Anobium

    Anobium - 14 hours ago

    Ever needed to execute some code for a specific time period? A really good use case is measuing a frequency of signal over a given time.

    Sounds easy. It is not. A for-next loop can 'sort' of work, but if the code is relocated to another page in Progmem ( 16Fs etc ) then the for-next loop would need adjust.

    Well... my latest change resolves this. I could not find another compiler with the feature so I need some input.

    This code works. It puts out a signal every 500ms. You can change the code size ( I am using delays ) but the compile will automatically 'pad' the code with the correct code to 'pad' to 500ms.

    #CHIP 16F18326
    #option Explicit 
    
    Dir PortA.1 Out
    
    // ----- Main body of program commences here.
    Do Forever
    
        PortA.1 = 1
    
    //  Do some code for 500 ms and no longer
        Repeat For 500 ms
            Wait 100 us 
            Wait 20 us
            Wait 110 us
            Wait 2000 us
    
            // end of do code
        End Repeat
    
        PortA.1 = 0
        Wait 500 ms
    Loop
    

    or, real a implementation

    #CHIP 16F18326
    //   #chip mega328p        // Uncomment to simply use the AVR chipset
    //   #chip LGT8F328P       // Uncomment to simply use the LGT chipset
    //   #chip mega4809       // Uncomment to simply use the AVRDx chipset
    
    Dir PortA.1 Out
    
    // ----- Main body of program commences here.
    
    Dim OVERFLOWS as LONG = 0
    
    Do Forever
    
        PortA.1 = 1
    
    //  Do code for 1000 ms and no longer
        Repeat For 1000 ms
            Wait 1000 us
            If TMR1IF = 1 Then
               OVERFLOWS = OVERFLOWS + 1
               TMR1IF = 0
            End If
            // end of do code
        End Repeat
    
        PortA.1 = 0
        Wait 1000 ms
    Loop
    

    This is the concept. Repeat a code section for a given time period. Simple really.

    What are your thoughts?

    Evan

     
  • Roger Jönsson

    Roger Jönsson - 13 hours ago

    Don't understand.
    What does "Repeat For" mean?
    Repeat (over and over) For 1000 ms and then End Repeat? Then clear port and then wait 1000ms, then set the port? = every 2000ms? That is how I read it. Probably wrong, that is why I am asking.
    What is it that compile will do to automatically 'pad' the code? Where?

     
  • Anobium

    Anobium - 12 hours ago

    The Repeat FOR TIMEPERIOD UNIT

    So, any do any code within the REPEAT - END REPEAT will be repeated until the TIMEPERIOD has expired.

    You want to measure a changing input port for 1000 ms. Yon would repeat the measurement until the time expired.

    Under the hood the compile is calculating the cycles of the code and it will exit when the timer expires.

     
  • Roger Jönsson

    Roger Jönsson - 9 hours ago

    So. The repeat loop runs for a specified time and during that time sampling an input or whatever, incrementing a variable each time the input changes and then use that variable value to see how many times it has incremented during that Repeat For time. The exact time for one round is the Wait + the time it takes for the rest of the code to complete one round? For slower measurement, then the wait time may be sufficient. -Is that what you mean?
    -Yes, can be useful.

     

    Last edit: Roger Jönsson 9 hours ago
    • Anobium

      Anobium - 9 hours ago

      Yes. That is the idea.

       
  • Roger Jönsson

    Roger Jönsson - 8 hours ago

    How is the repeat time determined?
    Do one need make sure that the code is consistent in timing, so that it does not change (longer or shorter execution due to some other input etc).

     
    • Anobium

      Anobium - 7 hours ago

      There is calculation.

      Elapsed time = pre padding cycles + user code cycles + post padding cycles . Where the compiler counts the user code cycles then pads as required. This means some constraints are applied like a GOTO, GOSUB, recursive REPEAT ( there may be others ). ( there may be others ).

      The goal is to count cycles, and add cycles to complete the loop in the specified time period.

       
  • Jerry Messina

    Jerry Messina - 4 hours ago

    I don’t see how that would work in a lot of cases... even in your simple example the number of cycles changes at runtime depending on the hdw flags, not to mention what about interrupts, library calls, subs, etc

     

Log in to post a comment.