Any example to use CAPTURE PINS as pulse counters?

cancel
Showing results for 
Search instead for 
Did you mean: 

Any example to use CAPTURE PINS as pulse counters?

586 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Fri Sep 18 09:04:47 MST 2015
Hi all,
I'm in the first steps programming LPC1347 and I would like to learn how to use capture pins to make a fan RPM monitor.
Mi idea is to make a program using ritimer. In each ritimer handler, read a variable increased by rising in a capture pin.

The counter will be up 500 pulses/sec so I think CT16B must be sufficient.

Has someone an example to study it?

Thanks in advance.
Labels (1)
0 Kudos
19 Replies

411 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Sun Oct 04 14:46:18 MST 2015
I don't know why... but now it works  :~
0 Kudos

411 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Sun Oct 04 10:39:29 MST 2015
Woow! I run the project in the IDE on other computer and the program works OK!  :O
So I've reinstalled IDE in my PC and LPCOpen libraries... but the problem persists  :~

I don't know what to do... it's so rare...
0 Kudos

411 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Wed Sep 30 10:10:56 MST 2015
I've just received a new LPCXpresso board and I've programmed it with the same code...
y TC is still =0.

Maybe do I need to inicialize a CLK, pinmux or any more?
0 Kudos

411 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Thu Sep 24 00:51:30 MST 2015
Is it possible that onboard led blinks depending CAP pin input... and TC remains =0??  :O
0 Kudos

411 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Wed Sep 23 16:21:05 MST 2015

Quote: emimad
I've copied RITI example and added TIMER32 Code.



This code is also working here (although it's not a new created project)  :((

So I suspect that your problem is a hardware problem  :O
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Wed Sep 23 15:57:12 MST 2015
I've copied RITI example and added TIMER32 Code.
I tried with CT32B0, CT32B1, CT16B0 and CT16B1 with the same result: TC =0 always.

Onboard led toggles with CAP pin input correctly .

My board is a LPC1347 rev A.

Thanks for your assistant  :) .
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Sep 22 18:11:36 MST 2015
Create a new project with RIT and Timer32 code, compile it and post it.

I'll debug the AXF file here...

BTW: Did you try to use another timer?

//init Timer32_1
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 12, (IOCON_FUNC3 | IOCON_MODE_PULLUP | IOCON_DIGMODE_EN)); //CT32B1_CAP0
 Chip_TIMER_Init(LPC_TIMER32_1);//init timer
 LPC_TIMER32_1->CTCR = 2;//falling edge
 LPC_TIMER32_1->TCR  = 1;//start timer
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Tue Sep 22 16:41:32 MST 2015

Quote: R2D2

Quote: emimad
I'm puting P0.17 to GND manually and I erased lines that were reseting counter... and CountValue is still 0.
I change the onboard led state by P0.17 value and it's blinking so the imput is correct.



:quest:

Debugger is your friend 

Is B[17] register of GPIO-PORT showing correct input values ?

Check PIO0-17 IOCON register, is it CT32B0_CAP0 ?

Check CT32B0 registers, is TCT = 1 and CTCR =2 ?

Create a new simple project and add RIT and Timer code there...



Debugging:
* B[0] [17]register of GPIO-PORT is working OK.
* IOCON for PIO0.17 is 0x12 (Pull-down resistor enabled and CT32B0_CAP0)
* CT32B0 TCR=1 and CTCR=2


... and TC register is always =0  |(


LPC_SYSCTL->SYSAHBCLKCTRL |= (1 << 9);     //Enables clock for 32-bit counter/timer 0.
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_FUNC2 | IOCON_MODE_PULLUP)); //CT32B0_CAP0
Chip_TIMER_Init(LPC_TIMER32_0);//init timer


LPC_TIMER32_0->CTCR = 2;                 //falling edge CT32B0_CAP0
LPC_TIMER32_0->TCR = 1;                 //start timer

CountValue = Chip_TIMER_ReadCount(LPC_TIMER32_0);


I'm frustrated...  :((
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Sep 22 16:11:10 MST 2015

Quote: emimad
I'm puting P0.17 to GND manually and I erased lines that were reseting counter... and CountValue is still 0.
I change the onboard led state by P0.17 value and it's blinking so the imput is correct.



:quest:

Debugger is your friend  :)

Is B[17] register of GPIO-PORT showing correct input values ?

Check PIO0-17 IOCON register, is it CT32B0_CAP0 ?

Check CT32B0 registers, is TCR = 1 and CTCR =2 ?

Create a new simple project and add RIT and Timer code there...


0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Tue Sep 22 12:19:12 MST 2015
I'm puting P0.17 to GND manually and I erased lines that were reseting counter... and CountValue is still 0.
I change the onboard led state by P0.17 value and it's blinking so the imput is correct.

And now, I've made the simpliest code to check if it works... but CountValue is always =0 (onboard led is blinking!)

int main(void) {
volatile uint32_t CountValue;

SystemCoreClockUpdate();
Board_Init();

setSYSTICK();
config7segm();

LPC_SYSCTL->SYSAHBCLKCTRL |= (1 << 9);

Board_LED_Set(0, true);

//init Timer32
Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 0, 17, 0);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_FUNC2 | IOCON_MODE_PULLUP)); //CT32B0_CAP0
Chip_TIMER_Init(LPC_TIMER32_0);//init timer

LPC_TIMER32_0->CTCR = 2;//falling edge CT32B0_CAP0
LPC_TIMER32_0->TCR = 1;//start timer

_delay_ms(600);

while (1) {
Imprime7segm(CountValue);
Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 0, 7, Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, 0, 17));
CountValue = Chip_TIMER_ReadCount(LPC_TIMER32_0);
}
}



I'd tried writing directly
Imprime7segm(Chip_TIMER_ReadCount(LPC_TIMER32_0));

and displays shows "0000" too  :((
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Sep 21 17:17:23 MST 2015
...and volatiles...
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Sep 21 17:11:12 MST 2015

Quote: emimad
CountValue is =0 always.



Then use an input signal  :~
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Mon Sep 21 16:23:36 MST 2015
CountValue is =0 always.
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Sep 21 16:11:03 MST 2015

Quote: emimad
I need some tutorials for first steps... It's hard for me to face easy projects with a lot of issues...



First lesson: post complete projects  ;-)

Second lesson: describe exactly what's not working...
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Mon Sep 21 15:47:54 MST 2015

Quote: R2D2

Quote: emimad
Mi idea is to make a program using ritimer. In each ritimer handler, read a variable increased by rising in a capture pin.



LPCOpen is including a RIT sample 

Setting this timer to 1s and reading TC value of your capture counter shouldn't be too difficult 

So you just need to read the user manual and let a timer count. That are 4 lines of code:

 //init Timer32
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_FUNC2 | IOCON_MODE_PULLUP)); //CT32B0_CAP0
 Chip_TIMER_Init(LPC_TIMER32_0);//init timer
 LPC_TIMER32_0->CTCR = 2;//falling edge CT32B0_CAP0
 LPC_TIMER32_0->TCR  = 1;        //start timer



I'm dumb, it doesn't works...
void RIT_IRQHandler(void){
/* Clearn interrupt */
Chip_RIT_ClearInt(LPC_RITIMER);
CountValue = Chip_TIMER_ReadCount(LPC_TIMER32_0);
LPC_TIMER32_0->TC=0; // reset TC
Chip_GPIO_WritePortBit(LPC_GPIO_PORT, 0, 7, !Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, 0, 7));
}

/**
 * @briefMain entry point
 * @returnNothing
 */
int main(void)
{
/* Generic Initialization */
SystemCoreClockUpdate();
Board_Init();

setSYSTICK();
config7segm();

LPC_SYSCTL->SYSAHBCLKCTRL |= (1 << 9);

Board_LED_Set(0, true);

/* Initialize RITimer */
Chip_RIT_Init(LPC_RITIMER);

/* Configure RIT for a 1s interrupt tick rate */
Chip_RIT_SetTimerInterval(LPC_RITIMER, TIME_INTERVAL);
NVIC_EnableIRQ(RIT_IRQn);

//init Timer32
Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, 0, 17, 0);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_FUNC2 | IOCON_MODE_PULLUP)); //CT32B0_CAP0
Chip_TIMER_Init(LPC_TIMER32_0);//init timer

LPC_TIMER32_0->CTCR = 2;//falling edge CT32B0_CAP0
LPC_TIMER32_0->TCR = 1;//start timer

_delay_ms(600);

while (1) {
Imprime7segm(CountValue);      // print value in 7 segments display
}
}



:((

I need some tutorials for first steps... It's hard for me to face easy projects with a lot of issues...
Fortunately I have your help R2D2 
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by emimad on Mon Sep 21 06:28:09 MST 2015
Thanks to all!
yes, R2D2... I have a program using RITIMER correctly  8-)  my problem is to know how to read TC value  :(     I'm newbie with Cortex, sorry.

Your answer was very clearifying so I'll add your code at the start of my main() function.

//init Timer32
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_FUNC2 | IOCON_MODE_PULLUP)); //CT32B0_CAP0
Chip_TIMER_Init(LPC_TIMER32_0);//init timer
LPC_TIMER32_0->CTCR = 2;//falling edge CT32B0_CAP0
LPC_TIMER32_0->TCR  = 1;        //start timer


In the RITIMER handle I'll add the next code:
CountValue = Chip_TIMER_ReadCount(LPC_TIMER32_0);
LPC_TIMER2->TC=0;                                       // reset TC


That should be enough  :)

Thank you very much  0:)
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Badman on Fri Sep 18 23:38:58 MST 2015
Sorry for my code on registers, now I noticed that this forum LPCOpen.
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri Sep 18 12:46:02 MST 2015

Quote: emimad
Mi idea is to make a program using ritimer. In each ritimer handler, read a variable increased by rising in a capture pin.



LPCOpen is including a RIT sample 

Setting this timer to 1s and reading TC value of your capture counter shouldn't be too difficult 

So you just need to read the user manual and let a timer count. That are 4 lines of code:

 //init Timer32
 Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 17, (IOCON_FUNC2 | IOCON_MODE_PULLUP)); //CT32B0_CAP0
 Chip_TIMER_Init(LPC_TIMER32_0);//init timer
 LPC_TIMER32_0->CTCR = 2;//falling edge CT32B0_CAP0
 LPC_TIMER32_0->TCR  = 1;        //start timer
0 Kudos

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Badman on Fri Sep 18 12:14:46 MST 2015
I not tested this code:
#include "LPC11xx.h"

#define KHZ_PRESCALE    (SystemCoreClock / 1000)

volatile uint16_t rpm, tmp_rpm;
volatile uint8_t status_flag = 0;

// obsluga przerwania TMR16B0
void TIMER16_0_IRQHandler(void)
{
if ( LPC_TMR16B0->IR & (0x1<<1) )
{
LPC_TMR16B0->IR = 1;// clear flag from MR0
rpm = tmp_rpm;
tmp_rpm = 0;
status_flag = 1;
}
if ( LPC_TMR16B0->IR & (0x1<<4) )// sprawdzenie czy przerwanie od kanalu zliczajacego
{
LPC_TMR16B0->IR = 0x1<<4;// skasowanie flagi przerwania
tmp_rpm++;
}
}

int main(void) {

LPC_SYSCON->SYSAHBCLKCTRL |= 1<<16;//wlaczenie zegara dla bloku IOCON_LOC
LPC_IOCON->PIO0_2 &= ~0x07;// zerowanie 3 pierwszych bitow
LPC_IOCON->PIO0_2 |= 0x02;// CT16B0_CAP0

// konfiguracja timera TMR16B0
LPC_SYSCON->SYSAHBCLKCTRL |= 1<<7;// wlaczenie zegara dla bloku timera TMR16B0
LPC_TMR16B0->PR = KHZ_PRESCALE - 1;   // ustawienie preskalera na 1 kHz
LPC_TMR16B0->MR0 = 1000;// for 1 s
LPC_TMR16B0->CTCR = 2;  // rosnace zbocze rozpoczyna zliczanie i generuje przerwanie
LPC_TMR16B0->MCR = (0x1<<0) | (0x1<<1);// interrupt and reset MR0
NVIC_EnableIRQ(TIMER_16_0_IRQn);

LPC_TMR16B0->TCR = 1;// wlaczenie timera

    while(1) {
    if (status_flag == 1)
    {
    status_flag = 0;
    printf("RPM=%d\r\n", rpm);
    }
    }
    return 0 ;
}

0 Kudos