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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
DoWhileButton=OnWait10msButtonCount+=1LoopifButtonCount>5thenifButtonCount>50then'Long push print "Long push"else'Short push print "Short push" end if end if wait 1 s end if
End Macro
~~~~~
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
;ChipSettings#chip16F690,8#configFOSC_INTOSC,WDTE_OFF;Defines(Constants)#defineLCD_IO4#defineLCD_RSPORTC.5#defineLCD_NO_RW#defineLCD_EnablePORTC.4#defineLCD_DB4PORTC.0#defineLCD_DB5PORTC.1#defineLCD_DB6PORTC.2#defineLCD_DB7PORTC.3DirPORTA.0InDirPORTA.1InDirPORTB.4InSubTest_button(inDestPortasword,inDestBit)If(PEEK(DestPort)andDestBit)=DestBitThenwait10msButtonCount=0DoWhile(PEEK(DestPort)andDestBit)=DestBitwait10msButtonCount+=1LoopifButtonCount>50then'Long push print "Long push" else 'Shortpushprint"Short push"endifwait1sclsEndifendSubdo;PORTA.0/RA0locatedonbit0ofPORTAregister(Table2.1of16F690datasheet);IfonthenPORTA=00000001i.e.1indecimalTest_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) 'CheckstatusofPORTA.1;PORTB.4/RB4locatedonbit4ofPORTBregister(Table2.1of16F690datasheet);IfonthenPORTB=00010000i.e.16indecimalTest_button(@PORTB,16)'CheckstatusofPORTB.4loop
Last edit: Jacques Nilo 2016-04-17
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
The same program without the sub works perfectly well:
Last edit: Jacques Nilo 2015-03-01
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
End Macro
~~~~~
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
@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.
Last edit: 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
Last edit: Jacques Nilo 2016-04-17