Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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
}
 
You want to read digital input or analog input ?
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top