lpcware

SCT used as PWM Generator does not work

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by ucniranjan on Wed Jan 22 04:56:32 MST 2014
Friends,

I am using the LPCXpresso 812 Board with the IDE. I have a simple program, wherein I initialize the SCT as a PWM timer, with 1/3 duty cycle. The SCT spec: 12MHz clock, 16-bit counter, 2 events and interrupt mode. I redirect the SCT output to pin 7. The main function instantiates 
PWM bursts of 20 pulses. When I run the code below, and connect the Logic Analyzer to pin 7, I do not see the PWM pulses. What could be wrong? I feel I am missing somethin in initializing the SCT. I have used the default libraries of LPCXpresso.

Thanks,
Niranjan

/****************************************************************************
Main Function
****************************************************************************/
#ifdef __USE_CMSIS
#include "LPC8xx.h"
#endif

#include "lpc8xx_clkconfig.h"
#include "lpc8xx_gpio.h"


static volatile uint32_t PWMCount = 0;

void SCT_IRQHandler(void)
{
PWMCount--;
if(PWMCount == 0)
{
LPC_SCT->CTRL_L    |= (1 << 2);
}
LPC_SCT->EVFLAG = (1 << 0);
}

void PWM_SCT_Init(void)
{
    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);                 // SCT clock

    LPC_SYSCON -> PRESETCTRL &= ~(0x1<<8);
    LPC_SYSCON -> PRESETCTRL |=  (0x1<<8);

    LPC_SCT->CONFIG |= (1 << 17);                          // Two 16-bit timers, auto limit
    LPC_SCT->CTRL_L |= (SystemCoreClock/12000000-1) << 5;  //  SCT clock = 12 MHz

    LPC_SCT->MATCH[0].L    = 360;                          // match 0 @ 30 usec
    LPC_SCT->MATCHREL[0].L = 360;
    LPC_SCT->MATCH[1].L    = 240;                          // match 1 @ 20 usec
    LPC_SCT->MATCHREL[1].L = 240;

    LPC_SCT->EVENT[0].STATE = 0x00000001;                  // event 0 only happens in state 0
    LPC_SCT->EVENT[0].CTRL  = (0 << 0)  |                  // MATCHSEL[3:0] = related to match 0
                              (1 << 12) |                               // COMBMODE[13:12] = match condition only
                              (1 << 14) |                              // STATELD[14] = STATEV is loaded into state
                              (0 << 15);                              // STATEV[15] = 0

    LPC_SCT->EVENT[1].STATE = 0x00000001;                  // event 1 only happens in state 0
    LPC_SCT->EVENT[1].CTRL  = (1 << 0)  |                  // MATCHSEL[3:0] = related to match 1
                              (1 << 12) |                               // COMBMODE[13:12] = match condition only
                              (1 << 14) |                              // STATELD[14] = STATEV is loaded into state
                              (0 << 15);                              // STATEV[15] = 0

    LPC_SCT->EVEN = (1 << 0);
    NVIC_EnableIRQ(SCT_IRQn);                              // enable SCT interrupt
    NVIC_SetPriority(SysTick_IRQn, 3);   // Give less priority to SysTick_IRQn

}

void Start_PWM(uint32_t pwmcount)
{
PWMCount = pwmcount;
LPC_SCT->CTRL_L    &= ~(1 << 2);                       // unhalt SCT

while(PWMCount)
{
LPC_SCT->OUT[0].CLR = (1 << 0);       // event 0 will set SCT_OUT_0, SETCLR0 = 0
LPC_SCT->OUT[0].SET = (1 << 1);      // event 1 will clear SCT_OUT_0, SETCLR0 = 0
}

}

/* Main Program */

  int main (void) {
  uint32_t regVal;

  SystemCoreClockUpdate();

  /* Config CLKOUT, mostly used for debugging. */
  regVal = LPC_SWM->PINASSIGN8 & ~( 0xFF << 16 );
  LPC_SWM->PINASSIGN8 = regVal | ( 12 << 16 );
  CLKOUT_Setup( CLKOUTCLK_SRC_MAIN_CLK );

  LPC_SWM->PINASSIGN6 = 0x07FFFFFF;                          // SCT OUTP_0 at P0.7
  PWM_SCT_Init();
     
int i1;
  while(1)
  {
  Start_PWM(20);
  for (i1=0; i1<100000; i1++) {  __NOP(); }
  }

}

Outcomes