From MCIS Wiki
Jump to: navigation, search

Programming the PIC16F877A Micro-Controller



The PIC16F877A Development Kit includes:

  • PIC16F877A Prototyping Board (Pictured) has:
    • PIC16F877A
    • One Potentiometer
    • One Pushbutton
    • Three LEDs
    • RS-232 Level Converter
    • ICD Jack
  • PCW Debugging/Programming Software
  • Breadboard
  • Parts box with:
    • 93LC56 serial EEPROM chip
    • Jumpers
    • DS1631 digital thermometer chip
    • NJU6355 real-time clock IC with attached 32.768kHz crystal
    • Two digit 7-segment LED module
    • Two 1K Ohm resistors
  • Exercise Booklet
  • DC Adapter (9VDC) and Cables
  • Carrying Case

Basic Applications

The first program just makes the green LED on the circuit board blink.

#include <16f877A.h>
#device ICD=True
#use delay (clock=20000000)
#define GREEN_LED PIN_A5

void main () {
   while (true) {
      output_low (GREEN_LED);
      delay_ms (1000);
      output_high (GREEN_LED);
      delay_ms (1000);

The first four lines are to tell the included PCW compiler what microchip I'm working with and how I've connected it to the computer.

The next line defines a constant variable named GREEN_LED that is equal to PIN_A5. A5 is the pin on the MCU that is connected to the green LED. This adds human readability to the program for turning on and off the LED.

in the main function there's a never ending loop that runs until the program is stopped.

output_low(GREEN_LED) sets the A5 pin (defined as GREEN_LED above) to low (or on).

output_high(GREEN_LED) sets the A5 pin (defined as GREEN_LED above) to high (or off).

delay_ms (1000) halts the program for 1000 milliseconds.

The above program created the following machine code:

008     PSP_DATA
015-016 CCP_1
015     CCP_1_LOW
016     CCP_1_HIGH
01B     CCP_2_LOW
01B-01C CCP_2
01C     CCP_2_HIGH
021     main.@SCRATCH1
022     @delay_ms1.P1
077     @SCRATCH
078     @SCRATCH
078     _RETURN_
079     @SCRATCH
07A     @SCRATCH
07B     @SCRATCH
09C.6   C1OUT
09C.7   C2OUT


Controlling a Video Camera With the PIC16F877A

The Goal

Low budget video and film makers have a hard time with getting full use of their art due to budgetary constraints. Cranes are quite expensive. The answer for the low budget film maker is to mount the camera on a pole and make the shot off of a monitor. The problem is that it's hard to control a camera when it's ten feet above the film maker's head. It's possible to use the LANC protocol to control the camera with a home made controller using the PIC16F877A MCU.

The Theory

The essential idea behind the MCU controlling the camera is that Sony developed the Control-L LANC protocol. It's a protocol that uses 3 pins for 2-way communication on a 9600 baud.

RS-232, which the MCU can communicate on, also uses 3 pins for 2 way communication on a 9600 baud.

The theory is that the protocols are similar enough that an RS-232 command from the MCU, can be received as a LANC command by the camera. not only details the LANC protocol but also suggests that interaction with a RS-232 device is possible.

The Functionality

The PIC16F877A has 33 pins that can be used for input and output. A remote control for a car can be connected to these I/O pins to allow the MCU to translate those commands to RS-232 commands to send to the camera.

Networking Multiple PICs

The Goal

write a program that requires the PICs to communicate to one another. Because the prototype boards have Red, yellow, and Green lights, a traffic light program would work well.

The Theory

Because the PIC can communicate via RS232 they can be plugged into each other and communicate to one another.

The Functionality

The PIC currently displaying the green light would be in command, timing when it's time to change and passing control to the other PIC. The buttons on the board could be used as crossing signals, this would introduce interrupts.

Controlling other Electronics

The Goal

connect the PIC to Other electronics via SSRs.

The Theory

An SSR(Solid State Relay) acts as a switch. It can turn on a 110V device when it receives a 5V signal. The PIC's I/O pins are 5V+. This allows a program to turn on and off a device through the SSR

The Functionality

The kit came with a digital thermometer that can be used to determine a temperature on the PIC. A simple program would be a climate control device. If the temperature is above a programmed temperature turn on the SSR circuit connected to a fan, if it's below the programmed temperature turn on the SSR circuit connected to a heater (or blow dryer for the sake of the example). Using the included 2 digit 7-segment LEDs the programmed temperature could be displayed and added up and down buttons would allow us to change the programmed temperature.


I had nothing but trouble with the devices. I would get 2 steps into my project and something would break. I spent many hours, weeks and months with tech support, shipping parts back and forth. I'd get it working again and get 2 steps into the project just to once again have more problems. I really enjoyed working on this project and wish I could have written a useful program, and created a cool functioning device.