[PIC] how can read inputs in micro controller pic16f877A.

Status
Not open for further replies.
How would you like to read inputs in microcontroller? Reading inputs is easy, you just need to configure a port as an digital inputs. It's done by special function registers TRISA, TRISB, TRISC. In general, PIC16F877A has ports configured as inputs by default(see datasheet), to set some inputs as a digital you may have to set ADCON1 special function register(see datasheet), because some inputs may be configured as analogue inputs.
 
thanks for help .
but i have a problem if i use that special function registers TRISA, TRISB, TRISC. i can't make anyone pin as input and output.
i have other way to make that work.
first i define pin at the top of the program and use if,else function to check the pin is 1 or 0.
if(input(sw1)==0)
{ duty1=duty1;
for(x=duty2;x<=0;--x)
duty2=x;
delay_ms(1);
}
 

Code:
void main()
{
    ADCON1=0x07;
    CMCON=0;
    TRISB0_bit = 1;                                // set RB0 pin as input
    TRISD0_bit = 0;                                // set RB0 pin as output
    RD0_bit=0;

    do
    {
        if (Button(&PORTB, 0, 10, 0))      // Detect logical one
        {
           RD0_bit= 1;
        }
        else if (Button(&PORTB, 0, 10, 1))     // Detect one-to-zero transition
        {
           RD0_bit= 0;                             // Update flag
        }
    } while(1);                                    // Endless loop
}
 
There are no input and output instructions per se on the PIC processors, the ports are mapped as processor registers. Reading a port register or a bit in a port register will tell you the logic levels on the pins. Similarly, writing to a port register makes a '1' bit drive the pin high and a '0' make it low.

The port direction (input or output) can be set pin by pin using the TRISx registers, a '1' makes the pin an input and '0' makes it an output. Many high level languages will take care of the port direction switching for you so when they see you writing to the port they will assume it is an output and configure the pins automatically. If you read a port or pins it will configure them as input automatically. You should still be able to set and read the directions in your code by writing or reading the TRIS registers. If you are using the pin bi-directionally, sometimes it is easiest to manipulate the TRIS bit yourself so you have greater control over it.

Brian.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…