Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:46 11 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : separating a number

Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 12:31pm 07 Jul 2014
Copy link to clipboard 
Print this post

Trying to do a countdown timer in which I have to separate the individual digits from thousands, hundreds, tens and ones (counting from 1 to 9999). This method works, but is there a better way that anyone has come across???

Z = 0
FOR Z = 0 TO 9999
THOU = INT(Z/1000)
HUND = INT((Z-(THOU * 1000))/100)
TENS = INT((Z-((THOU * 1000) + (HUND * 100)))/10)
ONES = Z - ((THOU * 1000) + (HUND * 100) + (TENS * 10))
'DO SOME STUFF
NEXT Z

I hope I won't be too embarrassed with the answers.

Thanks again!!!
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 12:39pm 07 Jul 2014
Copy link to clipboard 
Print this post

Jim
MMBasic has an integer divide implemented by a backslash \
Careful use of it could save a bit of time and code.

THOU = Z\1000
etc.

apart from that I have no suggestions
Bob
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 723
Posted: 12:51pm 07 Jul 2014
Copy link to clipboard 
Print this post

Nice thing,

Could not find any documentation on ebay?!
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 01:05pm 07 Jul 2014
Copy link to clipboard 
Print this post

EBAY???
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5111
Posted: 01:05pm 07 Jul 2014
Copy link to clipboard 
Print this post

Convert it to a string and use mid$ ?
The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3269
Posted: 01:57pm 07 Jul 2014
Copy link to clipboard 
Print this post

MOD would also come in handy, for example:
ONES = Z MOD 10

Geoff Graham - http://geoffg.net
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6220
Posted: 02:02pm 07 Jul 2014
Copy link to clipboard 
Print this post

This a string version.

I am not sure which method will be the fastest or most memory conservative.

DO
INPUT x
xstring$=RIGHT$("000"+STR$(x),4) ' this should work for all versions of MMBasic
units=VAL(MID$(xstring$,4,1))
tens=VAL(MID$(xstring$,3,1))
hundreds=VAL(MID$(xstring$,2,1))
thousands=VAL(MID$(xstring$,1,1))
PRINT thousands,hundreds,tens,units
LOOP UNTIL x = -1
END


Jim
VK7JH
MMedit
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:43pm 07 Jul 2014
Copy link to clipboard 
Print this post

Quick time test using numbers and strings...

test method courtesy of Peter G8JCF - this is running on a uMite 28 at 48mhz...

L=TIMER
FOR Z= 1 TO 9999
THOU = z\1000
HUND = ((Z-(THOU * 1000))\100)
TENS = ((Z-((THOU * 1000) + (HUND * 100)))\10)
ONES = Z - ((THOU * 1000) + (HUND * 100) + (TENS * 10))
NEXT Z
PRINT "Method 1 "; TIMER-L

L=TIMER
FOR Z= 1 TO 9999
xstring$=RIGHT$("000"+STR$(x),4) ' this should work for all versions of MMBasic
units=VAL(MID$(xstring$,4,1))
tens=VAL(MID$(xstring$,3,1))
hundreds=VAL(MID$(xstring$,2,1))
thousands=VAL(MID$(xstring$,1,1))
NEXT Z
PRINT "Method 2 "; TIMER-L


results...

Method 1 7090
Method 2 11544

I'm going to have to try the MOD method next...

Edited by viscomjim 2014-07-09
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6220
Posted: 05:15pm 07 Jul 2014
Copy link to clipboard 
Print this post

L=TIMER
FOR Z= 1 TO 9999
THOU = z\1000
HUND = ((Z-(THOU * 1000))\100)
TENS = ((Z-((THOU * 1000) + (HUND * 100)))\10)
ONES = Z - ((THOU * 1000) + (HUND * 100) + (TENS * 10))
'PRINT thou,hund,tens,ones
NEXT Z
PRINT "Method 1 "; TIMER-L

L=TIMER
FOR Z= 1 TO 9999
xstring$=RIGHT$("000"+STR$(x),4) ' this should work for all versions of MMBasic
units=VAL(MID$(xstring$,4,1))
tens=VAL(MID$(xstring$,3,1))
hundreds=VAL(MID$(xstring$,2,1))
thousands=VAL(MID$(xstring$,1,1))
'
NEXT Z
PRINT "Method 2 "; TIMER-L

L=TIMER
FOR Z= 1 TO 9999
THOU = z\1000
HUND = (Z MOD 1000)\100
TENS = (Z MOD 100)\10
ONES = Z MOD 10
'PRINT thou,hund,tens,ones
NEXT Z
PRINT "Method 3 "; TIMER-L


Results (uMite at 40MHz):
[13:12:37]Method 1 8320
[13:12:50]Method 2 13376
[13:12:55]Method 3 5248

Using MOD is a big improvement!

JimEdited by TassyJim 2014-07-09
VK7JH
MMedit
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 05:38pm 07 Jul 2014
Copy link to clipboard 
Print this post

Hell yeah TassyJim...

Method 1 6978
Method 2 11271
Method 3 4456

uMite at 48mhz

That absolutely rocks!!!!!

This will allow for an easy 100th of a second accuracy on a timer with display update, etc... Very Cool.

Here is my code for quick timer with 100th of a second display on adafruit i2c led display.


DIM T(10)
T(0)=&H3F:T(1)=&H06:T(2)=&H5B:T(3)=&H4F:T(4)=&H66
T(5)=&H6D:T(6)=&H7D:T(7)=&H07:T(8)=&H7F:T(9)=&H6F
I2C OPEN 400,100
I2C WRITE &H70, 0, 1, &H21
I2C WRITE &H70, 0, 1, &H81
I2C WRITE &H70, 0, 1, &HEF
Z=0
SETTICK 10, UPDATE, 1
TM=TIMER
DO:LOOP
UPDATE:
Z = Z + 1
THOU = z\1000
HUND = (Z MOD 1000)\100
TENS = (Z MOD 100)\10
ONES = Z MOD 10
I2C WRITE &H70, 0, 2, &H00, T(THOU)
I2C WRITE &H70, 0, 2, &H02, T(HUND)
I2C WRITE &H70, 0, 2, &H06, T(TENS)
I2C WRITE &H70, 0, 2, &H08, T(ONES)
IF Z = 9999 THEN
TM=TIMER-TM
PRINT "Z = ";Z;" TIMER = ";TM
TM=TIMER
Z=0
ENDIF
IRETURN

Edited by viscomjim 2014-07-09
 
halldave

Senior Member

Joined: 04/05/2014
Location: Australia
Posts: 121
Posted: 09:31pm 07 Jul 2014
Copy link to clipboard 
Print this post

would this code work for MAX7219 8-Bit Red LED Tube Display Module??

regards

David
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 12:54am 08 Jul 2014
Copy link to clipboard 
Print this post

Hi David,

The max7219 has an spi interface, which the uMite can do. I am experimenting with the 8 digit tube display and the 8x8 matrix that uses that chip and will report back soon (i hope) with code for those.

Forum member Boss has written code for the tube display for Maximite here.

A few changes are required for uMite as it has dedicated pins for spi interface and Maximite can use just about any pins for that. Still playing and will keep you posted.Edited by viscomjim 2014-07-09
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025