lpcware

LPC1768 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 amadeusz665 on Fri Mar 07 07:14:54 MST 2014
Hello,

I have a problem with lpc1768 program. I'm  writing in C my frequency counter. Below is code witch i done:



volatile uint32_t inputFreq, inputFreq2;
char Variablestr [10]; //when the variable is between 0 and 1024 make sure this number is 4 or higher!
char Variablestr2 [10];

void EnableCounter(void)
{

LPC_SC->PCONP |= 1 << 2; //Power up TimerCounter1

LPC_TIM1->TCR |= 1 << 0; // Counter mode
LPC_TIM1->CTCR |= 3; // Count on both edges
LPC_TIM1->CTCR |= 1 << 2; // CAP1.1 is the input pin for which the input signal needs to be connected.
LPC_PINCON->PINSEL3 |= ((1 << 7)| (1 << 6)); // Make P1.19 as CAP1.1


LPC_TIM1->TCR = 0x1; // Enable counter

}
void EnableTimer(void)
{
LPC_SC->PCONP |= 1 < 1;         //timer0 power on
LPC_TIM0->MR0 = 12499999;       //1sec
LPC_TIM0->MCR = 3;              //interrupt and reset control
                                   //3 = Interrupt & reset timer0 on match
                                   //1 = Interrupt only, no reset of timer0
NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
LPC_TIM0->TCR = 1;              //enable Timer0
lcdDrawText(20,60,"start_timer",LCDRed);

}

void TIMER0_IRQHandler (void)
{

if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt
{

LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag

inputFreq = LPC_TIM1->TC; // Read the counter value
inputFreq2 = LPC_TIM0->TC;
lcdClear(LCDBlack);
snprintf(Variablestr, 10,"%d",inputFreq);
lcdDrawText(100,250,Variablestr,LCDWhite);


snprintf(Variablestr2, 10,"%d",inputFreq2);
lcdDrawText(100,200,Variablestr2,LCDYellow);


LPC_TIM1->TCR |= 1 << 1 ; // Reset the counter

}

}

int main (void)
{
          EnableCounter();
          EnableTimer();
        
          while (1)
          {

         }

}


I want to read frequency from waveform generator continuosly.
When I run the program on my lcd I see only first frequency and after next interrupts I only see "0".
First frequency is ok.

I noticed, that comment line : "LPC_TIM1->TCR |= 1 << 1 ; // Reset the counter" the counter works better - he adds another frequency even when i change frequency on generator. But the number is total number of counts.

Thank you for any directions and help.

Outcomes