Embedded Electronics – Loading Program (PIC)

Last volume of magazine, we talked about constructing a stable voltage supply of 5V for embedded circuit. This volume, we continue with another portion of hardware, setup an interface for loading program (hex code) into microcontroller. We will be using UIC00B and PIC16F887 in the discussion. Other type of programmer and PIC microcontroller should have similar setup.

1.0 Introduction

Microcontroller has become a “must” element in building robot. From the basic line following robot to the complex driverless car, there is at least a microcontroller in it. Some are 8-bit, some uses 16-bit, or 32-bit and even 64-bit processor. It depends much on the complexity and processing speed needed on the robot.

No, we are not going to talk about 16-bit or 32-bit MCU (Microcontroller Unit), this is a discussion for beginner. We will focus on 8-bit PIC MCU only, particularly PIC16F887. Before we continue from the previous 5V supply circuit, I strongly recommend you to get yourself a multimeter with “continuity” check. It comes with a symbol of Diode. With RM30.00 or less you should be able to get a multimeter with that function, even the analog type of multimeter comes with this feature. Well, I will be using a multimeter worth RM25.00 for the rest of tutorial.

2.0 Basic Circuit

Assuming you managed to get the 5V supply that we discussed last volume working correctly and tested. Next, we will insert a PIC microcontroller to the breadboard and connect the power (positive or VDD) and ground (negative of VSS). PIC16F887 will be used as the microcontroller. Basically, the connections are the same because Micrcochip has designed the chip to be pin to pin compatible between 16F and 18F PIC.

If you get yourself a fresh new microcontroller, you will likely need to bend the legs (pins) of the DIP (dual inline package) to get the PIC to “fit” into the breadboard. I am sure you know what I am talking about when you get a new DIP (Dual Inline Package) of IC. Don’t worry, just try it, you will get it at the end! Anyway, please don’t over bend it. Try inserting to breadboard carefully as shown.

Oh, again if you are very very new (no offence) and no one ever told you, there is a really simple way to figure out where pin 1 is on an IC. The manufacturer of anything polarized (tantalum caps, electrolytic caps, LEDs, ICs, etc) will always put some sort of marking on the device to indicate how the device is supposed to be oriented. For ICs, there is a small dot or dimple on one end of the IC. The orange color circle in the picture is showing to this dot/dimple. The left to this dimple is pin 1, and the number increase.

Counting from the dimple, pin 1 is on the left and increases down the left side of the IC. The pin numbers jump to the right side row of pins and count up. Another way is to refer to its datasheet. There will be a pin diagram showing the pin assignment.

PIC16F887 should be in the breadboard, pin 11 and 32 (VDD) should be connected to the 5V rail and pins 12 and 31 (VSS) should be connected to GND on breadboard. If you turn your power circuit on, the PIC16F887 is now running, but it has nothing to run because there is no program inside it.

Actually this is not totally true – the PIC16F887 can start running code if and only if you have configured it correctly. There are 2 things you need to make sure. First, the crystal oscillator, since PIC16F887 offers internal crystal, we can use it which can save the cost and time to get an external crystal, but you need to set the correct configuration bits during programming. Just leave that for later discussion. Second concern is the !MCLR (Master Clear – active low) pin, it need to be pull-up to high voltage, or normally we called it tie to high, simply connect !MCLR pin to VDD (5V) via a resistor, 10K should be good enough. Now, you can add a push button “under” it. What is this? Well, that is exactly the Reset for PIC microcontroller, pressing the push button will reset the PIC microcontroller justlike resetting your computer – everything starts from beginning of program. If you press reset of computer, the computer will reload from DOS again. If you refer to PIC16F887 datasheet you will see the !MCLR label (pin 1) is written with a line above it. This is nomenclature that indicates the !MCLR pin is active low. OK, another term, what is “active low”? The !MCLR pin is an input pin and to activate the function of master clear (Reset) that pin need to be low, the reset function is activated with a low voltage at the input pin – thus active low. So unless you want the PIC MCU to stay in reset state, you’ll need to pull this pin high.

As suggested, adding a momentary switch “under” the !MCLR pin will offer a reset button. A momentary switch is a switch that is activated (or closed) while you’re pressing it and open when you release the button. These are sometime called “tactile switches” because they ‘click’ when you depress them giving the person pressing the button some ‘tactile’ feedback. I normally call them push button as we need to push it.

To test this button, take out your trusty multimeter and set it to the continuity setting. This is the setting on nicer, mid-grade multimeters that is crucial to troubleshooting and experimenting. Touch the probes together – you should hear a buzzer tone indicating that there is continuity or a (nearly) zero resistance path between the probes. Insert the push button into the breadboard and probe the two pins on side of the button. You should not hear the buzzer tone. Now press the button while holding the probes (quite challenging). By pressing the button you will make an electrical connection between these 2 pins – and multimeter will output the buzzer tone indicating there is electrical continuity between two probes.

If you picked 4-pin push button, pin 1/2 or 3/4 is permanently shorted (connected) inside the switch. Probing pin 1 and 2 or 3 and 4, you should hear a tone. If you picked pins 1/3 or 2/4, you won’t hear a noise – again, pressing the button will result in buzzer tone. Playing with the push button a few times will give you good idea on how it works. I am going to use the 2-pins push button for the example.

The schematic shows pin 1 of the reset push button connected to ground and pin 2 connected to !MCLR (pin 1 of PIC MCU) . Now if you have completed this connection on your breadboard, the reset button is ready. The schematic shown above is what we’re going for. The 10K resistor ‘pulls’ the MCLR pin high during normal operating time. By pulling the reset pin high, the PIC16F887 operates normally. When you push the reset button (S1), the !MCLR pin sees a continuous connection to ground. Since the resistance through the depressed switch is nearly zero, it wins (compared to the resistance of the 10K resistor!) and the !MLCR pin is pulled low, Master Clear (RESET) is activated and the PIC16F887 goes into reset. Release the button and the !MLCR pin is pulled high again and the PIC16F887 comes out of reset. Fun!

See the dimple from the PIC16F887 datasheet? Looking at the top of the IC (legs down), with the dimple to the top, pin numbers increase starting from 1 in the top left corner. This is how every IC pin is number. However, the orientation marking varies a bit between manufacturers and between real packaging types. Look for a non-congruent marking like a dimple, small dot, white arrow, a notched corner – anything that makes that area of the chip different from the other parts of the chip probably indicates pin 1. But to be safe, always look atdatasheet before hooking to anything.

Remember, do learn how to use the continuity setting on multimeter. It will be vital to troubleshooting down the road!

3.0 Loading Program

Each microcontroller manufacturer has a different method to get code in the flash memory of the microcontroller. We sometimes call this process download program or loading program, even burning program. Last volume of magazine, we did discuss about bootloader. The preferred method nowadays is via ICSP (In Circuit Serial Programming). ICSP allows you to program the PIC MCU without having to take out the PIC from the project board. This is not trivial! History was much more painful. Though bootloader also offer similar benefit as to ICSP, yet bootloader cannot be used on fresh new PIC MCU. Microchip has designed a relatively straight forward method that requires the control of a few pins (minimum 4 pins). Because of this simple interface, the hardware programmer that is required to connect your computer to this ICSP interface is very straight forward and cheap as well. We recommend UIC00B which offers exactly the function of ICSP to get your PIC burned with the code you developed.

Remember how we identified pin 1 on the IC from the dimple? Connectors also need polarization so that we don’t reverse the orientation of the connector and fry things. Unfortunately the way connectors are numbered is opposite that of ICs. In the picture of the ICSP connector, search for the small arrow indicating pin 1. An IC counts sequentially down one side. Connectors on the other hand, increase pin numbers, back and forth, or I can say reverse zigzag as you count the way down the connector. Pin 3 is VPP, pin 4 and 6 are GND, pin 5 is PGC and pin 7 is PGD.

The programming chain looks something like this:

1. There are plenty of free C compilers such as HI-TECH C PRO for Mid-Range, Mikro-C, CCS and even Microchip have C compiler. That is for C ANSI type of compiler, if you prefer BASIC, there are also a plenty of famous compiler for BASIC.

2. We will focus on using MPLAB IDE and HI-TECH C Compiler as there can be integrated in one window perfectly, also with UIC00B to download program.

3. Please install MPLAB IDE (preferable latest version) and also HI-TECH C Compiler for PIC10/12/16 MCU, both these software installer can bedownloaded from www.microchip.com. Before I forget, you can also get the PICkit 2 Programmer software from the same source, install it too.

4. You can always refer to Cytron’s DIY project documentation for the steps to install MPLAB IDE, HI-TECH C Compiler and method to use it. The main purpose is to enable you to write program in C language and convert it into HEX code (Machine code) and “burn” the hex code into PIC MCU via UIC00B’s ICSP pin.

5. Open PICkit 2 Programmer software, import the hex file.

6. The PIC MCU runs the machine code (*.HEX files) once the UIC00B done with the downloading and PIC is powered.

4.0 Programming Language

Well, if you are a newbie, I am sure you will bump into decision making to choose the programming language. It is normal, all of us gone through that headache.

What’s a C Compiler? In simple language, a C compiler is a program that gets a program written in the C language and outputs to HEX file, though the process is not necessary that way. We prefer to program in C because it is easier for us than assembly and more flexible than BASIC.

What’s a Hex file? This is a file that contains various hexadecimal characters. These hex ‘codes’ represent machine instructions that the PIC16F887 understand. This file is what gets sent down to the programmer (UIC00B), and the programmer loads these machine instructions onto the PIC16F887.

Before we can get too crazy, download and install MPLAB IDE, and HI-TECH C Compiler on the computer that you will be doing your codedevelopment. You may download the MPLAB IDE installer at our website (www.cytron.com.my)under DIY project. You can also download the installation steps from DIY page, with the name of “Installation of MPLAB and Hi-Tech C PRO“. Yes, also the PICkit2 Programmer software, it should come with UIC00B.

Working backwards up this list, I’ll provide you with an example ‘Hello World’ version for PIC microcontroller Hex file that will prove everything is working correctly on your PIC. With any microcontroller board, the first trick is always to get an LED to blink. This is the ‘Hello World’ of embedded systems.

5.0 Loading the Program

With the blink.hex file in hand, you now need to get it into the PIC. You will need to connect the UIC00B connector to the PIC16F887 accordingly. The easiest way to do this is with UIC-A (UIC00B Adapter) plus with 4 jumper wires.

Or you can connect the jumper wires directly from UIC00B 2×5 IDE connector to the breadboard. Yet, this is not very “proper” method as the jumper wires will disconnect from breadboard or IDE connector easily. There are only minimum four connection needed, Vpp (!MCLR – Pin 1), PGD (RB7 – Pin 40), PGC (RB6 – Pin 39) and GND (any GND will do).

Once everything is done, you may connect the UIC00B to USB port of computer, power up the AC to DC adapter. I have also added extra 0.1uF capacitors near to the VDD and VSS of PIC16F887. These decoupling caps are placed to help reduce noise into the IC. You may think you have a straight DC 5V but not really – these 0.1uF caps help reduce ripple on the 5V line. Yes, the program on PIC will probably run without them but they’re good to have installed.

Additionally we need an LED to control. This can be tied to any GPIO pin. I will use RB5 as example in the code.

The resistor/LED order does not matter – just remember (from previous volume of discussion) that is good to have the resistor! The GPIO pin doesn’t actually matter, but Blink.hex will toggle RB5 with the rate of 0.5 second (approximately). You’re getting closer! Time to program the PIC!

If you want to make thing faster, just open the PICkit2 Programmer which come with the UIC00B, import the Blink.hex file (which you need to download from www.robothead2toe.com.my, vol.8), write it to the PIC. Once the programming is successful, the LED should blink continuously. If you like to learn more, you can download the file from DIY page again “MPLAB Open Project_16F”. Follow the steps to create project and compile the C file.

6.0 Typical Problems

If you still are not able to program the PIC and this is where 99% of first time user end up, don’t give up, start digging in and troubleshoot.

1. Are the ICSP connections from 2×5 IDE connector correct? It’s easy to get the ICSP connector backwards. Take a look at the photos again.

2. Are there any loose wires? Pull out the multimeter and check that you’ve got 5V being delivered to the VDD, and GND pins on the PIC16F887.

3. Do the wires go into the ICSP connector have a solid connection?

4. Do you have your PIC connected to both power and ground? And is it being power up when you load program?

5. Is the 7805 supply outputting 5V?

There is a multitude of things to check. It’s hard! I know. But once you get things correctly set up, and that LED blinks – it will feel fantastic!

Ok – I’m going to assume that you got the code correctly loaded onto the PIC16F887 and that the LED is blinking.

Congratulations! You are now well on your way to a whole world of pain! Once you get one thing working, it’s hard to stop! GPS, datalogging, RF, PCB layout – it’s all just a couple hops away. We will add an extra push button next volume as digital input. RH2T

References:

Original article by Sparkfun Electronics: http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=57,