//***************************************************************************** // // 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); } }
int main(void) { volatile uint32_t ui32Load; volatile uint32_t ui32PWMClock; volatile uint8_t ui8Adjust; ui8Adjust = 254; SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); SysCtlPWMClockSet(SYSCTL_PWMDIV_64); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1); GPIOPinConfigure(GPIO_PF1_M1PWM5); HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01; HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0; GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_DIR_MODE_IN); GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); ui32PWMClock = SysCtlClockGet() / 64; ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1; PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, ui32Load); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8Adjust * ui32Load / 1000); PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_2); while(1) { if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4)==0x00) { ui8Adjust--; if (ui8Adjust < 10) { ui8Adjust = 10; } PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8Adjust * ui32Load / 1000); } if(GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0)==0x00) { ui8Adjust++; if (ui8Adjust > 254) { ui8Adjust = 254; } PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8Adjust * ui32Load / 1000); } SysCtlDelay(100000); } }
void stepAdjust() { if(RightMotorStep >LeftMotorStep +20) { if(banlanceR<=delta) return; banlanceL = banlanceL+delta; banlanceR = banlanceR-delta; PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, banlanceL);//×ó PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, banlanceR);//ÓÒ LeftMotorStep = 0; RightMotorStep = 0; ledTurnover(LED1); } //ÓÒÂÖתËÙСÓÚ×óÂÖ,µ÷½Ú×óÓÒÁ½ÂÖתËÙ²¢Çå0Á½ÂÖÂëÅ̼ÆÊý else if(LeftMotorStep > RightMotorStep+20) { if(banlanceL<=delta) return; banlanceL = banlanceL-delta; banlanceR = banlanceR+delta; PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, banlanceR);//Right PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, banlanceL);//Left LeftMotorStep = 0; RightMotorStep = 0; ledTurnover(LED2); } }
//PWM³õʼ»¯ void initPWM() { //³õʼ»¯ÍâÉè¶Ë¿Ú SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); //ÉèÖÃPWMÐźÅʱÖÓ SysCtlPWMClockSet(SYSCTL_PWMDIV_1); //ÉèÖÃPG2ºÍPD1ΪPWMÊä³ö GPIOPinConfigure(GPIO_PG2_PWM0); GPIOPinConfigure(GPIO_PD1_PWM1); GPIOPinTypePWM(GPIO_PORTG_BASE, PWM_L); GPIOPinTypePWM(GPIO_PORTD_BASE, PWM_R); //ÅäÖÃPWM·¢ÉúÄ£¿é1 PWMGenConfigure(PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN|PWM_GEN_MODE_NO_SYNC); //PWMGenConfigure(PWM_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN|PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 1000); //PWMGenPeriodSet(PWM_BASE, PWM_GEN_3, 800); // PWMPulseWidthSet(PWM_BASE, PWM_OUT_0,700);//×ó // PWMPulseWidthSet(PWM_BASE, PWM_OUT_1,500);//ÓÒ PWMPulseWidthSet(PWM_BASE, PWM_OUT_0,banlanceL);//×ó PWMPulseWidthSet(PWM_BASE, PWM_OUT_1,banlanceR);//ÓÒ //ʹÄÜPWM2Êä³ö PWMOutputState(PWM_BASE,PWM_OUT_0_BIT,true); //ʹÄÜPWM3Êä³ö PWMOutputState(PWM_BASE,PWM_OUT_1_BIT,true); PWMGenEnable(PWM_BASE, PWM_GEN_0); }
void motor(int hastighed) { unsigned long ulPeriod; // //! Compute the PWM period based on the system clock. // ulPeriod = SysCtlClockGet() / hastighed;//! line 99 if the @param hastighed is 100 there is 100 hz on pin PWM 1 with 200 there is 200 hz aso. // // Set the PWM0 period to 440 (A) Hz if on hastighed=100 // PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, ulPeriod); // //! line 109 and 110 Set PWM0 to a duty cycle of 25% and PWM1 to a duty cycle of 75%. // PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, ulPeriod / 4); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, ulPeriod * 3 / 4); if(hastighed > 0)//! line 112 to turn on PWM if they a set to a speed { PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true); } if(hastighed == 0)//! line 116 to turn of PWM and set the port to 0 so they a not run some thing on the motors { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0); PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, false); } }
void PWM_Setup() { SysCtlPWMClockSet(PWM_SYSCLK_DIV_64); // // Configure the PWM generator for count down mode with immediate updates // to the parameters. // PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); // Set the period. For a 50 KHz frequency, the period = 1/50,000, or 20 // microseconds. For a 20 MHz clock, this translates to 400 clock ticks. // Use this value to set the period. // PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, MAXPERIOD); // // Set the pulse width of PWM1 for a 75% duty cycle. // PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, 0); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 0); // 50MHZ is to fast PWMClockSet(PWM1_BASE,PWM_SYSCLK_DIV_64); // // Start the timers in generator 3. // PWMGenEnable(PWM1_BASE, PWM_GEN_3); // // Enable the outputs. // PWMOutputState(PWM1_BASE, PWM_OUT_6_BIT , true); }
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 DRV8833_InitMotorA(){ if(!SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0)) SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); if(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlDelay(3); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_1); PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); uint32_t duty = 0; PWMOutputUpdateMode(PWM0_BASE,PWM_OUT_0_BIT|PWM_OUT_1_BIT,PWM_OUTPUT_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, freq); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, duty); PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true); GPIOPinConfigure(GPIO_PB7_M0PWM1); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_7,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, duty); PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true); PWMGenEnable(PWM0_BASE, PWM_GEN_0); }
void DRV8833_InitMotorB(){ if(!SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0)) SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); if(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlDelay(3); GPIOPinConfigure(GPIO_PB4_M0PWM2); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_1); PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); uint32_t duty = 0; PWMOutputUpdateMode(PWM0_BASE,PWM_OUT_2_BIT|PWM_OUT_3_BIT,PWM_OUTPUT_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, freq); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, duty); PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, true); GPIOPinConfigure(GPIO_PB5_M0PWM3); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_5); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_5,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, duty); PWMOutputState(PWM0_BASE, PWM_OUT_3_BIT, true); HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x30; PWMGenEnable(PWM0_BASE, PWM_GEN_1); }
void tr_motors(uint8_t duty, uint8_t num_cells) { // Change State counter on drive task drivestate = DRIVESTATE_STRAFERIGHT; // Set num of turns wanted num_moves = num_cells; // Set duty cycle dutycycle = duty; // Set periods on Motors uint32_t period = PWMGenPeriodGet(PWM_MOTOR_BASE, PWM_GEN_TOPM); PWMPulseWidthSet(PWM_MOTOR_BASE, M1_OUT, ((duty * period)/100)); PWMPulseWidthSet(PWM_MOTOR_BASE, M2_OUT, ((duty * period)/100)); PWMPulseWidthSet(PWM_MOTOR_BASE, M3_OUT, ((duty * period)/100)); PWMPulseWidthSet(PWM_MOTOR_BASE, M4_OUT, ((duty * period)/100)); // Control Pins //M1:FWD, M2:REV, M3:REV, M4:FWD //M1:FWD, 1A hi, 1B lo GPIOPinWrite(GPIO_PORTD_BASE, PD2_M1A | PD3_M1B, PD2_M1A); //M2:REV, 2A lo, 2B hi, M3:REV, 3A lo GPIOPinWrite(GPIO_PORTE_BASE, PE1_M2A | PE2_M2B | PE3_M3A, PE2_M2B); //M3:REV, 3B hi GPIOPinWrite(GPIO_PORTA_BASE, PA5_M3B, PA5_M3B); //M4:FWD, 4A hi, 4B lo GPIOPinWrite(GPIO_PORTB_BASE, PB4_M4A | PB5_M4B, PB4_M4A); }
//****************************************************************** // Initialise the PWM generator (PWM1 & PWM4) //****************************************************************** void initPWMchan (void) { unsigned long period; SysCtlPeripheralEnable (SYSCTL_PERIPH_PWM); // // Compute the PWM period based on the system clock. // SysCtlPWMClockSet (PWM_DIV_CODE); PWMGenConfigure (PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenConfigure (PWM_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); period = SysCtlClockGet () / PWM_DIVIDER / MOTOR_RATE_HZ; PWMGenPeriodSet (PWM_BASE, PWM_GEN_0, period); PWMGenPeriodSet (PWM_BASE, PWM_GEN_2, period); PWMPulseWidthSet (PWM_BASE, PWM_OUT_1, period * main_duty / 100); PWMPulseWidthSet (PWM_BASE, PWM_OUT_4, period * tail_duty / 100); // // Enable the PWM output signal. // PWMOutputState (PWM_BASE, PWM_OUT_1_BIT, false); PWMOutputState (PWM_BASE, PWM_OUT_4_BIT, false); // // Enable the PWM generator. // PWMGenEnable (PWM_BASE, PWM_GEN_0); PWMGenEnable (PWM_BASE, PWM_GEN_2); }
void setDC(int32_t * DCmotors) { uint32_t period = PWMGenPeriodGet(PWM_MOTOR_BASE, PWM_GEN_TOPM); PWMPulseWidthSet(PWM_MOTOR_BASE, M1_OUT, (((DCmotors[0] * period)/100))-1); PWMPulseWidthSet(PWM_MOTOR_BASE, M2_OUT, (((DCmotors[1] * period)/100))-1); PWMPulseWidthSet(PWM_MOTOR_BASE, M3_OUT, (((DCmotors[2] * period)/100))-1); PWMPulseWidthSet(PWM_MOTOR_BASE, M4_OUT, (((DCmotors[3] * period)/100))-1); }
int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // Enable the GPIO A ports SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // Enable the GPIO E ports SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6); GPIOPinConfigure(GPIO_PB7_M0PWM1); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7); PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 6400000); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0) / 1.25); PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true); PWMGenEnable(PWM0_BASE, PWM_GEN_0); PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, 6400000); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1) / 1.25); PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true); PWMGenEnable(PWM0_BASE, PWM_GEN_1); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6|GPIO_PIN_7); // Set pin 7 as the output port GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_1|GPIO_PIN_2); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_5); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6|GPIO_PIN_7,64); // Give '1' to pin 7 GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_2,4); while(1) { GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6|GPIO_PIN_7,64); // Give '1' to pin 7 GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_2,4); SysCtlDelay(4000000*10); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6|GPIO_PIN_7,0); // Give '1' to pin 7 GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_2,0); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_5,32); SysCtlDelay(400000); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_5,0); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6|GPIO_PIN_7,128); // Give '1' to pin 7 GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_2,2); SysCtlDelay(4000000*10); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6|GPIO_PIN_7,0); // Give '1' to pin 7 GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_2,0); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_5,32); SysCtlDelay(400000); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_5,0); } }
void coast_motors(uint8_t duty) { // Change State counter on drive task drivestate = DRIVESTATE_IDLE; // Set duty cycle to zero PWMPulseWidthSet(PWM_MOTOR_BASE, M1_OUT, 0); PWMPulseWidthSet(PWM_MOTOR_BASE, M2_OUT, 0); PWMPulseWidthSet(PWM_MOTOR_BASE, M3_OUT, 0); PWMPulseWidthSet(PWM_MOTOR_BASE, M4_OUT, 0); }
//***************************************************************************** // //! Updates the duty cycle in the PWM module. //! //! This function programs the duty cycle of the PWM waveforms into the PWM //! module. The changes will be written to the hardware and the hardware //! instructed to start using the new values the next time its counters reach //! zero. //! //! \return None. // //***************************************************************************** static void PWMUpdateDutyCycle(void) { unsigned long ulWidth; // // Get the pulse width of the U phase of the motor. If the width of the // positive portion of the pulse is less than the minimum pulse width, then // force the signal low continuously. If the width of the negative portion // of the pulse is less than the minimum pulse width, then force the signal // high continuously. // ulWidth = (g_ulPWMDutyCycleRoll * ulPeriod) / 65536; //if(ulWidth < g_ulMinPulseWidth) //{ // ulWidth = g_ulMinPulseWidth; //} //if((g_ulPWMClock - ulWidth) < g_ulMinPulseWidth) //{ // ulWidth = g_ulPWMClock - g_ulMinPulseWidth; //} // // Set the pulse width of the ROLL phase of the motor. // PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, ulWidth); PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, ulWidth); // // Get the pulse width of the Pitch phase of the motor. // ulWidth = (g_ulPWMDutyCyclePitch * ulPeriod) / 65536; //if(ulWidth < g_ulMinPulseWidth) //{ // ulWidth = g_ulMinPulseWidth; //} //if((g_ulPWMClock - ulWidth) < g_ulMinPulseWidth) //{ // ulWidth = g_ulPWMClock - g_ulMinPulseWidth; //} // // Set the pulse width of the Pitch phase of the motor. // PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ulWidth); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, ulWidth); // // Perform a synchronous update of all three PWM generators. // PWMSyncUpdate(PWM_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT); PWMSyncUpdate(PWM1_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT); }
int main(void) { unsigned int i; int sum = 0; int current = 0; int hgt_percent = 0; int degrees = 0; STATE = IDLE; initClock(); initADC(); initYaw(); initMotorPin(); initDisplay(); intButton(); initConsole(); initPWMchan(); initCircBuf (&g_inBuffer, BUF_SIZE); // Enable interrupts to the processor. IntMasterEnable(); while (1) { //double dt = SysCtlClockGet() / SYSTICK_RATE_HZ; degrees = yawToDeg(); // Background task: calculate the (approximate) mean of the values in the // circular buffer and display it. sum = 0; for (i = 0; i < BUF_SIZE; i++) { current = readCircBuf (&g_inBuffer); sum = sum + current; } int newHght = ADC_TO_MILLIS(sum/BUF_SIZE); if(initialRead != 0) { hgt_percent = calcHeight(initialRead, newHght); } if (STATE == FLYING || STATE == LANDING){ PIDControl(hgt_percent, SysCtlClockGet() / SYSTICK_RATE_HZ); PWMPulseWidthSet (PWM_BASE, PWM_OUT_1, period * main_duty / 100); PWMPulseWidthSet (PWM_BASE, PWM_OUT_4, period * tail_duty / 100); } displayInfo((int)initialRead, hgt_percent, degrees); } }
/*********************************************** 函数名:MotorPwmFlash(short MOTO1_PWM,short MOTO2_PWM,short MOTO3_PWM,short MOTO4_PWM) 功能:更新四路PWM值 输入参数:MOTO1_PWM,MOTO2_PWM,MOTO3_PWM,MOTO4_PWM 输出:无 描述:四路PWM由定时器2输出,输入范围0-999 备注: ***********************************************/ void MotorPwmFlash(short MOTO1_PWM,short MOTO2_PWM,short MOTO3_PWM,short MOTO4_PWM) { // 调整占空比 PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, PERIOD_TIME); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, PERIOD_TIME); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, ((float)MOTO1_PWM/1000)*PERIOD_TIME); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, ((float)MOTO2_PWM/1000)*PERIOD_TIME); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, ((float)MOTO3_PWM/1000)*PERIOD_TIME); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, ((float)MOTO4_PWM/1000)*PERIOD_TIME); PWMSyncTimeBase(PWM0_BASE, PWM_GEN_0_BIT|PWM_GEN_1_BIT); ; }
void MotorInit(uint8_t report) { SystickInit(1); // 设置PWM时钟和系统时钟一致 SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // 使能PWM外设 SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); // 使能外设端口 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //设置对应管脚的PWM信号功能 GPIOPinConfigure(GPIO_PB4_M0PWM2); GPIOPinConfigure(GPIO_PB5_M0PWM3); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinConfigure(GPIO_PB7_M0PWM1); //设置PWM信号端口 GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); //PWM生成器配置 PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); //设置PWM信号周期 PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, PERIOD_TIME); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, PERIOD_TIME); //设置PWM信号占空比 PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 0); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 0); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, 0); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, 0); // 使能PWM输出端口 PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT|PWM_OUT_1_BIT|PWM_OUT_2_BIT|PWM_OUT_3_BIT, true); // 使能PWM生成器 PWMGenEnable(PWM0_BASE, PWM_GEN_0); PWMGenEnable(PWM0_BASE, PWM_GEN_1); // 使能PWm生成器模块的及时功能. PWMSyncTimeBase(PWM0_BASE, PWM_GEN_0); PWMSyncTimeBase(PWM0_BASE, PWM_GEN_1); if(report) UARTprintf("PWM初始化完成!\r\n"); }
void Handler() { TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, pwm); }
//-------------------------------- void pwm_stepper::Initialize() { g_pTheStepper = this; SysCtlPWMClockSet(SYSCTL_PWMDIV_1); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA6_M1PWM2); GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_6); PWMGenConfigure(Base, Generator, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); // Set the PWM period to 250Hz. To calculate the appropriate parameter // use the following equation: N = (1 / f) * SysClk. Where N is the // function parameter, f is the desired frequency, and SysClk is the // system clock frequency. // In this case you get: (1 / 250Hz) * 16MHz = 64000 cycles. Note that // the maximum period you can set is 2^16. PWMGenPeriodSet(Base, Generator, Default_StartSpeed); PWMPulseWidthSet(Base, PWM_OUT_2, 64); // Allow PWM1 generated interrupts. This configuration is done to // differentiate fault interrupts from other PWM1 related interrupts. PWMIntEnable(Base, PWM_INT_GEN_1); // Enable the PWM1 LOAD interrupt on PWM1. PWMGenIntTrigEnable(Base, Generator, PWM_INT_CNT_LOAD); // Enable the PWM1 interrupts on the processor (NVIC). IntEnable(INT_PWM1_1); // Enable the PWM1 output signal (PA6). // PWMOutputInvert(Base, PWM_OUT_2_BIT, true); PWMOutputState(Base, PWM_OUT_2_BIT, true); }
void motors_init(void) { int i; uint8_t pin_mask; uint32_t motor_per; // Set Pins to output/PWM in GPIO SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); GPIOPinConfigure(GPIO_PF2_M1PWM6); GPIOPinConfigure(GPIO_PF3_M1PWM7); GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3); // Configure the pin for standby control GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6); SysCtlPWMClockSet(SYSCTL_PWMDIV_64); // Configure the PWM for each pin: // Turn on the generators and set the PW to 0 // The output is still OFF. Turn on with set_motor_pwm_state for (i = 0; i < NUM_MOTORS; i++) { PWMGenConfigure(motors[i].pwm_base_module, motors[i].pwm_generator, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); motor_per = calc_cycles(MOTOR_PERIOD); PWMGenPeriodSet(motors[i].pwm_base_module, motors[i].pwm_generator, motor_per); PWMPulseWidthSet(motors[i].pwm_base_module, motors[i].pwm_pin, 0); PWMGenEnable(motors[i].pwm_base_module, motors[i].pwm_generator); pin_mask = 1 << (0x0000000F & motors[i].pwm_pin); PWMOutputState(motors[i].pwm_base_module, pin_mask, 0); } }
//***************************************************************************** // // Set PWM Duty Cycle // //***************************************************************************** void io_pwm_dutycycle(unsigned long ulDutyCycle) { unsigned long ulPWMClock; // // Get the PWM clock // ulPWMClock = SysCtlClockGet()/4; // // Set the global duty cycle // g_ulDutyCycle = ulDutyCycle; // // Set the period. // PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, ulPWMClock / g_ulFrequency); // // Set the pulse width of PWM1 // PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, ((ulPWMClock * g_ulDutyCycle)/100) / g_ulFrequency); }
//***************************************************************************** // // Set up the PWM0 output to be used as a signal source for the CCP1 input // pin. This example application uses PWM0 as the signal source in order // to demonstrate the CCP usage and also to provide a predictable timing // source that will produce known values for the capture timer. In a real // application some external signal would be used as the signal source for // the CCP input. // //***************************************************************************** static void SetupSignalSource(void) { // // Enable the GPIO port used for PWM0 output. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Enable the PWM peripheral and configure the GPIO pin // used for PWM0. GPIO pin D0 is used for PWM0 output. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); GPIOPinConfigure(GPIO_PD0_PWM0); GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0); // // Configure the PWM0 output to generate a 50% square wave with a // period of 5000 processor cycles. // PWMGenConfigure(PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN); PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, TIMEOUT_VAL); PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, TIMEOUT_VAL / 2); PWMOutputState(PWM_BASE, PWM_OUT_0_BIT, 1); PWMGenEnable(PWM_BASE, PWM_GEN_0); }
void DRV8833_MotorB(int32_t _speed){ if(_speed ==0){ HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x30; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4,0); //HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x20; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5,0); return; } uint8_t backwards=0; if(_speed < 0){ _speed = _speed*(-1); backwards=1; } if(_speed >= 1023) _speed = 1022; uint32_t Inverseduty = freq-(_speed*freq/1023); if(!backwards){ HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x10; //GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_4); //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4,GPIO_PIN_4); if(_speed == 1023){ //HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x20; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5,0); } else{ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, Inverseduty); HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) |= 0x20; } } else{ HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x20; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5,GPIO_PIN_5); if(_speed == 1023){ //HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x10; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4,0); } else{ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, Inverseduty); HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) |= 0x10; } } }
void stop_motors(uint8_t duty) { // Change State counter on drive task drivestate = DRIVESTATE_IDLE; dutycycle = duty; // Set periods on Motors uint32_t period = PWMGenPeriodGet(PWM_MOTOR_BASE, PWM_GEN_TOPM); PWMPulseWidthSet(PWM_MOTOR_BASE, M1_OUT, (((100 * period)/100) - 1)); PWMPulseWidthSet(PWM_MOTOR_BASE, M2_OUT, (((100 * period)/100) - 1)); PWMPulseWidthSet(PWM_MOTOR_BASE, M3_OUT, (((100 * period)/100) - 1)); PWMPulseWidthSet(PWM_MOTOR_BASE, M4_OUT, (((100 * period)/100) - 1)); // Set all motors to brake GPIOPinWrite(GPIO_PORTD_BASE, PD2_M1A | PD3_M1B, PD2_M1A | PD3_M1B); GPIOPinWrite(GPIO_PORTE_BASE, PE1_M2A | PE2_M2B | PE3_M3A, PE1_M2A | PE2_M2B | PE3_M3A); GPIOPinWrite(GPIO_PORTA_BASE, PA5_M3B, PA5_M3B); GPIOPinWrite(GPIO_PORTB_BASE, PB4_M4A | PB5_M4B, PB4_M4A | PB5_M4B); }
//-------------------------------- void pwm_stepper::Move(uint32_t nSteps) { m_nSteps = nSteps; m_nSpeed = m_nStartSpeed; m_nPhase = Phase_Accel; PWMGenPeriodSet(Base, Generator, m_nSpeed); PWMPulseWidthSet(Base, PWM_OUT_2, 64); PWMGenEnable(Base, Generator); }
/* * Controls Motor1 speed and direction. * * Duty goes from 0 to 1024 * The bigger the Inverseduty value, the faster the speed. * Remember, bigger match value, lower duty. Since in fast decay the digital pin * is at 1, the lower the duty, the faster the speed. Hence why Inverseduty is used. * (no need to make freq-Inverseduty since less is faster). * * Possibily I have this wrong as I don't remember if the pulse starts high or low. * */ void DRV8833_MotorA(int32_t _speed){ if(_speed ==0){ HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0xC0; return; } uint8_t backwards=0; if(_speed < 0){ _speed = _speed*(-1); backwards=1; } if(_speed >= 1023) _speed = 1022; uint32_t Inverseduty = freq-(_speed*freq/1023); if(!backwards){ HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x40; if(_speed == 1023){ //HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x80; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_7,0); } else{ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, Inverseduty-1); HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) |= 0x80; } } else{ HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x80; if(_speed == 1023){ //HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) &= ~0x40; //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6,0); } else{ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, Inverseduty-1); HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) |= 0x40; } } }
void GreenSetup(volatile uint8_t ui8Adjust){ GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3); GPIOPinConfigure(GPIO_PF3_M1PWM7); PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, ui32Load); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, ui8Adjust * ui32Load / 1000); PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_3); }
void bsp_lcd_bright_control(uint8 duty) { if(duty){ PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, 1); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, duty*PWMGenPeriodGet(PWM0_BASE,PWM_GEN_0)/100); }else{ PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, 0); } }
void RedSetup(volatile uint8_t ui8Adjust){ GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1); GPIOPinConfigure(GPIO_PF1_M1PWM5); PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, ui32Load); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, ui8Adjust * ui32Load / 1000); PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_2); }