lpcware

To find the frequency of input pwm signal lpc1549

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by abhishek.rai on Fri Oct 09 05:26:27 MST 2015
I have used this code to detemine frequency of PWM input....... can any body help me in determining the frequency plzz
thanks in advance


#include "board.h"

volatile uint32_t capture1;//capture 1
volatile uint32_t freq;//frequency
volatile uint16_t ticks;//SysTick ticks
volatile uint8_t  sec;//second flag
volatile uint32_t valid;//valid flag

//PWM Generator---------------------------------------------------------------
#define GENERATE_PWM//generate PWM signal to test capture function

#ifdef GENERATE_PWM
#define PWM_RATE250       //PWM rate
#define SCT_PIN_OUT4        //SCT output pin
#define SCT_INDEX1        //SCT match register

void SCT2_init(void)
{
Chip_SCTPWM_Init(LPC_SCT2);//init SCT2
Chip_SCTPWM_SetRate(LPC_SCT2, PWM_RATE);//set cycle (in SCT Match0 register)
Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);//enable SWM
Chip_SWM_EnableFixedPin(SWM_FIXED_SCT2_OUT4); //switch output: SCT2OUT3 = PIO0_6// abhi  PIO0_29
Chip_SCTPWM_SetOutPin(LPC_SCT2, SCT_INDEX, SCT_PIN_OUT);//set output
Chip_SCTPWM_SetDutyCycle(LPC_SCT2, SCT_INDEX, LPC_SCT2->MATCHREL[0].U/2);//set duty
Chip_SCTPWM_Start(LPC_SCT2);//and start
}
#endif

void SCT0_IRQHandler(void)
{
if(LPC_SCT0->EVFLAG & SCT_EVT_1 )//event?
{
  Board_LED_Toggle(0);//toggle LED
  valid =1;//set valid flag
  LPC_SCT0->EVFLAG = SCT_EVT_1;//reset event
}//end event
}

//SysTick Interrupt
void SysTick_Handler(void)
{
uint32_t states;

/* Get current masked port states */
states = Chip_GPIO_GetMaskedPortValue(LPC_GPIO, 0);

/* Toggle all the states */
states = ~states;
/* Write states back via masked set function. Only the enanled
   (masked states) will be changed. */
Chip_GPIO_SetMaskedPortValue(LPC_GPIO, 0, states);
ticks++;//inc ticks
if(ticks >= 10)//second timer
{
  ticks =0;//reset ticks
  sec =1;//second flag
  Board_LED_Toggle(0);
}//end second timer
}

void SCT0_init(void)
{
//input capture PIO0_17
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_MODE_PULLDOWN));
Chip_SCT_Init(LPC_SCT0);
//unified 32bit counter, SYNC all inputs, enable prescaler in CTRL register
LPC_SCT0->CONFIG =  SCT_CONFIG_32BIT_COUNTER | (1<<9);
LPC_SCT0->LIMIT = SCT_EVT_1;//events to limit counter
//set prescaler 72 to count µs
Chip_SCT_SetControl(LPC_SCT0,SCT_CTRL_PRE_L(72-1));
//input capture setting
LPC_INMUX->SCT0_INMUX[0] = 2;//SCT0_IN0 = P0IO_17
LPC_SCT0->REGMODE  = (SCT_EVT_1);//set capture register(s)
LPC_SCT0->CAPCTRL[1].U  = (SCT_EVT_1);//set capture control event

//event 1: capture rising
LPC_SCT0->EVENT[1].STATE = (SCT_EVT_1); // event happens in state
LPC_SCT0->EVENT[1].CTRL = (0)       |  // capture, not used
                           (0 << 5)  |  // OUTSEL[5]     = selects input
                           (0 << 6)  |  // IOSEL[9:6]    = 0 select SCT0_IN0
                           (1 << 10) |  // IOCOND[11:10] = 1 rising
                           (2 << 12) |  // COMBMODE[13:12] = IO condition
                           (1 << 14) |  // STATELD[14]   = STATEV is loaded
                           (1 << 15);   // STATEV[19:15] = new state 1

LPC_SCT0->STATE =1;//start state

//interrupts: rising
LPC_SCT0->EVFLAG = (SCT_EVT_1); //reset flags
LPC_SCT0->EVEN   = (SCT_EVT_1); //enable interrupts
NVIC_SetPriority(SCT0_IRQn, 0);//set priority
NVIC_EnableIRQ(SCT0_IRQn);

Chip_SCT_ClearControl(LPC_SCT0, SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);//and start
}

int main(void)
{
SystemCoreClockUpdate();
Board_Init();
Board_LED_Set(0, true);
//init SCT0 to capture PIO0_17
SCT0_init();//init SCT0
#ifdef GENERATE_PWM
//init SCT2 to generate PWM
SCT2_init();
#endif

//init SysTick to 10Hz
SysTick_Config(SystemCoreClock/10);
NVIC_SetPriority(SysTick_IRQn,2);//set priority
while(1)//loop
{
  if(sec)//second timer
  {
   sec =0;//reset second flag
   if(valid)//valid data
   {
    valid=0;//reset valid
    capture1 = LPC_SCT0->CAP[1].U;//get capture value
    freq = SystemCoreClock / capture1 /72;//calculate frequency
    printf("C1: %d ",capture1);
    printf("F: %d Hz\n",freq);
   } //end valid
   else
   {
    printf("No valid data!!\n");
   }
  }//end second timer
}//end loop
return 0 ;
}

Outcomes