Determining variable length (number of digits) possible?


Closed Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2013
    Posts
    1,104

    Default Determining variable length (number of digits) possible?

    Hello.
    I'm writing a custom parser for MAX7219 - it can display data and most of ASCII characters in any digit of connected 8 digit 7 segment led display.
    So say there is an input variable, XVR, which can hold any value from 1 to 9999.
    I have a loop, which will extract via DIG value of individual digits from this variable and send them to MAX7219
    The idea is to have loop repeat N times, where N is the total length of variable, and on each run, increase DIG value by 1, so next digit will be extracted, and in case of 2 digit variable, say from 11 to 99, it won't go thru whole display.
    On ZX Spectrum we had a LEN statement, but here we not.

    Any ideas?
    Name:  done.jpg
Views: 9050
Size:  197.7 KB

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    955


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    I will give a generic answer as my PICBasic skills are weak at the moment

    Assume your variable x contains 1000

    You can strip out digits like this from Right to left with position 0 at the right (least significant position) of the display

    Code:
        position = 0
         if x > 9999 then x=9999                  ' restrict to maximum you can display
    
         while x != 0
             digit[position] = x Mod 10            ' keep the digit
             x = x div 10                                  ' integer divide by 10
             position = position+1                   ' next display position
         end while
    This will automatically stop if the value is 1..9 at 1 digit, or 11..99 at 2 digits and so on.
    Last edited by Jerson; - 8th December 2020 at 03:31. Reason: formatting

  3. #3
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    Thanks, but will this give me variable length?
    I need to know number of digits in variable, to turn off unused digits on display (instead of showing zeroes there).

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    955


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    What you're looking for is commonly known as leading zero blanking. You could continue with filling blanks to digits after the value is zero. That is how you will achieve it.

  5. #5
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    IF XVR > 999 THEN
    [Work the first digit]
    ENDIF ;If not, no action taken
    IF XVR > 99 THEN
    [Work the 2nd digit]
    ENDIF ;If not, no action taken

  6. #6
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    I'm doing a more featured custom decoder, which will display both letters and digits on max7219 7 segment display. It will also have custom decoder for digital values. The code is below:

    Code:
    FOURD=12345 'test value
    dstart=2 'start segment
    DLEN=2 'decode length
    
    TEST: 'test digits
    FOR x=DLEN TO 1 STEP -1
    Z=FOURD DIG X+1	'EXTRACT VALUE
    GOSUB FOURDEC
    PAUSE 500
    NEXT
    GOTO TEST
    
    FOURDEC:	'DIGIT DECODER
    if Z=0 then Y=126
    if Z=1 then Y=48
    if z=2 then Y=109
    if z=3 then Y=121
    if z=4 then Y=51
    if z=5 then Y=91
    if z=6 then Y=95
    if z=7 then Y=112
    if z=8 then Y=127
    if z=9 then Y=115
    low load
    shiftout datapin,clockpin,1,[x+DSTART-1, y]	'DISPLAY THE LETTER
    high load
    RETURN
    This code works, but DLEN instead of cropping digits from the left, crops them from the right - say initial variable is "12345" and I want to display 2 digits from it, I'm getting "45" instead of "12", have no idea why. Same for start position - DSTART starts from rightmost position, instead of leftmost....

  7. #7
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    FOURD=12345
    FOURD=FOURD>>2 should return FOURD=123, right? but it returns 11419 !

  8. #8
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    Forgot to mention, startup shift fixed:
    shiftout datapin,clockpin,1,[X-DLEN+DSTART, y]

  9. #9
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    Quote Originally Posted by CuriousOne View Post
    FOURD=12345
    FOURD=FOURD>>2 should return FOURD=123, right? but it returns 11419 !
    Not sure how that happened. First, you wouldn't get "123", as Right Shift 2 (>> 2) is the same as Divide by 4; in which case 12345 >> 2 = 3086 and not 11419.

    Since you're dealing with a display, you may have to treat each place holder as a unique byte (perhaps value + $30 to convert it to ASCii); "12345" would be saved as $31, $32, $33, $34, $35 in an array. You can then shift your pointer 2 places to look at 123 by itself (as 3 individual bytes, of course).

  10. #10
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    I've written a PC based converter, which deals with ASCII conversion. It generates PBP code for EEPROM.
    Name:  pic2.jpg
Views: 6531
Size:  99.7 KB
    Name:  pic1.jpg
Views: 6510
Size:  73.9 KB

  11. #11
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    So back to shift.
    How to trim one variable from left to right, to value of another variable?

    say trim 12345 by 3 - mean, make 123 digits from it.

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,569


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    12345 / 10 = 1234
    12345 / 100 = 123
    12345 / 1000 = 12
    12345 / 10000 = 1

    /Henrik.

  13. #13
    Join Date
    Feb 2013
    Posts
    1,104


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    This requires too many IFs....
    On spectrum we had X=LEFT(Y,Z)....

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,986


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    You are mixing string with numeric values...

    12345 is a decimal number.

    LEN or any other similar command (LEFT, RIGHT, MID) will take as an input just string characters, not numeric values.

    Ioannis

  15. #15
    Join Date
    Sep 2009
    Posts
    750


    Did you find this post helpful? Yes | No

    Default Re: Determining variable length (number of digits) possible?

    Look up DIG. It isolates single digit, as you need it, without IF...

Similar Threads

  1. Replies: 11
    Last Post: - 9th March 2020, 13:37
  2. Measuring a variable freq/length pulse and generating more pulses.
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 18th September 2014, 10:10
  3. Determining how much RAM is available?
    By Art in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th February 2012, 17:04
  4. Determining LONG compiler
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th September 2011, 20:19
  5. HSERIN for variable length string
    By Pic2008 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 19th February 2010, 06:58

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts