Interfacing button with led to PIC16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by bobparihar, Mar 12, 2015.

  1. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    iam new to PIC micro controller
    i was doing an interfacing of button and led to the micro controller
    objective is simple on pressing the button led should glow.. on releasing the button led should off

    following is the program in Mikro C compiler

    void main() {

    TRISC.F0=0; // for making pin 0 of PORTC as output, for led

    TRISC.F1=1; // for making pin 1 of PORTC as input for button
    while(1)
    {
    PORTC.F0=0;
    while(PORTC.F1==0)
    {
    PORTC.F0=1;
    }
    }
    }

    [​IMG]

    the problem here is led glows independent on the button... its not working please correct me
    the same logic does work for 8051... but not here... why?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    You cannot assume that an input pin with nothing connected has a defined state.
    Some MCU has internal pull-up/pull-down resistors built-in.
    In your case, connect a pull-up resistor (1k to 10kΩ) from pin-16 to VDD.
     
    bobparihar likes this.
  3. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    thanks for help sir, but will you please elaborate?
     
  4. vinnnie

    New Member

    Jan 21, 2013
    13
    2
    And I find some errors in that code, I mean:
    Why you put a while in that case? With this code the led will glow only once and then the PIC program will end, I recommend to use some interrupt in this case.
     
  5. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    this code is just a demo.. i just want o clear my concepts on single pin accessing of pic16f877a mcu
    and i put a while because i want to poll the button status continuously on that pin
     
  6. vinnnie

    New Member

    Jan 21, 2013
    13
    2
    Oh, ok.
    Then you could starting with Mr.Chips said; he suggest connect Pin16 to 5Vdd and then connect to an push-button.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    While it may be hard to read due to bad formatting the code should work.
    Code (Text):
    1. void main() {
    2.  
    3.     TRISC.F0=0; // for making pin 0 of PORTC as output, for led
    4.  
    5.     TRISC.F1=1; // for making pin 1 of PORTC as input for button
    6.     while(1)
    7.     {
    8.         PORTC.F0=0;
    9.         while(PORTC.F1==0)
    10.         {
    11.         PORTC.F0=1;
    12.         }
    13.     }
    14. }
    So the port bit is turned off, then turned on if the button is low, then the loop runs again.

    Not the most elegant way of doing things but it works.
     
    bobparihar likes this.
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    As is, your diagram only has a known state when the button is pressed - it's connected to common, but what is the voltage on the input if the button is not pressed?

    You've left it floating in an undefined state (the simulator might even call this node 0 to be able to account for it).

    Add a resistor (10k is typical) from this input pin to Vcc, called a pullup resistor as it pulls the voltage up to Vcc, or, logic '1'. When the button is pressed, the input is connected directly to common and is at logic '0'.

    This way, you ensure your input is always at a known state, depending on the button state.
     
    bobparihar likes this.
  9. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    thanks for replying, what u think is the good way(code) to do this task..?
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    First your code will work just fine (assuming there is nothing else on PORTC.F1 which I did not check), it is just not the traditional way these things are handled as you have two interwoven loops. However, that scheme is used as is in assembly programs where every instruction is closely scrutinized to see if it is really necessary.

    For now I would not worry about what code to write, but here is an example of a "more traditional" way:
    Code (Text):
    1. void main() {
    2.     TRISC.F0=0; // for making pin 0 of PORTC as output, for led
    3.     TRISC.F1=1; // for making pin 1 of PORTC as input for button
    4.     while(1)
    5.     {
    6.     if (PORTC.F1==0)
    7.         {
    8.             PORTC.F0=1;
    9.         }
    10.     else
    11.     {
    12.             PORTC.F0=0;
    13.     }
    14.     }
    15. }
    This leaves the loop "balanced" (equal number of statements either path) and makes the intent of your code clear.

    What is more importaint is to start using a consistant "coding standard" where indents are used to seperate loops and other blocks of code so their meaning is clear to a human reading it.

    That human is usually you so be good to yourself. ;-)
     
  11. MCU88

    Member

    Mar 12, 2015
    360
    35
    Personally I would do this:

    Code (Text):
    1.  
    2. #define button PORTA.F0    // Assign pin bit 0 of porta to an button
    3. #define led001 PORTA.F1    // Assign pin bit 1 of porta to an led
    4.  
    5. void main()  // Program entry point
    6. {
    7.    while(1)  // Infinite loop
    8.    {
    9.       led001 = button;   // Copy logical state of button to led pin
    10.    }
    11. }
    12.  
     
Loading...