Reading Complete Port question

1968neil

Senior Member
Hi All,
Having a blonde moment.....

I am reading a full port
Code:
main:
let b1 = pinsC & %00111111
;************************************************************************************
;                     DATA_DEFINITION FROM SWITCH                                   *
;************************************************************************************
if b1 =  %00000001 then gosub SW1         ; HEX Value = $01
if b1 =  %00000010 then gosub SW2         ; HEX Value = $02
if b1 =  %00000011 then gosub SW3         ; HEX Value = $03
if b1 =  %00000100 then gosub SW4         ; HEX Value = $04
if b1 =  %00000101 then gosub SW5        ; HEX Value = $05
if b1 =  %00000110 then gosub SW6         ; HEX Value = $06
if b1 =  %00000111 then gosub SW7         ; HEX Value = $07
if b1 =  %00001000 then gosub SW8         ; HEX Value = $08
if b1 =  %00001001 then gosub SW9         ; HEX Value = $09
if b1 =  %00010000 then gosub SW10         ; HEX Value = $10


Goto main
What i cant get my head around is this :
The input is read from a 40 way switch that controls a PLL chip (like the ones used in old CB Radios back in the 80's), goes off and does its gosub and comes back. All fine.
What i need it to do is wait until the switch changes before it goes back to read the port.
Any input gladly received.

Regards
Neil
 

Buzby

Senior Member
You can only tell if the switch has moved by reading the port !.

At the end of each of your subs put something like this :

do ' Wait for switch change
let newval = pinsC & %00111111
if newval <> b1 then : Return : endif
loop

( This assumes you don't change b1 in your sub. )
 

1968neil

Senior Member
Thanks Buzby,
Spot on as usual.
I think the grey matter is getting old at this end :)
Rgds
Neil
 

Buzby

Senior Member
It can be streamlined even more.

Put the code I wrote in a sub of it's own, maybe called WaitForSwitch.

Then replace each RETURN in each of your subs with GOTO WaitforSwitch

( Some people don't like GOTO, but sometimes it's the best way. )
 

Aries

New Member
Then replace each RETURN in each of your subs with GOTO WaitforSwitch

( Some people don't like GOTO, but sometimes it's the best way. )
Alternatively, with the WaitForSwitch in its own subroutine, insert a gosub WaitForSwitch immediately before the return in your subroutines SW1-SW10. This avoids the spaghetti-like mix of GOTOs and subroutines but (as an old assembly-language programmer) I would probably do what Buzby suggests.

Also, you can change your original set of calls to something like this:
Code:
main:
let b1 = pinsC & %00111111
;************************************************************************************
;                     DATA_DEFINITION FROM SWITCH                                   *
;************************************************************************************
on b1 gosub WaitForSwitch,SW1,SW2,SW3,SW4,SW5,SW6,SW7,SW8,SW9,SW10,_
WaitForSwitch,WaitForSwitch,WaitForSwitch,WaitForSwitch,WaitForSwitch,SW10

Goto main
I'm not sure if you intended SW10 to be called when b1=10 or b1 = $10 (=16). The code above will work for either, because if b1 is in the range 11-15 then it always goes to WaitForSwitch. If you want to handle other values, then you can expand the on b1 gosub section

[Edited to correct list of gosubs]
 
Last edited:

1968neil

Senior Member
Thanks Guys,
I've got my old brain round that now.
I have forty switch positions to deal with in total in total.

Regards
Neil
 
Top