Sie sind auf Seite 1von 2

PWMService

PublicFunctions:InitAllPWM,SetDuty,SetDirection,SetFlywheelDuty,
SetServoAngle
ModuleFunctions:InitPWMWheelMotors

InitAllPWM
InitializePWMWheelMotors
EndInitAllPWM

InitPWMWheelMotors
InitializePWMforothermotorline:
InitializeDummyvariableusingvolatiletoavoidoveroptimization
EnabletheclocktothePWMmodule(PWM0)
EnabletheclocktoportB
SelectthePWMclockassystemclock/32
MakesurethePWMclockhasgottengoing
Forwheelmotors:
DisablePWMwhileinitializing
ProgramgeneratorAtogoto1atrisingcompareA,0atfalling
compareA
ProgramgeneratorBtogoto1atrisingcompareB,0atfalling
compareB
SetthePWMperiod,sincecountingupanddown,initializetheload
registertohalfthedesiredtotalperiod
Forflywheelmotorandservo:
DisablePWMwhileinitializing
ProgramgeneratorAtogoto1atrisingcompareA,0atfalling
compareA
ProgramgeneratorBtogoto1atrisingcompareB,0atfalling
compareB
SetthePWMperiodforservo(oktobethesameforflywheel),since
countingupanddown,intitializetheloadregistertohalfthedesired
totalperiod
EnablethePWMoutputs(0and1arewheelmotors,2and3areflywheel
andservo)
ConfigureportBpinstobePWMoutputs
ChoosetomapPWMtothosepins
Enablepins4,5,6,7onportBfordigitalI/O
Enablepins4,5,6,7asoutputs
Setup/downcountmodeforwheelmotors
Setup/downcountmodeforservoandflywheel
Ensuremotorsareoff
Ensureflywheelisoff
Ensureservoisatresetangle
EndInitPWM

SetDuty
IfWHICH_MOTORequalsMOTOR_ONE
IfSET_DUTYis0
SettheDCto0%byprogrammingthecomparevaluetothe
loadvalue
SincetheCmpADnaction(settozero)wins,theoutput
stayslow(per/20=per/2)

ElseifSET_DUTYis100
Settheloadvalueto0sincethedesiredhitimeisPer/2
(per/2per/2=0)
SincetheCmpADnaction(wassettozero)wins,change
generatoreventstoalwaysforcehi
Elsesetdesireddutycycle

ElseifWHICH_MOTORequalsMOTOR_TWO
IfSET_DUTYis0
SettheDCto0%byprogrammingthecomparevaluetothe
loadvalue
SincetheCmpADnaction(settozero)wins,theoutput
stayslow(per/20=per/2)

ElseifSET_DUTYis100

Settheloadvalueto0sincethedesiredhitimeisPer/2
(per/2per/2=0)
SincetheCmpADnaction(wassettozero)wins,change
generatoreventstoalwaysforcehi
Elsesetdesireddutycycle
EndSetDuty

SetDirection
IfWHICH_MOTORequalsMOTOR_ONE
IfDirectionequalsFORWARD

Setbitformotor1directionhi
Else

Setbitformotor1directionlo

Else
IfDirectionequalsFORWARD
Setbitformotor2directionhi
Else
Setbitformotor2directionlo
EndSetDirection

SetFlywheelDuty
SettheDCto0%byprogrammingthecomparevaluetotheloadvalue
SincetheCmpADnaction(settozero)wins,theoutputstayslow
SettheDCto100ismorecomplex
Settheloadvalueto0siincethedesiredhitimeisperiod/2
SinceCmpADnaction(setto0)wins,changegeneratoreventstoalways
forcehi
Turnoffenable
Elsesetdesireddutycycle
EndSetFlywheelDuty

SetServoAngle
Adjustdutycyclebetween1msand2msbasedonvalueofangle
EndSetServoAngle

EndPWMService

Das könnte Ihnen auch gefallen