A simple way to blink LED

Normally people turn LED ON then delay, turns it OFF and delay again and so on, just to blink a LED.

Let me to do it differently, just create function which receive a boolean value (0 = OFF, 1 = ON), then we can directly control this boolean state from the busy wait loop.

In kinetis microcontroller using default internal clock I need to loop about 50000 times to delay 500ms. Hmm, it is near to 65536 (0x10000), then I can do an infinite loop and just verify if this most significant bit is set or not:

  /* Blink blue LED to indicate we are here */
  for (; ; i++)
        blueled(i & 0x10000);

What is the difference?

A.: Using this approach you are calling the function which write to LED register much more times, but it reduces the number of lines of your code.

Pay attention, you cannot use this approach when writing to flash memories, otherwise you will wear out it too fast.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s