Getting Started with PIC Microcontroller – CCS C Compiler

Getting Started with PIC Microcontroller – CCS C Compiler

Contents

You are at the right place if you are a beginner to the field of microcontrollers. In this tutorial you will learn How to Blink an LED using PIC Microcontroller. PIC is a family of microcontrollers manufactured by Microchip Technology Inc. PIC stands for Peripheral Interface Controller. It is also referred to as Programmable Interface Controller or Programmable Intelligent Computer.

PIC16F877AAs all other microcontrollers PIC Microcontroller can be programmed using Assembly Language. As it is little bit difficult we prefer High Level Languages. Many high level language compilers are available for programming a PIC Microcontroller like MikroC, MPLAB XC8, Hi-Tech C, CCS C etc. In this tutorial we will use CCS C Compiler. CCS stands for Custom Computer Services, a Microchip PIC Microcontroller Tool Solutions company.

MikroC and CCS C are the best compilers for beginners as they includes a lot of built in libraries which enable us to program a PIC Microcontroller without the deep knowledge of its internal architecture. I think CCS C is the best High Level Language Compiler for PIC Microcontroller as it is almost hardware independent.

For more information and for downloading CCS Compiler please visit their website.


In this tutorial we will learn how to write outputs to an IO pin. In the following section, I am going to explain the basics of PIC Microcontroller Input Output configurations. As we are using CCS C Compiler you may skip it but every PIC programmer should know it. In this experiment we are using PIC 16F877A microcontroller.

PIC 16F877A PIN Diagram
PIC 16F877A PIN Diagram

VDD and VSS are the pins for providing power. For PIC 16F877A, VDD = 5V and VSS = GND (0V). Pin 13 & 14, OSC1 and OSC2 are for connecting oscillator which will provide the necessary clock for the operation of microcontroller. The 1st pin MCLR is the reset pin of PIC Microcontroller, it is an active low input. It should be connected to HIGH (VDD) for normal operations. IO (Input Output) pins in a PIC Microcontroller is divided in to different ports, eg : PORTA, PORTB, PORTC, PORTD etc. Each PORT is associated with two registers, TRIS and PORT which are named as TRISA, PORTA, TRISB, PORTB etc.

TRIS and PORT Registers in PIC Microcontroller

PORT and TRIS Register in PIC Microcontroller
PORT and TRIS Register in PIC Microcontroller

PORT and TRIS are the registers which handle discrete IO operations in PIC Microcontroller. TRIS stands for Tri-state. TRIS register determines the function of an IO pin. Logic 1 at TRIS register makes the corresponding pin Input while Logic 0 at TRIS register makes the corresponding pin Output. PORT register can be used to read input pins or to write status of output pins. For an Output Pin, Logic 1 at PORT register makes the corresponding pin HIGH state (VDD) while Logic 0 at PORT register makes the corresponding pin LOW state (VSS).  Reading PORT register reads the actual voltage levels on IO pins. If the actual voltage level is near to HIGH Level (VDD), corresponding PORT bit will be 1 and if the voltage level is near to LOW Level (VSS), corresponding PORT bit will be 0.  Hope that you can understand its working from the above image.

Prerequisite Knowledge

You should know the basics of C Programming for developing projects in CCS C Compiler. Following are some C concepts that may help you.

  • A number with a prefix ‘0b’ indicates a binary number.
  • A number with a prefix ‘0’ indicates an octal number.
  • A number with a prefix ‘0x’ indicates a hexadecimal number.
  • A number without prefix is a decimal number.

Let’s see some examples…

Decimal Binary Octal Hexadecimal
0 0b00000000 00 0x00
1 0b00000001 01 0x01
128 0b10000000 0200 0x80
255 0b11111111 0377 0xFF

CCS C Discrete IO Functions

get_tris_x()

This function is used to read TRIS register. Examples are given below.

tris_a = get_tris_A(); //Reads TRISA register
tris_b = get_tris_B(); //Reads TRISB register

set_tris_x()

This function is used to write TRIS register. Examples are given below.

set_tris_b(0xFF); //All pins of PORTB as Input
set_tris_c(0x00); //All pins of PORTC as Output

input_x()

This function is used to read data from an IO port, ie it returns the value of a PORT register. This function sets direction of all pins of the specified port as INPUT (TRIS bits 1) before reading the input.  Examples are given below.

a = input_a(); //Reads PORTA
b = input_b(); //Reads PORTB
c = input_c(); //Reads PORTC

output_x()

This function is used to write data to a PORT. This function sets direction of all pins of specified port as OUTPUT (TRIS bits 0) before writing the output. Examples are given below.

output_b(0xFF); //All pins of PORTB HIGH (VDD)
output_c(0x00); //All pins of PORTC LOW (VSS)

input()

This function returns the state of the specified pin. This function sets the direction of the specified pin as INPUT (TRIS bit 1) before reading the input. Examples are given below.

while(!input(PIN_B0)); //Waits for Pin B0 to goes HIGH
if(input(PIN_C0))
  //Pin C0 is HIGH Now

output_bit()

This function writes the specified value (1 or 0) to the specified IO pin. This function sets the direction of the IO pin to OUTPUT before writing the value.

output_bit(PIN_B0, 1); //Makes the pin B0 logic HIGH (VDD)
output_bit(PIN_B7, 0); //Makes the pin B7 logic LOW (VSS)

input_state()

This function reads the state of a pin without changing the direction of that pin as input() does.

b0 = input_state(PIN_B0); //Reads the state of B0 without changing its direction
c0 = input_state(PIN_C0); //Reads the state of C0 without changing its direction

input_change_x()

This function reads the value of the specified port and it compares with the result of last time input_change_x() function was called. It returns an 8 bit or a 16 bit number representing the changes on the port. A 1 corresponds if the value has changed and 0 corresponds if the value has unchanged.

portc_check = input_change_c();

output_float()

This function sets the specified pin to input mode (TRIS bit 1), which is in high impedance state.

output_float(PIN_B3); //Makes pin B3 Input
output_float(PIN_C5); //Makes pin C5 Input

output_drive()

This function sets the specified pin to output mode (TRIS bit 0).

output_drive(PIN_B6); //Makes pin B6 output
output_drive(PIN_C3); //Makes pin C3 output

output_high()

This function sets the specified pin to HIGH state (VDD).

output_high(PIN_A0); //Makes pin A0 HIGH
output_high(PIN_E0); //Makes pin E0 HIGH

output_low()

This function sets the specified pin to LOW state (VSS).

output_low(PIN_A2); //Makes pin A2 LOW
output_low(PIN_E1); //Makes pin E1 LOW

output_toggle()

This function toggles the HIGH/LOW state of the specified pin.

output_toggle(PIN_B1); //Toggles the state of PIN B1
output_toggle(PIN_B2); //Toggles the state of PIN B2

Circuit Diagram – LED Blinking

Blinking LED using PIC Microcontroller - Circuit Diagram
Blinking LED using PIC Microcontroller – Circuit Diagram

CCS C – Starting a New Project

Hope you download CCS C compiler from CCS Website and installed it.

  • Open the CCS C Software
  • File >> New >> Project Wizard
Opening New Project Wizard
Opening New Project Wizard
  • Name the Project
Project Wizard - Save Project As
Project Wizard – Save Project As
  • Select the device and Set Clock frequency
Project Wizard - Select Device and Set Clock Frequency
Project Wizard – Select Device and Set Clock Frequency
  • Click on Create Project
  • The New Project Wizard is completed, now you can see code editor.
  • Enter the Code

CCS C Code – LED Blinking

#include <main.h>

#use delay (clock=8000000)

void main()
{
  while(TRUE)
  {
    output_high(PIN_B0); //LED ON
    delay_ms(1000);      //1 Second Delay
    output_low(PIN_B0);  //LED OFF
    delay_ms(1000);      //1 Second Delay
  }
}
Enter the Code Here - CCS C Editor
Enter the Code Here – CCS C Editor
  • Then click on Compile
  • This will generate, hex file in your project folder. This is the file that to be burned to PIC Microcontroller. You can also simulate its working in Proteus ISIS.

Download Here

You can download the entire project files here.

Share this post

  • I am looking for someone who can write some basic codes as per my requirement in pic 18f series using ccs compiler

  • Sorry, I want to know where you did the design of the circuits, I thought it was Proteus but I don’t know how to do that. Very useful information

  • Hello Ligo,
    i am interfacing LM35 with pic18f4520 but i dont understand how to set analog pin and take its output and show it on LCD.
    please can you help?

  • about ccs c compiler and proteus 8.2.

    I have the following problem when pic18f45k22 stimulate using proteus 8 and ccs c compiler 5.051 and the code are following:

    #include

    #device ICD=TRUE

    #fuses HS,NOLVP,NOWDT

    #use delay (clock=20000000)

    #defi ne GREEN_LED PIN_A5

    void main () {

    while (TRUE) {

    output_low (GREEN_LED);

    delay_ms (1000);

    output_high (GREEN_LED);

    delay_ms (1000);

    }

    }

    The problem shown in fig.
    can any body tell me what the possible solution for it?

  • Thank you very much. It was very useful for a beginner like me. Are you going to continue


  • >