Qbasicnews.com

Full Version: Clock & Timer Program Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
The highlighted red part gives me an Overflow every time I run this program now.

Any idea why?

'to test it:
CLS
DO
timerVal! = TIMER
clockVal! = CLOCK / 4660.859#

LOCATE 1
PRINT USING "CLOCK: #####.###"; clockVal!
PRINT USING "TIMER: #####.###"; timerVal!
LOOP UNTIL LEN(INKEY$)


FUNCTION CLOCK&

' Get the number of timer ticks at
' 0000:046C
DEF SEG = 0
Ticks& = PEEK(&H46C)

Ticks& = Ticks& + PEEK(&H46D) * 256

Ticks& = Ticks& + PEEK(&H46E) * 65536
DEF SEG

' Latch the counter and obtain the PIT
' countdown status.
OUT &H43, &H4
LSB = INP(&H40)
HSB = 255 - INP(&H40)

' Compute the CLOCK& value
CLOCK& = Ticks& * 256 + HSB
END FUNCTION
[/quote]
Because 256 fits as an integer value, and QB assumes integer calculation, creating an overflow.

Fix it by adding an & to the end of 256:
Code:
CLS
DO
timerVal! = TIMER
clockVal! = CLOCK / 4660.859#

LOCATE 1
PRINT USING "CLOCK: #####.###"; clockVal!
PRINT USING "TIMER: #####.###"; timerVal!
LOOP UNTIL LEN(INKEY$)


FUNCTION CLOCK&

' Get the number of timer ticks at
' 0000:046C
DEF SEG = 0
Ticks& = PEEK(&H46C)

Ticks& = Ticks& + PEEK(&H46D) * 256& 'force long math

Ticks& = Ticks& + PEEK(&H46E) * 65536& 'force long math
DEF SEG

' Latch the counter and obtain the PIT
' countdown status.
OUT &H43, &H4
LSB = INP(&H40)
HSB = 255 - INP(&H40)

' Compute the CLOCK& value
CLOCK& = HSB + Ticks& * 256&  'force long math
END FUNCTION


To make your code appear in a code block as above, use the [ code ] and [ /code ] tags, (without the spaces)
Ok, I'll put that "&" next to the 256. Thanks.

Must have deleted it somehow or it got left out when I copied it from this site.
I wish someone would update that part of the faq. This isn't the first time someone's gotten confused.
u used quote instead or code that to improperly.

Quote: CLOCK& = Ticks& * 256 + HSB

I think you will need to modify this statement to read as:

[script="qb"]CLOCK& = Ticks& * 256& + HSB&[/script]

though i am not sure whether HSB will need a &
Good work TBBQ, now scroll up and read my previous answer, which had that corrected.
I think he meant the Syntax one.
[syntax="QBASIC" ]Print "Code goes here"[/syntax] (w/o spaces)

that would give you

[syntax="QBASIC"]Print "Code goes here"[/syntax]
Quote:I wish someone would update that part of the faq. This isn't the first time someone's gotten confused.

Done. Don't you have wiki access?
nah. or else I would've done it myself.

There are a few concerns before using that routine, mainly that the tick can be up to 1/18.2th of a second off under Windows, according to Antoni. It's perfectly accurate under DOS, though.

Man, I wish someone still had those Windows interrupt routines.
John Monti,
What are you doing that requires PEEKS and getting the TICKS of the clock? Are you just fooling around or do you have some real application for this time logic that needs this fine granularity?
*****
Pages: 1 2 3