"Hello World"
A hello world project delivers that sense of excitement because you got something working. However, a hello world project or code for the same purpose appears many times over in even some of the most sophisticated systems simply because when it says “Hello!”, we know it’s working.
NoteThis topic is introductory and should be read in conjunction with other topics in this series of training documents.
“Hello World” has become synonymous with that first project from PICs to .NET. The reason being that it’s a simple first project which gives a confidence boost and keeps you going onto the next project and it gives you something to show off to friends. In the long term, the “Hello World” project will often get used over and over again to establish if a device is starting up and functioning as it should - setting device fuses can be tricky. We could also refer to the “Hello World” as the system’s initialization routine before we get to the more complicated or sophisticated coding.
The simplest of “Hello World” example is toggling a LED. The code provided below illustrates the few simple steps required for the PIC18F45K22. Always ensure to consult the datasheet for the device you are working with as this will provide accurate and needed information on the device that you are using. Although there are great similarities between devices, nuances can lead to unexpected results – the device is always doing exactly what you are telling it to do.
Since Microchip continues to pack more features into it’s micros, one PIN is often shared with multiple internal peripherals. To use a PIN to switch an LED on requires that you disable the shared features on the PIN. If not disabled, the digital I/O which is not always the default feature may not be available leading to no results.
Once you have flashed your RADIX - DUO with this code, you will be able to tell if the device is starting up. When the device does not start, this can be used as a simple diagnostic tool. Items to check are PORT configurations, fuse settings, and so on. Simple things are often the reason why something complicated does not work, so focus on the simple reasons. A simple bad connection of an LED ground path, or a resistor not connected properly can be the root cause for the LED not flashing.
The diagram below shows the steps that the microcontroller will go through during its startup sequence. If you are a doodler, it’s a good idea to produce “documentation” on what you are working on. This also helps to conceptualize what you are doing and keeps you on track. This will also help keep your thoughts in one place. A pen or pencil diagram is usually sufficient and will help think things through.
This is more than just a flashing led, it helps with questions about whether a device has been set up properly and whether the code works. You will use and reuse the above code many times, and it's why so many systems have a device on or a Device Function light. Keep in mind that unless something is broken, the PIC is doing exactly what you tell it to do.