This code controls a DC motor with a switch. When the switch is pressed, the motor moves one way for 3 seconds. When the switch is pressed again, the motor moves in the reverses direction for 3 seconds.
From: jakilevy@mac.com
Subject: final code
Date: June 20, 2006 10:12:30 PM EDT
To: jakilevy@mac.com
‘******************************************************************************
‘Some explanations: *
‘For my HPWM dutycycle *
‘I Chose 0 because 0 is reverse – explanation follows: *
’128 is SLOW fwd … 255 is fast fwd *
’126 is SLOW REV … 0 fast rev *
‘ 0(rev)…..126(slow rev)…..127(stop)……128(slow fwd)…..255(fast fwd) *
‘ *
‘EXAMPLE HPWM *
‘HPWM 1,0,330 ‘pwm at 0% duty cycle @ 330Hz on CCP1 (portc.2) *
‘HPWM 2,127,330 ‘pwm at 50% duty cycle @ 330Hz on CCP2 (portc.1) *
‘some code pulled off of *
‘http://www.grayfuse.com/blog/wp-content/code/simpleHPWM_452.bas *
‘ Another good link for stepper motors: *
‘http://www.embeddedrelated.com/groups/piclist/show/9220.php *
‘******************************************************************************
‘http://www.melabs.com/resources/articles/boolean.htm
‘ ~ is not
‘ | is OR
‘Todd suggested using a ROLLER SWITCH to DETECT the position of my motor
‘in subroutine, dir = !dir
‘ speed = 255
‘ in subroutine , dutycycle = speed*dir
‘thanks to Rob Faludi for helping with this code
DEFINE OSC 4
‘Enable HPWM on PORTC.1 (HPWM 2)
DEFINE CCP2_REG PORTC
DEFINE CCP2_BIT 1
‘THIS IS FOR A POTENTIOMETER
‘ Define ADCIN parameters
‘ Set number of bits in result
DEFINE ADC_BITS 10
‘ Set clock source (3=rc)
DEFINE ADC_CLOCK 3
‘ Set sampling time in microseconds
DEFINE ADC_SAMPLEUS 10
‘ Set PORTA to all input
TRISA = %11111111
‘ Set up ADCON1 analog and right justify the result
adcon1 = %10000010
‘****************
‘DEFINE VARIABLES
‘****************
‘define adc vars
adcVar VAR WORD ‘ Create variable to store result
i var BYTE ‘this sets i as a variable – COUNTER
dir VAR bit ‘Boolean variable dir
speed VAR BYTE ’0-255
‘Initialize the direction
dir = 0
‘Initialize speed
speed = 255
start:
INCLUDE “modedefs.bas”
OUTPUT PORTB.7 ‘ Test LED
OUTPUT PORTB.6 ‘ Test LED
INPUT PORTD.2 ‘Switch 1
INPUT PORTD.3 ‘Roller Switch – 0
INPUT PORTD.4 ‘Roller Switch – 255
‘BLINK AN LED to Test the PIC
high portd.0
pause 500
low portd.0
pause 500
high portd.0
pause 500
low portd.0
Pause 500
main:
‘Read channel 0
‘ADCIN 0, adcVar
HPWM 2, 127, 1000 ‘ Frequency of 1000 suggested by Todd Holoubek
‘IF Switch 1 is pressed
‘TURN MOTOR
‘FOR a motor – 127 is 0 point
if portd.2 = 1 then
GOSUB motorGo
endif
‘If Switch 1 is NOT pressed – turn LED off – just as a test
if portd.2 = 0 then
‘Test LED’s
low PORTB.7
HIGH PORTD.6
endif
GoTo main
‘Subroutine
motorGo:
‘Drive motor for 3 seconds
‘test LED’S
higH PORTB.7
LOW PORTB.6
‘HPWM Channel,Dutycycle,Frequency
HPWM 2, dir*speed, 1000 ‘ Frequency of 1000 suggested by Todd Holoubek
pause 3000
‘REVERSE Direction
dir = ~dir
‘My regulator started overheating when the switch was NOT pressed
‘because the motor was getting stuck
‘so I decided to do HPWM for only 3 seconds
‘which is how long I needed to motor to run
‘After 3 seconds, I reverse the direction
return
Sphere: Related Content
