/* p11_4: Use PWM to control LED duty cycle */ /* This program is based on p11_3. Some parameters are changed to slow down the PWM frequency so that it runs slow enough to observe the duty cycle change from the LED with naked eyes. */ #include "TM4C123GH6PM.h" int main(void) { void delayMs(int n); int x = 0xFFFE; /* Enable Peripheral Clocks */ SYSCTL->RCGCPWM |= 2; /* enable clock to PWM1 */ SYSCTL->RCGCGPIO |= 0x20; /* enable clock to PORTF */ SYSCTL->RCC |= 0x00100000; /* use pre-divide for PWM clock */ SYSCTL->RCC |= 0x000E0000; /* set 64 for pre-divide for PWM */ /* Enable port PF3 for PWM1 M1PWM7 */ GPIOF->AFSEL = 8; /* PF3 uses alternate function */ GPIOF->PCTL &= ~0x0000F000; /* make PF3 PWM output pin */ GPIOF->PCTL |= 0x00005000; GPIOF->DEN |= 8; /* pin digital */ PWM1->_3_CTL = 0; /* stop counter */ PWM1->_3_GENB = 0x0000008C; /* M1PWM7 output set when reload, */ /* clear when match PWMCMPA */ PWM1->_3_LOAD = 0xFFFF; /* set load value with max value */ PWM1->_3_CMPA = x; /* set duty cycle to 50% */ PWM1->_3_CTL = 1; /* start timer */ PWM1->ENABLE = 0x80; /* start PWM1 ch7 */ for(;;) { x = x - 1000; if (x <= 0) x = 0xFFFE; PWM1->_3_CMPA = x; delayMs(300); } } /* delay n milliseconds (16 MHz CPU clock) */ void delayMs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 3180; j++) {} /* do nothing for 1 ms */ } /* This function is called by the startup assembly code to perform system specific initialization tasks. */ void SystemInit(void) { /* Grant coprocessor access */ /* This is required since TM4C123G has a floating point coprocessor */ SCB->CPACR |= 0x00f00000; }