lpcware

Frequency counter

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by larento on Sat Dec 27 00:26:53 MST 2014
Hi all,

I'm trying to implement a frequency counter on lpc1769 mcu on LPCXpresso LPC1769. Timer 2 is used as a counter while timer 1 is used as a 1 sec timer interrupt. So far i manage to get counts on P0.4(CAP2.[0]). However the counts are not the  same value value that i have input. I.e a 200Hz PWM signal was input to P0.4(CAP2.[0]) however only approximately 183-190Hz(counts) was returned. I have managed to trace down to the Chip_TIMER_Reset(LPC_TIMER2). By disabling this line i'm able to get near to input counts of 200Hz. How can i reset the count timer while counting accurately?


/*
===============================================================================
Name        : PulseCount.c
Author      : $(author)
Version     :
Copyright   : $(copyright)
Description : main definition
===============================================================================
*/

#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif

#include <cr_section_macros.h>

// TODO: insert other include files here

// TODO: insert other definitions and declarations here
uint32_t timerFreq;
uint32_t timer2count;
bool flag;

//function to initialize LED2
void LED2_init()
{
Chip_GPIO_Init(LPC_GPIO);
Chip_GPIO_SetPinDIR(LPC_GPIO,0,22,true);
Chip_GPIO_WritePortBit(LPC_GPIO,0,22,false);
}

//toggle change of LED2 status
void LED2_Toggle()
{
if (Chip_GPIO_GetPinState(LPC_GPIO,0,22)==1)
{
Chip_GPIO_WritePortBit(LPC_GPIO,0,22,false);
}
else
{
Chip_GPIO_WritePortBit(LPC_GPIO,0,22,true);
}

}

//initialise input pin to cap2.0
void timer2_Pin_init()
{
       
Chip_IOCON_Init(LPC_IOCON);
Chip_IOCON_PinMux(LPC_IOCON, 0, 4 ,IOCON_MODE_INACT, IOCON_FUNC3);
}

//Timer 1 interrupt
void TIMER1_IRQHandler(void)
{
if (Chip_TIMER_MatchPending(LPC_TIMER1, 1)) {
Chip_TIMER_ClearMatch(LPC_TIMER1, 1);
                //toggle LED2
LED2_Toggle();
//assign counts to timer count
timer2count = Chip_TIMER_ReadCount(LPC_TIMER2);
flag=true;

}
}


int main(void) {

#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
    // Read clock settings and update SystemCoreClock variable
    SystemCoreClockUpdate();
    // Set up and initialize all required blocks and
    // functions related to the board hardware
    Board_Init();
    // Set the LED to the state of "On"
    Board_LED_Set(0, true);
#endif
#endif

    /*Initialize LED2*/
    LED2_init();
    timer2_Pin_init();
    timer2count = 0;

    /* Enable timer 2 clock */
    Chip_TIMER_Init(LPC_TIMER2);
    /*Reset Timer2*/
    Chip_TIMER_Reset(LPC_TIMER2);
    /*Capture on rising edge timer*/
    //Chip_TIMER_CaptureRisingEdgeEnable(LPC_TIMER2,0);
    //LPC_TIMER2->CCR=1;
    /*Set clock source to Rising edge --Count control register*/
    Chip_TIMER_TIMER_SetCountClockSrc(LPC_TIMER2,TIMER_CAPSRC_RISING_CAPN ,1);


    /*Enable timer 1 clock*/
    Chip_TIMER_Init(LPC_TIMER1);
    /* Timer rate is system clock rate */
    timerFreq = Chip_Clock_GetSystemClockRate();
    Chip_TIMER_Reset(LPC_TIMER1);
    Chip_TIMER_MatchEnableInt(LPC_TIMER1, 1);
    Chip_TIMER_SetMatch(LPC_TIMER1, 1, (timerFreq/4-1));
    Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 1);


    /*Enable Timer 1 & 2*/
    Chip_TIMER_Enable(LPC_TIMER1);
    Chip_TIMER_Enable(LPC_TIMER2);

    /* Enable timer 1 interrupt */
    NVIC_ClearPendingIRQ(TIMER1_IRQn);
    NVIC_EnableIRQ(TIMER1_IRQn);




    while(1) {
    if (flag==true)
    {
    //prints the timer2count to debug screen
                printf("%u\n",timer2count);
                //by disabling this reset line , able to get close to 200Hz(counts)
    Chip_TIMER_Reset(LPC_TIMER2);
    flag=false;
    }
    }
    return 0 ;
}

Outcomes