LPC21xx Timer0 strange problem :S

Hi…

Im trying to generate 20 pwm signals for servos…

My timer0 config is simply generate the irq without reset TC or any else… Its running at 60mhz with a T0PR=2.

Lets explain the problem with an example:

if Im on T0TC=227101 and I set T0MR0 to 289901 (850us), irq halts at T0TC=357344 (instead of 289901)… but if I set T0MR0 to a much higher number, 5000us and above, then it halts correctly, at time, with a minimum difference between both…

My code basically looks like:

void T0isr (void) {

clear_irqT0(T0IR_MR0);

some_servos_stuff

T0MR0=T0TC + diff_in_uS * T0_ONE_US;

}

EDIT: I’ve just discovered that this problem does not occur if I reset the timer every irq… but why?

any approach to the problem?

What does the exact code that works (T0TC reset) look like?

One line that looks a little problematic (but not to the extent of being off my milliseconds!) is this:

T0MR0=T0TC + diff_in_uS * T0_ONE_US;

If diff_in_uS * T0_ONE_US takes a long time to calculate, and T0TC is fetched before the calculation, then the result will be different than if either of those conditions is not true. I would at least make the desired order explicit, either

u32 temp = diff_in_uS * T0_ONE_US;
T0MR0 = T0TC + temp;

or

u32 temp = T0TC;
T0MR0 = temp + diff_in_uS * T0_ONE_US;

But again, I don’t see this as being the cause of your problem, especially since things work when you reset T0TC. So I think you need to show more code for both working and not-working examples.

problem solved… caused because i place some “printf” through the UART with the obvious long delay in the IRQ

Thanks :slight_smile: