Saturday, October 22, 2016

Blinking LEDs

In the embedded world, blinking a LED is the equivalent to the “Hello World” program. The simple task of blinking a LED goes a long way to proving that you have work flow that works. It is a huge milestone for the project. This activity is sometimes known as bringing up the board. It is when a new board is provided and software is executed on it.

For those who are not familiar with the embedded concept, blinking a LED proves that your tool chain is good, your understanding of accessing the hardware is sound as well.
The most simple implementation of the blinking led application is

int main(void)
    while (1)
    { ^= 0X01;
    return 0;

The code will exclusive ORed the current value on PORTA.0, effectively flipping the bit. If are able to build this code and programmed it into your board, you can put a CRO on pin PORTA.0 and see a nice signal toggling up and down. The frequency and duty cycle are defaulted to the fastest setting possible.
If you able to see a toggling signal, you have reached a significant milestone. The hardware is considered good enough to start developing code upon. Lets break it down.
Your compiler and linker setup. By seeing the toggling signal on PORTA.0, it means that your tool chain has created binary code that your microcontroller can understand. It also means that your tool chain has linked all the binary, resolved the addresses in your code correctly, and that your specified memory map allows the microcontroller to boot. Getting the memory map is a tricky task as you have address locations and sizes correct for items such as stacks, heap, constant data etc.
Your debugging setup. By seeing the toggling signal, it shows that you can program the microcontroller with your debugger setup and trigger the code. If your debugger setup to the microcontroller is via JTAG, you can set break points and inspect variables and memory addresses. This is a big confidence booster to the developers as it provides them the tools to work on the problem.
Your hardware. By seeing the toggling signal, it shows that your hardware platform is powered correctly and is able to provide you with a stable platform to start developing code.
If you are wondering if the above code will actually blink a led. Well, unless the frequency of your microcontroller is quite low, it will probably not illuminate a LED. The frequency of the output is probably way too high. To have it blink, replace the while(1) loop with code that is tied to a timer event that occurs every 500ms. This will prove the hardware further. You will also have to check if the output pin can drive enough current it to the LED for illumination. Otherwise, you will have to drive transistor or a FET to drive the LED. 

One critical thing to remember when bringing up your board; the project is still very young. Even though you have proved some parts of your development flow, tool chain and development hardware, the rest of pieces of the puzzle are yet to be solved. However, it is a big step just to be able to blink a led. 

I am writing a Guide to Successful Unit Tests.
You can get it here at Leanpub. or here at Gumroad. and read about these topics and more.

No comments:

Post a Comment