Trying to get capture to work

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

Trying to get capture to work

450 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by LynnF on Sun Mar 14 14:37:11 MST 2010
Hi,
Any help would be appreciated. I'm thinking my problem is that I can't see the forest for the trees. :)
I am trying to measure a frequency by counting the time between two rising edges on the PIO1_8 pin.
Here is my code:

Quote:

uint32_t StartByte, StopByte;
// Configure the capture pin connected to the humidity sensor
//PIO1_8, frequency counter capture
//CT16B1_CAP0 Capture input 0 for 16-bit timer 1
//LPCXpresso pin 22
LPC_IOCON->PIO1_8 &= ~0x07;
LPC_IOCON->PIO1_8 |= 0x01;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
// Load capture register (CRO) with TC on rising edge upon a capture event on pin 22
// also generate interrupt flag so we know when to start and stop the frequency count
LPC_TMR16B1->CCR = 0x05;
// Set pre-scaler value at 0
LPC_TMR16B1->PR = 0x00;
// Reset Timer Control Register to 0
LPC_TMR16B1->TCR = 0x02;
// Start Timer
LPC_TMR16B1->TCR = 0x01;

// Wait until a capture event occurs and the interrupt flag is set
while(!LPC_TMR16B1->IR & (1<<4));
// Set the StartByte = to the value in the capture register
StartByte = LPC_TMR16B1->CR0;
// Clear the interrupt flag
LPC_TMR16B1->IR &= ~(1<<4);
// Wait until the next capture event occurs and the interrupt flag is set
while(!LPC_TMR16B1->IR & (1<<4));
// Set the StopByte = to the value in the capture register
StopByte = LPC_TMR16B1->CR0;
// Turn off the timer
LPC_TMR16B1->TCR = 0;
// Turn off the capture control
LPC_TMR16B1->CCR = 0x00;



If you can see what I have wrong please advise, thanks!
I know the principal works because I already have this type function working on a MicroChip MCtrl and am porting the code to the LPC1343.
0 Kudos
3 Replies

400 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wrighflyer on Mon Mar 15 09:13:08 MST 2010

Quote: unnati
[B][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/B]
[LEFT][SIZE=2]main()[/SIZE][/LEFT]

[LEFT][SIZE=2]{[/SIZE]

[SIZE=2]system_config();[/SIZE]
[LEFT][SIZE=2]timer16b0_init();[/SIZE][/LEFT]
[/LEFT]



[LEFT][SIZE=2]}[/SIZE][/LEFT]

[LEFT][SIZE=2]thanks!!![/SIZE][/LEFT]



[LEFT]I use the AVR version of GCC more than ARM but it's certainly true that in an embedded program that the C pre-amble CALLs main() and if it ever returns then in the runtime for AVR - and possibly ARM - the falls through to "__exit:" which, in the AVR case consists of a CLI opcode (global diable interrupts) and then a tight loop "here: RJMP here".[/LEFT]

[LEFT]If the GCC for ARM follows the same pattern then by your allowing it to fall out of main() means it could well be hitting similar code so even if you managed to configure the interrupt it would never happen.[/LEFT]

[LEFT]See what happens with:[/LEFT]
[LEFT]int[/LEFT]
 
[LEFT]main() [/LEFT]
 

[LEFT]{

[LEFT]system_config();
[LEFT]timer16b0_init();
while(1);[/LEFT]
[/LEFT]

}
[/LEFT]

 
 
 

[LEFT]which will keep execution within main()[/LEFT]

EDIT: OK, forget that, CodeRed suppress the standard C runtime then provide their own (C!) version in cr_startuplpc1X.c and that contains:
...
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (__main)[/SIZE]
[SIZE=2]  __main() ;[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][SIZE=2]  main() ;[/SIZE][/LEFT]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]  //[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[LEFT][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]  // main() shouldn't return, but if it does, we'll just enter an infinite loop [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]  //[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]  while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (1) {[/SIZE]
[SIZE=2]  ;[/SIZE][/LEFT]
[SIZE=2]  }[/SIZE]

[SIZE=2]so if you fall through main() in your own program it will be caught by this loop with interrupts enabled - so ignore my theory :-([/SIZE]
0 Kudos

400 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by unnati on Mon Mar 15 07:03:35 MST 2010
hi,

i am having a problem in generating timer interrupt.the program is not getting directed to the timer interrupt service routine.

have u tried timer interrupt??

please advice if u know about that..

the following is the program:

[B][SIZE=2][COLOR=#7f0055][LEFT]#include[/B][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff]"LPC11xx.h"[/LEFT]
[/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][LEFT]#include[/B][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff]"stdint.h"[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]
int8_t dig_count=0;
int16_t DISPLAY_DATA;
int16_t i;


system_config();
timer16b0_init();

int32_t c[]=
{
0x07C0, [/SIZE][SIZE=2][COLOR=#3f7f5f]//digit1 0[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]0x0BC0, [/SIZE][SIZE=2][COLOR=#3f7f5f]//digit2 1[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]0x0DC0, [/SIZE][SIZE=2][COLOR=#3f7f5f]//digit3 2[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]0x0EC0 [/SIZE][SIZE=2][COLOR=#3f7f5f]//digit4 3[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]};


system_config()
{
LPC_SYSCON->SYSMEMREMAP = 0x03;
LPC_SYSCON->SYSOSCCTRL = 0x00;
LPC_SYSCON->CLKOUTDIV = 0X78;
LPC_SYSCON->SYSAHBCLKCTRL = 0xDF;
LPC_IOCON->PIO2_0 = 0x00;
LPC_IOCON->PIO2_1 = 0x00;
LPC_IOCON->PIO2_2 = 0x00;
LPC_IOCON->PIO2_3 = 0x00;
LPC_IOCON->PIO2_4 = 0x00;
LPC_IOCON->PIO2_5 = 0x00;
LPC_IOCON->PIO2_6 = 0x00;
LPC_IOCON->PIO2_7 = 0x00;
LPC_IOCON->PIO2_8 = 0x00;
LPC_IOCON->PIO2_9 = 0x00;
LPC_IOCON->PIO2_10= 0x00;
LPC_IOCON->PIO2_11 = 0x00;
LPC_GPIO2->DIR = 0xFFF;[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][LEFT]return[/B][/COLOR][/SIZE][SIZE=2];
}


timer16b0_init()
{
LPC_IOCON->PIO1_11 = 0x01; [/SIZE][SIZE=2][COLOR=#3f7f5f]// TIMER16_0[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
LPC_TMR16B0->PR=0x0C; [/SIZE][SIZE=2][COLOR=#3f7f5f]//PRESCALE=12 FOR 12MHz[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]LPC_TMR16B0->MR0=0x3E8; [/SIZE][SIZE=2][COLOR=#3f7f5f]//COUNT=1000d FOR 1ms[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]LPC_TMR16B0->MCR=0x03; [/SIZE][SIZE=2][COLOR=#3f7f5f]//MR0[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]LPC_TMR16B0->TCR=0x02; [/SIZE][SIZE=2][COLOR=#3f7f5f]//RESET TIMER16B0[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]LPC_TMR16B0->TCR=0x01; [/SIZE][SIZE=2][COLOR=#3f7f5f]//START TIMER16B0[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]}

[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][LEFT]void[/B][/COLOR][/SIZE][SIZE=2] TIMER16_0_IRQHandler([/SIZE][B][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][SIZE=2])
{
LPC_TMR16B0->IR=1; [/SIZE][SIZE=2][COLOR=#3f7f5f]//CLEAR INTERRUPT FLAG[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT]
DISPLAY_DATA=c[dig_count];
dig_count++;
LPC_GPIO2->DATA = DISPLAY_DATA;
[/SIZE][B][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][SIZE=2](dig_count>3)
{
dig_count=0;
}
LPC_TMR16B0->TCR=0x02; [/SIZE][SIZE=2][COLOR=#3f7f5f]//reset TIMER16B0[/LEFT]
[/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055]return[/B][/COLOR][/SIZE][SIZE=2];
}
[/LEFT]
[/SIZE][B][SIZE=2][COLOR=#7f0055][LEFT]int[/B][/COLOR][/SIZE][SIZE=2] main()
{
system_config();
timer16b0_init();[/LEFT]
}

thanks!!!

[/SIZE]
0 Kudos

400 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by LynnF on Sun Mar 14 18:42:55 MST 2010
I found the problem and now everything is working.
0 Kudos