Shop OBEX P1 Docs P2 Docs Learn Events
Directly converting binary inputs into decimal? — Parallax Forums

Directly converting binary inputs into decimal?

PasserbyPasserby Posts: 17
edited 2009-03-06 05:01 in BASIC Stamp
For example, if I have something like this:

a = 1
b = 0
c = 1
d = 0

a being number 8, 1010 = 10.

is there a code out there which helps converts these values?

cheers.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-05 05:52
    You can directly access the bits of a variable. Read the Basic Manual pages 81-91.

    If you have a variable defined as a nibble (4 bits) called X, you can say:

    X.bit3 = a
    X.bit2 = b
    X.bit1 = c
    X.bit0 = d

    At this point, X is 10.

    You can also do: X = ((a << 1 + b) << 1 + c) << 1 + d
  • PasserbyPasserby Posts: 17
    edited 2009-03-05 06:02
    x VAR Nib
     
    a = 1
    b = 0
    c = 1
    d = 0
     
    x.bit3 = a
    x.bit2 = b
    x.bit1 = c
    x.bit0 = d
    
    




    Therefore, x would have the value of 10, right?

    So, meaning that if the binary is an 8 bit value now, I'll just have to extend the coding to x.bit7 = value, and change the variable type to Byte? [noparse]:)[/noparse]

    Post Edited (Passerby) : 3/5/2009 6:10:43 AM GMT
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-03-05 07:59
    That's right.

    x VAR byte
    x.bit0=h
    x.bit1=g
    ....
    x.bit7=a

    Where are you going with this? Your subject line is "directly converting binary INPUTs to decimal". It may be as easy as,

    x = INL

    That reads all 8 input pins, p0 to p7, directly into the variable x.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • PasserbyPasserby Posts: 17
    edited 2009-03-06 05:01
    Okay the basic idea of my work is that I will be implementing a phone with my basic stamp, which uses a "phone card" to dail the DTMF frequency out. There will be 2 places where·input will be possible, but I would like to filter it that only 1 input is allowed at a single time to avoid clashing of phone number. The·number buttons for the phone is using a push button switch, I can use the code such as this?

    /mock up code for input 8
    x Var Nib 
    
    
    a = 1 
    b = 0 
    c = 1 
    d = 0 
    
    /output = 8 
    
    x.bits3 = a 
    x.bits2 = b 
    x.bits1 = c 
    x.bits0 = d 
    
    when x = 8, num8 = 1 
    
    
    if num8 = 1 and (switch button 8)=1 then start /start is the beginning of the whole program. 
    if num8 = 1 and (switch button 8)=0 then DTMF(8) 
    if num8 = 0 and (switch button 8)=1 then DTMF(8) 
    else next
    
    PS: This is just a mock up coding, so it will not work, but just an idea.
    


    This is just a mock up code, I know it will not work.
    This is just a rough "sketch" of my idea. [noparse]:)[/noparse]

    Post Edited (Passerby) : 3/6/2009 5:07:57 AM GMT
Sign In or Register to comment.