Timer in ARM MBED LPC1768

Here we use the timer to count the time interval between LED ON and OFF and displayed count on serial window.

2.8k views1 min readUpdated Jan 3, 2023

Introduction

Timers in microcontrollers are used for introducing delay, counting events, generating waveforms and also for PWM generation.

In ARM MBED 32-bit timer are used to set time, read time, count time, etc. (between microseconds and seconds).

You can independently create, start and stop any number of Timer objects.

 

Note: Timers are based on 32-bit int microsecond counters, so they can only time up to a maximum of 2^31-1 microseconds (30 minutes). They are designed for times between microseconds and seconds. For longer times, you should consider the time() real time clock.

 

Timer

Create object of Timer

e.g.

Timer timer

 

Start()

To start the timer

e.g.

timer.start()

 

Stop()

To stop the timer

e.g.

timer.stop()

 

Reset()

To reset the timer

e.g.

timer.reset()

 

Read()

Read the passed time

e.g.

timer.read()

 

Read_ms()

Read the passed time in milisecond

e.g.

timer.read_ms()

 

Read_us()

Read the passed time in microsecond

e.g.

timer.read_us()

 

Program

// Count the time to toggle a LED
#include "mbed.h"
Timer timer;
DigitalOut led(LED1);
int begin, end;
int main() {
timer.start();
begin = timer.read_us();
led = !led;
end = timer.read_us();
printf("Toggle the led takes %d us", end - begin);
}

Components Used

Powered byMouser Electronics

Comments0

Join the discussion — share a question or tip.

Be the first to share your thoughts on this guide.