Docsity

PIC C Tutorial Using CCS C Compiler - Lab | ECET 209, Lab Reports of Electrical and Electronics Engineering

Toggle sidebar
bg1
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.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download PIC C Tutorial Using CCS C Compiler - Lab | ECET 209 and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity!

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

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.

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 RB 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