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
 

1968neil

Senior Member
Further on from this is am now back on this little project.....
I have yet to use tables and was wondering if i could do the following easily and what would be the best route / method ?
I am trying to add a Picaxe in between the switch and the PLL Input of an old radio and change the resulting switch position data to the PLL based upon a table

1 Store the Switch Position Data as a know quantity (we already have this in the above posts)
2 Using a single switch select a different table to output to the PLL.

Any input much appreciated
Rgds
Neil
 

hippy

Technical Support
Staff member
Sounds like it should be fairly easy. It seems to me you are reading a 6-bit value which indicates which position the switch is in as a BCD $00 to $39 value and you need to select a value to output depending on switch position and another switch which chooses between two settings.

Code:
Symbol SELECT_SWITCH = PinB.1

PowerOnReset:
  lastSwitch = -1

MainLoop:
  Do
    thisSwitch = PinsC & %00111111
    thisSwitch = SELECT_SWITCH * $80 | thisSwitch 
    If thisSwitch <> lastSwitch Then
      lastSwitch = thisSwitch
      ReadTable thisSwitch, outputValue
      Gosub OutputTheOutputValue
    End If
  Loop
Then your Table can have two entries for the input position and whether the select switch is 0 or 1, moved into the msb of the switch value -

Code:
;      s xxyyyy
Table %00100101, (?) ; Select = 0, Position = 25
Table %10100101, (?) ; Select = 1, Position = 25
If your select switch comes in on a 'C.x' pin it's even easier as you don't need that additional SELECT_SWITCH, can simply change the value you mask PinsC with.
 

1968neil

Senior Member
Sounds like it should be fairly easy. It seems to me you are reading a 6-bit value which indicates which position the switch is in as a BCD $00 to $39 value and you need to select a value to output depending on switch position and another switch which chooses between two settings.

Code:
Symbol SELECT_SWITCH = PinB.1

PowerOnReset:
  lastSwitch = -1

MainLoop:
  Do
    thisSwitch = PinsC & %00111111
    thisSwitch = SELECT_SWITCH * $80 | thisSwitch
    If thisSwitch <> lastSwitch Then
      lastSwitch = thisSwitch
      ReadTable thisSwitch, outputValue
      Gosub OutputTheOutputValue
    End If
  Loop
Then your Table can have two entries for the input position and whether the select switch is 0 or 1, moved into the msb of the switch value -

Code:
;      s xxyyyy
Table %00100101, (?) ; Select = 0, Position = 25
Table %10100101, (?) ; Select = 1, Position = 25
If your select switch comes in on a 'C.x' pin it's even easier as you don't need that additional SELECT_SWITCH, can simply change the value you mask PinsC with.
Thanks Hippy,

I'll have play with that over the upcoming Bank Holiday, Hopefully i can get my old brain round it :)
 
Top