Thursday, September 2, 2010

PIC16F887/877 programming in C Tutorial 1 (Getting Started)

PIC16F887 and PIC16F877 are the members of PIC16F88X and PIC16F87X families respectively. PIC16F887 is the new version launched by microchip to take place of PIC16F877.For detail information please refer to their datasheets.

In this tutorial i will show you how to program PIC16F887/877 in C.
So first of all download mikroc pro for pic compiler also download its user manual. After this create a new project choose P16F887 or 877 whatever you have, and 4MHz crystal etc. If you dont know how to create a new project refer to its manual or download document 'how to create first project' from its site.

Note that i will use PIC16F887 and 4MHz external crystal (although PIC16F887 also have internal crystal) you can use 8MHz and what so ever. For 4MHz oscillator setting should be XT where as for 8MHz or above it should be HS (see fig below).

Lets start with a simple program 'LED Chaser', before we start this lets check out the PIC16F887 pin out.
pinout of pic16f887
As you can see there are five multiplex i/o ports, in PIC16F887, from portA to portE. For our first program we need only one port let say portD and 5v at pins 11 & 32, ground pins 12 & 31, 4MHz xtal at pins 13 & 14. Dont be panic to see more than one name of pins, i will tell you the pin functions when we use them.

After creating the project just write the below code in mikroc ide and build it(CTRL + F9), if you want to change the project setting, fuse bits, clock etc,  goto>>project>>edit project.

Code:
void main() {
        int x[]={1,2,4,8,16,32,64,128,256};  // int array
        int i;
        portc=0;      //to clear port
        trisc=0;        //as output

///////////////////// comment this block if you are using pic16f877a/////////
        ANSEL  = 0;                        // Configure AN pins as digital I/O
        ANSELH = 0;
       
        C1ON_bit = 0;                      // Disable comparators
        C2ON_bit = 0;
 ////////////////////////////////////////////////////////////////////

 while(1){
  
      for(i=0; i<=8; i++){
         portc=x[i];
         delay_ms(100);
      }          //for bracket

         }            //while bracket 

}                //main bracket

Now come towards code; firt select the port you want to use, i this case it is portc. Then clear the port by assigning it 0. Trisx, the SFR  is used to set the port as input or output, where x can be a,b,c,d, and e; the port which you are using. Trisc=0 means the portc is selected for output. For input change it to Trisc=255; means all pins of portc as ready for input. If you want take i/p at single pin instead of whole then use trisxy_bit=1, where y could be from 0 to 7; the pin you at want to take i/p and y is the respective port.

There are two comparators in 887, to disable them assign 0 to c1 and c2 bits. Also to use pins as digital i/o assign 0 to ansel and anselh; dont be panic when we do ADC i will tell you about this. Note that for 877a these two thing is not required.

Now for loop; as for led chaser we want to on led at power of 2 sequence that is 1,2,4 till 256 as port is 8-bit so 2^8=256; that is why have save these values in a array.
To get visual we have to add some delay, to do this just write delay_ms(100); for 100 ms delay. You can increase it if you want.

As we want to see our o/p continuously not only for a single time, we use while loop; while(1) means this loop will run forever.

project setting
Schematic: 
Give the hex file path, of this project, to isis and simulate it. Dont forget to attach reset circuitry.

Reset Circuit:

18 comments:

  1. I dont see pins 11 and 32 in your ISIS schematic of the connections. In fact the ISIS schematic for 887/877 dont show power connections - how do i get these into the simulation?

    ReplyDelete
  2. @Zohaib
    Im new in PIC, Kindly tell me how to do simulation and burner used for PIC

    ReplyDelete
    Replies
    1. After designing the PIC circuit in proteus, use hex file to simulate it.
      for burner first read this:
      http://electronicseverywhere.blogspot.com/2009/08/getting-started-with-microcontrollers.html
      or you can make pictik2 clone:
      http://electronicseverywhere.blogspot.com/2011/12/pickit-2-clone.html

      Delete
  3. Dear Zohaib
    Both links are not available at the forum.
    I know the assembly and c programming, just want to do practical on PIC 16F887. I have installed MPLAB V8.xx, kindly tell me how to create hex file,

    ReplyDelete
    Replies
    1. Google it you will find how to make first project, or check these links:
      http://picprojects.org.uk/projects/mplab/mplabhowto.htm

      http://www.microchip.com/stellent/groups/SiteComm_sg/documents/Training_Tutorials/en528144.pdf

      Delete
    2. By the way links are fine just copy these urls and paste in browser, thats how they will work.

      Delete
  4. pliz help me with three passwords program using PIC16F887

    ReplyDelete
  5. 324 Undeclared identifier 'ANSEL'
    10 324 Undeclared identifier 'ANSELH'
    12 324 Undeclared identifier 'C1ON_bit
    13 324 Undeclared identifier 'C2ON_bit'

    these are the comments i m getting after building the above code for 16F877 with 4MHz Osc.
    plz make me understand y so it is?

    ReplyDelete
    Replies
    1. These are for 887 not for 877. Just comment these lines.

      Delete
  6. REQUEST INDICATE HOW TO FADE LED. IM MAKING 12 LED CHASER WITH COMET TAIL EFFECT(SWEEPING MOTION) FOLLOWED LED TO BE FADED AND OFF.HOWTO WRITE C CODE?

    ReplyDelete
  7. how we interface ADE7758 with PIC16F877a.

    ReplyDelete
  8. Replies
    1. First you must learn the basic. thats all

      Delete
  9. Exploring how the convenience afforded by online browsing is changing the business. microbt whatsminer m30s

    ReplyDelete
  10. I should say only that its awesome! The blog is informational and always produce amazing things. Black satta king

    ReplyDelete
  11. All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks https://python.engineering/best-laptop-for-cricut-maker/

    ReplyDelete