View previous topic :: View next topic |
Author |
Message |
IztokA
Joined: 08 Jan 2016 Posts: 2 Location: Tržič, >Slovenija
|
Posted: Wed Feb 17, 2016 9:21 pm Post subject: Execution time |
|
|
Hello, colleauges!
I cannot find execution times for Bascom commands and i need help.
How many clock ticks uses following code:
Do
Loop Until Tcnt0 = 1
Incr A
Timer0 = 0
I used timer1 for generating pulses of 2MHz (toggle, to get 1MHz output), connecting OC1A pin for output with T0 for input for Timer0. Clock is 16 MHz. Output frequency is ok - 1us, but counting, resulting in A, is not accurate - in 1s I don't get 1 million pulses, but around 380000 to 400000.
I think this code, written above, takes missing pulses.
Is this (my thinking) correct?
What i should do to improve code?
Thanks for answering to a newby.
Iztok
(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
olhexy
Joined: 03 Apr 2011 Posts: 192 Location: Tilburg, Netherlands
|
Posted: Wed Feb 17, 2016 11:31 pm Post subject: |
|
|
You should show the complete code from the beginning of your program: $hwstack, $crystal, config timer1, ...... and so on.
It cannot be that much to show how you programmed the timer.
I see you are newbie. If you show code (just copy-and-paste) it here and mark it as Just like you do bold, italic or underline. It's real simple!
If I want to know the exact timing I run the code with simulator of AvrStudio 4.19 (build 730).
Get it from here (you have to fill in a form): http://www.atmel.com/tools/STUDIOARCHIVE.aspx
(Secret) direct link: http://www.atmel.com/Images/AvrStudio4Setup.exe
Last edited by olhexy on Wed Feb 17, 2016 11:52 pm; edited 1 time in total |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Feb 17, 2016 11:47 pm Post subject: |
|
|
With a clock of 16 Mhz, you only get 16 operations per 1MHz of signal. Bascom is fast, but not that fast !
A good tool for looking at execution times is the Bascom simulator. Just run your code, insert a stop before and after the part you want to test, and look at the cycle count in the status bar. But there will be limitations when you want to look at things like timers, since these are hardware provided by the actual chip.
The Do...loop with its compare alone will take more than that. You can dramatically improve your code by just testing for a timer overflow or compare register flag set, rather than looking for a timer count value of 1 ( or anything else ). But it still may not be fast enough to do what you want.
But there are better ways. The timers have input capture registers, and you can use those to count or trigger on external pulses. You can also read a timer at a capture edge, and read it again at a following edge, a subtract will then give you directly the number of timer ticks between pulse edges. Note that its better to just read and save the timer value at each edge, than to zero the timer at one edge, then read it at the next edge. Since the two operations are both just reads, you dont get uncertainties generated by when exactly the timer is cleared.
Suggest you read carefully the Atmel data sheets for the chip you use, you will need this to get the most out of the various timer and counter modes available. The Bascom examples also cover some of this, but you still need to read the Atmel data. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
IztokA
Joined: 08 Jan 2016 Posts: 2 Location: Tržič, >Slovenija
|
Posted: Thu Feb 18, 2016 8:47 pm Post subject: |
|
|
Thank you both, guys!
After your answers I figured out that i was too demanding of 16 MHz clock for the task.
I slowed down output frequency to 100kHz and everything works fine.
For analysing that simple code i used both suggestions from you, guys, and both shows me up that code wont work well.
So, because i don't really need so high frequency, i slower down everything and it's still ok.
Thank you and best regards from Slovenia! |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|