290 9 PROGRAM DEVELOPMENT TECHNIQUES
Listing 9-7 Motor driver program - segmented.
absmotor.h (header file)
#ifndef AbsmotorH #define AbsmotorH
class AbstractMotor
{
private:
int Speed;
public:
AbstractMotor();
void SetSpeed(int speed); int GetSpeed();
virtual void Off()=0; virtual void Forward()=0; virtual void Reverse()=0; virtual void Brake()=0; virtual ~AbstractMotor(){}
};
#endif
absmotor.cpp (function file)
#include "absmotor.h"
AbstractMotor::AbstractMotor()
{
Speed =0;
}
void AbstractMotor::SetSpeed(int speed)
{
Speed = speed;
if(Speed > 255) Speed = 255; // Limit upper value if(Speed < 0) Speed = 0; // Limit lower value
}
int AbstractMotor::GetSpeed()
{
return Speed;
}
pport.h (header file)
9 PROGRAM DEVELOPMENT TECHNIQUES 291
#define PportH
class ParallelPort
{
private:
unsigned int BaseAddress; unsigned char InDataPort1;
public:
ParallelPort(); ParallelPort(int baseaddress);
void WritePort0(unsigned char data); void WritePort2(unsigned char data); unsigned char ReadPort1();
virtual ~ParallelPort(){}
};
#endif
pport.cpp (function file)
#include <dos.h>
#include "pport.h"
ParallelPort::ParallelPort()
{
BaseAddress = 0x378; InDataPort1 = 0;
}
ParallelPort::ParallelPort(int baseaddress)
{
BaseAddress = baseaddress; InDataPort1 = 0;
}
void ParallelPort::WritePort0(unsigned char data)
{
outportb(BaseAddress,data);
}
void ParallelPort::WritePort2(unsigned char data)
{
outportb(BaseAddress+2,data ^ 0x0B);
}
unsigned char ParallelPort::ReadPort1()
292 9 PROGRAM DEVELOPMENT TECHNIQUES
{
InDataPort1 = inportb(BaseAddress+1);
//Inverting Most significant bit to compensate
//for internal inversion by printer port hardware. InDataPort1 ^= 0x80;
//Filter to clear unused data bits D0, D1 and D2 to zero. InDataPort1 &= 0xF8;
return InDataPort1;
}
motor.h (header file)
#ifndef MotorH #define MotorH
#include "absmotor.h" #include "pport.h"
class Motor : public AbstractMotor, public ParallelPort
{
public:
Motor(int baseaddress=0x378); void Off();
virtual void Forward()=0; virtual void Reverse()=0; virtual void Brake()=0; virtual ~Motor(){}
};
#endif
motor.cpp (function file)
#include "motor.h"
Motor::Motor(int baseaddress):ParallelPort(baseaddress)
{
Off();
}
void Motor::Off()
{
WritePort0(0x00);
}
dcmotor.h (header file)
#ifndef DcmotorH #define DcmotorH
9 PROGRAM DEVELOPMENT TECHNIQUES 293
#include "motor.h"
class DCMotor : public Motor
{
public:
DCMotor(int baseaddress=0x378); virtual void Forward();
virtual void Reverse(); virtual void Brake();
};
#endif
dcmotor.cpp (function file)
#include "dcmotor.h"
DCMotor::DCMotor(int baseaddress):Motor(baseaddress)
{
}
void DCMotor::Forward()
{
int j;
for(j = 0; j < GetSpeed(); j++) WritePort0(0x09);
for(;j < 256; j++) WritePort0(0x00);
}
void DCMotor::Reverse()
{
int j;
for(j = 0; j < GetSpeed(); j++) WritePort0(0x06);
for(;j < 256; j++) WritePort0(0x00);
}
void DCMotor::Brake()
{
WritePort0(0x0C);
}
294 9 PROGRAM DEVELOPMENT TECHNIQUES
stepper.h (header file)
#ifndef StepperH #define StepperH
#include "motor.h"
enum MOTORTYPE {UPFS, UPHS, BPFS, BPHS};
class StepperMotor : public Motor
{
private:
MOTORTYPE MotorType; unsigned char Switching[8]; int CycleIndex;
int MaxIndex;
public:
StepperMotor(MOTORTYPE motortype = UPFS, int baseaddress=0x378);
virtual void Forward(); virtual void Reverse(); virtual void Brake();
};
#endif
stepper.cpp (function file)
#include <dos.h>
#include "stepper.h"
StepperMotor::StepperMotor(MOTORTYPE motortype,
int baseaddress): Motor(baseaddress)
{
MotorType = motortype; CycleIndex = 0;
switch(MotorType)
{
case UPFS: MaxIndex = 4; Switching[0] = 0x11; Switching[1] = 0x12; Switching[2] = 0x22; Switching[3] = 0x21; break;
case UPHS: MaxIndex = 8;
9 PROGRAM DEVELOPMENT TECHNIQUES 295
Switching[0] = 0x01;
Switching[1] = 0x11;
Switching[2] = 0x10;
Switching[3] = 0x12;
Switching[4] = 0x02;
Switching[5] = 0x22;
Switching[6] = 0x20;
Switching[7] = 0x21; break;
case BPFS: MaxIndex = 4; Switching[0] = 0x99; Switching[1] = 0x69; Switching[2] = 0x66; Switching[3] = 0x96; break;
case BPHS: MaxIndex = 8; Switching[0] = 0x99; Switching[1] = 0x09; Switching[2] = 0x69; Switching[3] = 0x60; Switching[4] = 0x66; Switching[5] = 0x06; Switching[6] = 0x96; Switching[7] = 0x90;
}
}
void StepperMotor::Forward()
{
if(++CycleIndex == MaxIndex) CycleIndex = 0; WritePort0(Switching[CycleIndex]); delay(259-GetSpeed());
}
void StepperMotor::Reverse()
{
if(--CycleIndex == -1) CycleIndex = MaxIndex -1; WritePort0(Switching[CycleIndex]); delay(259-GetSpeed());
}
void StepperMotor::Brake()
{
switch(MotorType)
{
296 9 PROGRAM DEVELOPMENT TECHNIQUES
case UPFS: case UPHS: WritePort0(0x11); break;
case BPFS: case BPHS: WritePort0(0x99);
}
}
main.cpp (user program)
//*********************************************************** // Motor driver program using Virtual Functions (chapter 8). //*********************************************************** #include <dos.h>
#include <conio.h> #include <stdio.h> #include <stdlib.h> #include <iostream.h>
#include “motor.h” #include "dcmotor.h" #include "stepper.h"
void main()
{
Motor *MotorPtr; int Selection;
clrscr(); |
|
|
|
cout << endl << " |
MOTOR MENU"; |
cout << endl << " |
~~~~~~~~~~" << endl; |
cout << " |
1 |
DC Motor" << endl; |
cout << " |
2 |
UPFS" << endl; |
cout << " |
3 |
UPHS" << endl; |
cout << " |
4 |
BPFS" << endl; |
cout << " |
5 |
BPHS" << endl; |
cout << " |
6 |
QUIT" << endl; |
cout << endl; |
|
|
cout << " |
Select the MOTOR Number: "; |
cin >> Selection;
switch(Selection)
{
case 1: MotorPtr = new DCMotor;
9 PROGRAM DEVELOPMENT TECHNIQUES 297
break;
case 2: MotorPtr = new StepperMotor(UPFS); break;
case 3: MotorPtr = new StepperMotor(UPHS); break;
case 4: MotorPtr = new StepperMotor(BPFS); break;
case 5: MotorPtr = new StepperMotor(BPHS); break;
case 6: return;
default: cout << endl;
cout << " Unspecified Motor type...."; cout << " PRESS a key to END Program!"; getch();
exit(1); // Exits the program
}
if(MotorPtr == NULL)
{
cout << "Memory allocation failed " << endl; getch();
exit(1);
}
cout << "**********************************" << endl; cout << "* CONNECT BOARD POWER SUPPLY NOW *" << endl;
cout << "**********************************" << endl; cout << endl;
cout << " After connecting power,”;
cout << “ press a key to continue " << endl; getch();
cout << endl;
cout << " Keypress changes Speed/Rotation (& Braking)." << endl;
//..... Motor control part starts here .....
MotorPtr->SetSpeed(150); while(!kbhit()) MotorPtr->Forward(); getch(); // clear keyboard buffer
MotorPtr->SetSpeed(255); while(!kbhit()) MotorPtr->Forward(); getch();
MotorPtr->SetSpeed(150);