IC PIC16F877A

IC PIC16F877A

Fr4,000 Fr3,000

HOW TO PROGRAM THE INPUT AND OUTPUT PORTS OF PIC16F877A

As we have studied 5 input and output ports namely PORTA, PORTB, PORTC, PORTD and PORTE which can be digital as well as analog. We will configure them according to our requirements. But in case of analog mode, the pins or the ports can only act as inputs. There is a built in A to D converter which is used in such cases. Multiplexer circuits are also used.

But in digital mode, there is no restriction. We can configure the ports as output or as input. This is done through programming. For PIC the preferable compiler is mikro C pro which can be downloaded from their website.

There is a register named as ‘TRIS’ which controls the direction of ports. For different ports there are different registers such as TRISA, TRISB etc.

  • If we set a bit of the TRIS register to 0, the corresponding port bit will act as the digital output.
  • If we set a bit of the TRIS register to 1, the corresponding port bit will act as the digital input.

For example to set the whole portb to output we can write the program statement as:

TRISB=0;

Now the port will act as the output port and we can send any value on the output such as

PORTB=0XFF;

FF represents all 1’s in binary i.e. FF=11111111, now all the pins of port b are high. If we connect LEDs at all the pins then they will all start glowing in this condition.

If we want to negate the values of the port b we can use the statement:

PORTB=~PORTB;

Now all the pins of the port b will be low.

CODE TO LIGHT UP A SINGLE LED/ FLASHING LED

void main()

 {

      TRISB.F0 = 0  // the direction of RB0 is set as output

                 //or TRISB = 0xFE (0xFE = 11111110)

      do // setting the infinite loop

      {

        PORTB.F0 = 1; // setting the RB0 pin to high

        Delay_ms(500); // delay of 500 milli seconds

        PORTB.F0 = 0; // setting the RB0 pin to low

        Delay_ms(500); // again a delay of 500 milli seconds

     }while(1);

 }

CIRCUIT DIAGRAM FOR FLASHING LED WITH PIC16F877A

LED blinking

Design this circuit in proteus. Connect the pins with the source, ground and oscillator as explained in the pin description section. An LED is connected on pin 33 RB0, a resistor is used to limit the current and to prevent the LED from burning up. Write and compile the program in mikro C pro. Burn the controller with the hex file by double clicking on the controller in proteus and run the circuit successfully. best way to learn any microcontroller is to check its data sheet.

Description

PIC16f877a finds its applications in a huge number of devices. It is used in remote sensors, security and safety devices, home automation and in many industrial instruments. An EEPROM is also featured in it which makes it possible to store some of the information permanently like transmitter codes and receiver frequencies and some other related data. The cost of this controller is low and its handling is also easy. Its flexible and can be used in areas where microcontrollers have never been used before as in coprocessor applications and timer functions etc.

PIN CONFIGURATION AND DESCRIPTION OF PIC16F877A

As it has been mentioned before, there are 40 pins of this microcontroller IC. It consists of two 8 bit and one 16 bit timer. Capture and compare modules, serial ports, parallel ports and five input/output ports are also present in it.

PIC16F877A Pin configurationPIC16F877A Pin configuration

PIN 1: MCLR

The first pin is the master clear pin of this IC. It resets the microcontroller and is active low, meaning that it should constantly be given a voltage of 5V and if 0 V are given then the controller is reset. Resetting the controller will bring it back to the first line of the program that has been burned into the IC.

PIC16F877A reset

A push button and a resistor is connected to the pin. The pin is already being supplied by constant 5V. When we want to reset the IC we just have to push the button which will bring the MCLR pin to 0 potential thereby resetting the controller.

PIN 2: RA0/AN0

PORTA consists of 6 pins, from pin 2 to pin 7, all of these are bidirectional input/output pins. Pin 2 is the first pin of this port. This pin can also be used as an analog pin AN0. It is built in analog to digital converter.

PIN 3: RA1/AN1

This can be the analog input 1.

PIN 4: RA2/AN2/Vref-

It can also act as the analog input2. Or negative analog reference voltage can be given to it.

PIN 5: RA3/AN3/VREF+

It can act as the analog input 3. Or can act as the analog positive reference voltage.

PIN 6: RA0/T0CKI

To timer0 this pin can act as the clock input pin, the type of output is open drain.

PIN 7: RA5/SS/AN4

This can be the analog input 4. There is synchronous serial port in the controller also and this pin can be used as the slave select for that port.

PIN 8: RE0/RD/AN5

PORTE starts from pin 8 to pin 10 and this is also a bidirectional input output port. It can be the analog input 5 or for parallel slave port it can act as a ‘read control’ pin which will be active low.

PIN 9: RE1/WR/AN6

It can be the analog input 6. And for the parallel slave port it can act as the ‘write control’ which will be active low.

PIN 10: RE2/CS/A7

It can be the analog input 7, or for the parallel slave port it can act as the ‘control select’ which will also be active low just like read and write control pins.

PIN 11 and 32: VDD

These two pins are the positive supply for the input/output and logic pins. Both of them should be connected to 5V.

PIN 12 and 31: VSS

These pins are the ground reference for input/output and logic pins. They should be connected to 0 potential.

PIN 13: OSC1/CLKIN

This is the oscillator input or the external clock input pin.

PIN 14: OSC2/CLKOUT

This is the oscillator output pin. A crystal resonator is connected between pin 13 and 14 to provide external clock to the microcontroller. ¼ of the frequency of OSC1 is outputted by OSC2 in case of RC mode. This indicates the instruction cycle rate.

crystal interfacing with PIC16F877A

PIN 15: RC0/T1OCO/T1CKI

PORTC consists of 8 pins. It is also a bidirectional input output port. Of them, pin 15 is the first. It can be the clock input of timer 1 or the oscillator output of timer 2.

PIN 16: RC1/T1OSI/CCP2

It can be the oscillator input of timer 1 or the capture 2 input/compare 2 output/ PWM 2 output.

PIN 17: RC2/CCP1

It can be the capture 1 input/ compare 1 output/ PWM 1 output.

PIN 18: RC3/SCK/SCL

It can be the output for SPI or I2C modes and can be the input/output for synchronous serial clock.

PIN 23: RC4/SDI/SDA

It can be the SPI data in pin. Or in I2C mode it can be data input/output pin.

PIN 24: RC5/SDO

It can be the data out of SPI in the SPI mode.

PIN 25: RC6/TX/CK

It can be the synchronous clock or USART Asynchronous transmit pin.

PIN 26: RC7/RX/DT

It can be the synchronous data pin or the USART receive pin.

PIN 19,20,21,22,27,28,29,30:

All of these pins belong to PORTD which is again a bidirectional input and output port. When the microprocessor bus is to be interfaced, it can act as the parallel slave port.

PIN 33-40: PORT B

All these pins belong to PORTB. Out of which RB0 can be used as the external interrupt pin and RB6 and RB7 can be used as in-circuit debugger pins.