Motor Speed Selector

 

Photograph of the prototype hardware:

 

 

Circuit diagram:

 

 

Controlling program:

 

(*$d 2313 - device of choice *)

(*$c 4    - clock rate *)

(* Demonstrate use of the PWM output to drive a DC motor

   and keypad to enter the speed *)

PROGRAM MotorSpeedController;

(* Function: Waits for a keypress on the numeric keypad; sets the speed of

             a dc motor to one of 10 rates: 0 = slowest, 9 = fastest.

             '#' zeroes the display,

             '*' ends the program.

   Author:   C.A.G.

   Date:     21/05/02

   Version:  1.0 *)

USES

  Keypad, PWMgen;         (* import library modules *)

VAR

  speedCode  : byte;        (* key stroke entered on pad *)

 

(* import from Keypad *)

PROCEDURE InitKeyPad; LIBRARY;

FUNCTION GotKeyPress(VAR keyPress:BYTE):BOOLEAN; LIBRARY;

 

(* import from PWM *)

PROCEDURE InitPWM(freq, dutyCycle: BYTE); LIBRARY;

PROCEDURE PWMon; LIBRARY;

PROCEDURE PWMoff; LIBRARY;

PROCEDURE SetDutyCycle(dutyCycle:BYTE); LIBRARY;

 

BEGIN

  InitPWM(#2,#0); (* set prf to ~1kHz[4MHz clock], duty cycle=0 *)

  InitKeyPad;

  PWMoff;

  REPEAT

    (*wait for a keypress*)

    REPEAT                                        

    UNTIL GotKeyPress(speedCode);

    (* check which key is pressed *)

    IF (speedCode <= #9)AND(speedCode >= #0) THEN (* scan display *)

      BEGIN

        speedCode := #10*(speedCode+#1);  (* convert to duty cycle 10..100 *)

        SetDutyCycle(speedCode);        (* update motor speed *)

      END

    ELSE IF speedCode = #11 THEN (* its a '#'so stop motor *)

      PWMoff

    ELSE IF speedCode = #10 THEN (* its a '*'so start motor *)

      PWMon;                      

  FOREVER;

END.

 

Back