Setting Up MPLAB Version 5.5 to use the Hi-Tech C Compiler

This tutorial note describes how to set up MPLAB so that the Hi-Tech C Compiler tools will work within the MPLAB IDE. You will also be able to use the MPLAB Simulator with your C source code.

To start with, you need to have the Hi-Tech software installed on your Computer.


You can get a “Demo” copy of the Hi-Tech C Compiler from Hi-Tech. This is freely available and is targeted towards the “Hobbyist” lobby. You can access Hi-Tech Home page at The screenshot below shows what their home page looks like.

The Compiler you want is the Hi-Tech C Lite version.

When you install the Hi-Tech compilers for the PIC 16 series they will be installed into a folder in the root directory called PICCLITE.

It is better to have installed MPLAB version 5.5 before installing the Hi-Tech C compiler tools. Version 5.5 has the support for the Hi-Tech Lite version.

Once you have the Hi-Tech C compiler tools installed you could, if you want, run the compiler IDE from a DOS (or Virtual 86) window. The screen shot below shows what it looks like.


As you can see, it's quite a nice IDE even it is a DOS program. The problem is there is no simulation so it’s a bit limiting. That’s where the MPLAB come in.

We can attach the H0-Tech C Compiler to MPLAB. Here is how.

Fire up MPLAB and Enter Projects -> install language tool.


The above window opens. Set it up as shown in order to access the Hi-Tech Language Suite, Tool Name, and finally the path to the executable.

Now open a C source file example from the Hi-Tech samples folder.


And you will see an ADC program. This is not really a program, just a C function. You need to change it into a C program with a main function.

Modify the program so it looks like the one shown below.

#include<pic.h>

#include"adc.h"

#define_16F877

unsigned char channel;

void main(void)

{

do{

ADCON0 = (channel < 3) + 0xC1;// enable ADC, RC osc.

ADGO = 1;

while(ADGO)

continue;// wait for conversion complete

if (ADRESH < 0x1f)

PORTB = 0x01;

else

PORTB = 0x02;

}while(1);

}

Save it as adcdemo.c (leaving the original one intact.)

Now in MPLAB go to Projects -> Edit Project and click on the hex file


When you do this you should see the following window.


This as you can see is now set up for Hi-Tech C compiler. Tick the items shown in this window and click on OK to return to the Edit Project window.

Click the Add Node button and in the following window


Click on the program name (all right I forgot to call it adcdemo.c!). Click on OK and you will be back in the Projects window.


Here you can see that the C source file has been added to the project. Click on OK.

Now, back in MPLAB, click on Options drop down menu and select environmental set-up.


Select Projects tab and fill in the text areas as illustrated below.


Click on Apply, then OK.

Now you are ready to build the .hex file.

Go to Projects, click on Build All. The following should be seen.


Showing that you have successfully compiled linked the program. Note that there will be some activity before this window appears, as the Hi-Tech C compiler compiles your code. The Hi-Tech C compiler is a DOS program so you will see a "black" DOS screen appear then go, after compilation. At the end of it all, you see the above screen.

As you would with MPLAB, Open up the windows as shown in the screen shot below.


You should be able to single step through the C source code in the same way you could with an assembly program.

If you double click on a name in the Special function register window you will see the Modify box. Here you can modify the value of a register, in this case it is the ADRESH register (to alter the program flow).

Another way to control your program is to use the Asynchronous Stimulus Dialog shown below.


Click on help button to find out how to use this feature.

Another Example - Using the PIC16F84.

Here is another example using the 16F84 PIC device.

The program code is shown below.

/* TEST PROGRAM USING MPLAB WITH HI-TECH C COMPILER */

#include <pic.h>

#define _16f84

unsigned char inbyte;

void main(void) {

STATUS |=0x20;

TRISA = 0x1f; // all input

TRISB = 0x00; // all output.

STATUS &=0x0df;

while(1) {

do{

input:

inbyte = PORTA; //get switches

inbyte &= 0x01; // mask all but bit 0.

}while (inbyte != 0x01);

PORTB = 0x01;

inbyte = PORTA;

inbyte &= 0x02;

if (inbyte == 0x02)

PORTB = 0x02;

else

PORTB = 0x01;

}// end while.

}//end of main

  • Create a new file with the MPLAB editor, save it exp84.c.
  • Create a new project (in the usual way) and set it up with EDIT PROJECT, call it exp84pj. Add the .c source file to this (in the usual way).
  • Use BUILD ALL to create a working .hex file.
  • Now create a stimulus file called ra.sti (as illustrated below).

CYCLE RA1 RA0

35 0 0

48 1 0

64 0 1

77 1 1

93 1 1

Note that there must NOT be a gap line between the title and first set of stimulus values (the stimulus file will not work if there is a gap line here).

Add it to your program with DEBUG -> STIMULUS -> PIN STIMULUS -> ENABLE, and select the file ra.sti.

To use the stimulus file you will need to run through the program a number of times. You should see the RA port values change as you step through the C source code.

How did I arrive at the STEP values in the ra.sti file?

By stepping thought the program with the STOPWATCH and noting the execution time value each time I stepped onto a port input statement. These are the values used in the stimulus file.

Finally..

The screen dump below illustrates the program after it has been executing for 78 us.

You can see that the RA port is reading 01h from the stimulus file (we have stepped past the 64us point).

scr

This tutorial has introduced you to the idea of using the Hi-Tech C compiler with MPLAB. The Hi-Tech C compiler can be used to develop C programs for the PIC, using the familiar MPLAB development environment. The "Lite" version of the Hi-Tech C compiler tools has limitations (see help file in the Hi-Tech DOS version for details - below). It should however be powerful enough to develop the kind of program we are working on here in the module.

The following details have been re-produced from the “readme” file attached to the Hi-Tech C compiler files.

"HI-TECH Software has provided this freeware version of the PICC Compiler,

as a means for hobbiests and students to be introduced to our

PIC Compiler.

This new version of the HI-TECH PICC Lite C compiler has been updated

and now supports the 16F877 and 16F877A in addition to the 12F627,

12F629, 16F627, 16C84, 16F84, and 16F84A.

Please take note, that the command line driver is called PICL.EXE and the

compiler comes with no printf() support for longs or floats. There is no

source code supplied for any library routines. Only two RAM banks of the

16F627, 16F877 and 16F877A processors are supported. There is also a

limitation on ROM size to 2k on the 16F877 and 16F877A."

Programming the PIC Micro: When you have compiled and simulated your program you are ready to download it to the PIC Micro board. To do this you need to evoke the PPP Programming software. This is contained in the Program Files -> Matrix Multimedia -> Common -> PPP folder (as installed). It is a good idea to create a short cut to the desktop so it is handy to reach. Once you evoke it for a session, leave it on the task bar (as with MPLAB) so both programs are easy to reach.

The screen below shows what it looks like.


As you can see in this example screen shot, the .hex file is located in the same folder as the source code file (in this case PICCLITE\samples\84app\app84.hex.

The PPP program automatically looks for .hex files when you try to open a file, ignoring all others.

The middle speed button opens the configuration window.


This should be set up to look like the one shown above. Click OK to confirm.

The main PPP window should re-appear. Clock on the right hand speed button to start programming. The PPP program will "program", then read back, the PIC content. This is to confirm that the device has been programmed correctly.

This concludes the tutorial on how to install and use the hi-Tech PIC C compiler with MPLAB.

©Dr Peter D Minns

PDM2003.