Skip to main content

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:


Comments

Popular posts from this blog

Power Amplifier with voltage regulator 4 × 50 Watt TDA8588

Power Amplifier with voltage regulator 4 × 50 Watt TDA8588    The TDA8588 is a multiple voltage regulator combined with four independent audio power amplifiers configured in bridge tied load with diagnostic capability. The output voltages of all regulators except regulators 2 and 3 can be controlled via the I2C-bus. However, regulator 3 can be set to 0 V via the I2C-bus. The output voltage of regulator 2 (microcontroller supply) and the maximum output voltage of regulator 3 (mechanical digital and microcontroller supplies) can both be either 5 V or 3.3 V depending on the type number. The maximum output voltages of both regulators are fixed to avoid any risk of damaging the microcontroller that may occur during a disturbance of the I 2C-bus. The amplifier diagnostic functions give information about output offset, load, or short-circuit. Diagnostic functions are controlled via the I2C-bus. The TDA8588 is protected against short-circuit, over-temperature, open ground and open VP connectio

Digital Voltmeter Circuit with ICL7107

Description. The circuit given here is of a very useful and accurate digital voltmeter with LED display using the ICL7107 from Intersil. The ICL7107 is a high performance, low power, 3.5 digit analog to digital converter. The IC includes internal circuitry for seven segment decoders, display drivers, reference voltage source and a clock. The power dissipation is less than 10mW and the display stability is very high. The working of this electronic circuit is very simple. The voltage to be measured is converted into a digital equivalent by the ADC inside the IC and then this digital equivalent is decoded to the seven segment format and then displayed. The ADC used in ICL7107 is dual slope type ADC. The process taking place inside our ADC can be stated as follows. For a fixed period of time the voltage to be measured is integrated to obtain a ramp at the output of the integrator. Then a known reference voltage of opposite polarity is applied to the input of the integrator and allowed to r

Akira HTS 38DVD HTS – Circuit Diagram Home Theater system

Home theater system - AkiraHTS38DVD   Used ICs: KA5H0165RN (SMPS control) – LC6883807 – BU1923F – BD4740G – STA505 – PS9702B – WW8721 – LA1844M Exploded Circuit Diagram Click on the schematics to magnify google.com/+GopakumarGopalan