How to Control Push Button Switch

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

How to Control Push Button Switch

Postby mohdismiaswaly » Tue Jan 28, 2014 11:06 pm

Hello,

I want to use a push button switch as an input, LED as an output. When I press switch (ON), LED will ON and it will remain ON until the switch is pressed again, the LED will OFF until the switch is pressed again, LED will again ON and this process keep on continuing.

I have written my own code. Please check where is the mistake? The output shows the LED keep on blinking.
Thanks
CODE: SELECT_ALL_CODE
----------------------------------------------------------------------------------------------------------------------------------------------------
LIST      P=18F4550   F=INHX32 ; list directive to define processor
#INCLUDE <P18F4550.inc>        ; processor specific variable definitions
CONFIG FOSC = HS
CONFIG WDT  = OFF
CONFIG PBADEN = OFF
CONFIG MCLRE = ON
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRT = OFF
;==================================================================================
; VARIABLE DEFINITION (GPR)
;==================================================================================

D1       EQU      0X20   ;REGISTER FOR DELAY
D2      EQU      0X21   ;REGISTER FOR DELAY
D3      EQU      0X22   ;REGISTER FOR DELAY

;=====================================================================================
; WRITE YOUR PROGRAM HERE
;=====================================================================================

      ORG     0x00             ; processor reset vector
        GOTO    main              ; go to beginning of program

main:

; initialize of your PIC, setting the general I/O in TRIS
   
      BSF      STATUS,5                    ;SWITCH TO BANK1; BIT 5 OF STATUS REGISTER IS SET TO 1
      BSF      TRISB,3               ;SWITCH
      BSF      TRISB,2               ;CHECK LED
      BCF      TRISB,4               ;LED
      BCF      STATUS,5                    ;SWITCH TO BANK0; BIT 5 OF STATUS REGISTER IS SET TO 0
         

; The main program begin here
      

      MOVLW   B'00001111'
      MOVWF   ADCON1,0

UP      BTFSS   PORTB,3
      CALL   KEEP
      CALL   LED
      GOTO    UP
   
LED      BTFSS   PORTB,2
      CALL   ON
      CALL   OFF
      RETURN

ON      BSF      PORTB,4
      CALL   DELAY
      GOTO   UP

OFF      BCF      PORTB,4
      CALL   DELAY
      RETURN

KEEP   BTFSS   PORTB,2
      CALL   OFF
      CALL   ON
      RETURN   

;========================================================================================
; DELAY SUBROUTINE
;========================================================================================

DELAY      MOVLW   D'5'         
         MOVWF   D3                     
         MOVLW   D'3'         
         MOVWF   D2         
         MOVLW   D'1'         
         MOVWF   D1         
LINE1      DECFSZ   D1            ;DECREASE THE VALUE OF D1 AND SKIP THE NEXT LINE WHEN IT REACH ZERO
         GOTO   LINE1         ;IF NOT ZERO, IT WILL LEAD THE PROGRAM TO 1 LINE ABOVE
LINE2      DECFSZ   D2            ;DECREASE THE VALUE OF D2 AND SKIP THE NEXT LINE WHEN IT REACH ZERO
         GOTO   LINE2         ;IF NOT ZERO, IT WILL LEAD THE PROGRAM TO 5 LINE ABOVE
LINE3      DECFSZ   D3            ;DECREASE THE VALUE OF D3 AND SKIP THE NEST LINE WHEN IT REACH ZERO
         GOTO   LINE3         ;IF NOT ZERO, IT WILL LEAD THE PROGRAM TO 9 LINE ABOVE
         RETURN               ;RETURN FROM SUBROUTINE


          END                     ; directive 'end of program'
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: How to Control Push Button Switch

Postby Ali_Challenger » Thu Dec 25, 2014 2:59 am

The code for this situation is to declare a Boolean variable, for instance bool x, then follow:

1, when the button been pressed set x to true.

2, when the button been released, check if the x is true then set it to false, then make toggle to your output.
Ali Challenger
Master Student @ FKEKK-MENC-UTEM
Mob.0187895080
https://www.facebook.com/pages/Challeng ... 8039157929
User avatar
Ali_Challenger
Apprentice
 
Posts: 37
Joined: Tue Oct 28, 2014 8:07 pm
Location: Melaka


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 7 guests

cron