electroSome

Getting Started with PIC Microcontroller – CCS C Compiler

LED

LED

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.

As 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

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

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

CCS C – Starting a New Project

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

Opening New Project Wizard
Project Wizard – Save Project As
Project Wizard – Select Device and Set Clock Frequency

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

Download Here

You can download the entire project files here.

Exit mobile version