PIC Tutorial


 What Is a Microcontroller?

A microcontroller is an inexpensive single-chip computer. Single-chip computer means that the entire computer system lies within the confines of the integrated circuit chip. The microcontroller on the encapsulated sliver of silicon has features similar to those of our standard personal computer.

Primarily, the microcontroller is capable of storing and running a program (its most important feature). The microcontroller contains a CPU (central processing unit), RAM (random-access memory), ROM (read only memory),

I/O (input/output) lines, serial ports, timers, and sometimes other built-in peripherals such as AD (analog-to-digital) converters.

PIC16F84A JE0021



Why Use a Microcontroller?

Microcontrollers, as stated, are inexpensive computers. The microcontroller’s ability to store and run unique programs makes it extremely versatile. For instance, one can program a microcontroller to make decisions (perform functions) based on predetermined situations (I/O-line logic) and selections.

The microcontroller’s ability to perform math and logic functions allows it to mimic sophisticated logic and electronic circuits.

Microcontrollers are responsible for the “intelligence” in most smart devices on the consumer market.

Look in any hobbyist electronics magazine from this country or any other. You will see articles that feature the use of microcontrollers, either directly or embedded in the circuit’s design. Because of their versatility, microcontrollers add a lot of power, control, and options at little cost. It therefore becomes essential that the electronics engineer or hobbyist learn to program these microcontrollers to maintain a level of competence and to gain the advantages microcontrollers provide in his or her own circuit designs.

If you examine consumer electronics, you will find microcontrollers embedded in just about everything. This is another reason to become familiar with microcontrollers.


Microcontroller Systems

A microcontroller is a single-chip computer. Micro suggests that the device is small, and controller suggests that it is used in control applications. Another term for microcontroller is embedded controller, since most of the microcontrollers are built into (or embedded in) the devices they control.

A microprocessor differs from a microcontroller in a number of ways. The main distinction is that a microprocessor requires several other components for its operation, such as program memory and data memory, input-output devices, and an external clock circuit. A microcontroller, on the other hand, has all the support chips incorporated inside its single chip. All microcontrollers operate on a set of instructions (or the user program) stored in their memory. A microcontroller fetches the instructions from its program memory one by one, decodes these instructions, and then carries out the required operations.

Microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast, it has several disadvantages. An assembly program consists of mnemonics, which makes learning and maintaining a program written using the assembly language difficult. Also, microcontrollers manufactured by different firms have different assembly languages, so the user must learn a new language with every new microcontroller he or she uses.

Microcontrollers can also be programmed using a high-level language, such as BASIC, PASCAL, or C. High-level languages are much easier to learn than assembly languages. They also facilitate the development of large and complex programs.

Basically, a microcomputer executes a user program which is loaded in its program memory. Under the control of this program, data is received from external devices (inputs), manipulated, and then sent to external devices (outputs). For example, in a microcontroller-based room temperature control system the microcomputer reads the temperature using a temperature sensor and then operates  a fan to keep the room temperature at the required value.

In this tutorial we will be using C programming Language. The MikroC Pro for PIC compiler will be used. We will also use our PIC development kit with ISP (In-circuit Serial Programmer).

pik kit with isp JE0002

PIC development kit with on-board programmer

You can download the trial version at http://www.mikroe.com/mikroc/pic/. Install the software and create a new project as shown below:

Creating new Project

The easiest way to create a project is by means of the New Project Wizard, drop-down menu:  Project > New Project

Start creating your New project, by clicking Next button:

New Pro

Step One – Select the device from the device drop-down list :

select device

Step Two – Enter the oscillator frequency value :

Enter osc

Step Three – Specify the location where your project will be saved :

spec loc

Step Four – Add project file to the project if they are avaiable at this point. You can always add project files later using Project Manager but if their is no file to add click Next:add file

Step Five – Select Include none in the Library Manager :

Selec lib

Step Six – Click Finish button to create your New Project :

click finish

When you click “Finish” the code editor (where you type your code) will open as in figure below:

code template

You can now start your coding.


1. Flashing LEDs

 Type the following Code to blink some LEDs connected on portb of PIC16F887 in our training kit.

ANSEL = 0;                     //Configure  all AN pins as digital I/O 
ANSELH = 0;               
void main() {
TRISB = 0x00;                  // set portb to be output
do {
      PORTB = 0x00;            // Turn OFF LEDs on PORTB
      Delay_ms(1000);          // 1 second delay
      PORTB = 0xFF;            // Turn ON LEDs on PORTB
      Delay_ms(1000);          // 1 second delay
   }  while(1);                // Endless loop

Simulating with MPLAB

Type the following code inside the code editor.

unsigned int count;                  // declare a variable count as an unsigned integer
void main() {
ANSEL = 0;                           // Configure all AN pins as digital I/O
ANSELH = 0; 
C1ON_bit = 0;                        // Disable comparators
C2ON_bit = 0;

TRISA = 0;                           // PORTA is input
TRISB = 0;                           // PORTB is output
TRISC = 0;                           // PORTC is output
PORTB = 0;
do {
      PORTB = count;                 // send the value of count to portb
      count++;                       // increase count by 1
      if (count >= 12)count = 0;     // if count is greater than or equal to 12, reset 
                                     // its value to zero to start over again
      while(1);                      // endless loop

1. Open Tools >> Options >> Output Option, and check the “Generate COFF file” option, and click the OK button.
2. After that, compile the project by clicking the build icon or pressing Ctrl + F9.
3. Next, open MPLAB IDE, and select the appropriate device (P16F887) by choosing Configure >> Select Device… :

select dev

4. After device selection, click on the File >> Import. Open file dialog box should appear. Then, go to the project folder and open the generated COFF file, LED.cof :

5. Then, select the MPLAB SIM from the Debugger >> Select Tool menu for software debugging :

sel tool

6. Click on View >> Watch to open the watch window. Click on the drop down list on left-side, select special function registers TRISA, TRISB  and PORTB and click Add SFR to add them.

add sfr

7. Click the drop down list on right-side, select the variable count and click Add symbol to add it.

add symbol

8. Now,you can start debugging the code by clicking Step into button on the Debug toolbar, or by pressing F8 : You can then watch the values of the added registers and variable count in the watch window as you step into the code.


Leave a Reply