Shop OBEX P1 Docs P2 Docs Learn Events
PBASIC/H-bridge question — Parallax Forums

PBASIC/H-bridge question

nol1nol1 Posts: 19
edited 2005-07-20 03:47 in Robotics
Hello All,

I thought this would be fairly simple- but I was wrong. Hopefully someone can help. Im working on a bot that uses an H-bridge to drive two dc motors. In order to make it go forward (or any other direction) 3 pins need to go high. This is fine if I just wanted it to go in one direction, but since I eventually want it to rome anywhere, I have a problem because the stamp sends multiple pins high and sets the h-bridge to "brake" with my current code (because all 5 pins go high). My code currently looks like this:

'Forward
High 1
High 3
High 5
'Backward
High 1
High 4
High 2

I searched the pbasic reference to see if I can set 3 pins high on one line of code, but maybe I missed it. Do I need to set up constants perhaps? Any suggestions would be greatly appreciated My fabrication skills are obviously better than my programming skills!

Thanks,
NOL1

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-16 22:31
    Use the label PINS to set all 16 bits or PINA/PINB to set 8 bits simultaneously.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • nol1nol1 Posts: 19
    edited 2005-07-16 22:58
    Paul,
    Thanks for the reply. Unfortunatley, Im not sure how to do that. Any code example would be great.

    Nol1
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-16 23:30
    Im sorry, I gave you the wrong info (Acrobat Reader corrupts itself each time I use it requiring me to reinstall after every use). The actual label is OUTS, OUTL, OUTH. By assigning a value to the label you set the pins which are defined as outputs to that value, so for your first example you would use OUTL = %00101010, for the second example use OUTL = %00010110.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • nol1nol1 Posts: 19
    edited 2005-07-17 00:40
    Paul,
    Im still not very sure what to do with your info. I apologize for being so slow. I copied and pasted both the OUTl and OUTH labels into a new program,and it did not turn the motors on. How do I integrate those labels into a program to turn the motors on? Im using a BS2.

    Nol1
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-17 01:11
    What version of the stamp are you using?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • nol1nol1 Posts: 19
    edited 2005-07-17 01:34
    Paul,
    Its a BS2
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-17 01:40
    Yes, "Sets" of Stamp pins have been pre-defined.

    OutA is pins 0, 1, 2, 3 used as outputs.
    OutB is pins 4, 5, 6, 7 used as outputs.

    OutL (OutLow, get it?) is 0..7

    So, you can set high or low sets of four pins at a time. Now all you have to do is assign the pins so you can control the groups you desire to control.
  • nol1nol1 Posts: 19
    edited 2005-07-17 03:36
    Allan,
    Thank you for the reply. Could you please give me a small code example using pins 1,3, and 5 that would get set to high?

    Nol1
  • Jonathan MorrisonJonathan Morrison Posts: 23
    edited 2005-07-17 05:15
    I think either of these·lines of code should do it (I am new to PBASIC so I may be wrong):

    OUTL = %01010100···· ' Set pins 1,3 and 5 to HIGH

    or

    OUTS = %0101010000000000···· ' Set pins 1,3 and 5 to HIGH
  • nol1nol1 Posts: 19
    edited 2005-07-17 22:37
    Jonathon,

    Thanks for the reply- It tried your code and unfortunately it did not work. Is there anything else I need to add to your code to make the motors turn on?-ie, declaring varibles or constants or subroutines? Does anyone else have any suggestions?

    Thank you,
    nol1
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-18 15:25
    Have you defined the direction of the pins in question as outputs? I don't understand why else you would be having this problem, unless you have for some reason damaged your BS2, but your original post makes me think this isn't the reason.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • nol1nol1 Posts: 19
    edited 2005-07-19 00:25
    Paul,
    Here is the code I have tried as per yours and Jonathon's suggestion:

    OUTPUT 1
    OUTPUT 3
    OUTPUT 5

    OUTS = %0101010000000000

    This does not turn the two motors on. The bs2 is not damaged (I dont think) because after running the above code, if I run :
    High1
    High3
    High5
    the motors turn on/forward.
    I guess the question I should be asking is-What type of code sequence would you write to make pins 1,3,5 go high using just one line of code?

    Thanks again,
    nol1
  • Jonathan MorrisonJonathan Morrison Posts: 23
    edited 2005-07-19 02:09
    Maybe the problem is that the pins directions are input on startup. HIGH sets direction then sets the pin to 1. Try this:

    DIRS = %0101010000000000 ' Set pins 1,3 and 5 to be outputs
    OUTS = %0101010000000000 ' Set pins 1,3 and 5 to high
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-19 02:35
    Pins 1-5 are all dedicated to the H-bridge correct? You only need to do a DIRL = %00111110 once at the beginning of your program and those pins will be kept as output, OUTL = %00101010 to go forward and OUTL = %00010110 to go backward.

    Jonathan, binary numbers are expressed in most significant bit order for PBASIC, I believe your binary numbers were written in least significant bit order.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 7/20/2005 3:47:56 AM GMT
  • nol1nol1 Posts: 19
    edited 2005-07-19 04:20
    Paul,
    Thank you very much for your patience. Your code worked for the backward motion part, but only one motor turns with your forward code. What exactly is the logic behind the "%00010110" scheme? I understand that it sets pins high or low with the 1 or 0 respectively, but when I try random combos of the 1's and 0's not much happens. If I could understand how to translate:
    High 1
    High 5
    High 3
    Into the %0101.... format, I would be a very happy man!

    Thanks again,
    nol1
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-19 13:44
    No problem, glad to help.

    Ok %00010110 is a binary number, the rightmost number is called the least significant bit, the leftmost bit is called the most significant bit. When you write a value to OUTL you are assigning values to pins 0 through 7, the rightmost bit represents pin 0, leftmost bit represents pin 7, so reading the above binary value you are setting pins 1, 2 and 4 high (each digit place represents the value of the pin 1=high, 0=low), you are also simultaneously setting bits 3 and 5 to low. The other bits (0,6 and 7) do not affect the value on pins 0,6 and 7 because the DIRL = %0011... set them as inputs.

    As far as OUTL = %00101010 not working for forward, Im not sure because I don't know what points in the H-bridge are connected to which pins. (ie I can't determine if setting pins 1, 3 and 5 high and pins 2 and 4 low will correctly generate the signals to make the motor go forward, Id have to see a schematic or your description of thier connections). You state only one motor works with the forward code, are you sure the two motors·(and H-bridges) are connected the same? And you are duplicating the same signal to both h-bridges by setting pins 1,3 and 5 high and 2 and 4 low?

    To summerize the binary numbers again, assigning a binary value to OUTL:
    OUTL = %00101010  corresponds to
    pins    76543210  so you setting bits 1, 3 and 5 high and 2 and 4 low. 
     
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 7/19/2005 2:16:43 PM GMT
  • nol1nol1 Posts: 19
    edited 2005-07-20 03:04
    Paul,

    Good news. With your help, Im finally able to proceed! The problem was (besides my ignorance) was that the DIRL = %0011110 code was missing a 1, so pin # 1 was being setup initially as an input instead of an output. Thank you for explaining the binary scheme- it was easier than I thought. So far, my now successful and very simple code looks like this:

    DIRL = %00111110 'sets pins to output
    ' 76543210 pin numbers
    Main:
    OUTL = %00101010 'forward
    PAUSE 3000
    OUTL = %00010110 'backward
    PAUSE 3000
    OUTL = %00011010 'Right turn
    PAUSE 3000
    OUTL = %00100110 'Left turn
    PAUSE 3000
    GOTO Main

    Thanks again,
    nol1
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-20 03:47
    Oh I missed that, thanks, glad you figured out the missing bit.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
Sign In or Register to comment.