New Light Weight Champion – PIC16F887

New Light Weight Champion – PIC16F887

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

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

 

 

 

1.0 PIC16F877A

 

Yes, PIC16F877A is very famous since I started with the PIC microcontroller back in the year 2002. Everyone uses either PIC16F84A or PIC16F877A. Let’s take a look at 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 emerged as the lightweight champion in the 2000s. Personally, I have been using PIC16F877A for many projects, from LED blinking to mobile robots. It serves me well and there is nothing much I can complain about. Yet, as an engineer, we always demand better solutions, and I am pretty sure those guys at Microchip know about it. In 2008, Microchip introduced the PIC16F887 and suggested it as a substitution for 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 them 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 speeds from 32KHz to 8MHz. This internal crystal can be tuned through the software register and it offers switchable operating frequencies between internal and external crystals during program runtime. With this option, we can save the money needed for an external crystal, avoid a breakdown when the external crystal stop working, and have two extra I/O pins that were initially used for an external crystal. The two extra pins are RA6 and RA7 which is a bidirectional I/O pins, great! If you compare with the 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 noticed, analog input is getting more important because more sensors come with analog output as it is the easiest method to deliver a wide range of information by just a single wire interface. These sensors include an 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 sensors into your design. Despite the increased number of analog inputs, 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 an 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 a digital pin every time it reads the digital input pin, and the same step apply when the program wants to read the 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 pins is truly independent of each other. PIC16F887 uses ANSEL and ANSELH registers 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 the previous discussion. This pin is RE3. PIC16F877A offers 33 I/O pins 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 that we normally use for Reset purposes. For PIC16F887, the 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 a 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 at only half the price of a PIC16F877A. This is the main factor, more features yet a cheaper price; with this, we definitely have no reason to resist it.

 

3.0 Getting Started

Since we have decided to use the new lightweight champion, let's get started. I will utilize the SK40C from Cytron Technologies that I have been using to develop projects as the Starter Kit. So what should we do? Just start with a 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 the LCD when you power it up. Well, we need to modify the sample source code given for PIC16F877A and load it 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 the SK40C product page. Since we are using a different model, we will need to configure the compiler to compile for it. The 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, the 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 to project -> 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 a language toolsuite, Cytron uses the HI-TECH C compiler for sample source code development. Therefore you will need to get the compiler installed earlier. The compiler comes with an 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 be able to complete them. Finally, you have a C project as shown in the 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 modifications in order for it to be executable on PIC16F887. First, it is the configuration bit. Replace the original configuration settings with new configuration settings.

 

The next modification needed is for ADC configuration. PIC16F877A uses ADCON1 to configure the digital and analog pins. 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 the next volume. Compile and load the hex code into PIC16F887 on SK40C via the UIC00B programmer. It is quite straightforward as the User’s Manual of SK40C shows the detailed step.

 

 

4. 0 Conclusion

If you have been following closely the steps mentioned, everything should be working well. You should get a message on the LCD and the LEDs should blink alternately. I have made some changes to 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 photos 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 to the 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 the coming volume, stay tuned! RH2T

References:

1. PIC16F88X Datasheet

2. SK40C Rev1.0 User’s Manual

3. UIC00B User’s Manual

BUY