/* www.MicroDigitalEd.com * p11_2.c Variable duty cycle PWM to control LED intensity * * This program uses Timer_A0.4 to generate PWM output at 60 Hz * and variable duty cycle. * The program is based on p11_1.c with two changes. * 1. In the infinite loop, the duty cycle is increased by 5% * every 50ms. When it reaches 100%, it wraps around. * 2. The timer output is available on P2.7 and is also mapped to * P2.1 (Green LED). * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0 * on XMS432P401R Rev C. */ #include "msp.h" void delayMs(int n); void portRemap(void); int main(void) { /* Configure P2.7 as Timer A0.4 output */ P2->SEL0 |= 0x80; P2->SEL1 &= ~0x80; P2->DIR |= 0x80; portRemap(); /* remap output to P2.1 green LED */ /* configure TimerA0.4 as PWM */ TIMER_A0->CCR[0] = 50000-1; /* PWM Period */ TIMER_A0->CCR[4] = 500; /* begin from 1% */ TIMER_A0->CCTL[4] = 0xE0; /* CCR4 reset/set mode */ TIMER_A0->CTL = 0x0214; /* use SMCLK, count up, clear TA0R register */ while (1) { /* increase duty cycle by 5% */ TIMER_A0->CCR[4] += 2500; if (TIMER_A0->CCR[4] > 50000) /* wrap around when reaches 100% */ TIMER_A0->CCR[4] = 500; /* begin from 1% */ delayMs(50); } } void portRemap(void) { PMAP->KEYID = 0x2D52; /* unlock PMAP */ P2MAP->PMAP_REGISTER1 = 23; /* remap P2.1 to 23 (TPM0.4) */ P2->DIR |= 2; P2->SEL0 |= 2; P2->SEL1 &= ~2; PMAP->CTL = 1; /* lock PMAP */ PMAP->KEYID = 0; } /* delay milliseconds when system clock is at 3 MHz */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* Delay */ }