//***************************************************************************** // // 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) { 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 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); }
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 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); }
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); } }
/* * ======== PWMTiva_getPeriodCounts ======== * @pre Function assumes that handle is not NULL */ unsigned int PWMTiva_getPeriodCounts(PWM_Handle handle) { unsigned int key; unsigned int period; PWMTiva_HWAttrs const *hwAttrs = handle->hwAttrs; key = Hwi_disable(); period = PWMGenPeriodGet(hwAttrs->baseAddr, hwAttrs->pwmOutput & PWM_GEN_MASK); Hwi_restore(key); return period; }
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 bsp_pwm0_init(void) { /*Enable device*/ SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); /*Set clock divider*/ PWMClockSet(PWM0_BASE,PWM_SYSCLK_DIV_64); /*Enable PWM pin*/ GPIOPinConfigure(LCD_PWM_CHANNEL); GPIOPinTypePWM(LCD_PWM_PORT, LCD_PWM_PIN); /*Configure PWM generator*/ PWMGenConfigure(PWM0_BASE, PWM_GEN_0,(PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC)); /*Set PWM timer period*/ PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0,gSysClock/10000); /*Set width for PWM0*/ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 50*PWMGenPeriodGet(PWM0_BASE,PWM_GEN_0)/100); /*Enable output*/ PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, 0); /*Enable Generator*/ PWMGenEnable(PWM0_BASE, PWM_GEN_0); }
void bsp_pwm_for_sense_init(void) { /*Enable device*/ SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); /*Set clock divider*/ PWMClockSet(PWM0_BASE,PWM_SYSCLK_DIV_1); /*Enable PWM pin*/ GPIOPinConfigure(GPIO_PK5_M0PWM7); GPIOPinTypePWM(SENSE_THRES_PORT, SENSE_THRES_PIN); /*Configure PWM generator*/ PWMGenConfigure(PWM0_BASE, PWM_GEN_3,(PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC)); /*Set PWM timer period*/ PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3,gSysClock/1000000); /*Set width for PWM0*/ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, 1*PWMGenPeriodGet(PWM0_BASE,PWM_GEN_3)/5); /*ensable output*/ PWMOutputState(PWM0_BASE, PWM_OUT_7_BIT, 1); /*Enable Generator*/ PWMGenEnable(PWM0_BASE, PWM_GEN_3); }
void quad_pwm_init(void) { // Motor 1:Port D pin 0 (PD0) // Motor 2:Port D pin 1 (PD1) // Motor 3:Port A pin 6 (PA6) // Motor 4:Port A pin 7 (PA7) // Red LED: Port F pin 1 (PF1) // Blu LED: Port F pin 2 (PF2) // Grn LED: Port F pin 3 (PF3) SysCtlPWMClockSet(SYSCTL_PWMDIV_32); //Enable the peripheral devices/ports SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //Enable LED SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinConfigure(GPIO_PD0_M1PWM0); //motors 3 GPIOPinConfigure(GPIO_PD1_M1PWM1); //motors 4 GPIOPinConfigure(GPIO_PA6_M1PWM2); //motors 1 GPIOPinConfigure(GPIO_PA7_M1PWM3); //motors 2 GPIOPinConfigure(GPIO_PF1_M1PWM5); //red led GPIOPinConfigure(GPIO_PF2_M1PWM6); //blue led GPIOPinConfigure(GPIO_PF3_M1PWM7); //green led //motos 1 and 2 GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_6); GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_7); //motors 3 and 4 GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_1); //the RGB LED GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); //motors config PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN); PWMGenConfigure(PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN); //led config PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN); PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN); //motors 周期配置 PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, 40000); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, 40000); //led PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, 65530); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, 65530); //motors pluse width config PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_3, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); //led pulse width config PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, PWMGenPeriodGet(PWM1_BASE, PWM_OUT_0) / 2); PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, true); //motors 1 PWMOutputState(PWM1_BASE, PWM_OUT_1_BIT, true); //motors 2 PWMOutputState(PWM1_BASE, PWM_OUT_2_BIT, true); //motors 3 PWMOutputState(PWM1_BASE, PWM_OUT_3_BIT, true); //motors 4 PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true); //red led PWMOutputState(PWM1_BASE, PWM_OUT_6_BIT, true); //blue led PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true); //green led PWMGenEnable(PWM1_BASE, PWM_GEN_0); PWMGenEnable(PWM1_BASE, PWM_GEN_1); PWMGenEnable(PWM1_BASE, PWM_GEN_2); PWMGenEnable(PWM1_BASE, PWM_GEN_3); }
void bsp_pwm_for_sense_set(uint16 x, uint16 y) { if(x <= y){ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, x*PWMGenPeriodGet(PWM0_BASE,PWM_GEN_3)/y); } }
//***************************************************************************** // // Configure PWM0 for a 25% duty cycle signal running at 250Hz. This example // also shows how to invert the PWM signal every 5 seconds for 5 seconds. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Set the PWM clock to the system clock. // SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer operation. // InitConsole(); // // Display the setup on the console. // UARTprintf("PWM ->\n"); UARTprintf(" Module: PWM0\n"); UARTprintf(" Pin: PD0\n"); UARTprintf(" Configured Duty Cycle: 25%%\n"); UARTprintf(" Inverted Duty Cycle: 75%%\n"); UARTprintf(" Features: PWM output inversion every 5 seconds.\n\n"); UARTprintf("Generating PWM on PWM0 (PD0) -> State = "); // // The PWM peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); // // For this example PWM0 is used with PortB Pin6. The actual port and // pins used may be different on your part, consult the data sheet for // more information. // GPIO port B needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the GPIO pin muxing to select PWM00 functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PB6_M0PWM0); // // Configure the PWM function for this pin. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6); // // Configure the PWM0 to count up/down without synchronization. // PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_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. // TODO: modify this calculation to use the clock frequency that you are // using. // PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 64000); // // Set PWM0 to a duty cycle of 25%. You set the duty cycle as a function // of the period. Since the period was set above, you can use the // PWMGenPeriodGet() function. For this example the PWM will be high for // 25% of the time or 16000 clock ticks (64000 / 4). // PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, PWMGenPeriodGet(PWM0_BASE, PWM_OUT_0) / 4); // // Enable the PWM0 Bit0 (PD0) output signal. // PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true); // // Enable the PWM generator block. // PWMGenEnable(PWM0_BASE, PWM_GEN_0); // // Loop forever while the PWM signals are generated. // while(1) { // // Print out that the level of PWM is normal. // UARTprintf("Normal \b\b\b\b\b\b\b\b"); // // This function provides a means of generating a constant length // delay. The function delay (in cycles) = 3 * parameter. Delay // 5 seconds arbitrarily. // SysCtlDelay((SysCtlClockGet() * 5) / 3); // // Invert PWM0 signal. // PWMOutputInvert(PWM0_BASE, PWM_OUT_0_BIT, true); // // Print out that the level of PWM is inverted. // UARTprintf("Inverted\b\b\b\b\b\b\b\b"); // // This function provides a means of generating a constant length // delay. The function delay (in cycles) = 3 * parameter. Delay // 5 seconds arbitrarily. // SysCtlDelay((SysCtlClockGet() * 5) / 3); // // Switch PWM0 signal back to regular operation. // PWMOutputInvert(PWM0_BASE, PWM_OUT_0_BIT, false); } }
//***************************************************************************** // // Configure the PWM0 block with dead-band generation. The example configures // the PWM0 block to generate a 25% duty cycle signal on PD0 with dead-band // generation. This will produce a complement of PD0 on PD1 (75% duty cycle). // The dead-band generator is set to have a 10us or 160 cycle delay // (160cycles / 16Mhz = 10us) on the rising and falling edges of the PD0 PWM // signal. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Set the PWM clock to the system clock. // SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // // Set up the serial console to use for displaying messages. This is just // for this example program and is not needed for PWM operation. // InitConsole(); // // Display the setup on the console. // UARTprintf("PWM ->\n"); UARTprintf(" Module: PWM0\n"); UARTprintf(" Pin(s): PD0 and PD1\n"); UARTprintf(" Features: Dead-band Generation\n"); UARTprintf(" Duty Cycle: 25%% on PD0 and 75%% on PD1\n"); UARTprintf(" Dead-band Length: 160 cycles on rising and falling edges\n\n"); UARTprintf("Generating PWM on PWM0 (PD0) -> "); // // The PWM peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); // // For this example PWM0 is used with PortD Pins 0 and 1. The actual port // and pins used may be different on your part, consult the data sheet for // more information. GPIO port D needs to be enabled so these pins can be // used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the GPIO pin muxing to select PWM functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PD0_PWM0); GPIOPinConfigure(GPIO_PD1_PWM1); // // Configure the GPIO pad for PWM function on pins PD0 and PD1. Consult // the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure the PWM0 to count up/down without synchronization. // Note: Enabling the dead-band generator automatically couples the 2 // outputs from the PWM block so we don't use the PWM synchronization. // PWMGenConfigure(PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_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 - 1. // TODO: modify this calculation to use the clock frequency that you are // using. // PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 64000); // // Set PWM0 PD0 to a duty cycle of 25%. You set the duty cycle as a // function of the period. Since the period was set above, you can use the // PWMGenPeriodGet() function. For this example the PWM will be high for // 25% of the time or 16000 clock cycles (64000 / 4). // PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, PWMGenPeriodGet(PWM_BASE, PWM_OUT_0) / 4); // // Enable the dead-band generation on the PWM0 output signal. PWM bit 0 // (PD0), will have a duty cycle of 25% (set above) and PWM bit 1 will have // a duty cycle of 75%. These signals will have a 10us gap between the // rising and falling edges. This means that before PWM bit 1 goes high, // PWM bit 0 has been low for at LEAST 160 cycles (or 10us) and the same // before PWM bit 0 goes high. The dead-band generator lets you specify // the width of the "dead-band" delay, in PWM clock cycles, before the PWM // signal goes high and after the PWM signal falls. For this example we // will use 160 cycles (or 10us) on both the rising and falling edges of // PD0. Reference the datasheet for more information on dead-band // generation. // PWMDeadBandEnable(PWM_BASE, PWM_GEN_0, 160, 160); // // Enable the PWM0 Bit 0 (PD0) and Bit 1 (PD1) output signals. // PWMOutputState(PWM_BASE, PWM_OUT_1_BIT | PWM_OUT_0_BIT, true); // // Enables the counter for a PWM generator block. // PWMGenEnable(PWM_BASE, PWM_GEN_0); // // Loop forever while the PWM signals are generated. // while(1) { // // Print out indication on the console that the program is running. // PrintRunningDots(); } }
/* * ======== PWMTiva_setDuty ======== * @pre Function assumes that handle is not NULL */ void PWMTiva_setDuty(PWM_Handle handle, uint32_t duty) { unsigned int key; uint8_t maxDutySet; uint16_t period; uint16_t pwmGenerator; uint16_t pwmGenPeriod; uint32_t newDuty; PWMTiva_Object *object = handle->object; PWMTiva_HWAttrs const *hwAttrs = handle->hwAttrs; /* Get the PWM generator, generator period and instance output bit */ pwmGenerator = hwAttrs->pwmOutput & PWM_GEN_MASK; pwmGenPeriod = *(object->pwmStatus)->genPeriods + ((hwAttrs->pwmOutput / PWM_OUT_0) - 1); /* Get the generator period */ period = PWMGenPeriodGet(hwAttrs->baseAddr, pwmGenerator); switch(object->dutyMode) { case PWM_DUTY_COUNTS: /* Duty specified as PWM timer counts */ Assert_isTrue(duty <= period, NULL); maxDutySet = (duty >= period); newDuty = (duty && duty < period) ? duty : period; break; case PWM_DUTY_TIME: /* Duty is specified in microseconds */ Assert_isTrue(duty <= pwmGenPeriod, NULL); maxDutySet = (duty >= pwmGenPeriod); newDuty = (duty * (object->pwmStatus)->cyclesPerMicroSec) / (object->pwmStatus)->prescalar; if (!(newDuty) || newDuty > period) { newDuty = period; } break; case PWM_DUTY_SCALAR: /* Duty specified as a number [0 - 65535] scaled to the period */ Assert_isTrue(duty <= PWMTiva_MAX_MATCH_VALUE, NULL); maxDutySet = (duty >= PWMTiva_MAX_MATCH_VALUE); newDuty = period; if (duty && duty < PWMTiva_MAX_MATCH_VALUE) { newDuty = (newDuty * 100) / PWMTiva_MAX_MATCH_VALUE; newDuty = (newDuty * duty) / 100; if (!newDuty) { newDuty++; } } break; default: Log_print1(Diags_USER1, "PWM: (%p) unsupported PWM duty mode; duty unchanged", (UArg) handle); return; } key = Hwi_disable(); /* * The PWM peripheral cannot generate a duty of 0 when in count down mode * or a duty equal to period when in count up-down mode. To generate a 0 * duty when in count down mode, the PWM duty is set * to the period value (output remains active) and output polarity is * inverted. Additionally, if the output is changed from 0 (to a non-zero * value) the PWM output polarity must be inverted again. * * Likewise, to generate a duty equal to the period when in count up-down * mode, the PWM duty is set to the period value and the output polarity is * inverted. * * The code below determines if the PWM is in count down or count up-down * mode and inverts the PWM output polarity if necessary. * For more details refer to the device specific datasheet and the following * E2E post: * http://e2e.ti.com/support/microcontrollers/stellaris_arm/f/471/t/137249.aspx * http://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/354826.aspx */ if (HWREG(hwAttrs->baseAddr + pwmGenerator) & PWM_GEN_MODE_UP_DOWN) { /* * PWM in count up/down mode - invert output if setting duty to or * changing from MAX */ if ((maxDutySet && object->pwmDuty != period) || ((!maxDutySet) && object->pwmDuty == period)) { HWREG(hwAttrs->baseAddr + PWM_O_INVERT) ^= object->pwmOutputBit; } } else { /* * PWM in count down mode - invert output if setting duty to or * changing from 0 */ if (((!duty) && object->pwmDuty) || (duty && (!object->pwmDuty))) { HWREG(hwAttrs->baseAddr + PWM_O_INVERT) ^= object->pwmOutputBit; } } object->pwmDuty = (duty) ? newDuty : 0; PWMPulseWidthSet(hwAttrs->baseAddr, hwAttrs->pwmOutput, newDuty); Hwi_restore(key); Log_print2(Diags_USER2, "PWM: (%p) duty set to: %d", (UArg) handle, duty); }
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_GPIOC); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4); GPIOPinConfigure(GPIO_PE4_M0PWM4); GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_4); GPIOPinConfigure(GPIO_PC4_U4RX); GPIOPinConfigure(GPIO_PC5_U4TX ); GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5); GPIOPinConfigure(GPIO_PE5_M0PWM5); GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_5); PWMGenConfigure(PWM0_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, 6400000); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) / 1.25); PWMOutputState(PWM0_BASE, PWM_OUT_4_BIT, true); PWMGenEnable(PWM0_BASE, PWM_GEN_2); PWMGenConfigure(PWM0_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, 6400000); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_5, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) / 1.25); PWMOutputState(PWM0_BASE, PWM_OUT_5_BIT, true); PWMGenEnable(PWM0_BASE, PWM_GEN_2); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7,68); UARTConfigSetExpClk(UART4_BASE, SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); UARTEnable(UART4_BASE); UARTCharPut(UART4_BASE,'a'); while(1) { SysCtlDelay(4000000*10); temp = 1; UARTSend((uint8_t *)"Fast",4); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) / 1.25); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_5, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) / 1.25); SysCtlDelay(4000000*10); temp = 0; UARTSend((uint8_t *)"Slow",4); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) / 3); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_5, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) / 3); } }
void servo_parallel(void) { uint32_t period = PWMGenPeriodGet(PWM_SERVO_R_BASE, PWM_GEN_SERVO_R); PWMPulseWidthSet(PWM_SERVO_R_BASE, SERVO_R_OUT, ((DUTY_PARALLEL * period)/100)); PWMPulseWidthSet(PWM_SERVO_L_BASE, SERVO_L_OUT, ((DUTY_PARALLEL * period)/100)); }
void servo_tangent(void) { uint32_t period = PWMGenPeriodGet(PWM_SERVO_R_BASE, PWM_GEN_SERVO_R); PWMPulseWidthSet(PWM_SERVO_R_BASE, SERVO_R_OUT, ((DUTY_TANGENT * period)/100)); PWMPulseWidthSet(PWM_SERVO_L_BASE, SERVO_L_OUT, ((DUTY_TANGENT * period)/100)); }