New Light Weight Champion – PIC16F887

By Ober Choo, RH2T Magazine, Vol.7, Jan 11

I am sure if you are a supporter of PIC microcontroller, you would be sure to know the famous PIC16F877A. Yet, time flies and now new PIC model that aims to replace 877A has emerged, PIC16F887.



1.0 PIC16F877A


Yes, PIC16F877A is very famous since I started with PIC microcontroller back in year 2002. Everyone uses either PIC16F84A or PIC16F877A. Let’s take a look on the specification and features of PIC16F877A:

· Maximum Operating Speed: 20MHz

· Operating Voltage: 4.0 – 5.5V

· Flash Memory: 8192 words

· Data SRAM: 368 bytes

· Data EEPROM: 256 bytes

· ICSP via 2 pins

· I/O pins: 33

· ADC Channels: 8

· Timers: 2 x 8-bit, 1 x 16-bit

· PWM: 2 channels

· UART, SPI and I2C

With the above features and its relatively low cost, PIC16F877A has emerged as the light weight champion in 2000s. Personally I have been using PIC16F877A for many projects, from LED blinking to mobile robot. It serves me well and there is nothing much I can complain about. Yet, as an engineer we always demand for better solution, and I am pretty sure those guys at Microchip know about it. In 2008, Microchip introduced the PIC16F887, and suggested it as a substitution of the older model, PIC16F877A.

 2.0 PIC16F887 vs PIC16F877A

PIC16F887 has the same pin diagram as PIC16F877A. Therefore, you can replace the PIC16F877A on your old project with PIC16F887 without any problem. What is so special about this new model of 8-bit PIC microcontroller? Let’s compare it side by side.



As the table shows, memory wise, both 887A and 887 are the same, whereby both have 8K words of program memory and 368 bytes of data memory. What attracted me to 887 is the internal crystal that offers speed from 32KHz to 8MHz. This internal crystal can be tuned through software register and it offers switchable operating frequency between internal and external crystal during program runtime. With this option, we can save the money needed for external crystal, avoid breakdown when the external crystal stop working and you have two extra I/O pin that was initially used for external crystal. The two extra pins are RA6 and RA7 which is bidirectional I/O pin, great! If you compare with PIC16F877A pin diagram you will not get these two pins.



What’s next? The ADC of course! PIC16F887 has increased the analog input to 14 channels, and every pin can be configured to analog or digital independently. As you might have notice, analog input is getting more important because more sensors come with analog output as it is the easiest method to deliver wide range of information by just single wire interface. These sensors include infrared distance sensor, Ultrasonic range finder, Accelerometer, Gyro, Gas sensor, LDR, and many more. With extra analog input, you are able to integrate more analog sensor into your design. Despite the increased number of analog input, there is another good feature in this model for those who do not need the analog input. In a scenario where some of you experience failure after you connect analog sensor to, let’s say, RA0 and RA5, digital input to RA2; and when you want to develop the program you notice that to make RA0 and RA5 become analog input, RA2 will become analog input too. Once the pin is analog, requesting digital logic will result in wrong info. However, you are still able to use it, provided that the program changes the analog pin to digital pin every time before it reads digital input pin, and same step apply when the program wants to read analog input pin. Isn’t it troublesome? Many would agree with me. Worry no more with PIC16F887 as the selection of analog input or digital input pin are truly independent of each other. PIC16F887 uses ANSEL and ANSELH register to configure 14 channels of AN pin to analog input or digital pin. ANSEL controls eight pins of PORTA+PORTE while ANSELH controls six more pins at PORTB.

OK, what else can we get? If you have noticed, there is one I/O pin left out from previous discussion. This pin is RE3. PIC16F877A offers 33 I/O pin while PIF16F887 provides 36 I/O. We know two extra I/O are from the crystal pins, where is another? It is actually the MCLR pin which we normally use it for Reset purpose. For PIC16F887, user may have the choice to tie MCLR internally to VDD and use pin 1 as RE3. However, this pin can only be used as digital input.

I think that those facts mentioned are sufficient for me to replace my PIC with this new model, but there is another important factor for me to consider before migrating to it. Everyone would agree with me, of course that is the price. PIC16F887 is much cheaper than PIC16F877A. You should be able to get a PIC16F887 with only half the price of PIC16F877A. This is the main factor, more features yet cheaper price; with this, we definitely have no reason to resist it.

3.0 Getting Started

Since we have decided to use the new light weight champion, let get started. I will utilize the SK40C from Cytron Technologies that I have been using to develop project as the Starter Kit. So what should we do? Just start with simple achievement and move on. With SK40C, setting up the hardware will only take you five seconds. Here you go; the PIC16F887 is sitting on SK40C with the 2×16 LCD and 20MHz crystal.


Since the PIC16F887 is brand new, there should not be any output (LEDs) or message on LCD when you power it up. Well, we need to modify the sample source code given for PIC16F877A and load into it. Let’s get started.


 Here you go, the sample source code from Cytron Technologies for SK40C using PIC16F877A. You can download it from SK40C product page. Since we are using different model, we will need to configure the compiler to compile for it. Simplest way to configure is to create a new C project for it.



Don’t forget to extract it as it comes in zipped files. Normally I will put all the needed files in a folder.



 Yes, simplest method to get the source code ready for PIC16F887 is to create a C project and modify the C file. Let’s get started. Open the MPLAB IDE. Go toProject -> Project Wizard …. You will see a welcoming window, click Next. Step One is to choose the PIC microcontroller model.



The next step is to choose language toolsuite, Cytron uses HI-TECH C compiler for sample source code development. Therefore you will need to get the compiler installed earlier. The compiler comes with MPLAB IDE installer. You will need to choose Hi-Tech Universal ToolSuite and click next.

The rest of the steps are standard and you should able to complete it. Finally you have a C project as shown in figure. Add the “led blinking.c” file into Source Files. Click on the “led blinking.c” and you will see the codes.



In the “led blinking.c” file, you will need to do some small modification in order for it to be executable on PIC16F887. First, it is the configuration bit. Replace the original configuration settings with new configurations settings.



Next modification needed is for ADC configuration. PIC16F877A uses ADCON1 to configure digital and analog pin. PIC16F887 uses ANSEL and ANSELH for this purpose.



OK, we are done and ready to compile it. Since SK40C come with 20 MHz crystal, let’s utilize it for now, I will show you how to waive it later, maybe in next volume. Compile and load the hex code into PIC16F887 on SK40C via UIC00B programmer. It is quite straight forward as the User’s Manual of SK40C shows the detail step.





4. 0 Conclusion

If you have been following closely to the steps mentioned, everything should be working well. You should get message on LCD and the LEDs should blink alternately. I have made some changes on the LCD message by adding the “PIC16F887” at the back of “SK40C”, you are free to modify too. This proves the program is running. Do send us some of your project photo using PIC16F887, it will motivate us. You can start using the PIC16F887 for your project, just modify the main program, create your own functions, add in library, etc. Have fun with it. I think what I’ve shared here is good enough for this volume of ROBOT. Head to Toe magazine, but it is yet the end of PIC16F887 sharing. I will talk about the utilization of internal crystal and ADC in coming volume, stay tuned! RH2T


1. PIC16F88X Datasheet

2. SK40C Rev1.0 User’s Manual

3. UIC00B User’s Manual


Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top