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
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.