BasicMicro - Forums

www.basicmicro.com
It is currently Sat Feb 04, 2012 6:46 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: delay in instructions
PostPosted: Wed Mar 18, 2009 5:33 am 
Offline
Citizen

Joined: Mon Jun 23, 2008 8:39 am
Posts: 18
Dear All,
I need to have a pause of exactly 1 millisec in a loop of 14 instructions (main <some instructions> goto main). How doing it? It's better to run a timer0 or the command code pauseclk / pauseus?

Kind regard
Deny Antonino


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Wed Mar 18, 2009 11:15 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
PAUSECLK and PAUSEUS will probably get you very close to what you want without the headaches of assembly. The PAUSEUS will give you the most resolution to achieve closest timing. If you want to achieve a >complete loop< that times out at 1000milliseconds from MAIN to the time you go back to MAIN, you'll have to use an oscilloscope or logic analyzer to get the exact timing. It is common practice to have an event, such as turning on an LED at the routine's start and turning it off at the end, to give yourself some points to actually measure for this purpose. For instance:

MAIN ;start of loop
HIGH 0 ;LED connected to P0 goes on, start of routine, measurement starts
--your code goes here--
PAUSEUS value ;use this to achieve the full 1000milliseconds within the loop. Change 'value' to get it right.
LOW 0 ;LED goes off, loop code ends, end of measurement
GOTO MAIN ;end of loop

The thing is, in order to keep the perfect timing, you must leave the HIGH 0/LOW 0 in forever, thus losing a pin.
kenjj

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Thu Mar 19, 2009 2:14 am 
Offline
Citizen

Joined: Mon Jun 23, 2008 8:39 am
Posts: 18
Dear Kenjj,
the question is that I've a code with some instructions such below:

main:
High P16 ' led on
if <condition1> then <instruction1>
endif
if <condition2> then <instruction2>
endif
if <condition3> then <instruction3>
endif
.....
if <condition10> then <instruction10>
Low P16 'led off
goto main

I don't know exactly how much delay it takes every instruction ablove, so I can't calculate the value for pause/pauseus to have a time of 1 second (exactly 1 second, or 1000 millisec) for every loop, as well.

Please let me know something about...

Regards
Deny


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Thu Mar 19, 2009 9:38 am 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
All those IF-THEN instructions mean you'll NEVER have an exact idea how long the loop will run. Each loop will take a different length of time. Any one, none, or all, of them might be true and run in each loop.

What exactly are you building? How is it supposed to work?
kenjj

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Thu Mar 19, 2009 2:21 pm 
Offline
Citizen

Joined: Mon Jun 23, 2008 8:39 am
Posts: 18
Dear Kenjj,
I've noticed that every instruction takes a different delay, but I did not think that the delay is big. In a for loop from 0 to 4000 and with an internal pause of 250 microsec, I wait for 3 seconds, not for 1 second, just I supposed....
The example of code is below:

main
counter1 = 0
counter2 = 0
counter3 = 0
counter4 = 0
counter5 = 0
counter6 = 0
counter7 = 0
counter8 = 0
low P16
for i = 0 to 4000
status1 = inl
pauseus 250 'applico una pausa di 0.25 millisec per avere una freq max di 2000 hz
status2 = inl
counters = status1 ^ status2
if counters.bit1 = 1 then 'bit1 corrisponde al canale C2
counter2 = counter2 + 1
endif

next

high P16
pause 10
serout P20,I9600,["C2:",dec counter2]
goto main

I want to realize a datalogger that has 8 inputs for counters (anemometers with output in square wave signal) and my goal is to scan all the 8 channels with sampling time of 1 second 'exactly' ONE second....

Kind regard & thank you for your support
Deny Antonino


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Thu Mar 19, 2009 4:59 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
OK, a few things:
1. I can't read your Italian comments
2. You don't show the VAR definitions for the variables counter1 thru counter8, counters, status1 and staus2 and any I may have missed
3. I need to see ALL your code
4. You can zero all your variables instantly, no matter the size, with the CLEAR command.
5. Give me the manual version you are using and the PDF's page number with the explanation for "inl".
6. If you want to do 4000 loops, your FOR has to be "0 to 3999" or "1 to 4000".

This is BASIC, not assembly. The timing loop isn't set in stone. When you do "PAUSEUS 250", the compiler finds the token for "PAUSEUS" in code space, figures out which library handles that, starts the library, gets the "250", loads it into a register, THEN finally runs a delay loop lasting 250uS. This is an educated guess here, but I think largely correct. Remember, BMicro says the Atom does APPROXIMATELY 33,000 instructions per second. Somebody may have sat down and come up with a timing chart for all the Atom commands, but you still have to factor in inaccuracies of the processor's clock.

Later!
kenjj

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Fri Mar 20, 2009 2:35 am 
Offline
Citizen

Joined: Mon Jun 23, 2008 8:39 am
Posts: 18
Dear Kenjj,
the complete code I've written for testing it is:

counter1 var word
counter2 var word
counter3 var word
counter4 var word
counter5 var word
counter6 var word
counter7 var word
counter8 var word
counters var byte
status1 var byte
status2 var byte



main
counter1 = 0
counter2 = 0
counter3 = 0
counter4 = 0
counter5 = 0
counter6 = 0
counter7 = 0
counter8 = 0
low P16
for i = 1 to 4000
status1 = inl
pauseus 250
'I set a pause of 0.25 millisec to acquire a signal of max freq of 2000 HZ
status2 = inl
counters = status1 ^ status2
'I compare every bit each other so that if the i-th bit is different between
'status1 and status2 I have a transition in input signal
if counters.bit0 = 1 then 'bit0 corresponds to channel 1
counter1 = counter1 + 1 'counter1 increments itself for every transition 1-0 or 0-1 so that in one second I have all
' the transitions in variable counter1 for channel 1
endif
if counters.bit1 = 1 then 'bit1 corresponds to channel 2
counter2 = counter2 + 1 'counter2 increments itself for every transition for channel 2
endif
if counters.bit2 = 1 then
counter3 = counter3 + 1
endif
if counters.bit3 = 1 then
counter4 = counter4 +1
endif
if counters.bit4 = 1 then
counter5 = counter5 + 1
endif
if counters.bit5 = 1 then
counter6 = counter6 +1
endif
if counters.bit6 = 1 then
counter7 = counter7 + 1
endif
if counters.bit7 = 1 then
counter8 = counter8 +1
endif


next

high P16
'LED on for tests delay in for - next cycle
pause 10
serout P20,I9600,["C1:",dec counter1,"C2:",dec counter2,"C3:",dec counter3]
'it sends to the display the strings
goto main

The manual I read the instructions for ATOM is of version 3.0.0.2 and the page in wgich is explained the command "INL" is 43.
I have only read the code for sampling the 8 channels for counters, if I also add the code for analogic channels (ADIN...) the delay is increasing more and more.. :o :o

Kind Regards
Deny


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Fri Mar 20, 2009 1:32 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Yes, but EVERY command takes time to run. Approximately 30uS or so for even the simplest command. Even your "PAUSEUS 250" actually runs beyond 250uS what with command set up time, register loading, and cleanup work when the timeout finishes.
If all you had was:
Code:
MAIN
FOR i = 1 to 4000
PAUSEUS 250  ;4000 x 250uS = 1 second
next
GOTO MAIN

you STILL wouldn't run this simple code in one second exactly. It takes time to process the FOR-NEXT loop and the GOTO MAIN. And then, after you collect the two samples, you perform many IF-THEN conditional routines. This adds greatly to the FOR-NEXT loop time.

In short: you can't get what you want with this method using Atom BASIC. If you study the time it takes to run all those IF-THENs, you may find they take 200 to 300 uSeconds! In effect, they become your timing between samples. But you are then forced to figure how to do sampling before and after you do the conditionals, which is rather hard to picture what the sequence would look like! "First sample, IF-THENs, second sample" does not compute! And then you add the ADINs and the timing becomes so spread out you have no hope of sampling 4000 times per second. You are now looking at doing this in C or assembly. Or maybe something as simple as a BASIC that compiles to pure machine code, not tokens like the Atom compiler.

I can't help you any further. Good luck on your project!
kenjj

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Wed Feb 10, 2010 2:27 pm 
Offline
Citizen

Joined: Mon Feb 01, 2010 9:40 pm
Posts: 43
Regarding this post, is it possible to wait for a timer here? you set the timer at the very beginning of the loop and let it run. Meanwhile you do required processing (which should take less than 1 sec, but does not have to be exact) and then wait till the timer reaches 1 sec count and proceed with the loop. This would require to use timer interrupt, I guess - something like SETCOMPARE, but in assembly, because the precise timing is required.

Speaking of assembly, does anyone know some good code samples illustrating usage of assembly language in Basic. I would really appreciate this! I know there is no official manual on this, but some examples would be very helpful too.

Thank you!


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Wed Feb 10, 2010 3:30 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Rita, do me a large favor and start a new post with your questions. This is almost a year old. Please have an exact idea of what you want to do and be prepared to post code to study. The problems with the original poster is:
1. he changed his request a few times and
2. had no equipment to determine what the loop times actually were and
3. wanted to introduce conditionals, which means he'll never have the same loop time for every loop, unless all eight inputs die and produce the same exact meaningless input value.

In the mean time, do a search here and in the lynxmotion(.com) Boebot/Atom forums looking for "ASM{ " or "assembly".
Take care.

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Wed Feb 10, 2010 7:45 pm 
Offline
Citizen

Joined: Mon Feb 01, 2010 9:40 pm
Posts: 43
Thank you for the link, Ken.
I don't think it's worth to create a new thread. It was more a note related to the original post than a specific question. When I'll have one I'll definitely create brand new thread for it :)


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Tue Mar 16, 2010 9:05 am 
Offline
Citizen

Joined: Wed Feb 10, 2010 1:09 pm
Posts: 16
This seems to go along with my question so I'll post in this thread instead of starting another.l

Here is a section of my code for the "ALARM" subroutine:

Quick rundown of what I need to do. I'm mainly referring to the code bolded. I need a 30s delay between the high to pin 27 and pin 26 which is what I did. However, during that 30s if another channel is tripped(device monitors 8 channels) the processor won't pick it up during those 30s and if said trip goes back to normal before device "wakes up" after 30s then the operator will have no idea of what happened. Soo..I know you can't run a timer in background b/c this is a single threaded processor, but is there a solution to put the delay for 30s while still letting the processor do its work?

ALARM: 'high 26 pusles pin 9 on the board;high 25 brings pin 10 to GRD;high 27 pulses pin 8 on the board
if test=1 then endalarmsub 'pulse solenoid for 250ms
if battshut=1 then endalarmsub
if lobatt=1 then
high 26:high 27:high 25
pause 250
low 26:low 27
goto endalarmsub
endif

if once=1 then endalarmsub
if cond(7)<>0 and prime=line then
high 27
sleep 30
high 26:high 25


pause 250
low 26:low 27:once=1
endif
if cond(7)=1 then
high 27:high 26:high 25
pause 250
low 26:low 27:once=1
endif


if once2=1 then endalarmsub
high 27
pause 250 '
low 27:once2=1
endalarmsub: return


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Tue Mar 16, 2010 1:50 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Hello, Brock. Have a look at this thread:
http://forums.basicmicro.net/viewtopic.php?f=485&t=9186&sid=69a4630c3ddde8210dcc43bf90b66aad
Look for AcidTech's reply. This is for an external interrupt. In Rev B Atom parts this applied to pins P0 thru P3. The later Rev D parts have extint capabilities on pins P0 thru P7, if I remeber correctly. But the new parts make you select which pin is sensitive in a register.
Anyways, try AcidTech's code and see if it works for you. If not, let us know. I can pass this on to the programmer. If that's the case, I'll need your part type and which Studio version you are using.
BTW, it's best to start new posts because it is easier for people to find useful info in a small thread rather than some post with multiple subjects, even if they are sorta related. Be a pal, start another post next time.
Later.

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Tue Mar 16, 2010 2:10 pm 
Offline
Citizen

Joined: Wed Feb 10, 2010 1:09 pm
Posts: 16
As always thanks for the response Kenjii.

Most forums I've been a part of blast you for not using the search feature so I thought I was doing good by not starting another thread ;). I'll be sure to start a new thread in future.


Top
 Profile  
 
 Post subject: Re: delay in instructions
PostPosted: Tue Mar 16, 2010 2:18 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Yes, well, I found this using a search, but those other guys take it for granted you have the perfect search string to use. I knew I'd seen this conversation before and knew "israsm" was the just the phrase to use in the search. And, et voilas!, there she was!
BTW, you beat me by two or three minutes. I just changed that address to the thread. If the earlier address won't work for you, try the new one, it should work just fine. The earlier address was based on the search results. It dawned on me later that someone using one of these search addresses I supplied was denied access.
Live and learn.
So, sure, start a new post AFTER you do a thorough search. ;)

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO