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();
  }

  }

}

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.