Blocking wait for pin change on a LPC1114 microcontroller

ccdmicrocontrollernxppwm

I need to synchronize the rising edge of a PWM with another signal pulse's falling edge on a LPC1114FN28 (trying to drive a TCD1304DG), but am failing to achieve this so far. I have tried the following:

while (LPC_GPIO0->MASKED_ACCESS[ClockMask]);    //wait for the clock to go low
while (!(LPC_GPIO0->MASKED_ACCESS[ClockMask]));   //wait for the start of PWM cycle
LPC_GPIO0->MASKED_ACCESS[ICGMask] = 0x0;   //make the ICG pin go low

where the ClockMask is a mask of the corresponding PWM pin, running at 1MHz frequency, 50% duty cycle and ICGMask a mask of the pin I'm trying to synchronize with the PWM.

The strange problem I encountered is that one of the two whiles seem to block forever for some reason and any code after them does not get executed. When I remove them, it works OK.
This could be probably caused by the instructions for the while lasting the same as is the PWM period and thus the condition would always see the pin in the same state, but that seems rather improbable to me…

I am running the chip at 48MHz from the IRC and the PWM measured with my oscilloscope is practically flawless.

So what could I be doing wrong? Is there any better way to do this? Would writing this part of code in assembler help with the timings (they need to be pretty precise)? If so, is there any tutorial for this?

Thanks in advance for any answers!

Edit:
Even using interrupts I was unable to get sufficiently invariable results – the timing of the interrupt after the pulse edge changes. I will try more methods…

Other methods did not work at all. This is probably simpy unachievable, but I do not know, what else could I be doing wrong if not the timing (as the Toshiba CCD is still not responding).

Best Answer

I assume that you have changed the IOCON of the PWM output pin to change its function from GPIO pin to Counter/Timer output pin. Your while statements are then trying to use the same pin as a GPIO input, and my guess is that you just can't do that.

You haven't told us the duty cycle of your PWM signal but at 1MHz you only have 48 clock cycles for the entire period. If the duty cycle gets down below 10% or above 90% it will be very difficult to catch both the low time and the high time. This might be a good place for hand-optimized assembly code.

In any event, it will be difficult to detect an edge on a 1MHz signal and toggle another output bit very quickly. I'm not sure you could still say that the output was "synchronized" to the PWM edge.