PDA

View Full Version : Group - ungroup of commands



fratello
- 4th May 2012, 14:16
Hi !
I need to do this : when I push a button (main program), I send through a pin this command (gosub command): Header, body,pause, body, pause .. where "body" repeat as long I push the button ( header it's something like "010001010", where 0 is us pause, 1 is "x" us of logic "1" ; the body resembles header).
Next time when I push the button, the command must be the same : Header, body, pause, body, pause, so on, so on ...
I cant find a way to send just for one time the header at each press of button ... I try to increment one variable (n=n+1) but wherever I put its reset, the results are not the expected ... the entire command are send (including header).
Please, give me a clue ! Thanks in advance ! Regards !

Jerson
- 4th May 2012, 14:42
Something like this?



main:

' somewhere in main

if ButtonIsPressed then gosub ButtonProcess

goto main


ButtonProcess:
gosub SendHeader ' send the 010001010 pattern with this subroutine
while ButtonIsPressed ' as long as the button remains pressed,
gosub SendBody ' send the body pattern
Pause xxx ' followed by the pause
wend ' loop @ while statement

' when button is released, you may want to send something too!!
gosub SendTrailer ' similar to the header but sent at end of button press. Makes it a bit exciting !!
return


Regards

fratello
- 4th May 2012, 16:46
Thank You !
Unfortunately, my code it's a little ... special and this procedure dont work :( ...
My code :

Main:
;reading buttons on line 2
Portb.0 = 0
TrisA.1 = 1
pause 10
status_3 = PortA & 011100
Portb.0 = 1
TrisA.1 = 0
select case status_3
case 12
Gosub Starting ; this is "header"
Gosub Sursa ; ths is "body"
case 20
Gosub Starting ; this is "header"
Gosub VolUp ; this is "body", composed by Adresa + VolUp
case 24
Gosub Starting
Gosub VolDn
end select
Goto main

STARTING:
PORTB.4 = 0
pauseus 8400
PORTB.4 = 1
pauseus 4200
Return

ADRESA:
For i = 0 to 7
lookup2 i, [a,a,a,a,b,b,b,a], j
PORTB.4 = 0
PauseUs 526
PORTB.4 = 1
PauseUs j
next i
Return

VolUp:
GOSUB ADRESA
for i = 0 to 7
lookup2 i, [b,b,a,b,b,b,b,a], j
PORTB.4 = 0
PauseUs 526
PORTB.4 = 1
PauseUs j
next i
Pause 12
Return

HenrikOlsson
- 5th May 2012, 08:08
Why wouldn't Jersons procedure work with that code? You just need to think about how it works and APPLY it to your program...




case 12
Gosub Starting ; this is "header"
While (PortA & %00011100) = 12
Gosub Sursa ; ths is "body"
Wend

OR, if you want to be SURE to send 'body' atleast one time.


case 12
Gosub Starting ; this is "header"
Gosub Sursa
While (PortA & %00011100) = 12
Gosub Sursa ; ths is "body"
Wend

Also, since you're always sending the header you could move that to outside of the Select Case structure. Send 'header' then select which 'body' to send. That'll save a couple of GOSUBS.

/Henrik.

fratello
- 5th May 2012, 11:45
Still don't work...
First try:

;reading buttons on line 2
Portb.0 = 0
TrisA.1 = 1
pause 10
status_3 = PortA & %00011100
Portb.0 = 1
TrisA.1 = 0
select case status_3
case 20
gosub starting
While (PortA & %00011100) = 20
Gosub VolUp
wend
end select
Goto Main
the "code" generated is ... totally inadequate (picture 1).

Second try :

;reading buttons on line 2
Portb.0 = 0
TrisA.1 = 1
pause 10
status_3 = PortA & %00011100
Portb.0 = 1
TrisA.1 = 0
select case status_3
case 20
gosub Starting
Gosub VolUp
While (PortA & %00011100) = 20
Gosub VolUp
wend
end select
Goto main
the "code" generated is ...full, with "header" at any VolUp command ! (picture 2).
What do I do wrong ?

HenrikOlsson
- 6th May 2012, 14:38
Hi,
One possible cause is that when you read PortA inside the WHILE-WEND loop PortA.1 is not read properly since you make it an output before entering the loop. If you're going to read the pins they need to be inputs.

/Henrik.

fratello
- 6th May 2012, 14:46
Wherever I put this code (While (PortA & %00011100) = 20 : Wend) the result are always the same : full command is sending.

Recap :
- if I push button for VolUp (case 20) I need to send this code : Starting (heading) , followed by VolUp command (body). If the VolUp it still pressed, while the button is pressed, only body must be sending ;
- the next time I press VolUp, the whole process must be repeated : Starting, followed by body while buton is pressed ...

Maybe instead of (While (PortA & %00011100) = 20 : Wend) I must use another command ... but I dont realise which one :( ...

fratello
- 6th May 2012, 15:21
Bingo ?!

;reading buttons on line 2
Portb.0 = 0
TrisA.1 = 1
pause 10
status_3 = PortA & %00011100
; Portb.0 = 1
; TrisA.1 = 0
select case status_3
case 20
gosub starting
while (PortA & %00011100) = 20
gosub volup
wend
end select
Portb.0 = 1 ; I've moved these here
TrisA.1 = 0 ; " "
Goto main
I think You are right again, Mr. Henrik ! I will made hardware test and post the results ! Thanks ! Regards !

fratello
- 9th May 2012, 12:58
The code posted earlier ...


;reading buttons on line 2
Portb.0 = 0
TrisA.1 = 1
pause 10
status_3 = PortA & 011100
select case status_3
case 20
gosub starting
while (PortA & 011100) = 20
gosub volup
wend
end select
Portb.0 = 1
TrisA.1 = 0
Goto main
Work verry, verry fine !!!
Thank You, Mr.Henrik !!!

fratello
- 13th July 2015, 08:12
Mr.Henrik, I need Your advice, again ...
I make some "experiments" with my specific matrix ... searching for best solution.
I use the code from previous post, with good results.
But now I need to have a little different function of buttons.
I need to have portb.x = 1 while correspondent button.x is pressed.
I don't understand why the code don't work proper ...

@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON & _LVP_OFF
include "alldigital.pbp"
Define OSC 4 ' 4MHz
CMCON = 7 ' Disable on-chip comparator, PORTA in digital mode


TRISA = %00011100 ; coloane la RA2, 3, 4 ; randuri la RA1, 0 ,7
PORTA = %00011100
PORTB = %00000000
TRISB = %00000000

status_1 VAR BYTE

Main:
;reading buttons on row 1
PortA.0 = 0
TrisA.1 = 1
pause 10
status_1 = PortA & %00011100
; PortA.0 = 1
; TrisA.1 = 0
select case status_1

case 12
while status_1 = 12
portb.1 = 1
wend
portb.2 = 0

case 20
while status_1 = 20
portb.2 = 1
wend
portb.2 = 0

case 24
while status_1 = 24
portb.3 = 1
wend
portb.3 = 0

end select
PortA.0 = 1
TrisA.1 = 0

Goto main

I KNOW I made some mistakes, but I DONT KNOW where ... Please, can You help me ?! Thank You so much !

HenrikOlsson
- 13th July 2015, 08:39
You're not updating status_1 within the while-wend loop so it will always be whatever it was when you entered that loop.


status_1 = PortA & %00011100 '<---This is where status_1 gets its value.
; PortA.0 = 1
; TrisA.1 = 0
select case status_1

case 12
while status_1 = 12 ' <---- Here, if you don't "refresh" the status_1 variable within the loop its value will NEVER change.
portb.1 = 1
wend
portb.2 = 0


/Henrik.

fratello
- 13th July 2015, 09:12
Hm, now is clear why don't work !
It's possible to refresh this status ? I guess not ..
Or it's another way to get results ?
Sorry I am so dumb ...

fratello
- 13th July 2015, 10:22
LE : Solution (not so elegant, but working) :

Main:
;reading buttons on row 1
PortA.0 = 0
TrisA.1 = 1
pause 10
if porta.2 = 0 then
portb.1 = 1
while porta.2=0 : wend
portb.1 = 0
endif

if porta.3 = 0 then
portb.2 = 1
while porta.3=0 : wend
portb.2 = 0
endif

if porta.4 = 0 then
portb.3 = 1
while porta.4=0 : wend
portb.3 = 0
endif

PortA.0 = 1
TrisA.1 = 0


;reading buttons on line 2
Portb.0 = 0
TrisA.1 = 1
pause 10
if porta.2 = 0 then
portb.4 = 1
while porta.2=0 : wend
portb.4 = 0
endif

if porta.3 = 0 then
portb.5 = 1
while porta.3=0 : wend
portb.5 = 0
endif

if porta.4 = 0 then
portb.6 = 1
while porta.4=0 : wend
portb.6 = 0
endif

Portb.0 = 1
TrisA.1 = 0

Goto main

HenrikOlsson
- 13th July 2015, 15:09
Hi,

It's possible to refresh this status ? I guess not ..
Of course there is, same way as you got the status in the first place, here with a little twist:


Main:
GOSUB GetStatus ' Get inital state

Select Case status_1

Case 12
PortB.1 = 1
WHILE status_1 = 12
GOSUB GetPortStatus ' <--- Now we'll update status_1 so we know when the button is released.
WEND
PortB.1 = 0

'.... And so on
'.... and so on
'.... and so on

Goto Main

GetPortStatus:
status_1 = PortA & %00011100
RETURN

/Henrik.

fratello
- 13th July 2015, 16:37
Wow ! Nice ! Thanks !
How far am I from this technique :( ...

HenrikOlsson
- 13th July 2015, 17:04
Just stick with it.
Make sure you always try to really think what's going on, think what the program is actually doing and what the cause of that is.

Like, OK, when the value of this or that variable is 12 I'm going to loop around here untill the value is no longer 12 - that's fine. What makes the variable change value? Well nothing because I'm not evaluating anything within the loop. Duh, I need to make sure the value CAN change by reading the port and evaulate the result WITHIN the loop, not outside of it....

/Henrik.