Vous êtes sur la page 1sur 9

Purdue University Calumet

Department of Electrical and Computer Engineering Technology

PIC C Tutorial using “CCS” C Compiler


Lab Objective:

Create a step by step tutorial explaining the programming a 16F88 PIC


Microprocessor using MPLAB v7.61 and PIC C Compiler.

Procedure

Setup

1. Open MPLAB program by selecting (double click) Program folder on


desktop. When folder opens select (double click) MPLAB icon.
2. When program loads select (click) Project from the menu bar. From the
Project dropdown menu select Project Wizard. A window will appear that
looks like the figure below:

3. Click on the Next button to continue. This will start a step by step procedure to
help you set up for your program.
4. The next screen that comes up is used to select the chip that will be used.
Microchip has a number of chips available, but for this tutorial we will be using
the PIC Microchip 16F88. In the middle of the screen is a drop down menu
with a list of chips, select 16F88 and press Next.

2
5. The next step is to select your language tool set. We will be programming with
the PIC C software that is already installed on the computers.

The default Active ToolSuite is the Microchip MPASM Toolsuite. In the box under
the Toolsuite contents there are 3 different compilers. If a compiler has a red X
next to it, it does not have a good link and you will have to tell the computer where
to find the program.

Now Change the Active Toolsuite to CCS C Compiler for PIC 12/14/16/18, by
using the drop down menu. As you can see there is a red X next to the compiler
that we would like to use. You can type in the location that is listed above or select
the Browse button and find the link listed above.

The lab computers refresh every time that a student signs on; therefore you will
have to link the compiler every time that you do a program.

3
6. The next step in the set up is creating a project name and selecting a directory
for your project. Under project name give your project a name. This simple
tutorial will be turning an LED on and off, so I named mine LED and placed it
in the directory LED. When you are satisfied with your project name and
destination folder press Next.

4
7. The Final step in your set up is to add existing files to your project. If you had a
program already created you would add them here, as well as any other files you
need. For this tutorial we will add our files later so just hit next.

5
8. The final window that appears will be a Summary of the information that you
have entered for your project. Ensure that the device, Toolsuite and File are
correct for your project. If they are correct press Finish, otherwise press back
and correct the errors.

6
Writing Program

Now it is time to create a file to put into our project. This can be done one of three
ways:
• pressing the New File icon
• selecting File from the menu bar then selecting New File
• press Control and N together.

When a new file is created a fresh window will appear that is labeled Untitled.
When you save the program it will change to the location of the file.

Type the following code into this window and save in the directory that you created
for your program.

#include <16f88.h> //Header file for chip being used

//main body of program


void main (void)
{

/******************************************************************
* //sets portb of chip to 00000001 (RB0 is input all else output) *
******************************************************************/
set_tris_b(0x01);

//creates endless loop could also have been done with while (1)
start:

if (!input(pin_B0)) //is RB0 false / 0 / low


{output_bit(pin_B1,0);} //make RB1 false / 0 / low

if (input(pin_B0)) //is RB0 true / 1/ high


{output_bit(pin_B1,1);} //make RB1 true / 1 / high

goto start; //repeat loop at start


}

After we save this file we need to add it to the project. This program could have
been created with any word processing file and saved with a .c extention. If this is
done then we just have to add it to the program. Under Project in the menu tool bar
select Add files to Project. Select the .c file that you have created and it will be
added to your project. You will see that it is loaded under the Source Files in your
project window. If you have Header files, Library files, or any other file this is how
you would add them. This project does not require any other files so none will be
entered.

7
Selecting Debugger
The next thing that we would like to do is simulate our project. Select Debugger
from the tool bar. Select he first option, which is Select Tool, then select MPLAB
SIM. You will notice that a new tool bar appears. This toolbar will allow us to run,
pause, animate, step through and reset a program.

Compiling
We are now ready to compile our program. To compile we select Project from the
toolbar then select Compile or you can press F10. If there are errors in your
program that require attention they will appear in the OUTPUT screen. To view
the location of the error in your program, double click on the line displaying the
error and it will take you to the spot in the program that contains the error. When
your program compiles properly the OUTPUT screen will display:

BUILD SUCCEEDED: date completed

Simulation
To check if your program acts as it should, we need to simulate it. BE AWARE
the speed at which the computer simulates your program and the actual speed the
chip will run will be very different.

First lets find our output so we can view what is happening. Select Special
Function Register from under View. We need to look at PORT B, bits 0 and 1.
Bit 0 is our input and Bit 1 is our output. When Bit 0 is high then Bit 1 will go
high. To make our program act as if it were in the real world we need to have a
way to simulate an input. To create this stimulus we select Stimulus Controller –
New Scenario from under Debugger. Click on the blue square and a drop down
list will appear. Select RB0 from this list. Click the white square next to RB0
under Action and select Toggle. You should see an arrow appear under Fire. When
your program is animated and this arrow is pressed it will simulate an action on
RB0.

When you hit the animate button you will see a green arrow go through the program
pointing at the instruction it is reading. Look at the SFR window, the Port B
specifically. It is all zeros. If you press the fire button to toggle RB0 to high, you
see the next program scan Port B is 00000001, indicating that the input RB0 has is
high. In the program the first if statement goes false and the second becomes true,
causing Port B to become 00000011, indicating that RB1 (output) is now high (light
on). Each time that you toggle RB0, RB1 will change to match. You can also step
though the program, one instruction at a time. Each time you press the Step into
button the scan will move to the next instruction. When you press the Fire button to
toggle RB0 no action will take place until the next instruction is called.

Loading Program into PIC chip

8
The next step will be to load the program onto the chip. This is accomplished with
an Epic programmer that can be checked out. The programmer takes
communication from the LPT port on the computer. It is powered by a 16 Volt
power supply or 2-9Volt batteries.

After the programmer is plugged into the computer we need to start the EPICWin
software, which is located in the programming folder. When the program is started
it will look as follows.

Ensure that your chip is the one showing. Next we need to ensure there is no
program installed on our chip. Select Run then Erase. This should erase all data
from our chip. To verify that the chip has been erased select Run then Blank
Check. Each time that the programmer is working the red LED will illuminate.

The next step is to load the hex file into the program for loading, by selecting File
then Open. When the dialog box comes up find the hex file created from your
program. Before loading your program onto the chip you need to verify the
configuration. Under Configuration you need to select the type of Oscillator that
you are using to clock your chip, as well as any other setup used in your program.
When all your configurations are correct and you are ready to program your chip,
select Run then Program. When the red LED on the programmer turns back off,
the program is loaded into your chip and ready to use. Place the chip into your
circuit and test your program.

Vous aimerez peut-être aussi