Motor Controller MC7 PWM Need Help with code...

Hi all I am having trouble getting 1 motor to go ccw and 1 motor to go cw at the same time
Below is the code that I am using, it works fine otherwise. I have tried making·· high fwd_portleft· and·high rev_portright and the others low but it seems to lock up there.. Please any help would be appreciated.

'· written by Carl A. Kollar modified by FV

'Purpose:· control by BS2px of the MC7 motor Controller
·Can be used with
'·either 12 or 24 volt models.
'Motor action: The motor (1) Gradually increases in speed in the forward
'· direction until it reaches full speed. (2) Gradually
'· reduces speed until it stops. (3) Gradually increases speed
'· in the reverse direction until it reaches full rev. speed
'· (4) Gradually decreases speed until it comes to a stop.
'· (5) Does it all over again.

Define Constants & Variables
enable_portleft con 0: low enable_portleft 'disable MC7 while initializing
pwm_out_portleft con 1
fwd_portleft con 2
rev_portleft con 3
enable_portright con 4: low enable_portright 'disable MC7 while initializing
pwm_out_portright con 5
fwd_portright con 6
rev_portright con 7
duty var byte
cycles var byte: cycles = 10

min_speed var byte: min_speed = 52 'pwm value for 1.0V out
max_speed var byte: max_speed = 165 'pwm value for 3.2V out
x var byte: x = 0· 'counter variable
pause_time var word: pause_time = 15 'pause between counter increments
max_speed_time var byte: max_speed_time = 150

low fwd_portleft 'Put MC7 in "neutral" by making both direction ports low
low rev_portleft
high enable_portleft 'enable the left controller
low fwd_portright 'Put MC7 in "neutral" by making both direction ports low
low rev_portright
high enable_portright 'enable the right controller

The Main Program
'· (All sub-routine names are self-explanatory)
·gosub set_forward_direction
·gosub min_to_max
·gosub max_to_min
·gosub set_reverse_direction
·gosub min_to_max
·gosub max_to_min
·gosub set_right_direction
·gosub min_to_max
goto main


'Bring motor speed gradually from a stop to full speed
·for x = min_speed to max_speed 'gradually increase speed
·pwm pwm_out_portleft,x,cycles

·pwm pwm_out_portright,x,cycles

·pause pause_time 'wait specified time
·for x = 1 to max_speed_time 'stay at max. speed for awhile
·pwm pwm_out_portleft,max_speed,cycles
·pwm pwm_out_portright,max_speed,cycles


'Bring motor speed gradually to a stop from full speed
·for x = max_speed to min_speed 'gradually decrease speed
·pwm pwm_out_portleft,x,cycles

·pwm pwm_out_portright,x,cycles

·pause pause_time 'wait specified time
·high fwd_portleft 'enable forward
·high fwd_portright 'enable forward

·low rev_portleft 'reverse not enabled

·low rev_portright 'reverse not enabled

·low fwd_portleft 'forward not enabled
·high rev_portleft 'enable reverse

·low fwd_portright 'forward not enabled
·high rev_portright 'enable reverse

high·fwd_portleft 'Left motor forward· enabled
low·rev_portleft ' left motor reverse not enabled
low fwd_portright 'Right motor forward not enabled
high rev_portright 'right motor enable reverse

Post Edited (Bozook) : 12/15/2005 12:57:07 PM GMT


  • 11 Comments sorted by Votes Date Added
  • edited December 2005 Posts: 0Vote Up0Vote Down
    If you are using PWM to power your motors, you will only get one running at a time. The PWM function will have to complete for one motor before the other one starts up. To get simultaneosly running motors, you will need a dedicated motor controller, or you will need to "roll your own" using a faster processor such as the SX.

    Truly Understand the Fundamentals and the Path will be so much easier...

    Post Edited (Tom Walker) : 12/13/2005 7:17:22 PM GMT
  • edited December 2005 Posts: 6Vote Up0Vote Down
    Hi Tom thanks for the reply. I am using 2 MC7 Motor Controllers made by Diverse electronics On there site they show a BS2 hooked up to 1 motor and a sample code to test the motor. I have the code working on both motors forward and backward. I just cannot get one motor to go in rev while the other motor goes in forward.
  • edited December 2005 Posts: 0Vote Up0Vote Down
    Looking at page 3 of the docs for the MC7 ( ) which has a DPDT switch to control the direction. You would need to replace that switch with an electronic one using transistors and have the stamp turn on which ever one you want for the direction you want to go.

  • edited December 2005 Posts: 6Vote Up0Vote Down
    Hi Gearhead thanks for the reply. Page 5 in the ( ) Shows how to use the stamp with the controller. I have the motors working forward and backwards with the controllers the way they are. I have talked to Diverse Electronics and they say it's in the stamps coding .. They say (Diverse) that they sell allot of them and other customers make the controllers do what I want them to do. It's just a matter how's its coded Diverse says. (racking my Brains)..

    Post Edited (Bozook) : 12/14/2005 12:40:37 AM GMT
  • edited December 2005 Posts: 6Vote Up0Vote Down
    Here is a link to a Picture of the controllers and hook up to the BS2px

    and the·schematic NOTE! The schematic only shows one controller

    Post Edited (Bozook) : 12/14/2005 1:55:29 AM GMT
  • edited December 2005 Posts: 0Vote Up0Vote Down

    Looking at the code for your set_left_direction should look something like this.


    ' Set left motor MC7 to reverse
    low fwd_portleft 'forward not enabled
    high rev_portleft 'enable reverse

    ' Set right motor MC7 to forward
    high fwd_portright 'enable forward
    low rev_portright 'reverse not enabled

    If you set high rev_portleft and high fwd_portright should make it turn to the left. Your code posted above looks like it would turn to the right.

    When it reaches the set_left_direction sub, what does it do?
  • edited December 2005 Posts: 6Vote Up0Vote Down
    GearHead: When it reaches the set_left_direction sub, what does it do?

    When it gets to that gosub it stops or freez's and don't do noting. Thats my problem. I have changed the ports around and all and it just freez's at that point.
  • edited December 2005 Posts: 0Vote Up0Vote Down
    I was having a look at this, because it seems there should be a simple solution if both motors work fine in one direction. For one thing, i have no idea why the constants are there? Is that supposed to be " pin connections" ? Hope you have a pin directive set for the outputs. And, as far as the code goes, if you look at the input states of the controllers in reverse, when you go back to the turn right sub, you are trying to drive an input high, when it is already in a high state. This could be locking up the contoller or the stamp.I would try either driving all the contoller inputs low before going to the turn sub, or try it at the start of the program, to see if that may be the trouble. Or even omit the code for the motor that is already programmed to move in the proper direction already in the sub. I don't know if i am right on this, but it would be worth a shot. Never used a px, may be issues with that also.

  • edited December 2005 Posts: 6Vote Up0Vote Down
    Hi Kelvin thanks for the reply. In answer to your question "Is that supposed to be " pin connections" Yes con 1-7 are the pin directives. I have tried putting the sub set_right_direction first and have tried it by its self. does the same thing anywhich way ..freeze's up..

    Post Edited (Bozook) : 12/15/2005 1:10:34 PM GMT
  • edited December 2005 Posts: 0Vote Up0Vote Down
    Someone can correct me if i am wrong, but i would dump the pin constant thing, and try a regular pin definition, stating the proper direction of the pins.

Sign In or Register to comment.