This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer0 precision in the LPC2103

Hi,
I want to generate a very precise timing using Timer0 on the LPC2103, so I configure it with a MATCH resgister to generate a precisely an Interrupt each 1s(could be 1ms or 10ms), so here is a code that I wrote:

//----------------------------------------------------------------
#include<lpc2103.h>

void Timer0_IRQ_Handler(void) __irq{
IOPIN ^= 0x00000200;    // Toggle LED (BREAKPOINT HERE)

T0IR |= 0x00000001;     // clear Timer0 Match Inerrupt flag
T0TCR = 0x00000001;     // restart Timer0 after Int Execution

VICVectAddr = 0x00000000;
}


int main(void){
// GPIO Config
IODIR = 0x00000200;

// Timer0 config
T0TCR = 0x00000002;     // reset Timer0
T0PR  = 0x00000000;     // Clear prescaler
T0MCR = 0x00000007;     // Intterupt and Reset on MATCH 0 and STOP
T0MR0 = 0x03840000;     // Match  every 1s (1/f=3*19.6608MHz)
T0TCR = 0x00000001;     // start Timer0

VICVectCntl0 =0x00000024;
VICIntEnable= 0x00000010;
VICVectAddr0= (unsigned)Timer0_IRQ_Handler;
        while(1){
        }
}
//----------------------------------------------------------------

PCLK = masterclock =19.6608MHz*3 =58.982400 MHz

So with the configuration T0MR0 = 0x03840000; I get in the simulator the Timming = 1.00002177s at the 1st instruction inside the Int handler means 21.77 uS(probably the duration of istruction generated by the compiler to handle correctly the interruption)

Than I tried to compensation of this timing and I chaged the Match0 config register to:

T0MR0 = 0x0383FAFA; So I got :
T=1.00000003s means about 30nS of errors.

My problem is when I run again the simulator anothertime I got
T=1.99997886 instead of 2.00000006 and next time T=2.99995768 ans so on ..

any one have an idea why I got these troubles and how I can really generate a real precise timing or perhaps it's a simulator problem (keil realview/uVison).
I used the Fast Interrupt instead vectored ones, same things..

Thanks

  • Many things.

    1) The first time isn't just the interrupt response time, but also the time for your program to start. The simulator doesn't start to count time when you turn on the interrupt.

    2) Why don't you use automatic reload of the timer? Then it will trig an interrupt _exactly_ once/ms. The actual time that your ISR toggles the LED will jitter a bit depending on if the processor is busy with another ISR or you have any code sections with interrupts disabled. But in the end, you will get exactly 1000 interrupts / second since you will not introduce any gaps without the timer running.

    3) Do _not_ use magic constants like:

    0x03840000;     // Match  every 1s (1/f=3*19.6608MHz)
    


    The datasheet has a formula for computing this constant. #define your input frequency, and then compute the constant using this forumula. Then people can see what you are doing.

    4) You haven't really told us what you want to do. It is hardly to toggle a LED every 1000us. What do you need your "very precise timing" for? Spend a _long_ post on describing what you are going to use your program for, and not what you think your current code does. A timer can perform a great number of tricks, but we can't even know if any tricks are applicable unless you focus on your requirements, instead of focusing on your code.