PDA

View Full Version : Counter



Fredrick
- 29th April 2015, 13:07
Anyone have a simple example of how I can do something like this but with larger numbers then 65535?

Counter VAR WORD

Loop:

Counter = Counter + 1
lcdout $FE, line1,"Counter:", #Counter
PAUSE 100

Goto Loop

HenrikOlsson
- 29th April 2015, 13:51
Well, the obvious, if using an 18F part

Counter VAR LONG
Main: ' Don't use Loop as a label, it's a reserved word.

Counter = Counter + 1
lcdout $FE, line1,"Counter:", DEC Counter ' I think there's a problem with using # with LONG
PAUSE 100

Goto Main

If you don't have an 18F part and all you want is a simple counter then perhaps something like this might work (not tested), it should count to 655 milions and a bit beyond:


TenThousands VAR WORD
Ones VAR WORD

Ones = Ones + 1
If Ones = 10000 THEN
Ones = 0
TenThousands = TenThousands + 1
ENDIF

LCDOUT $FE, line1, Counter: "
IF TenThousands > 0 THEN
LDCOUT DEC TenThousands, DEC4 Ones
ELSE
LCDOUT DEC Ones
ENDIF


/Henrik.

Fredrick
- 3rd May 2015, 20:24
The code works perfect on a 16F886, thank you Henrik!

Ioannis
- 4th May 2015, 06:03
I'll bet that even if you own PBP3 and a 18F series chip, the code Henrik posted, will be using less memory and will be much faster than using Long variables.

Ioannis

HenrikOlsson
- 4th May 2015, 06:15
Thanks for the feedback Fredrick!
Just a note, you don't need PBP3 to use LONGs, support for that were added in v2.5.
You do need an 18F part though. But, as Ioannis said, for a simple counter like that it's probably not worth the "cost" having them enabled.

/Henrik.

Ioannis
- 4th May 2015, 08:43
Right, my error. Its been too long, right?

Ioannis

Art
- 4th May 2015, 09:09
It does work for display, I did 15 digit numbers with a 16F628 the same way it was done on paper in school
with allowance for 16 digit result for a character LCD
Just a shame if you wanted the result in a variable, but you could probably write a good & proper calculator.



123456789012345 *
012345678901234
_______________

_______________