Hello World

Document Information

DocuDocument Title First Project: Hello World
Document Number & Revision> 403 - 0004 - 1.0
Release Date> ApriApril  13, 2019
Document Status> Rev 1: Original Document

Contact»

Synopsis

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.

Note
This topic is introductory, and should be read in conjunction with other topics in this series of training documents.
 

Introduction

“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.

 

Hello World

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.

 

Note
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.
/* Hello World */ TRISB = FE; // RB0 is set as output Void main(){ while(1){ LATB.B0 = 1; Delay_MS(500); LATB.B0 = 0 Delay_MS(500); }

 

Once you have flashed your DEVCC-40 with this code, you will be able to tell if the device is startting 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, 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.

Conclusion on Hello World

This is more than just a flashing led, it helps with questions around whether a device has been setup properly and whether the code works. You will use and reuse the above code many times, and its why so many system have a device on or Device Function light. Keep in mind that unless something is broken, the PIC is doing exactly what you tell it to do.