' Program: MOT41-V6.OSC ' (revised: 02/25/08) ' This program ramps DC Motor speed ' up and down & forwards and backwards ' using the SN754410 H-Bridges on the ' OOBOT40 controller board. ' Modified for OOPic III+ v.C.1.1 ' chip, and compiler version 6.x. ' NOTES: ' 1. OOBOT40 jumpers J8+J9 should have lower ' 2 pins on each connected together (PWM ' out to h-bridge enable pins - factory ' default setting). ' 2. connect motor 1 across HDR3 M1+M2. ' 3. connect motor 2 across HDR4 M5+M6. ' 4. apply motor power from +M pin to ground. ' --- Declarations --- Dim x As Byte ' general index Dim M1 As New oDCMotor2 ' motor 1 Dim M2 As New oDCMotor2 ' motor 2 Dim pwmfrq As New oPWM ' PWM motor speed Dim frq As New oFreqH Dim spk As New oSpeaker '//////////////////////////////////////// Sub Main() Call setup_motors Do Call jingle Call motrun_dir1 Call play1 Call motrun_dir2 Loop End Sub ' ///////////////////////////////////////// Sub setup_motors() ' OOPic PWM peripheral must be configured ' separately. Same PWM setup is used for ' both motors. Call setup_pwm ' the following setup is based upon the ' OOBOT40 pcb wiring. ' motor M1 connected between HDR3 M1+M2. M1.IOLineP = 18 ' PWM1 to U3-En1 M1.IOLine1 = 24 ' RD0 to U3-In1 M1.IOLine2 = 25 ' RD1 to U3-In2 M1 = 0 M1.Operate = 1 M1.Brake = cvOff ' motor M2 connected between HDR4 M5+M6. M2.IOLineP = 17 ' PWM2 to U4-En1 M2.IOLine1 = 28 ' RD4 to U4-In1 M2.IOLine2 = 29 ' RD5 to U4-In2 M2 = 0 M2.Operate = 1 M2.Brake = cvOff End Sub ' /////////////////////////////////////// Sub setup_pwm() ' PWM frequency = 5Mhz / (prescale*(period+1). ' NOTE - VERY IMPORTANT - the magnitude of ' DCMotor2 Object values MUST BE LESS THAN ' the PWM period value chosen. pwmfrq.Period = 255 ' Specific motors run better for specific PWM ' frequency values. Inexpensive toy motors ' generally run better with low PWM frequencies. ' Select prescale value from 1 of following 3 lines ' for best operation of specific motors used. ' pwmfrq.PreScale = 0 ' prescale=1; freq=19531hz ' pwmfrq.PreScale = 1 ' prescale=4; freq=4882hz pwmfrq.PreScale = 2 ' prescale=16; freq=1220hz pwmfrq.Operate = 1 End Sub ' /////////////////////////////////////// Sub motrun_dir1() ' ramp velocity up+down in one direction. ' (duty cycle starts above 0, since most motors ' do not turn on for very low values) For x = 40 To 125 Step 5 M1 = x M2 = x ' send busy indicator. Call pip1 Cmd.Delay = 200 Next x Cmd.Delay = 1000 For x = 40 To 125 Step 5 M1 = 125 - x M2 = 125 - x Call pip1 Cmd.Delay = 200 Next x End Sub ' //////////////////////////////////////// Sub motrun_dir2() ' ramp velocity up+down in other direction. For x = 40 To 125 Step 5 M1 = -1*x M2 = -1*x Call pip2 Cmd.Delay = 200 Next x Cmd.Delay = 1000 For x = 40 To 125 Step 5 M1 = -1*(125 - x) M2 = -1*(125 - x) Call pip2 Cmd.Delay = 200 Next x End Sub ' //////////////////////////////////// Sub play1() frq.Beep(64000,100) frq.Beep(64500,100) frq.Beep(65000,100) frq.Beep(63500,50) frq.Beep(63600,50) frq.Beep(0,100) frq.Beep(5500,100) frq.Beep(0,100) frq.Beep(5500,100) frq.Beep(0,100) End Sub ' /////////////////////////////////////// Sub pip1() frq.Beep(64286,2) ' 2000 hz End Sub ' /////////////////////////////////////// Sub pip2() frq.Beep(63036,2) ' 1000 hz End Sub ' /////////////////////////////////////// Sub jingle() E :E :E :P :E :E :E :P :E :G :C :D :E ooPIC.Delay = 100 End Sub Sub E() ' Plays note 'E' spk.Beep(61743,80,20) End Sub Sub G() ' Plays note 'G' spk.Beep(62346,80,20) End Sub Sub C() ' Plays note 'C' spk.Beep(60757,80,20) End Sub Sub D() ' Plays note 'D' spk.Beep(61278,80,20) End Sub Sub P() ' Pauses 1/10 of a second ooPIC.Delay = 100 End Sub