Wednesday, November 4, 2009

LPC2148 Timer

I'm a newbie in LPC2148 timer programming.I faced a lot problems for finding out the real value of the datas for creating a 1sec delay.So I deceided to write it clearly in my blog so that no one else would be so hackled as me.
First of all make it clear that you have to include the Timer0 block of the Microcontroller.Its entirely necessary for accurate and effective delay.
The main registers that are required in Timer0 block are Prescaler Register(PR),Timer Counter(TC),Match Register0(MR0).
The prescaler Register increases by unity on each clock cycle until it reaches the value that we have set for it in Prescaler Register.After that value is reached at the advance of the next clock cycle the Prescaler Register counting is reset to start from 0 and the Timer Counter will increase by unity.We have to put a value in the Match Register which will monitor the value at Timer Counter and will be capable of taking appropriate action once the desired value is reached by the TC.

For example if the processor clock cycle(Pclk) is 12MHz i.e., the we need 12,000,000 clock cycle to complete 1sec.Let us put the value 9999(=0x270F) in the prescaler register.Then the Timer Counter will increase after every 10000 clock cycles.
Hence we need 1200 in the MR0 for covering 12,000,000 cycles and producing 1 sec delay.The total delay code will be such---

void delay()
{
// unsigned long check_value = 0x000004B0;
T0PR = 0x0000270F;
T0MR0 = 0x000004B0 ;
T0TCR = 0x01; // the timer0 is activated
while(T0TC != T0MR0);
T0MCR = 0x00000002;

T0TCR = 0x02;

}


If u r using keil compiler this code wont act as expected in the simulator.I warn u never believe the simulator.Alwqays try the code in the hardware.

3 comments:

  1. code is working fine just you need to change ..
    while(T0TC==0);

    thanks...

    ReplyDelete
  2. Hi,

    My name is KC Owens, I’m a college student and I love to travel! While cruising the Internet, I found your site and really enjoyed reading your posts. I have been to countries all over Europe with just my backpack and a camera. Since I am a college student and I have significant bills, it can be difficult to find ways to travel the world. However, I have done this several times, with less than ten pounds of luggage and while on a college dime!

    I was hoping that you would allow me to write a post for your site to share my tips and tricks with your readers. I put a lot of time into my traveling, it is my biggest passion and I would love to inspire others by sharing my stories, mistakes and triumphs. I look forward to hearing from you!

    Best,

    KC Owens

    ReplyDelete