iceaway

PWM Init

Jan 18th, 2012
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. static void init_pwm(void)
  2. {
  3.     /* Set the PWM clock source */              
  4.     SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
  5.     delay(10);
  6.    
  7.     /* Enable the PWM peripheral */
  8.     SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
  9.     delay(10);
  10.        
  11.     /* Enable output pins. PORTA already enabled for UART */
  12.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
  13.     delay(10);
  14.    
  15.     /* Configure PWM pins */    
  16.     GPIOPinConfigure(GPIO_PA6_M1PWM2);
  17.     GPIOPinConfigure(GPIO_PA7_M1PWM3);
  18.     GPIOPinConfigure(GPIO_PN4_M1PWM4);
  19.     GPIOPinConfigure(GPIO_PN5_M1PWM5);
  20.     GPIOPinConfigure(GPIO_PN6_M1PWM6);
  21.     GPIOPinConfigure(GPIO_PN7_M1PWM7);
  22.    
  23.     /* Configure pin for PWM output */
  24.     GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_7 | GPIO_PIN_6);
  25.     GPIOPinTypePWM(GPIO_PORTN_BASE,
  26.                    GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
  27.    
  28.     PWMGenConfigure(PWM1_BASE,
  29.                     PWM_GEN_1,
  30.                     PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_SYNC | PWM_GEN_MODE_DBG_RUN);
  31.                    
  32.     PWMGenConfigure(PWM1_BASE,
  33.                     PWM_GEN_2,
  34.                     PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_SYNC | PWM_GEN_MODE_DBG_RUN);
  35.                    
  36.     PWMGenConfigure(PWM1_BASE,
  37.                     PWM_GEN_3,
  38.                     PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_SYNC | PWM_GEN_MODE_DBG_RUN);
  39.  
  40.     /* Invert the output? */
  41.     PWMOutputInvert(PWM1_BASE,
  42.                     PWM_OUT_2 | PWM_OUT_3 | PWM_OUT_4 |
  43.                     PWM_OUT_5 | PWM_OUT_6 | PWM_OUT_7,
  44.                     true);
  45.    
  46.     /* Synchronize the time base between generators 1, 2 and 3 */
  47.     PWMSyncTimeBase(PWM1_BASE, PWM_GEN_1_BIT | PWM_GEN_2_BIT | PWM_GEN_3_BIT);
  48.  
  49.     /* Set the PWM frequency to 19532 Hz */
  50.     PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, PWM_PERIOD);
  51.     PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, PWM_PERIOD);
  52.     PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, PWM_PERIOD);
  53.    
  54.    
  55.     PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, 200); /* PA6 */
  56.     PWMPulseWidthSet(PWM1_BASE, PWM_OUT_3, 800); /* PA7 */
  57.     PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4, 300); /* PN4 */
  58.     PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 400); /* PN5 */
  59.     PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, 500); /* PN6 */
  60.     PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, 600); /* PN7 */
  61.    
  62.     PWMGenEnable(PWM1_BASE, PWM_GEN_1);
  63.     PWMGenEnable(PWM1_BASE, PWM_GEN_2);
  64.     PWMGenEnable(PWM1_BASE, PWM_GEN_3);
  65.    
  66.     PWMSyncUpdate(PWM1_BASE, PWM_GEN_1_BIT | PWM_GEN_2_BIT | PWM_GEN_3_BIT);
  67.  
  68.     /* Trigger an AD conversion on each cycle */
  69.     PWMGenIntClear(PWM1_BASE, PWM_GEN_1, PWM_TR_CNT_LOAD);
  70.     PWMGenIntTrigEnable(PWM1_BASE, PWM_GEN_1, PWM_TR_CNT_LOAD);
  71.    
  72.     PWMOutputState(PWM1_BASE,
  73.                    PWM_OUT_2_BIT | PWM_OUT_3_BIT | PWM_OUT_4_BIT |
  74.                    PWM_OUT_5_BIT | PWM_OUT_6_BIT | PWM_OUT_7_BIT,
  75.                    true);
  76.                    
  77.     /****************** PWM0 TRIGGER TEST *************************************/
  78.     SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
  79.     delay(10);
  80.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
  81.     delay(10);
  82.     GPIOPinConfigure(GPIO_PH1_M0PWM1);
  83.     GPIOPinTypePWM(GPIO_PORTH_BASE, GPIO_PIN_1);
  84.     PWMGenConfigure(PWM0_BASE,
  85.                     PWM_GEN_0,
  86.                     PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_SYNC | PWM_GEN_MODE_DBG_RUN);
  87.     PWMSyncTimeBase(PWM0_BASE, PWM_GEN_0_BIT);
  88.     PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, PWM_PERIOD);
  89.     PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 400);
  90.     PWMGenEnable(PWM0_BASE, PWM_GEN_0);
  91.     PWMSyncUpdate(PWM0_BASE, PWM_GEN_0_BIT);
  92.     PWMGenIntClear(PWM0_BASE, PWM_GEN_0, PWM_TR_CNT_LOAD); /* | PWM_INT_CNT_LOAD); */
  93.     PWMGenIntTrigEnable(PWM0_BASE, PWM_GEN_0, PWM_TR_CNT_LOAD); /* | PWM_INT_CNT_LOAD); */
  94.     PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true);
  95.     SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_PWM0);
  96.     /**************************************************************************/
  97. }
Advertisement
Add Comment
Please, Sign In to add comment