While Loop Ends Unexpectedly.

basically miscellaneous, if it doesn't fit another sub-forum enter it here, we might even promote it to its own sub-forum
danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

While Loop Ends Unexpectedly.

Post by danlee58 »

I have a series of Low true 10 msec pulses that repeat every 80 msecs. I read the state on P1.25, and output an inverted version on P0.10. I use this code for a test.

while (1){

junk = IN1(25);

if ((junk && 1) == 1) LOW(10)
else HIGH(10);
}

After a reset the correct pulse will sometimes occur on P0.10, but I only get one such pulse. Most times I have to reset multiple times to get a single pulse.

I think the while loop is ending after a few iterations. Sometimes it catches the low pulse, but most times it only samples high levels before it stops.

What would cause a While Loop to stop after a few cycles?



danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: While Loop Ends Unexpectedly.

Post by danlee58 »

I had Timer1 Interrupt enabled. The Main program While loop hangs when the Interrupt occurs. I must be missing a clear interrupt instruction.

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: While Loop Ends Unexpectedly.

Post by danlee58 »

It appears that the interrupt never returns to the main program. Here is my interrupt routine.

unsigned int TIME = 0;

__attribute__ ((interrupt)) void TIMER1_IRQHandler(void)

{

if ((FIO1PIN2 && 0x04) == 1)TIME = (T1_TC & 0X1fffffff);
T1_IR = 0x10; //Reset Capture Channel 0 Interrupt.
T1_TCR = 2; //Reset Timer 1
T1_TC = 0; //Clear Timer1 Counter

VICIntEnClear0 = 0x4; //Reset Timer 1 interrupt
VICIntEnable0 = 0x4; //Enable Timer 1 interrupt
T1_TCR = 1; //Enable Timer 1
}
//End Of File;

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: While Loop Ends Unexpectedly.

Post by basicchip »

I think you want

if ((FIO1PIN2 && 0x04) == 1)TIME = (T1_TC & 0X1fffffff);

to be

if (FIO1PIN2 & 0x04) TIME = (T1_TC & 0X1fffffff); // if you are looking at P1.18 to be high

-------- in the LPC175x6x header file we use ----------------

LPC_GPIO1->PIN2

so you must have built your own header file.

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: While Loop Ends Unexpectedly.

Post by danlee58 »

I put a printf into both the interrupt and the main program. On the terminal I get a sequence of -1s, followed by a sequence of 0s. This corresponds to the signal being high, then going low. Then I get a continuous series of 32s. This represents the value in the 'TIME' integer. I should only get one 32, before it goes back to -1s

Timer1 is set to interrupt on the Falling edge of the signal, as well as the Rising edge. It only records TC on the Rising edge. TIME contains the period of the low pulse.
T1_CCR = 0x38;

It appears that once the interrupt responds to a Rising edge, it never goes back to the main program. I am monitoring P1.18 with a logic analyzer, and see no problems with the signal.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: While Loop Ends Unexpectedly.

Post by basicchip »

These 3 statements are redundant, and maybe NVIC programming within an interrupt causes an issue

T1_TC = 0; //Clear Timer1 Counter -- you just did that with T1_TCR=2

VICIntEnClear0 = 0x4; //Reset Timer 1 interrupt
VICIntEnable0 = 0x4; //Enable Timer 1 interrupt

------------

and usually I clear the interrupt as the last statement of the IRQ

T1_IR = 0x10; //Reset Capture Channel 0 Interrupt.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: While Loop Ends Unexpectedly.

Post by basicchip »

You mentioned you are interrupting on both high and low going captures, but your IRQ handler seems to ignore one, don't you want

if (FIO1PIN2 & 0x04) TIME = (T1_TC & 0X1fffffff); else HITIME = (T1_TC & 0X1fffffff) // if you are looking at P1.18 to be high

And while I occasionally use a printf inside an IRQ handler to make sure it is getting triggered, beyond that it is not a good practice, especially with serial interrupts that when the buffers get full the serial output waits.

Better is to wiggle a pin, then you can see the relationship of interrupt and other events.

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: While Loop Ends Unexpectedly.

Post by danlee58 »

I am only trying to measure the time of the low pulse, not the high time. I only used the printf to verify what I have seen on the output pin. I'll try wiggling a pin in the interrupt, and eliminate the printf.

I interrupt on the falling edge just to reset TC, so TC will read the time between the falling & rising edge, when the rising edge interrupt occurs.

The time reading that I have seen (32) seems way too short for a 10 msec. duration pulse.

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: While Loop Ends Unexpectedly.

Post by danlee58 »

I wiggle P1.28 in the interrupt. It first appears at the right time after a reset, on the first falling edge of P1.18, but then just oscillates, preventing the main program from running. I could print the Logic Analyzer screen, but it has a black background, and it will use a ton of ink. I'll try a screen capture.

danlee58
Posts: 210
Joined: Thu Jan 17, 2013 2:29 am

Re: While Loop Ends Unexpectedly.

Post by danlee58 »

I did a screen capture, now I need a way to upload the Logic Analyzer screen.

Post Reply