//***************************************************************************** // // 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(); } }
//***************************************************************************** // // Configure Timer1B as a 16-bit PWM with a duty cycle of 66%. // //***************************************************************************** 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); // // The Timer1 peripheral must be enabled for use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); // // For this example CCP3 is used with port E pin 4. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port E needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Configure the GPIO pin muxing for the Timer/CCP function. // This is only necessary if your part supports GPIO pin function muxing. // Study the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PE4_CCP3); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for Timer/PWM operation. // InitConsole(); // // Configure the ccp settings for CCP pin. This function also gives // control of these pins to the SSI hardware. Consult the data sheet to // see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypeTimer(GPIO_PORTE_BASE, GPIO_PIN_4); // // Display the example setup on the console. // UARTprintf("16-Bit Timer PWM ->"); UARTprintf("\n Timer = Timer1B"); UARTprintf("\n Mode = PWM"); UARTprintf("\n Duty Cycle = 66%%\n"); UARTprintf("\nGenerating PWM on CCP3 (PE4) -> "); // // Configure Timer1B as a 16-bit periodic timer. // TimerConfigure(TIMER1_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM); // // Set the Timer1B load value to 50000. For this example a 66% duty // cycle PWM signal will be generated. From the load value (i.e. 50000) // down to match value (set below) the signal will be high. From the // match value to 0 the timer will be low. // TimerLoadSet(TIMER1_BASE, TIMER_B, 50000); // // Set the Timer1B match value to load value / 3. // TimerMatchSet(TIMER1_BASE, TIMER_B, TimerLoadGet(TIMER1_BASE, TIMER_B) / 3); // // Enable Timer1B. // TimerEnable(TIMER1_BASE, TIMER_B); // // Loop forever while the Timer1B PWM runs. // while(1) { // // Print out indication on the console that the program is running. // PrintRunningDots(); } }
//***************************************************************************** // // Configure PWM0 for a load interrupt. This interrupt will trigger everytime // the PWM0 counter gets reloaded. In the interrupt, 0.1% will be added to // the current duty cycle. This will continue until a duty cycle of 75% is // received, then the duty cycle will get reset to 0.1%. // //***************************************************************************** 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 PWM0 operation. // InitConsole(); // // Display the setup on the console. // UARTprintf("PWM ->\n"); UARTprintf(" Module: PWM0\n"); UARTprintf(" Pin: PD0\n"); UARTprintf(" Duty Cycle: Variable -> "); UARTprintf("0.1%% to 75%% in 0.1%% increments.\n"); UARTprintf(" Features: "); UARTprintf("Variable pulse-width done using a reload interrupt.\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 Pin0. 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 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_PD0_PWM0); // // 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_PORTD_BASE, GPIO_PIN_0); // // Configure the PWM0 to count down without synchronization. // PWMGenConfigure(PWM_BASE, PWM_GEN_0, 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(PWM_BASE, PWM_GEN_0, 64000); // // For this example the PWM0 duty cycle will be variable. The duty cycle // will start at 0.1% (0.01 * 64000 cycles = 640 cycles) and will increase // to 75% (0.5 * 64000 cycles = 32000 cycles). After a duty cycle of 75% // is reached, it is reset to 0.1%. This dynamic adjustment of the pulse // width is done in the PWM0 load interrupt, which increases the duty // cycle by 0.1% everytime the reload interrupt is received. // PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 64); // // Enable processor interrupts. // IntMasterEnable(); // // Allow PWM0 generated interrupts. This configuration is done to // differentiate fault interrupts from other PWM0 related interrupts. // PWMIntEnable(PWM_BASE, PWM_INT_GEN_0); // // Enable the PWM0 LOAD interrupt on PWM0. // PWMGenIntTrigEnable(PWM_BASE, PWM_GEN_0, PWM_INT_CNT_LOAD); // // Enable the PWM0 interrupts on the processor (NVIC). // IntEnable(INT_PWM0); // // Enable the PWM0 output signal (PD0). // PWMOutputState(PWM_BASE, PWM_OUT_0_BIT, true); // // Enables the PWM generator block. // PWMGenEnable(PWM_BASE, PWM_GEN_0); // // Loop forever while the PWM signals are generated and PWM0 interrupts // get received. // while(1) { // // Print out indication on the console that the program is running. // PrintRunningDots(); } }