EmbDev.net

Forum: ARM programming with GCC/GNU tools Count external pulses using LPC1768


Author: Joseph Chan (Company: MagicKingdom) (jchan12345)
Posted on:

Rate this post
0 useful
not useful
Anyone knows how to count external pulses using CAP0.0 of LPC1768 ?

Do you have a working code ?

Here is my code but it does not work.  Don't know why I always read 0 
from LPC_TIM0->TC with external pulses going into P1.26.


//Set PINSEL3 [21:20] = 11 for P1.26 as CAP0.0
LPC_PINCON->PINSEL3 |= (0x3 << 20);

// set pin P1.26 as input
LPC_GPIO1->FIODIR &=~(1<<26);

// PCTIM0 Timer0/Counter0Power On
LPC_SC->PCONP |= 2;

// Counter Reset
LPC_TIM0->TCR = 2;

//Counter Mode:TC incremented on rising edges on the CAP0.0 for
// timer0
LPC_TIM0->CTCR = 1;

// Capture on rising edges on the CAP0.0.
// TC -> CR0
LPC_TIM0->CCR = 0;

// Counter Enable.
//LPC_TIM0->TCR = 1; // I try 1 too and it still does not work
LPC_TIM0->TCR = 0;  // 0=Enable from data sheet

Author: A mirzaee (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
use this code
#include <RTL.H>
#include <LPC17XX.H>
#include <Ports.H>
#include <LCD.H>
#include <uart.h>

static char in_line[160];
int pos = 0;

int i;


void ComProc()
{
    myLog((u8*)&in_line,3);
  if ( strcmp(in_line,"cls") == 0)
    cls();
  for ( i=0; i < 160; i ++ )
  in_line[i]=0;
  pos = 0;
}

void EnableCntr0()
{
    //Set PINSEL3 [21:20] = 11 for P1.26 as CAP0.0
     LPC_PINCON->PINSEL3 |= (0x3 << 20);

  // set pin P1.26 as input
  LPC_GPIO1->FIODIR &=~(1<<26);

  // PCTIM0 Timer0/Counter0Power On
  LPC_SC->PCONP |= 2;

  // Counter Reset
  LPC_TIM0->TCR = 2;

  //Counter Mode:TC incremented on rising edges on the CAP0.0 for
  // timer0
  LPC_TIM0->CTCR = 1;

  // Capture on rising edges on the CAP0.0.
  // TC -> CR0
  LPC_TIM0->CCR = 0;

  // Counter Enable.
  //LPC_TIM0->TCR = 1; // I try 1 too and it still does not work
  LPC_TIM0->TCR = 1;  // 0=Enable from data sheet
}

void EnableCntr2()
{
  //Set PINSEL0 [9:8] = 11 for P0.4 as CAP0.0   --->p0.5 ;ar mikonad
   LPC_PINCON->PINSEL0 |= (0x3 << 8);

  // set pin P0.4 as input
  LPC_GPIO1->FIODIR &=~(1<<4);

  // PCTIM0 Timer1/Counter0Power On
  LPC_SC->PCONP |= 2;

  // Counter Reset
  LPC_TIM2->TCR = 2;

  //Counter Mode:TC incremented on rising edges on the CAP2.0 for
  // timer1
  LPC_TIM2->CTCR = 1;

  // Capture on rising edges on the CAP0.0.
  // TC -> CR0
  LPC_TIM2->CCR = 0;

  // Counter Enable.
  //LPC_TIM0->TCR = 1; // I try 1 too and it still does not work
  LPC_TIM2->TCR = 1;  // 0=Enable from data sheet
}


void EnableTimer2()
{
  //Set PINSEL0 [9:8] = 11 for P0.4 as CAP0.0   --->p0.5 ;ar mikonad
   LPC_PINCON->PINSEL0 |= (0x3 << 8);

  // set pin P0.4 as input
  LPC_GPIO1->FIODIR &=~(1<<4);

  // PCTIM0 Timer1/Counter0Power On
  LPC_SC->PCONP |= 2;

  // Counter Reset
  LPC_TIM2->TCR = 2;

  //Counter Mode:TC incremented on rising edges on the CAP2.0 for
  // timer1
  LPC_TIM2->CTCR = 0;

  // Capture on rising edges on the CAP0.0.
  // TC -> CR0
  LPC_TIM2->CCR = 0;

  // Counter Enable.
  //LPC_TIM0->TCR = 1; // I try 1 too and it still does not work
  LPC_TIM2->TCR = 1;  // 0=Enable from data sheet
}


void EnableCntr1()
{
//Set PINSEL3 [5:4] = 11 for P1.18 as CAP1.0
 LPC_PINCON->PINSEL3 |= (0x3 << 4);
//LPC_PINCON->PINSEL3 |= (0x3 << 7);

// set pin P1.18 as input
LPC_GPIO1->FIODIR &=~(1<<18);
LPC_GPIO1->FIODIR &=~(1<<19);

// PCTIM0 Timer1/Counter1Power On
//LPC_SC->PCONP |= 2;

// Counter Reset
LPC_TIM1->TCR = 2;

//Counter Mode:TC incremented on rising edges on the CAP0.0 for
// timer0
LPC_TIM1->CTCR = 1;

// Capture on rising edges on the CAP1.0.
// TC -> CR0
LPC_TIM1->CCR = 0;

// Counter Enable.
//LPC_TIM0->TCR = 1; // I try 1 too and it still does not work
LPC_TIM1->TCR = 1;  // 0=Enable from data sheet
}


int main()
{
  char c=0;
  u8 prn[100];
  int cntr0;
  int cntr1;

  int cntr2;
  //const unsigned long TCR_COUNT_RESET = 2, TCR_COUNT_ENABLE = 0x01;

  SystemInit();
  LPC_GPIO2->FIODIR |= 0xFF; // P2[0..7] =

  LCD_Init();

  LcdLog = 1;

  LCD_Clear(White);
  LCD_SetBackColor(Red);
  LCD_SetTextColor(Blue);


  UART2_Init();
  logstat=1;
  myLog("Started...",1);
  myLog("LCD--->OK",2);
  myLog("Start UART2",3);
  Uart2Log =1;
  myLog("UART2--->OK",4);
  cls();

  EnableCntr0();
  EnableCntr1();
  //EnableCntr2();
  EnableTimer2();

  while(1)
  {
  cntr0= LPC_TIM0->TC;
  cntr1= LPC_TIM1->TC;
  cntr2= LPC_TIM2->TC;
  sprintf(prn,"%d %d %d %l \n\r", cntr0,cntr1, cntr2);
  UART2_SendString (prn) ;
    c = getkey();
  if ( c != 0 && c != 13)
  {
    in_line[pos++] = c;
    LCD_ShowString(1,19*16, in_line);
  }
  if ( c== 13 )
  {
     ComProc();
  }

  }

}

Author: Ramana Madishetty (Company: Duffifie) (alchemist2805)
Posted on:
Attached files:

Rate this post
0 useful
not useful
How to count the external low pulses (low occupancy time)?

As in above code, Counter Mode:TC incremented on rising edges.

I using the PPD42NJ particle matter sensor, which is to count the low 
pulse occupancy time.

Reply will be appreciated.

Thanks

Author: JojoS (Guest)
Posted on:

Rate this post
0 useful
not useful
The counter mode is the wrong mode when you want to measure pulse width 
times. At first, you should get the user manual for the LPC176x, its 
UM10360.
Then you need to configure a timer that runs with a clock with the 
desired resolution of the measurement. If you want 1 µs resolution the 
clock must be set to 1 Mhz.
Then set CCR to capture rising and falling edges and generate an event.
In the ISR, you need to check the input pin if a falling or rising edge 
occured. On a falling edge, save contents of the CR (capture register) 
as start time. On a rising edge, save CR as stop time and add the 
difference to a sum counter.
For the same counter, you can program a MATCH register with a value of 
30e6 and create another interrupt. This will fire after 30 s for the 
final calculation of on-off times.

Author: Ramana Madishetty (Company: Duffifie) (alchemist2805)
Posted on:
Attached files:

Rate this post
0 useful
not useful
Thanks for your reply JojoS ,

As you mentioned, am trying to read the low occupancy time in the ISR 
with Rising Edge and falling Edge. So, initially am trying to see the 
time in UART for both rising and falling edge.

I have done the changes, But still am not able to enter in ISR - if 
condition. Please see the attached code.

Let me know, if any suggestions

Thanks

Author: JojoS (Guest)
Posted on:

Rate this post
0 useful
not useful
this condition will never become true:
   if((LPC_TIM0->IR & (1<<4)) == 0x01) // if MR0 interrupt

   // try this:
   if((LPC_TIM0->IR & (1UL<<4))) // if MR0 interrupt

Also it is no good idea to use printf() in an ISR. For testing it may 
work, but depends on the code for the output device. It is better to 
collect the results in an array and print that in the main().
But at least the interrupt indicator LED1 should toggle on the capture 
interrupt.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.