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.
#CHIP16F18326// #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 chipsetDirPortA.1Out// ----- Main body of program commences here.DimOVERFLOWSasLONG=0DoForeverPortA.1=1// Do code for 1000 ms and no longerRepeatFor1000msWait1000usIfTMR1IF=1ThenOVERFLOWS=OVERFLOWS+1TMR1IF=0EndIf// end of do codeEndRepeatPortA.1=0Wait1000msLoop
This is the concept. Repeat a code section for a given time period. Simple really.
What are your thoughts?
Evan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
or, real a implementation
This is the concept. Repeat a code section for a given time period. Simple really.
What are your thoughts?
Evan
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?
The
Repeat FOR TIMEPERIOD UNITSo, 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.
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
Yes. That is the idea.
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).
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.
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