You are on page 1of 45

Programming PIC16F877A

Using CCS C Compiler and US-

Why Use C for Embedded Programming?

1. Portable – code can be retargeted to

different processors
2. Clarity – C is easier to understand than
3. Compilers produce code that is close to
manually-tweaked assembly language in
both code size and performance
From .c to .hex
C Compilers for Embedded Systems

 CCS C Compiler
 HI TECH Compiler

 The CCS Compiler has an extensive range of

inbuilt functions
→ Save time and speed up the learning process
 The HI TECH Compiler is based towards
those who are following a company policy
specifying the ANSI standard with hardware
functions sometimes resembling Assembler
more than C
Installing CCS C Compiler IDE
 Copy CCS C directory to your hard drive
 Run pcwhupd.exe (in this directory)
 Copy the following files to installed directory
 : PCM.CRG for 16-bit PIC
 Run Patch.exe file
 Run Pcw.exe file
Installing US-Burn
1. Copy USB burn driver directory to your
2. Plug USB cable (connected to the USB port
of SaigonTech PIC Board) to computer USB
3. Turn on the power switch on the PIC board
(see the next slides)
Click the Browse button to select the driver folder

…\USB burn driver

Succefull installation

Do the steps 2 and 3 for other USB ports.

A Sample C Program:
Blinking 8 LEDs
Program Description

 Make LED’s on PORTB blink every second

Cable Wiring
 Use a 8 pin ribbon cable to connect PIC’s
jumpers B0 – B7 to LEDs’ jumpers L0 – L7
Electronic Circuit
Writing Program in PCW C
Compiler IDE
Creating a .c File
Creating a .c File

 File name: Blink8LEDs

 Choose the path
 Click Save
Editing Blink8LEDs.c
Compiling a C Program
Using US-Burn to Load a .hex
File to the PIC
 Put 16F877A in the USB Programmer socket
(pay attention to the PIC direction on the
socket, otherwise you will damage the PIC)
 Plug the USB cable to the PC and to the PIC
 Turn on the power switch of PIC Board
 Run Usburn14.exe
Microcontroller Setting
Run program on the board
 Turn off the power switch on the PIC board
 Put the programmed PIC in the Run socket
on the board (pay attention to the PIC
direction on the socket, otherwise you will
damage the PIC)
 Turn on the power switch on the PIC board to
see the LEDs blink.


Summary of the
Look at an Alternative of BlinkLEDs.c

 Is this listing better than the previous one? Why?

One of the Most Important Skills in

5/6 Programming pins

8 10-bit A/D channels
2 Oscillator Inputs
2 RS-232 inputs
33 I/O ports
2 PWM channels
The PIC16F877A

 PIC: Peripheral Interface Controller

Pin Functions

 MCLR – to reset the PIC

 VDD – power supply
 VSS – ground
 OSC1, OSC2 – connect to oscillator
 The pins having the notation R can all be
used as digital inputs or outputs
 The pins that are labeled as AN can be used
as analog inputs
 The pins CCP1 and CCP2 are used for PWM

1. Delete NOLVP from #fuses and rerun the

program. What is the effect of this action?
2. Instead of using the PORTB, use PORTC to
rewrite the example
 Try to use NOLVP and without it
Why is NOLVP Needed?
 In Single Supply Programming or Low – Voltage
Programming mode (using single voltage VDD 5V
to programming):
 The LVP bit will be set
 Pin B3 will have PGM function → B3 is cleared
 To make RB3 become a normal I/O pin, LVP has
to be disabled
 #fuses NOLVP

 Instead of using the PORTB, use PORTA to

rewrite the example
More Details on PORTA
 In the PIC16 series Port A is the analog port
connected to the analog to digital converters
By default this port is configured as analog
⇒ You will not be able to output or input data reliably
on Port A until you configure it as a digital port
Can’t work
Pin RA4

PIC 16F877A
How to Make RA4 Work Properly
 RA4 is made to be an open-drain output
⇒ A pull-up resistor is needed to make it work
properly VDD

Pull-up Resistor
Can’t work
Pin RA4 Pin RA4 Open-drain output

PIC 16F877A PIC 16F877A


 Programming PIC Microcontrollers in BASIC.
mikroElektronika © 1998 – 2004.
 PICC. An introduction to programming the Microchip
PIC in C. Nigel Gardner. Bluebird Electronics 1998