//***************************************************************************** // // The interrupt handler for the for PWM0 interrupts. // //***************************************************************************** void PWM0IntHandler(void) { // // Clear the PWM0 LOAD interrupt flag. This flag gets set when the PWM // counter gets reloaded. // PWMGenIntClear(PWM_BASE, PWM_GEN_0, PWM_INT_CNT_LOAD); // // If the duty cycle is less or equal to 75% then add 0.1% to the duty // cycle. Else, reset the duty cycle to 0.1% cycles. Note that 64 is // 0.01% of the period (64000 cycles). // if((PWMPulseWidthGet(PWM_BASE, PWM_OUT_0) + 64) <= ((PWMGenPeriodGet(PWM_BASE, PWM_GEN_0) * 3) / 4)) { PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, PWMPulseWidthGet(PWM_BASE, PWM_OUT_0) + 64); } else { PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 64); } }
void rampGenericPWM(bool rampDirection, uint32_t pwmBase, uint32_t pwm, uint32_t pwmBit, uint32_t delay) { // Start value is the current PWM pulse width regardless the ramp direction uint32_t i = PWMPulseWidthGet(pwmBase, pwm); if (rampDirection == PWM_RAMP_UP) { uint32_t targetPwmLoad = PWMGenPeriodGet(pwmBase, PWM_GEN_3); PWMOutputState(pwmBase, pwmBit, true); for (; i < targetPwmLoad; i += PWM_STEP) { PWMPulseWidthSet(pwmBase, pwm, i); SysCtlDelay(delay); } } else // rampDirection == PWM_RAMP_DOWN { for (; i > PWM_LOW; i -= PWM_STEP) { PWMPulseWidthSet(pwmBase, pwm, i); SysCtlDelay(delay); } PWMOutputState(pwmBase, pwmBit, false); } }
void setSpeed (unsigned int final_speed, uint32_t motor){ //Takes in the speed to set and the motor to apply it too. unsigned int speed = PWMPulseWidthGet(PWM0_BASE, motor); //Find out if speed is less than or greater than the new speed // and accelerate/decelerate to it. while (speed > final_speed){ if (speed > final_speed){ speed -= 5; } else { speed = final_speed;} PWMPulseWidthSet(PWM0_BASE, motor, speed); } while (speed < final_speed ){ if (speed < final_speed){ speed += 5; } else { speed = final_speed;} PWMPulseWidthSet(PWM0_BASE, motor, speed); } }