All Source Code

Share

PWM

Basic PWM driver |TM4C123

/* Using M1PWM7, write a program to create 1kHz frequency with 50% duty cycle on PF3 pin (green LED). Use System Clock of 16MHz without division. Set the options of PWMGENB register to set the output when reload and clear the output when PWMCMPA match. */

#include "TM4C123GH6PM.h"

int main(void)
{
    /* Enable Peripheral Clocks */ 
    SYSCTL->RCGCPWM |= 2;       /* enable clock to PWM1 */
    SYSCTL->RCGCGPIO |= 0x20;   /* enable clock to PORTF */
    SYSCTL->RCC &= ~0x00100000; /* no pre-divide for PWM clock */

    /* 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 = 16000;      /* set load value for 1kHz (16MHz/16000) */
    PWM1->_3_CMPA = 8000;       /* set duty cycle to 50% */
    PWM1->_3_CTL = 1;           /* start timer */
    PWM1->ENABLE = 0x80;        /* start PWM1 ch7 */
    	
    for(;;) { }
}

Add Comment

Your email address will not be published. Required fields are marked *

Phone: +447586761483
SW11 2DL London UK
Winstanley Rd,