PDA

View Full Version : Timer - pause? ..?



xobx
- 3rd December 2007, 10:11
How do you make a code work like this:

When I push a button a timer starts and when I push it again it stops and make that time into a variabel. So I can use that time in my program something like this:



...
...
HIGH PORTB.1
PAUSE "The Time"
LOW PORTB.1
...
...

mackrackit
- 3rd December 2007, 10:35
How accurate do you need it and how long will the timer run at a time?

If you need to do other things while the timer is running then an interrupt will be needed. Search the forum for timers.

If you want something quick and dirty and nothing else will be happening when the time is collected this idea might work for you.

When the button is pushed goto a time loop and stay there until the button is pushed again.
While in that loop have a pause for a certain amount of time . Count to a variable each time (T=T+1) through the loop. When you are out of the loop read the variable.

Say the pause is 100 ms and it goes through the loop 10 times. that would equal 1 second plus execution time.

mackrackit
- 3rd December 2007, 10:44
http://www.picbasic.co.uk/forum/showthread.php?t=2129

http://www.picbasic.co.uk/forum/showthread.php?t=632&highlight=olympic+timer

xobx
- 3rd December 2007, 11:41
Both includes a LCD wich make it harder to understand the code :/

Dave
- 3rd December 2007, 11:48
xobx, Just declare a word size variable and then pass it as the variable to the pause statement, like this:

time var word 'time in milliseconds

HIGH PORTB.1
PAUSE time
LOW PORTB.1

Dave Purola,
N8NTA

nomad
- 3rd December 2007, 12:28
maybe something like this? (§ indicates new line) §loop: §button portb.0,1,255,0,but1,0,loop §gettime: §pause 100 §thetime = thetime + 1 §button portb.0,1,255,0,but1,0,gettime §flash: §for blink = 1 to thetime §high portb.1 §pause 100 §next blink §low portb.1 §goto loop

nomad
- 3rd December 2007, 12:36
max number of pauses will be 65535. so with pause 100 max time will be 6553.5 seconds or just under 110 mins. for longer times use a longer pause, for better accuracy use a smaller pause. it would also need a line in gettime to make sure it doesn't overflow.

nomad
- 4th December 2007, 02:08
This will be more readable:


loop:
button portb.0,1,255,0,but1,0,loop

gettime:
pause 100
thetime = thetime + 1
button portb.0,1,255,0,but1,0,gettime

flash:
for blink = 1 to thetime
high portb.1
pause 100
next blink
low portb.1
goto loop

xobx
- 4th December 2007, 11:40
Ok but I doesnt understand this line :/

button portb.0,1,255,0,but1,0,loop

Ioannis
- 4th December 2007, 17:13
PBP Pro manual, page 47.

All parameters explained

Ioannis