oDCMotor2 Object
An Object that controls a DC motor using a L293D H-Bridge.
Description:
 

An oDCMotor2 Object is a Hardware Object that uses three digital I/O lines to control the direction, speed, and braking of a DC Motor by communicating with an H-Bridge circuit which handles the voltage and current requirements of the DC Motor it is connected to.  The H-Bridge that the oDCMotor Object is designed to work with has 2 drive inputs and a PWM input.  The L293 and L298 H-Bridges are configured in this way.

The oDCMotor2 Object is capable of driving a DC Motor at 255 different speeds, in forward or reverse plus free spinning, active braking, and friction braking modes.  The speed at which the motor spins is specified by a single value which can be configured to have a range of 0 to 255 or -128 to +127.

When the speed value is configured with a range of -128 to +127, the speed value is then also used to specify the direction that the motor turns.  When this value is 0, the motor is at free-spinning rest.  When the value is a positive number from 1 to 127, the motor turns forward at the speed indicated by the value.  The higher the value, the faster the motor turns.  When the value is a negative number from -1 to -128, the motor turns backwards at the speed indicated by the value.  The lower the number, the faster the motor turns in reverse.

When the speed value is configured with a range of 0 to 255, the motor turn in a direction specified by a direction value at the rate specified by the speed value. When this value is 0, the motor is at free-spinning rest.  When the value is 1 to 255, the motor turns forward at the speed indicated by the value.  The higher the value, the faster the motor turns. 

The physical direction that the motor will turn can be set to:  forward turns clockwise and reverse turns counter-clockwise  or  forward turns counter-clockwise and reverse turns clockwise.

A brake value is used to apply brakes and when this value is set to 1, the speed value is ignored and the motor will quickly stop. The method used to stop the motor is active braking.

The maximum number of oDCMotor Objects that can be dimensioned in a single application program is 2, limited only be the number of PWM channels available in the OOPic which are used to control the speed of the DC Motors.

Each PWM channel's cycle rate can be set as high as 5-Mhz or as low as 312.5-Khz.

Operation:
  The oDCMotor2 Object monitors the Value and the Brake properties, and based on their numeric values controls the direction, speed, and braking of a DC Motor by outputting control signals to a H-Bridge motor driver circuit such as the L293D.  To control the speed of the motor, a Pulse-Width-Modulated (PWM) clock cycle is outputted on the I/O line specified by the IOLineP property.  The direction that the motor spins is controlled by setting one of the control lines specified by IOLine1 and IOLine2 properties to 1 while setting the other to 0. Whether or not the brakes are applied is controlled by outputting the same value to both of the control lines specified by IOLine1 and IOLine2 properties.  The L293D H-Bridge monitors the state of all three of these control lines and controls the DC motor accordingly.

In normal operation, a 0 in the Value property will cause the PWM line to output 0 Volts, causing the motor to be at a full stop.  If the Value property is greater than 0, then the Pulse-Width of the PWM output is set to correspond to the Value property, the Direction property is set to 0, the I/O line specified by the IOLine1 property is set to 5 Volts and the I/O line specified by the IOLine2 property is set to 0 Volts. This will cause the DC Motor to go forward at a speed specified by the Value property.  The higher the number (up to 127), the faster the motor will turn.  If the Value property is less than 0, then the Pulse-Width of the PWM output is set to correspond to the absolute value of the Value property, the Direction property is set to 1, the I/O line specified by the IOLine1 property is set to 0 Volts and the I/O line specified by the IOLine2 property is set to 5. This will cause the DC Motor to go backwards at a speed specified by the Value property.  The lower the number (down to negative 128), the faster the motor will turn in reverse.

If the Unsigned property is set to 1, then the operation is modified so that the Value property is always expected to be a positive value.  In this case, the application program is expected to set the value of the Direction property, and the control lines specified by the IOLine1 and IOLine2 properties are set according to the value that the Direction property was set to.

The actual direction that the DC Motor spins when going in the "forward" direction can be reversed by setting the InvertOutD property to 1.

When the Brake property is set to 1, Both of the control lines specified by the IOLine1 and IOLine2 properties are set to the value specified by the InvertOutB property, and the PWM output is set to the value specified by the Mode property.  The required state of the PWM while braking is on depends on the control circuit that is used. If the motor is being driven by an L293D H-Bridge, then setting both the control lines to the same value will cause the Motor to generate power back into itself, effectively driving the motor back the other way which causes the motor to come to an abrupt stop.  In this case, the Mode property need to be 1 which will set the PWM line to 5 Volts so that the motor will remain on to allow the braking power to be applied.  However, if the Brake line is connected to a mechanical braking system, then the Mode property needs to be 0 which will set the PWM line to 0 Volts so that the Motor will shut off when the mechanical brake is applied.

The combination of the Period and the PreScale properties specify the PWM clock cycle's frequency. The PreScale property specifies how many times a 5-Mhz clock is divided. This scaled down 5-Mhz clock is used to increment a Period-Duration Counter. A full PWM clock cycle has been reached when the Period-Duration Counter reaches the value specified by the Period property. The PreScale property can be set to divide the 5-Mhz clock by 1, 4, or 16, giving frequencies of 5-Mhz, 1.25-Mhz, and 312.5-Khz. The Period property can be set to any value from 1 to 255. This results in 765 possible frequencies.

The Speed of the DC Motor is controlled by the Pulse-Width of the logic-high portion of the cycle determined by the Value property. At the beginning of each PWM clock cycle, the I/O Line specified by the IOLineP property is set to 5 Volts. Each time the Period-Duration Counter is incremented it is compared to the value specified by the Value property. When the Period-Duration Counter reaches the value specified by the Value property the I/O Line is set to 0-Volts.

The Period property also dictates the resolution of the PWM pulse. If the Period property is set to 255 then the Value property's range is 0 - 255. Likewise, if the Period property is set to 10, then the Value property's effective range is 0 - 10, because any value above 10 will never be reached by the Period-Duration Counter.

The Operate property specifies if the High going PWM pulse is generated. When the Operate property is set to 1 the oPWM Object starts generating PWM pulses. When the Operate property is set to 0 the PWM pulses are discontinued and the I/O Line specified by the IOLineP is set to 0-Volts. If a current PWM pulse was in progress it is cut short and the I/O line is immediately set to 0-Volts.  The control lines specified by the IOLine1 and IOLine2 properties are not changed when Operate is set to 0.

Remarks:
  Two physical Pulse-Width-Modulators are implemented within the OOPic.

The oDCMotor2 Object is functionally identical to the oDCMotor object with the exception of how the Direction and Brake control lines work.

Storage size & Availability:
 

The following table lists the size and availability of the oDCMotor2 Object:

ObjectSizeFirmware VersionCompiler Version
oDCMotor2 5 Bytes B.2.0 5.00
Properties:
 

The following table lists the Properties of the oDCMotor2 Object:

Property Description
Address

Returns a pointer to the address of the oDCMotor2 Object instance.

Data-Type: Address, Read-Only

Data-Range: 0 - 127

Brake

A value that specifies if the brake output is on.

When the Brake property is set to 1 the brake output goes active and the PWM output is set to the value specified by Mode.  The active output level of the Brake output depends on the value of the InvertOutB property.

Data-Type: Bit, Flag

Data-Range: 0 - 1

BrakeInvertOutBBrake Output
00 0 Volts
01 5 Volts
10 5 Volts
11 0 Volts
Direction

Specifies the direction that the motor will turn or indicates the direction that the motor is turning.

If the Unsigned property is 0 the Direction property is set to 0 if the Value property is a positive number and set to 1 if the Value property is a positive number.
If the Unsigned property is 1 the Direction property controls the output values of the control lines specified by the IOLine1 and IOLine2 properties.

Data-Type: Bit, Flag

Data-Range: 0 - 1

DirectionDirection Output
0 0 Volts
1 5 Volts
InvertOutB

A value that specifies if the brake output is inverted.

The InvertOutB property specifies what logic level the brake output gets set to when the brake output goes active.

Data-Type: Bit

Data-Range: 0 - 1

InvertOutBBrakeIOLine1 OutputIOLine2 Output
00 Controlled by Direction Controlled by Direction
01 5 Volts 5 Volts
10 Controlled by Direction Controlled by Direction
11 0 Volts 0 Volts
InvertOutD

A value that specifies if the direction output is inverted.

Data-Type: Bit, Flag

Data-Range: 0 - 1

InvertOutDDirectionIOLine1 OutputIOLine1 Output
00 5 Volts 0 Volts
01 0 Volts 5 Volts
10 0 Volts 5 Volts
11 5 Volts 0 Volts
IOLine1

A value that specifies the physical I/O Line to use for the first control output line.

Data-Type: Byte

Data-Range: 0 - 31

IOLine2

A value that specifies the physical I/O Line to use for the second control output line.

Data-Type: Byte

Data-Range: 0 - 31

IOLineP

A value that specifies the physical I/O Line to use for the PWM output.

Data-Type: Bit

Data-Range: 0 - 1

The following table list the values of the IOLineP Property.
IOLinePConstantDescription
0  

The PWM signal is outputted on I/O Line 18. 
18 can be assigned to IOLineP in place of 0 but it will read back as 0.

1  

The PWM signal is outputted on I/O Line 17. 
17 can be assigned to IOLineP in place of 1 but it will read back as 1.

Mode

A value that specifies the level that the PWM line will be set to when the brake is on.

Data-Type: Bit

Data-Range: 0 - 1

ModeConstantDescription
0 cvOff When the brake line is set to 1, the PWM output will be set to 0 Volts.
1 cvOn When the brake line is set to 1, the PWM output will be set to 5 Volts.
Operate

A value that specifies if the motor operates.

Data-Type: Bit, Flag

Data-Range: 0 - 1

Period

A value that specifies the frequency of the PWM cycle.

Data-Type: Byte

Data-Range: 0 - 255

PreScale

A value that specifies the base frequency of the Period property.

Data-Type: Nibble

Data-Range: 0 - 3

The following table list the values of the PreScale Property.
PreScaleDescription
0

The base frequency is 5-Mhz

1

The base frequency of 5-Mhz is divided by 4 to get 1.25Mhz

2

The base frequency of 5-Mhz is divided by 16 to get 312 500hz

3

The base frequency of 5-Mhz is divided by 16 to get 312 500hz (same as 2)

Unsigned

A value that specifies if the Value property is signed or unsigned.

Data-Type: Bit

Data-Range: 0 - 1

UnsignedValue Range
0 -127 to +127
1 0 to 255
Value

A value that specifies how fast and optionally in what direction the motor turns.

Data-Type: Byte, Default

Data-Range: 0 to 255 or -128 to +127

ValueUnsignedPWM OutputDirection
1270 Set to 1 (full-speed) Set to 0
1 to 1260 Set to Value * 2 Set to 0
00 Set to 0 (off) Set to 0
-1 to -1270 Set to abs(Value) * 2  Set to 1
-1280 Set to 1 (full-speed) Set to 1
01 Set to 0 (full-stop) unchanged
1 to 2541 Set to Value unchanged
2551 Set to 1 (full-speed) unchanged
Connections:
  The L293D H-Bridge is a dual H-Bridge and each one of its H-Bridges is a set of 2 push-pull drivers that use a total of 3 I/O lines.  IOLine1 and IOLine2 can be any 2 I/O lines of the OOPic's 31 I/O lines.  IOLineP can be I/O line 17 or 18.  The L293D H-Bridge requires a power supply that is capable of handling enough current to dive the attached DC motor.
L293D H-Bridge Driver and DC Motor.
L293D
Pin
NameDescriptionFirst set
I/O Name
Second set
I/O Name
OOPic
I/O Line
1 Enable 1 Output Enable for Outputs 1 and 2 IOLineP   17 or 18
2 Input 1 Control Line for Output 1 IOLine1   Any*
3 Output 1 Output 1      
4 Gnd Gnd Gnd   Gnd
5 Gnd Gnd Gnd   Gnd
6 Output 2 Output 2      
7 Input 2 Control Line for Output 2 IOLine2   Any*
8 Vs Supply Voltage of up to 36      
9 Enable 2 Output Enable for Outputs 3 and 4   IOLineP 17 or 18
10 Input 3 Control Line for Output 3   IOLine1 Any*
11 Output 3 Output 3      
12 Gnd Gnd   Gnd Gnd
13 Gnd Gnd   Gnd Gnd
14 Output 4 Output 4      
15 Input 4 Control Line for Output 4   IOLine2 Any*
16 Vss Logic Supply Voltage volts      
Caution:
  Do NOT connect the outputs of the OOPic directly to a DC Motor. The TTL circuitry of the OOPic is not designed to drive the power requirements of a DC Motor.  Doing so can potentially damage the OOPic's TTL outputs.

*Caution: I/O lines 16 through 23 are used for special purposes.  Be sure not to specify any of these I/O lines if these special functions are in use.

Example:
  In the following example, the oDCMotor2 Object is used.
' This program ramps a DC Motor's speed
' up and down / forwards and backwards.

Dim A As New oDCMotor2

Sub Main() 
  A.IOLineP = 17
  A.IOLine1 = 27
  A.IOLine2 = 25
  A.Operate = 1
  A.Brake = cvOff
  Do
    For A = -100 To 100
      OOPic.Delay = 2
    Next A
    For A = 100 To -100 step -1
      OOPic.Delay = 2
    Next A
  Loop 
End Sub
See Also:
 

oDCMotor - An Object that controls a DC motor using a LMD18200T H-Bridge.
oDCMotorMT - An Object that controls a DC motor using a Mondo-tronics H-Bridge.
oDCMotorWZ - An Object that controls a DC motor using a Wirz 203 H-Bridge.
oNavCon
- An Object that provides navigation control for two Motor Objects.
oNavConIE - An Object that provides navigation control for two Motor Objects using two quadrant encoders.