Shop OBEX P1 Docs P2 Docs Learn Events
Logic Gates — Parallax Forums

Logic Gates

JoltnjoeJoltnjoe Posts: 12
edited 2004-12-17 03:04 in BASIC Stamp
I want to have pins 0 through 9 a logic inputs
using pins 11 through 14 as outputs to a BCD Decoder

If I want pins 0-9 at 000000011
to output 11-14 as 0010

·IF IN0=0,IN1=0,IN2=0,IN3=0,IN4=0,IN5=0,IN6=0,IN7=0,IN8=1.IN9=1
THEN LOW 11,LOW 12,HIGH,13,LOW 14

Is there a different way to do this
I want to go from
000000000·· BCD 0000
to
000000001·· BCD 0001
to
000000011·· BCD 0010
to
000000111·· BCD 0011
Etc
111111111·· BCD 1000

To do this as a constant LOOP

Comments

  • JoltnjoeJoltnjoe Posts: 12
    edited 2004-12-14 11:21
    This is Joltnjoe

    Will This program Work?

    ' EM 805 A/D Converter using Flash
    ' This Program is using the binary output of a A/D Converter
    ' using a Flash system

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}

    DO

    ·IF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0) AND
    ··· (IN6=0) AND (IN7=0) AND (IN8=0) AND (IN9=0)
    ··· THEN
    ··· LOW 11
    ··· LOW 12
    ··· LOW 13
    ··· LOW 14
    ·ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0)
    ··· AND (IN6=0) AND (IN7=0) AND (IN8=0) AND (IN9=1)
    ··· LOW 11
    ··· LOW 12
    ··· LOW 13
    ··· HIGH 14
    LOOP

    Help!!!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-14 14:21
    What happens when you have an input like %11111100?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Lee HarkerLee Harker Posts: 104
    edited 2004-12-14 16:38
    It appears you are trying to create an output of a 4 bit binary number that represents the highest bit that is on. If that is the case, there is a nifty Pbasic function called "priority encoder" that would help out.
    Lee
  • agentileagentile Posts: 101
    edited 2004-12-14 17:00
    JoltnJoe,
    If you know that you logic inputs will always be %00000001, %00000011,... %11111111, then you can write a routine to read the bits of your 10-bit word from LSB to MSB. A rough example of the decoder would work like this:

    N=0 ' This is the decimal number
    startread: ' begin subroutine
    if input.lowbit=1 then shiftbits ' read the lowest bit, if = 0, then N = 0, if =1, then go to the next bit
    debug dec N ' print to screen decimal value of N

    shiftbits: ' routine to right shift bits
    input=input>>1 ' move the second bit into the lsb
    N=N+1 ' update N
    if N>9 then done
    goto startread

    done:
    end

    hope this helps,
    agentile
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-14 17:50
    Here's another approach:

    Test_Bits:
      bCount = 0
      hBit = 0
      FOR idx = 0 TO 8
        IF bitsIn.LOWBIT(idx) = 1 THEN
          bCount = bCount + 1
          hBit = idx + 1
        ENDIF  
      NEXT
      RETURN
    


    This returns the bit count and the highest bit set (duplication of NCD operator).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • JoltnjoeJoltnjoe Posts: 12
    edited 2004-12-15 04:04
    I tried this program it ran but did not do what·I planed

    I had the Input statements on one line (the only way it worked)

    but can I have it run like this

    ' EM 805 A/D Converter using Flash
    ' This Program is using the binary output of a A/D Converter
    ' using a Flash system

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}


    DO
    ·IF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0)
    ·AND (IN6=0) AND (IN7=0) AND (IN8=0) AND (IN9=0) THEN
    ··· LOW 11
    ··· LOW 12
    ··· LOW 13
    ··· LOW 14
    ··· PAUSE 500

    ·ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0)
    ·AND (IN6=0) AND (IN7=0) AND (IN8=0) AND (IN9=1) THEN
    ··· LOW 11
    ··· LOW 12
    ··· LOW 13
    ··· HIGH 14
    ··· PAUSE 500

    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0)
    ·· AND (IN6=0) AND (IN7=0) AND (IN8=1) AND (IN9=1) THEN
    ··· LOW 11
    ··· LOW 12
    ··· HIGH 13
    ··· LOW 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0)
    ·· AND (IN6=0) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· LOW 11
    ··· LOW 12
    ··· HIGH 13
    ··· HIGH 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0)
    ·· AND (IN6=1) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· LOW 11
    ··· HIGH 12
    ··· LOW 13
    ··· LOW 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=1)
    ·· AND (IN6=1) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· LOW 11
    ··· HIGH 12
    ··· LOW 13
    ··· HIGH 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=1) AND (IN5=1)
    ·· AND (IN6=1) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· LOW 11
    ··· HIGH 12
    ··· HIGH 13
    ··· LOW 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=1) AND (IN4=1) AND (IN5=1)
    ·· AND (IN6=1) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· LOW 11
    ··· HIGH 12
    ··· HIGH 13
    ··· HIGH 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=0) AND (IN2=1) AND (IN3=1) AND (IN4=1) AND (IN5=1)
    ·· AND (IN6=1) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· HIGH 11
    ··· LOW 12
    ··· LOW 13
    ··· LOW 14
    ··· PAUSE 500
    ·· ELSEIF (IN0=0) AND (IN1=1) AND (IN2=1) AND (IN3=1) AND (IN4=1) AND (IN5=1)
    ·· AND (IN6=1) AND (IN7=1) AND (IN8=1) AND (IN9=1) THEN
    ··· HIGH 11
    ··· LOW 12
    ··· LOW 13
    ··· HIGH 14
    ··· PAUSE 500
    ·· ENDIF

    ·· LOW 11
    ·· LOW 12
    ·· LOW 13
    ·· LOW 14
    ·· PAUSE 5

    LOOP

    Thank You

    Logically Yours

    Joltnjoe
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-15 05:55
    Man, that's a lot of work -- and possibly consuming a lot of code space dealing with all those bits (tricky internally).· Wtih PBASIC 2.5 came the SELECT..CASE structure.· You could recode something like this (partial):

    bitsIn = INS & $1F
    SELECT bitsIn
      CASE %000000000
        LOW 11
        LOW 12
        LOW 13
        LOW 14
      CASE %100000000
        LOW 11
        LOW 12
        LOW 13
        HIGH 14
     
    ...
     
    ENDSELECT
    PAUSE 500
        
        
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • JoltnjoeJoltnjoe Posts: 12
    edited 2004-12-16 04:57
    How does this program identify the pin number?

    How does it work in layman's terms?

    I have a hard time programing but I need to learn

    I tried PIC's but no support

    I built a programable Tank ( Tamaya body ) using the Beo-Bot style design

    Thank You

    Joltnjoe
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-16 15:40
    Perhaps you should download a copy of "What's A Microcontroller?" and give it a read; it will get you up to speed on PBASIC programming pretty quickly.· Here's an explanation of the snippet is suggested:

    bitsIn = INS & $1F         ' copy inputs to bitsIn; clear all but P0 - P8 values
    SELECT bitsIn              ' compare bitsIn to a list of values
      CASE %000000000          ' if bitsIn = %000000000
        LOW 11                 ' make all controll pins low 
        LOW 12
        LOW 13
        LOW 14
      CASE %100000000          ' if bitsIn = %100000000
        LOW 11
        LOW 12
        LOW 13
        HIGH 14                ' make P14 high 
     
    ...
     
    ENDSELECT
    PAUSE 500                  ' wait 0.5 seconds
    
    


    Note that this is only a partial listing; its purpose to show you that SELECT-CASE is a better programming option than the gigantic IF-THEN constructs used in an example above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 12/16/2004 3:42:51 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-16 16:04
    Jon,
    ·· If I may step in just a moment...Yes, he should definately read the WAM tutorial.· Meanwhile, looking at it from his perspective, I think I know what he wants in the explanation...
    JoltnJoe,
    ·· Jon's code works by reading ALL the pins you are reading at the same time, instead of testing each pin one at a time as you were.· Once you read the WAM tutorial you should have a better understanding of how this works, but it boils down to reading the ALL the pins at once:
    bitsIn = INS & $1F         ' copy inputs to bitsIn; clear all but P0 - P8 values
    

    This puts the state of ALL the pins into bitsIn.· Minus the pins you don't need.
    It's the same as your code:
    IF (IN0=0) AND (IN1=0) AND (IN2=0) AND (IN3=0) AND (IN4=0) AND (IN5=0) AND
        (IN6=0) AND (IN7=0) AND (IN8=0) AND (IN9=0)
        THEN
    
    


    Except that you don't need that whole block of code for each change in the state of the pins.· Instead Jon's code is checking the state by using:
    CASE %000000000          ' if bitsIn = %000000000
    



    Which refers to the state of each input you were checking one at a time.· But in your code you're checking the state of each pin, for each pattern.· Jon is checking the state of the pins once, with less code, then comparing those values to the individual patterns you were looking for.

    Definately read the "What's A Microcontroller"...It'll help alot down the road (And now!).· Good luck.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-16 16:38
    Thanks for the assist, Chris. Joe... get WAM (you can download it free), you'll be glad you did.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • JoltnjoeJoltnjoe Posts: 12
    edited 2004-12-17 02:55
    I bought the kit and went through the book

    I am a very logical thinking person but trying to get into programing

    Like I said I like the product because of the support network
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 03:04
    Programming is logic applied to a specific problem. It will come, give yourself time and start small before working your way into very complex projects.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.