Menu

Push button issue

Help
2015-03-01
2015-03-03
  • Jacques Nilo

    Jacques Nilo - 2015-03-01

    Hi again GCBasicers !
    I run into a problem with the following code. I never get out the Do while Button = On ... Loop even when I release the push button. But I do enter the sub OK when I push the button.
    And the same program without the Sub works ok (see below...)
    Any ideas ?
    Thanks

    ;Chip Settings
    #chip 16F690, 8
    #config FOSC_INTOSC, WDTE_OFF
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTC.5
    #define LCD_NO_RW
    #define LCD_Enable PORTC.4
    #define LCD_DB4 PORTC.0
    #define LCD_DB5 PORTC.1
    #define LCD_DB6 PORTC.2
    #define LCD_DB7 PORTC.3
    
    #define Red_button      porta.0         ;Set red pushbutton
    #define White_button    porta.1         ;Set white pushbutton
    
    Dir Red_button   In
    Dir White_button In
    
    Sub Test_button(Button)
      If Button = On Then
        ButtonCount = 0
        Do While Button = On
          Wait 10 ms
          ButtonCount += 1
        Loop
        If ButtonCount > 5  then           'Debounce
          cls
          if ButtonCount > 50 then         'Long push
             print "Long push"
          else                             'Short push
             print "Short push"
          end if
        end if
        wait 1 s
      End if
    end sub
    
    do
     Test_button(White_button)
    loop
    

    The same program without the sub works perfectly well:

    ;Chip Settings
    #chip 16F690, 8
    #config FOSC_INTOSC, WDTE_OFF
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTC.5
    #define LCD_NO_RW
    #define LCD_Enable PORTC.4
    #define LCD_DB4 PORTC.0
    #define LCD_DB5 PORTC.1
    #define LCD_DB6 PORTC.2
    #define LCD_DB7 PORTC.3
    
    #define Red_button      porta.0         ;Set red pushbutton
    #define White_button    porta.1         ;Set white pushbutton
    
    Dir Red_button   In
    Dir White_button In
    
    DO
      If White_button = On Then
        ButtonCount = 0
        Do While White_Button = On
          wait 10 ms
          ButtonCount += 1
        Loop
        If ButtonCount > 5  then           'Debounce
          cls
          if ButtonCount > 50 then         'Long push
             locate 0,0
             print "Long push"
             locate 1,0
             print ButtonCount
          else                             'Short push
             locate 0,0
             print "Short push"
             locate 1,0
             print ButtonCount
          end if
        end if
        wait 1 s
        cls
      End if
    loop
    
     

    Last edit: Jacques Nilo 2015-03-01
  • William Roth

    William Roth - 2015-03-02

    Hi Jacques,

    A constant such as a pin name cannot be passed to a SUB, so use MACRO instead.

    Replace the SUB with this MACRO and test.

    ~~~~
    Macro Test_button (Button)
    if Button = ON then
    wait 10 ms 'debounce
    ButtonCount = 0

           Do While Button = On
               Wait 10 ms
               ButtonCount += 1
           Loop
    
           if ButtonCount > 5  then
               if ButtonCount > 50 then   'Long push
                   print "Long push"
               else                       'Short push
                   print "Short push"
               end if
           end if
           wait 1 s
       end if
    

    End Macro
    ~~~~~

     
  • Jacques Nilo

    Jacques Nilo - 2015-03-02

    Hi Bill,
    Thanks for the trick. It does work perfectly!
    To Anobium: what about mentioning that restriction (quite logical after all) in the Subroutines parameter help chapter ?
    Jacques

     
  • Anobium

    Anobium - 2015-03-02

    @Jacques. I will - a good idea.

    Also, have a search in the Help File for TOGGLE. This shows the method to pass port parameters to a sub routine.

     
  • Jacques Nilo

    Jacques Nilo - 2015-03-02
     

    Last edit: Jacques Nilo 2015-03-03
  • Jacques Nilo

    Jacques Nilo - 2015-03-03

    Hello all!
    I did my homework:-). I found an interesting thread about passing objects to functions and also read the PEEK command help file. Following the Toggle approach suggested by Anobium I have adjusted the code as follows. It uses the PEEK command and the @ parameter.
    As far as the comparison with the Macro approach is concerned it looks like it boils down to a trade off between portability and size. The macro approach will duplicate n times the asm code if the macro is called n times with n different calling arguments. The PEEK approach is PIC dependant since you have to pass the destination bit adress as an argument.
    Jacques

    ;Chip Settings
    #chip 16F690, 8
    #config FOSC_INTOSC, WDTE_OFF
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTC.5
    #define LCD_NO_RW
    #define LCD_Enable PORTC.4
    #define LCD_DB4 PORTC.0
    #define LCD_DB5 PORTC.1
    #define LCD_DB6 PORTC.2
    #define LCD_DB7 PORTC.3
    
    Dir PORTA.0 In
    Dir PORTA.1 In
    Dir PORTB.4 In
    
    Sub Test_button(in DestPort as word, in DestBit)
      If (PEEK(DestPort) and DestBit) = DestBit Then            
        wait 10 ms
        ButtonCount = 0
        Do While (PEEK(DestPort) and DestBit) = DestBit
          wait 10 ms
          ButtonCount += 1
        Loop
        if ButtonCount > 50 then         'Long push
          print "Long push"
        else                             'Short push
          print "Short push"
        end if
        wait 1 s
        cls
      End if
    end Sub
    
    do
    
    ;PORTA.0/RA0 located on bit 0 of PORTA register (Table 2.1 of 16F690 datasheet)
    ;If on then PORTA=00000001 i.e. 1 in decimal
    
      Test_button(@PORTA,1)           'Check status of PORTA.0
    
    ;PORTA.1/RA1 located on bit 1 of PORTA register (Table 2.1 of 16F690 datasheet)
    ;If on then PORTA=00000010 i.e. 2 in decimal
    
      Test_button(@PORTA,2)           'Check status of PORTA.1
    
    ;PORTB.4/RB4 located on bit 4 of PORTB register (Table 2.1 of 16F690 datasheet)
    ;If on then PORTB=00010000 i.e. 16 in decimal
    
      Test_button(@PORTB,16)          'Check status of PORTB.4
    
    loop
    
     

    Last edit: Jacques Nilo 2016-04-17

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.