LPC1768 frequency counter

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC1768 frequency counter

1,333 Views
lpcware
NXP Employee
NXP Employee
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.
Labels (1)
0 Kudos
2 Replies

637 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by amadeusz665 on Tue Mar 11 07:08:10 MST 2014
Great thank you for your answer ;)

I forgotten add line "LPC_TIM1->TCR = 1; " in my Timer handler. Now it's fine, but the number of counts is too small. I think when I move  my lcd function inside while() the result will be better. 
0 Kudos

637 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Tue Mar 11 06:13:42 MST 2014
repeat(forever) { never call subroutines from inside an interrupt }

*printf, lcdDrawText, fopen/fread/fwrite/fclose, malloc/free, any debug-output via UART... it all belongs in task-time.
task-time=your 'main loop'.

Try cleaning up the interrupt and move your information inside your while(1){ ... }

-Perhaps it would be a great idea to...
while(1)
{
asm("wfi");/* wait until any interrupt occurred, in some cases the above mentioned timer */
...
/* your lcd code here */
}


Now this might not fix your problem, but there's a good chance that you will get it fixed faster.
0 Kudos