PIC C Programming

In this article I’ll show you how to program Microchip PIC microcontrollers using HI-TECH PICC compiler and basic PIC development board. The board consists of PIC16F876A, LED bar graph and 20MHz oscillator. PICKIT3 compatible programmer was used to download the program into PIC’s memory. Till now all published project’s source code was written in Assembly language. This is a first simple design based on C. I’ve used MPLAB 8.6 IDE and included HI-TECH PICC Lite version of C compiler. Currently Microchip provides more advanced development environment – MPLABX, however for this project 8.6 version should be sufficient. In this tutorial we’ll see how to output a binary count to one of PIC’s ports – PORTB.

PIC C Programming Prototype Board First of all you should follow Project Wizard instruction and create a new project selecting HI-TECH PICC Lite as your tool suite .Create a new file and paste the source code provided below. Save the file with c extension. Attach the file to the project and finally build the project. Make sure that included header files are present in the project library. Here is the source code:
//—————————————————————————————-
#include <pic.h>
__CONFIG(HS & UNPROTECT); // Setup the configuration word
#define _XTAL_FREQ 2000000  // Define oscillator as 20Mhz
void main(void)
{
TRISB=0x00// All 8 bits of POTRB will be used as outputs
for(;;)
{
PORTB = PORTB++; //Increase PORTB count
__delay_ms(1000);
}
}
//—————————————————————————————–
Design schematic and PICKIT3 programmer connections are shown in the next figure. Make sure to connect PGC and PGD lines correctly otherwise your microcontroller will not be recognized by MPLAB. Header and C source files can be downloaded here.

PIC C Programming Schematic