PIC16f628A multiple buttons

Thread Starter

Prasanna K Routray

Joined Sep 17, 2015
21
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:
list p=p16f628A
#include <p16f628A.inc>

__CONFIG _HS_OSC & _CP_OFF & _PWRTE_OFF & _WDT_OFF & _LVP_OFF & _MCLRE_ON

#define ledBlue PORTB,0 ;  RB0
#define ledGreen PORTB,1 ;  RB1
#define ledYellow PORTB,2 ;  RB0
#define ledRed PORTB,3 ;  RB1
#define botao1 PORTA,0 ; stsrts from RA0
#define botao2 PORTA,1 ; stsrts from RA0
#define botao3 PORTA,2 ; stsrts from RA0
#define botao4 PORTA,3 ; stsrts from RA0


org 0x00
goto iniciar

iniciar
         banksel TRISB ; Selecção do banco 1
         bcf TRISB,0
         bcf TRISB,1
         bcf TRISB,2
         bcf TRISB,3
         banksel PORTB ; Selecção do banco 0
         MOVLW 0X07 ; put 0X07(literal or constant) in w
         MOVWF CMCON ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)
     
Main    btfsc    botao1    ;Port A, input bit 0 = red button pushed?
        goto    Main1    ;Yes
        MOVLW   b'00000011' ; put 0X07(literal or constant) in w
        MOVWF   PORTA ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)     
        btfsc    botao2    ;Port A, input bit 0 = red button pushed?
        goto    Main2    ;Yes
     
   
       clrf    PORTB    ;turn off all LEDs
       goto    Main
Main1 
        MOVLW   b'00000001' ; put 0X07(literal or constant) in w
        MOVWF   PORTB ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)     
        goto    Main

Main2 
        MOVLW   b'00000011' ; put 0X07(literal or constant) in w
        MOVWF   PORTB ; RA1 passa a ser digital em vez de analógico(move content of W to CMCON)     
        goto    Main

end
 
Last edited by a moderator:

dannyf

Joined Sep 13, 2015
2,197
Easy:

Code:
  switch (button_pressed()) {
    case 1: light_1led(); break;
    case 2: light_2led()2; break;
    case 3: light_3leds(); break;
    .....
  }
 

Thread Starter

Prasanna K Routray

Joined Sep 17, 2015
21
Easy:

Code:
  switch (button_pressed()) {
    case 1: light_1led(); break;
    case 2: light_2led()2; break;
    case 3: light_3leds(); break;
    .....
  }



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

ericgibbs

Joined Jan 29, 2010
18,865
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.
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,
 

Thread Starter

Prasanna K Routray

Joined Sep 17, 2015
21
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
}
 

Attachments

Top