Professional Documents
Culture Documents
Module
ServoMotor.c
Revision
1.0.1
Description
This is a template file for implementing a simple service under the
Gen2 Events and Services Framework.
Notes
History
When Who
--------------------
****************************************************************************/
#define Forward 0
#define Backward 1
#define CWTurn 2
#define CCWTurn 3
#define LeftMotor 0
#define RightMotor 1
#define LoadPWMPow 11
#define FirePWM 9
#define defaultLat 7
#define defaultPow 4
void DefaultBall(void){
//put servo motors into default position by setting default PWMs: defaultLat and
defaultPow to the two motors
HWREG( PWM0_BASE+PWM_O_1_CMPA) = (HWREG(PWM0_BASE+PWM_O_1_LOAD)) * (100 -
defaultLat)/100;
HWREG( PWM0_BASE+PWM_O_1_CMPB) = (HWREG(PWM0_BASE+PWM_O_1_LOAD)) * (100 -
defaultPow)/100;
}
#define ChargePWMPow 12
void ChargeBall(void){
//put power servo motor to ChargeBall position by setting ChargePWMPow to it
HWREG( PWM0_BASE+PWM_O_1_CMPB) = (HWREG(PWM0_BASE+PWM_O_1_LOAD)) * (100 -
ChargePWMPow)/100;
}
#define FirePWMLat 9
void FireBall(void){
//put latch servo motor to FireBall position by setting FirePWM to it
HWREG( PWM0_BASE+PWM_O_1_CMPA) = (HWREG(PWM0_BASE+PWM_O_1_LOAD)) * (100 -
FirePWM)/100;
}
#define ResetPWMPow 3
void ResetBall(void){
//put power servo motor to ResetBall position by setting ResetPWMPow to it
HWREG( PWM0_BASE+PWM_O_1_CMPB) = (HWREG(PWM0_BASE+PWM_O_1_LOAD)) * (100 -
ResetPWMPow)/100;
}
//servo motor PWM are PB4: M0PWM2 (M0PWM1GenA) & PB5:M0PWM3 (M0PWM1GenB)
void InitServo(void){
// PB4(M0PWM2) is used to generate PWM (up-down counting)
// start by enabling the clock to the PWM Module (PWM0)
HWREG(SYSCTL_RCGCPWM) |= SYSCTL_RCGCPWM_R0;
// make sure that the PWM module clock has gotten going
while ((HWREG(SYSCTL_PRPWM) & SYSCTL_PRPWM_R0) != SYSCTL_PRPWM_R0)
;
// Set the PWM period. Since we are counting both up & down, we initialize
// the load register to 1/2 the desired total period. We will also program
// the match compare registers to 1/2 the desired high time
HWREG( PWM0_BASE+PWM_O_1_LOAD) = ((PeriodInMS * PWMTicksPerMS))>>1; // Period
20ms
// Set the initial Duty cycle on A to 50% by programming the compare value
// to 1/2 the period to count up (or down). Technically, the value to program
// should be Period/2 - DesiredHighTime/2, but since the desired high time is
1/2
// the period, we can skip the subtract
HWREG( PWM0_BASE+PWM_O_1_CMPA) = HWREG( PWM0_BASE+PWM_O_1_LOAD)>>1; // init
servo to have 50% duty cycle, possibly need to tune
// now choose to map PWM to those pins, this is a mux value of 4 that we
// want to use for specifying the function on bit4 and bit5
HWREG(GPIO_PORTB_BASE+GPIO_O_PCTL) =
(HWREG(GPIO_PORTB_BASE+GPIO_O_PCTL) & 0xff00ffff) +
(4<<(4*BitsPerNibble)) + (4<<(5*BitsPerNibble));
// set the up/down count mode, enable the PWM generator and make
// both generator updates locally synchronized to zero count
HWREG(PWM0_BASE+ PWM_O_1_CTL) = (PWM_1_CTL_MODE | PWM_1_CTL_ENABLE |
PWM_1_CTL_GENAUPD_LS |
PWM_1_CTL_GENBUPD_LS);
}