Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1.

After installing PIC C compiler, open it and choose Microchip 14 bit from the list as shown in
figure1.
2. From File menu choose New; create a new source file and save it anywhere you need, but
remember that this orphan file will extend to 10 files after compiling, so the best choice is to
put it in a folder.

FIG 1: PIC C start screen. FIG 2: The body of any program. FIG 3: compile
confirmation.
3. Now you can type the body of the program, any program must contain these lines that shown
in figure2. The second line will set the CONFIGURATION WORD in the PIC; this word is
a location in the memory and used for store the general settings of a PIC.
4. From Compile menu choose Compile, or simply press F9, show figure3.

Proteus Simulation program


We will use Proteus for simulating PIC response, it has many variety modeling libraries, and its
powerful concentrates in MCUs and MPUs modeling, along with wide range of supporting chips
such that AVR MCU series, 8051 MCU series, Basic stamp, HC11 MCU series, ARM CPU, Z80,
Motorola 68K CPU, and most PIC's families, also it has a debugger, registers contents viewer and
many other features.
The start screen for Proteus ISIS professional –Blue Icon- is shown in figure1 below.

FIG 1: Proteus start screen.

Figure below shows the design tool bar.

Components Terminals Generator VI Mode Rotating and


Mode mirroring tools *
Mode Mode

1
Tool's name Tool's functions (The most important).
Components Mode Import MCUs, CPUs, Resistors, Sensors, Motors, etc….

Terminals Mode Power (Vcc), Ground (GND).

Generator Mode Sinusoidal, Square, External Audio file, Pulse, etc….

VI Mode Oscilloscope, Virtual terminal, Voltmeter, Ammeter.

* The rotating and mirroring tools are used to change the direction for any component.

Figure2 shows what happens if VI (Virtual Instruments) Mode is pressed (activated).

FIG 2: VI Mode.

Now, the only thing you must do is to choose any instruments from the above list in the figure and
then click anywhere in the model screen (Sheet) for including it in your simulation.
Suppose now that you want to choose PIC16F877A to make a simulation then you must follow the
following steps:

1. Click on Components Mode.


2. After that click on letter P, P stands for Pick, show figure3 below.

FIG 3: Empty component list, click on P to pick a device.


3. A new window will be displayed; it contains all components, with a powerful search engine,
as shown in figure4.5.
4. In the Keyword fields, type 16f877a as a whole word, or simply type 16f87 to list all
PIC16F87xx proteus supported devices in this range, show figure4 in the next page.
5. In Results list, make sure to highlight PIC16F877A, then press OK button. If you want to
pick another component, without pressing OK button; simply double click on PIC16F877A
will be listing it automatically into devices list, refer to figure4.4, and then delete the contents
of the Keyword text box and make your new search again.

2
FIG 4: Pick devices window.

After choosing all components for your design, put the components on the model screen, simply by
choosing any components and clicking anywhere on the sheet, after that you must wire all
components as drawn in your schematic, note that if your simulation contains MCUs, MPUs,
FPGAs… or any programmed device; then you must load the firmware or program into the device,
for MCUs say PIC16F877A; double click on it, or right click on the device and choose Edit
Properties (Ctrl+E); after that load the HEX file from Edit Component window, show figure5
below.

FIG 5: led1.HEX will be loaded into PIC16F877A.

Also from the same window i.e. Edit Component shown above, you must set the used MCU's
frequency clock for example 1 MHz, 4 MHz, 32 KHz, etc…; the crystal is connected by default.

important notes: In PIC16F877A pin_1 (MCLR; Master clear) is used for


reset. It is active low and this is mean that it must connected to 5 volt (V CC, VDD) in
normal mode, and to make a reset for the PIC it must connected to 0 volt (GND,
VSS).
In simulation you have a choice to connect it or not (for PIC16F only), but in reality you
must connect it. In the same way the crystal oscillator (XTAL) on pins 13 and 14, it is
optional in proteus simulation but a must in reality.
Finally, don’t forget to fed the PIC with 5 volt and GND on pin_32 (VDD) and
pin_31 (VSS) respectively. In proteus simulator they are connected by default!

3
Digital I/O (PART ONE)
The first step for mastering any MCU is having a knowledge of how to use pins for digital input and
output, as we say previously PIC16F877A has 33 I/O pins partitioned into 5 ports and each port has a
specific number of pins i.e. A(6), B(8), C(8), D(8) and E(3) as shown in figure below, note that we can
access the port as a whole or simply treating each pin individually.

0 1 2 3 4 5 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2

A B C D E
Layout: Ahmed Ihmeid

Note: the term bit mode will be used when dealing with a single pin either for input or output, for ports
we will use byte mode term; although ports A and E have less than 8 bits I/O.
PIC C compiler comes with a variety functions for dealing with digital I/O for ports and pins, table below
shows the most important functions, all of these functions and more will be handled and explained using
examples.

Function Description
Output_bit( pin, bit ) Output 1(high voltage) or 0(low voltage) to the specified pin.
Input( pin ) Returns 1 or 0 corresponding to current pin status.
Output_X( data ) X is port name i.e. A,B,C,D,E . data is 8 bit (1 Byte).
Input_X( ) Returns 1 byte of data according to each pin state of port X.

In this section many examples and peripherals will be examined:


1. Using Output function: This example shows how to use simple digital output functions in bit
mode, time delay functions, and show many of proteus aspects, each line in this example is
explained in detail.
2. Using I/O functions: Shows more digital I/O functions, more peripherals.
3. Interfacing with 7-segment display: More H/W, more S/W, but friendly more; in overall: good
interfacing and programming practice.
Each example will explain something either for PIC, compiler, or proteus. The code of the first example
explained line by line, so I will step over any similar lines in the next examples either in this section or
other sections.

4
1. Using Output functions.
Example name: Flashing LED.
Main goals: Introduction to PIC C and proteus, using bit mode functions.
DESCRIPTION: A LED (Light Emitting Diode) is connected to PIC16F877A at pin RB7, initially it
will be ON, and after a delay of 0.5 second its state will be changed to OFF, then
after half of second too; it will be toggled to ON and so on……., in other words the
state of the LED will be changed every 0.5 second.

1. After opening proteus ISIS professional, choose components mode from design tool bar then pick
PIC15F877A and an interactive LED, show figure below (proteus explained previously).

Note that to pick an interactive LED, simply type in the Keyword field 'LED-', show figure 3.1:

Figure 3.1: Choosing an interactive LED.

2. Put the components on the model screen (Sheet), and wire them as shown in figure5.2, as we
mentioned in the previous section you will find ground (GND) in the terminals list after clicking on
terminal mode.
3. Now go to PIC C compiler and create a new source file, after that write the following code shown
below, then compile it.

#include <16F877A.h>
#FUSES XT
#USE DELAY(CLOCK = 4000000)
//=========================

void main()
{
set_tris_b(0x7F);
while(1)
{
output_high(pin_b7);
delay_ms(500);
output_low(pin_b7);
delay_ms(500);
}
}

Figure 3.2: Flashing LED code and layout.

4. After a successfully compilation, load the HEX file that generated from compilation process to the
PIC, refer to figure4.6 in the previous section, the HEX file will be stored in the same source file
directory, say flashingLed.HEX, also you must modify the processor frequency clock to 4MHz, refer
to PIC C in page 5.
5. Finally, from Debug menu choose Execute, or press F12 or simply click on play button that shown
below the design tools.

You might also like