Shop OBEX P1 Docs P2 Docs Learn Events
Need help dividing a variable... sytax question — Parallax Forums

Need help dividing a variable... sytax question

Rangerx52Rangerx52 Posts: 3
edited 2011-04-24 10:45 in General Discussion
So program specifics aside, i'm not actually using a stamp, i'm using a pic16f628a, running picbasic pro.

basically the exact same thing, just 40 bucks cheaper, and no built in regulator (like its good for anything anyway)


So in my program, i have a 4 digit variable, which is transferred to the chip through a series of pulses, via the COUNT command.

the Count result is stored as a 4 digit variable, which we will call X. Lets say that X has a value of 1234

i need to store each digit in a separate variable, and have it represented as a solitary digit.

so one variable is 1, one is 2, one is 3, and one is 4. The digit is needed for its binary value.

Now i know that i can select bits or bytes from a variable, but i dont know how to single out a numeric digit from a string, and store it

any ideas?

Comments

  • Rangerx52Rangerx52 Posts: 3
    edited 2011-04-24 02:32
    figured part of it out.

    for anyone wondering:
    an example;
    for 38
    38 in decimal, converted to binary is 0010 0110
    naturally the first and second nybbles do not correspond to 3 and 8, since 3 is 0011 and 8 is 1000. i would need 00111000 in order to make the variable's bits useful

    but 00111000 IS 38.

    in hex.
    so all i need to do is store my variable as a hex value instead of a decimal one, and the integers will be reflected as individual nybbles..


    Here comes the last obstacle-
    if i have a variable, 38. but i want to change it to $38. how would i add the $ prefix?
    i thought about maybe something like "x=$(x) or the like, but i cannot seem to find the magic command
  • Mike GMike G Posts: 2,702
    edited 2011-04-24 06:16
    38 decimal is 0x26 or 0010_0110
    0011_1000 is 0x38 which is 56 decimal

    What you want is called binary-coded decimal.

    If you're writing the data to a screen you probably what ASCII encoding. Either way, binary-coded decimal or ASCII encoding are easy to find with an Internet search. You might even try picbasic pro support as this is a Parallax forum not a PIC or picbasic pro support forum.
  • Rangerx52Rangerx52 Posts: 3
    edited 2011-04-24 07:14
    I'm aware of bytecode, and the applications for it- none of which are applicable to my project. I am not driving an led screen.

    I am also aware that 38 in decimal is 26 in hex- my point was that the end binary string i WANT, is the result of inputting the number i have available, but identified as a hex value instead of a decimal one. I'll avoid having it in decimal at all if i can.
    All i need to know is this:
    if i can say
    A=10
    B=A

    how can i end up with
    A=10
    B=$A (in this case, $10)
    how can i change the value of B, using a decimal number?


    Picbasic is near identical to pbasic, but that's immaterial, since i'm using a bs2 to dry run the code since it has an easier debug function
  • vaclav_salvaclav_sal Posts: 451
    edited 2011-04-24 08:12
    All sarcasm aside, you need to go back to first grade or even kindergarten or wherever you learned how to represent / convert numbers in given numbering system.

    The number can be represented by polynomial.

    Your decimal example

    1234
    1*1000 + 2*100 + 3*10 + 4*1

    Now you need to reverse the process by dividing the number (dividend) by divisor ( starting with the highest) getting the result ( quotient) and subtracting the polynomial member value.
    In the attached example I used fixed length of four digits as you require, I'll leave it to your skills to apply this to any length / size of number and any numbering system.
    Please - do not call the result / quotient BCD. The individual quotiens are still in binary, the entire representation of 1234 in your memory - four locations will be BCD.
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    
    DecimalDigit VAR Word     ' dividend
    Index VAR Word
    Divisor VAR Word          ' divisor
    Digit VAR Byte           ' quotient
    
    Divisor = 1000
    ' if a / b = c THEN a is the dividend, b the divisor, AND c the quotient.
    
    
    Main:
      DEBUG "Enter decimal "
      DEBUGIN DEC DecimalDigit
      DEBUG CR
      DEBUG ? DecimalDigit
    
      FOR Index = 0 TO 3                    ' fixed / constant  digits in number!
    
      Digit = DecimalDigit / Divisor
      DEBUG ? Digit                         ' wanted result
    
      DecimalDigit = decimalDigit - (digit * divisor)
      DEBUG ? DecimalDigit
    
      Divisor = Divisor/ 10
      DEBUG ? divisor
      NEXT
    
     'STOP
    
    END
    
    
    
  • Mike GMike G Posts: 2,702
    edited 2011-04-24 10:45
    Rangerx52 wrote:
    if i can say
    A=10
    B=A

    how can i end up with
    A=10
    B=$A (in this case, $10)
    how can i change the value of B, using a decimal number?

    Here's lies the confusion because B is already equal to 10. Decimal 10 is 0x0Ah ($A). 0x10h ($10) is decimal 16. Logically, 10 does not equal 16 and therefore 0x0A does not equal 0x10.

    The integer number ten can be represented as 10, 0x0A, $A, 100/10, 1 * 10 but it is always 10.

    It sounds like you're trying to encode numbers. If you want 0x0A to take up two memory locations like | 1 | 0 | then you have to do what vaclav_sal suggested above. That's called BCD. If you added 0x30 to each of the memory locations, you would have the ASCII code for 1 (0x31) and 0 (0x30). Which if you sent to the DEBUG terminal would display 10.
      DEBUG num1, num2
    
Sign In or Register to comment.