Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * MSP432 PWM control of tri-color LED
- *
- * Description: DriverLib example using PWM to control color and brightness
- * of tri-color LED. LED1 reflects the current duty cycle change
- * rate. sw1 turns the LED1 blink on/off. The PWM duty cycle controls
- * the brightness of the tri-color LED (LED2). Color change occurs each
- * complete cycle through the PWM duty cycles (brightness) of each color
- * on LED2.
- *
- * MSP432P401
- * ------------------
- * | |
- * | | Duty Cycle - press to select
- * P1.0 LED --|sw1 sw2|-- 1 sec (default), 1/2 sec., 1/4 sec., 1/8 sec.,
- * blink | | 1/16 sec 1/32 sec., 1/42 sec., 1/56 sec.
- * on/off | |
- * | | (returns from 1/56 back to 1 on next press)
- * | |
- * | |
- * Author: David C. Rankin
- *******************************************************************************/
- /* DriverLib Includes */
- #include <ti/devices/msp432p4xx/driverlib/driverlib.h>
- /* Standard Includes */
- #include <stdint.h>
- #include <stdbool.h>
- #define PWM_CCR0 127
- #define PWM_CCR0_MIN 20
- #define PWM_CCR0_STEP 1
- #define TA1_CCR0 46875
- static volatile bool toggle; /* toggle P1.0 LED (false - off) */
- static volatile bool dirUp; /* dir flag for RGB LED PWM step */
- static volatile uint8_t color; /* RGB LED color (R or G or B) */
- static volatile int16_t ta0_ccr1; /* Timer_A0 CCR1-3 value */
- static volatile uint_fast16_t ta1_ccr0; /* Timer_A1 CCR0 value */
- /* TimerA0 UpMode Configuration Parameter for PWM */
- const Timer_A_UpModeConfig upConfigTA0 =
- {
- TIMER_A_CLOCKSOURCE_SMCLK, /* SMCLK Clock Source */
- TIMER_A_CLOCKSOURCE_DIVIDER_1, /* SMCLK/1 = 3MHz */
- PWM_CCR0, /* 128 clock cycles */
- TIMER_A_TAIE_INTERRUPT_DISABLE, /* Disable Timer interrupt */
- TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE, /* Disable CCR0 interrupt */
- TIMER_A_DO_CLEAR /* Clear divider, direction, count */
- };
- /* Timer_A0 CCR1 compare configuration parameters
- * NOTE: CCR0 only supports the following output modes
- * TIMER_A_OUTPUTMODE_OUTBITVALUE
- * TIMER_A_OUTPUTMODE_SET
- * TIMER_A_OUTPUTMODE_TOGGLE
- * TIMER_A_OUTPUTMODE_RESET
- * that is why you use CCR1 - CCR3 for the compare registers
- * with TIMER_A_OUTPUTMODE_TOGGLE_RESET
- * (while you can force CCR0 to take TOGGLE_RESET - it is Undefined Behavior)
- * See Tech Reference 19.2.5.1 pg 791.
- * Toggle_Reset starts duty-cycle at CCR1-3 and stops when CCR0 roll-over occurs.
- */
- Timer_A_CompareModeConfig compareConfigA01 =
- {
- TIMER_A_CAPTURECOMPARE_REGISTER_1, /* Use CCR1 as compare register */
- TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, /* Disable CCR interrupt */
- TIMER_A_OUTPUTMODE_TOGGLE_RESET, /* Toggle-reset output mode */
- 125 /* Compare value (CCR1) Dim */
- };
- /* Timer_A0 CCR2 compare configuration parameters */
- Timer_A_CompareModeConfig compareConfigA02 =
- {
- TIMER_A_CAPTURECOMPARE_REGISTER_2, /* Use CCR2 as compare register */
- TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, /* Disable CCR interrupt */
- TIMER_A_OUTPUTMODE_TOGGLE_RESET, /* Toggle-reset output mode */
- 0 /* Compare value (CCR2) off */
- };
- /* Timer_A0 CCR3 compare configuration parameters */
- Timer_A_CompareModeConfig compareConfigA03 =
- {
- TIMER_A_CAPTURECOMPARE_REGISTER_3, /* Use CCR3 as compare register */
- TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, /* Disable CCR interrupt */
- TIMER_A_OUTPUTMODE_TOGGLE_RESET, /* Toggle-reset output mode */
- 0 /* Compare value (CCR3) off */
- };
- /* TimerA1 UpMode Configuration Parameter for Interrupt where PWM/color is changed */
- const Timer_A_UpModeConfig upConfigTA1 =
- {
- TIMER_A_CLOCKSOURCE_SMCLK, /* SMCLK Clock Source */
- TIMER_A_CLOCKSOURCE_DIVIDER_64, /* SMCLK/64 = 48.875KHz */
- TA1_CCR0, /* ~1/sec on/off */
- TIMER_A_TAIE_INTERRUPT_DISABLE, /* Disable Timer interrupt */
- TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE, /* Enable CCR0 interrupt */
- TIMER_A_DO_CLEAR /* Clear divider, direction, count */
- };
- /* Port mapper configuration register (See Table 13-1 Tech Reference, Pg 702)
- * Timer_A0 CCR1-3 are mapped to take the PWM output for the RGB LED.
- */
- const uint8_t port_mapping[] =
- {
- PM_TA0CCR1A, PM_TA0CCR2A, PM_TA0CCR3A, PM_NONE, PM_NONE, PM_NONE, PM_NONE, PM_NONE
- };
- int main(void)
- {
- toggle = true;
- dirUp = false;
- color = 1;
- ta0_ccr1 = PWM_CCR0;
- ta1_ccr0 = TA1_CCR0;
- /* Stop Watchdog */
- MAP_WDT_A_holdTimer();
- /* Configuring P1.0 as output */
- MAP_GPIO_setAsOutputPin (GPIO_PORT_P1, GPIO_PIN0);
- MAP_GPIO_setOutputLowOnPin (GPIO_PORT_P1, GPIO_PIN0);
- /* Confinguring P1.1 & P1.4 as an input and enabling interrupts */
- MAP_GPIO_setAsInputPinWithPullUpResistor (GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN4);
- MAP_GPIO_clearInterruptFlag (GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN4);
- MAP_GPIO_enableInterrupt (GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN4);
- MAP_GPIO_interruptEdgeSelect (GPIO_PORT_P1, GPIO_PIN1 |
- GPIO_PIN4, GPIO_HIGH_TO_LOW_TRANSITION);
- /* Initialize main clock to 3MHz */
- MAP_CS_setDCOCenteredFrequency (CS_DCO_FREQUENCY_3);
- /* Configuring TimerA1 for Up Mode to generate an interrupt on CCR0 roll-over */
- MAP_Timer_A_configureUpMode (TIMER_A1_BASE, &upConfigTA1);
- /* Remapping TA1CCR1-3 output pin to P2.0 (red green blue LED of LED2) */
- MAP_PMAP_configurePorts ((const uint8_t *) port_mapping,
- PMAP_P2MAP,
- 1,
- PMAP_DISABLE_RECONFIGURATION);
- /* Set pin P2.0 - P2.2 as output pins */
- MAP_GPIO_setAsPeripheralModuleFunctionOutputPin (GPIO_PORT_P2,
- GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2,
- GPIO_PRIMARY_MODULE_FUNCTION);
- /* Configure the compare functionality of Timer_A0 */
- MAP_Timer_A_initCompare (TIMER_A0_BASE, &compareConfigA01);
- MAP_Timer_A_initCompare (TIMER_A0_BASE, &compareConfigA02);
- MAP_Timer_A_initCompare (TIMER_A0_BASE, &compareConfigA03);
- /* Configure Timer_A1 for Up Mode */
- MAP_Timer_A_configureUpMode (TIMER_A0_BASE, &upConfigTA0);
- /* Enabling interrupts and going to sleep */
- MAP_Interrupt_enableSleepOnIsrExit();
- MAP_Interrupt_enableInterrupt (INT_PORT1);
- MAP_Interrupt_enableInterrupt (INT_TA1_0);
- /* Enabling MASTER interrupts */
- MAP_Interrupt_enableMaster();
- /* Starting the Timer_A0 and Timer_A1 in up mode */
- MAP_Timer_A_startCounter (TIMER_A0_BASE, TIMER_A_UP_MODE);
- MAP_Timer_A_startCounter (TIMER_A1_BASE, TIMER_A_UP_MODE);
- while (1)
- {
- MAP_PCM_gotoLPM3();
- }
- }
- /*
- * Port 1 interrupt handler. This handler is called whenever switches attached
- * to P1.1 (S1) and P1.4 (S2) are pressed.
- */
- void PORT1_IRQHandler(void)
- {
- uint32_t status = MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P1);
- MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, status);
- /* Handles S1 button press turns P1.0 LED off
- * (leaves timer running for P2.0 PWM display)
- */
- if (status & GPIO_PIN1)
- {
- if (toggle) {
- MAP_GPIO_setOutputLowOnPin (GPIO_PORT_P1, GPIO_PIN0);
- }
- toggle = !toggle;
- }
- /* Handles S2 button press - cycles through PWM CCR0 (duty-cycle off) for RGB LED
- * The P1.0 is toggled on/off at this rate to indicate relative PWM setting.
- */
- if (status & GPIO_PIN4)
- {
- uint_fast16_t new_ccr0 = ta1_ccr0 >> 1; /* divide rate by 2 */
- if (new_ccr0 > 1000) { /* if > 1000, use as is */
- ta1_ccr0 = new_ccr0;
- }
- else if ((new_ccr0 += (new_ccr0 >> 1)) > 700) { /* > 700, add 1/2 value */
- ta1_ccr0 = new_ccr0;
- }
- else { /* reset to 1 sec */
- ta1_ccr0 = TA1_CCR0;
- }
- if (toggle) { /* if P1.0 LED visible, turn off to start new rate */
- MAP_GPIO_setOutputLowOnPin (GPIO_PORT_P1, GPIO_PIN0);
- }
- /* stop Timer_A1, reinitialize up mode, set new CCR0 value, start counter */
- MAP_Timer_A_stopTimer (TIMER_A1_BASE);
- MAP_Timer_A_configureUpMode (TIMER_A1_BASE, &upConfigTA1);
- MAP_Timer_A_setCompareValue (TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0,
- ta1_ccr0);
- MAP_Timer_A_startCounter (TIMER_A1_BASE, TIMER_A_UP_MODE);
- }
- }
- /*
- * Timer A1 interrupt handler. toggles the P1.0 LED
- */
- void TA1_0_IRQHandler(void)
- {
- MAP_Timer_A_clearCaptureCompareInterrupt (TIMER_A1_BASE,
- TIMER_A_CAPTURECOMPARE_REGISTER_0);
- if (toggle) { /* if P1.0 being shown, toggle output */
- MAP_GPIO_toggleOutputOnPin (GPIO_PORT_P1, GPIO_PIN0);
- }
- if (ta0_ccr1 <= PWM_CCR0_MIN) { /* if PWM countdown reaches or below min, */
- ta0_ccr1 = PWM_CCR0_MIN; /* set to min. (brightest) */
- dirUp = !dirUp; /* change count direction */
- }
- else if (ta0_ccr1 >= PWM_CCR0) { /* if PWM count up reaches or above max. */
- ta0_ccr1 = PWM_CCR0; /* set to max (dimmest) */
- dirUp = !dirUp; /* change direction */
- color += 1; /* change RGB color */
- if (color > 3) { /* if greater than 3 */
- color = 1; /* reset to 1 */
- }
- }
- ta0_ccr1 += dirUp ? PWM_CCR0_STEP : -PWM_CCR0_STEP; /* decrease or increases brightness */
- /* set CCR1-3 to PWM value for currently active RGB color
- * set max + 1 (off) for the other 2 non-active colors
- */
- MAP_Timer_A_setCompareValue (TIMER_A0_BASE,
- TIMER_A_CAPTURECOMPARE_REGISTER_1,
- (color == 1) ? ta0_ccr1 : PWM_CCR0 + 1);
- MAP_Timer_A_setCompareValue (TIMER_A0_BASE,
- TIMER_A_CAPTURECOMPARE_REGISTER_2,
- (color == 2) ? ta0_ccr1 : PWM_CCR0 + 1);
- MAP_Timer_A_setCompareValue (TIMER_A0_BASE,
- TIMER_A_CAPTURECOMPARE_REGISTER_3,
- (color == 3) ? ta0_ccr1 : PWM_CCR0 + 1);
- /* equivalent but long way to do it */
- // switch (color) {
- // case 1:
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, ta0_ccr1);
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_2, PWM_CCR0 + 1);
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_3, PWM_CCR0 + 1);
- // break;
- // case 2:
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, PWM_CCR0 + 1);
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_2, ta0_ccr1);
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_3, PWM_CCR0 + 1);
- // break;
- // case 3:
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, PWM_CCR0 + 1);
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_2, PWM_CCR0 + 1);
- // MAP_Timer_A_setCompareValue (TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_3, ta0_ccr1);
- // break;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement