Sunday, October 23, 2011

how to take input with pic 16f877

Discription :
Any microcontroller based system typically has an input and a corresponding output .taking simple output  with a pic micocontroller has been explained  in led blinking with pic 16f877 .this article i will explain you how to provide an input to the pic and get corresponding output using pic 16f877
PIC 16f877 series normally has five input/output ports.they are used for the input/output interfacing with other devices/circuits.Most of thses port pins are multiplexed for handling alternate function for peripheral features on the devices.All ports in PIC chip are are biderctional .When the peripheral action is enabled in a pin, it may not be used as its general input/output functions.The PIC 16f877 chip basically has 5 input/output ports. the five I/O PORTS its functions are given below .
  1. PORTA and the TRIS A Registers
  2. PORTB and the TRIS B registers
  3. PORTC and the TRIS C registers
  4. PORTD and the TRIS D registers
  5. PORTE and the TRIS E registers
To configure a particular ports/pin as input ,the corresponding TRIS register/TRIS bit should be set to high(1=5v).for output,and TRIS register/bit should be set to low (0=0v)
for example :
for port D ,to set the entier port D as input
TRISD=11111111=0XFF
for set the port d as out put
TRISD=0b0000000=0
In this program, a switch has been conncted on port pin RA0 of port A to provide input to the microcontroller .The input is monitored as an output on LED connected to pin RD0 .The connections are show in the figure 1

CODE :
Lab 2: switch on LED by button with PIC16F877
Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Oct 23,      2011 by charaf zouhair
*/
void main() {
adcon1=0x07;
TRISD = 0b00000000; // PORTC All Outputs
TRISA=0B11111111;

while(1) {
if (porta.f0==0){portd.f0=1;}
else
portd.f0=0;
}
for more information about code contact me charaf_zohair@hotmail.com

No comments: