PIC16f628A multiple buttons

Discussion in 'Embedded Systems and Microcontrollers' started by Prasanna K Routray, Sep 17, 2015.

  1. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0
    Hello.

    I want to know how to work with multiple buttons and LED's for my work. I have 4buttons and 2LED's. when I press 1st button one LED must glow. when I Press Two buttons then 2 LED's must glow and when I press 3buttons it should work in an opposite manner to 1st case.

    can anyone please help me?

    This is where I'm

    Code (ASM):
    1.  
    2. list p=p16f628A
    3. #include <p16f628A.inc>
    4.  
    5. __CONFIG _HS_OSC & _CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_ON
    6.  
    7. #define ledBlue PORTB,0 ;  RB0
    8. #define ledGreen PORTB,1 ;  RB1
    9. #define ledYellow PORTB,2 ;  RB0
    10. #define ledRed PORTB,3 ;  RB1
    11. #define botao1 PORTA,0 ; stsrts from RA0
    12. #define botao2 PORTA,1 ; stsrts from RA0
    13. #define botao3 PORTA,2 ; stsrts from RA0
    14. #define botao4 PORTA,3 ; stsrts from RA0
    15.  
    16.  
    17. org 0x00
    18. goto iniciar
    19.  
    20. iniciar
    21.          banksel TRISB ; Selecção do banco 1
    22.          bcf TRISB,0
    23.          bcf TRISB,1
    24.          bcf TRISB,2
    25.          bcf TRISB,3
    26.          banksel PORTB ; Selecção do banco 0
    27.          MOVLW 0X07 ; put 0X07(literal or constant) in w
    28.          MOVWF CMCON ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)
    29.      
    30. Main    btfsc    botao1    ;Port A, input bit 0 = red button pushed?
    31.         goto    Main1    ;Yes
    32.         MOVLW   b'00000011' ; put 0X07(literal or constant) in w
    33.         MOVWF   PORTA ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)    
    34.         btfsc    botao2    ;Port A, input bit 0 = red button pushed?
    35.         goto    Main2    ;Yes
    36.      
    37.    
    38.        clrf    PORTB    ;turn off all LEDs
    39.        goto    Main
    40. Main1
    41.         MOVLW   b'00000001' ; put 0X07(literal or constant) in w
    42.         MOVWF   PORTB ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)    
    43.         goto    Main
    44.  
    45. Main2
    46.         MOVLW   b'00000011' ; put 0X07(literal or constant) in w
    47.         MOVWF   PORTB ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)    
    48.         goto    Main
    49.  
    50. end
    51.  
     
    Last edited by a moderator: Sep 17, 2015
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Easy:

    Code (Text):
    1.  
    2.   switch (button_pressed()) {
    3.     case 1: light_1led(); break;
    4.     case 2: light_2led()2; break;
    5.     case 3: light_3leds(); break;
    6.     .....
    7.   }
    8.  
     
  3. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Compile that and repost in .asm.
     
  4. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0


    Is this in assembly language? I think this is in c language. I need to know the method in assembly language ...
     
  5. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0



    But this is in c language. I need in assembly language...
     
  6. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0



    Is this in assembly language? I need the codes in assembly language..please help me by understanding my problem. I need to know it .
     
  7. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0


    I'm sorry but how to compile this easily ?
     
  8. ericgibbs

    AAC Fanatic!

    Jan 29, 2010
    2,503
    380
    hi Prasanna,
    In your code definitions you have PORTA for 4 buttons and PORTB for 4 LED drivers.

    I do not understand what this means, and when I press 3buttons it should work in an opposite manner to 1st case

    E

    Please post your Code as an attachment without line numbers,
     
  9. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    hello,
    it is simple after spending so much time in forum.
    use if statement it will work all right..!!
     
  10. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0
    I have 4 buttons(I can keep them in on state or off state, but manually as sensors)
    I have 2 LED(RED and GREEN)

    case-1: 1 button in on state then LED Green is ON & LED Red is OFF
    case-2: 2 buttons in on state then LED Green is ON & LED Red is ON
    case-3: 3 buttons in on state then LED Green is OFF & LED Red is ON
    case-4: 4 same as case-3

    if(button1==1 & button2==0 & button3==0 & button4==0)
    {
    LED Green is ON & LED Red is OFF
    }
    Elesif(button1==1 & button2==1 & button3==0 & button4==0)
    {
    LED Green is ON & LED Red is ON
    }
    Elseif(button1==1 & button2==1 & button3==1 & button4==0)
    {
    LED Green is OFF & LED Red is ON
    }
    Elseif(button1==1 & button2==1 & button3==1 & button4==1)
    {
    LED Green is ON & LED Red is ON
    }
    Else
    {
    LED Green is OFF & LED Red is OFF
    }
     
  11. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    I know. I was replying to @dannyf, not you.
     
  12. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Typically done by using a compiler.
     
  13. jayanthd

    Member

    Jul 4, 2015
    274
    29
    I can write a code (asm) for you but please zip and post the complete MPLAB / MPLAB X MPASM project files.
     
  14. Prasanna K Routray

    Thread Starter New Member

    Sep 17, 2015
    8
    0
    OK. I will for sure...
     
  15. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    Hi Tech c is fine
     
Loading...