Variable input

Hi all,

It might be a simple question but I can’t find the option/syntax anywhere. Or maybe it isn’t even possible. Anyway…

I want to read an input with a variable input number instead of reading a input directly by it’s address. So instead of doing this;

Value = IN0

I want to do something like this:

PinNr con 0 
Value = IN PinNr

I’m working on the terrain adaption and want to be able to update one leg at the time. So I’ve got a function like “update legâ€

I don’t think you can get the value of an I/O pin like that. :frowning: Wonder if you can read in a port and then just mask off the bits (pins) you’re not interested in. I don’t know how to do that either. lol I know, I’m no help…

Use lookup

lookup pin,[in0,in1,in2,in3,in4,in5,in6,in7],value

lookup returns the value of the expression at the index specified.

If you want something faster then use the byte and word pin variables.

mypins var word
mypin var bit
mypins = INS ;INS is pin states of p0 to p15 IIRC
mypin=mypins.bit0(pinnumber)

You may be able to do this to cut out a step:

mypin = INS.bit0(pinnumber) ;.bit0 points to the first bit, (array) treats it like an array of bits.

You can also use any of the other IN variables in a similar fashion.

Also while I am at it. Here are the old style IN variables and NEW STYLE which is in Basic Micro Studio. OUT and DIR variables follow the same usage.

//old style
INE Pins 0-31
INS Pins 0-15
INES Pins 16-31
INL Pins 0-7
INH Pins 8-15
INEL Pins 16-23
INEH Pins 24-31
INA Pins 0-3
INB Pins 4-7
INC Pins 8-11
IND Pins 12-15
INEA Pins 16-19
INEB Pins 20-23
INEC Pins 24-27
INED Pins 28-31

//new style
INL0 Pins 0-31
INL1 Pins 32-63

INW0 Pins 0-15
INW1 Pins 16-31
INW2 Pins 32-47
INW3 Pins 48-63

INB0 Pins 0-7
INB1 Pins 8-15
INB2 Pins 16-23
INB3 Pins 24-31
INB4 Pins 32-39
INB5 Pins 40-47
INB6 Pins 48-55
INB7 Pins 56-63

INN0 Pins 0-3
INN1 Pins 4-7
INN2 Pins 8-11
INN3 Pins 12-15
INN4 Pins 16-19
INN5 Pins 20-23
INN6 Pins 24-27
INN7 Pins 28-31
INN8 Pins 32-35
INN9 Pins 36-39
INNA Pins 40-43
INNB Pins 44-47
INNC Pins 48-51
INND Pins 52-55
INNE Pins 56-59
INNF Pins 60-63

IN0 Pin 0
IN1 Pin 1
IN2 Pin 2
IN3 Pin 3
IN4 Pin 4
IN5 Pin 5
IN6 Pin 6
IN7 Pin 7
IN8 Pin 8
IN9 Pin 9
IN10 Pin 10
IN11 Pin 11
IN12 Pin 12
IN13 Pin 13
IN14 Pin 14
IN15 Pin 15
IN16 Pin 16
IN17 Pin 17
IN18 Pin 18
IN19 Pin 19
IN20 Pin 20
IN21 Pin 21
IN22 Pin 22
IN23 Pin 23
IN24 Pin 24
IN25 Pin 25
IN26 Pin 26
IN27 Pin 27
IN28 Pin 28
IN29 Pin 29
IN30 Pin 30
IN31 Pin 31
IN32 Pin 32
IN33 Pin 33
IN34 Pin 34
IN35 Pin 35
IN36 Pin 36
IN37 Pin 37
IN38 Pin 38
IN39 Pin 39
IN40 Pin 40
IN41 Pin 41
IN42 Pin 42
IN43 Pin 43
IN44 Pin 44
IN45 Pin 45
IN46 Pin 46
IN47 Pin 47
IN48 Pin 48
IN49 Pin 49
IN50 Pin 50
IN51 Pin 51
IN52 Pin 52
IN53 Pin 53
IN54 Pin 54
IN55 Pin 55
IN56 Pin 56
IN57 Pin 57
IN58 Pin 58
IN59 Pin 59
IN60 Pin 60
IN61 Pin 61
IN62 Pin 62
IN63 Pin 63

Hi,

Thanks for the quick response.

I’ve tried option 1 with the lookup function:

[code]ButA var bit
PinToRead con 12

loop:
lookup PinToRead, [IN0,IN1,IN2,IN3,IN4,IN5,IN6,IN7,IN8,IN9,IN10,IN11,IN12,IN13,IN14,IN15], ButA

if ButA=0 then
sound P9, [40\1000]
endif
goto loop
[/code]
This works perfectly!

I’ve also tried the second suggestion but I can’t get the array to work. This is the test program:

[code]ButA var bit
PinToRead con 12

FSRPin var byte
mypins var word

loop:
butA = mypins.bit0(PinToRead)

if ButA=0 then
sound P9, [40\1000]
endif
goto loop[/code]
No reaction on input 12. No idea what I’m doing wrong here…

Thanks again!

Xan

Hi Xan,

Not sure about the option #2 as it may depend on which way the array stuff works. Also not sure of assigning it to a bit value. If the array works you may need to do something like:

 ButA = (mypins.bit0(PinToRead)) <> 0

If it were me and I did not want to get tricky and use assembly language using the internal port table to grab it, I would try something like:

ButA = (INE & (1 <<PinToRead)) <> 0

Kurt

Hi Kurt,

Thanks for your reply.

I can’t get both of your suggestions to work. LOL
Did you test them or is it just a sketch? :wink:

Xan

Hi Xan,

Sorry I did not actually test them. :blush: :laughing:

So you shamed me into trying them out:

First Nathans approach worked! :wink: And actually so did both of mine! 8)

The problem was your test program:

[code]ButA var bit
PinToRead con 12

FSRPin var byte
mypins var word

loop:
mypins = INS ; You need to get the state of the IOs :slight_smile:
butA = mypins.bit0(PinToRead)

if ButA=0 then
sound P9, [40\1000]
endif
goto loop[/code]

Kurt

Talking about shame… :blush:

The only thing is that I made a failure in the post. I did read the inputs only not INS but INE.

And you know what the silly thing is, It still doesn’t work :s
Even fully copy/pasting you’re code doesn’t work. Tried INS and INE this time.

Maybe it has something to do with the IDE? I’m using the ‘Old’ 8.0.1.7 version.

I’ve tried another BAP just to be sure. I had some hardware failures in the future that cost me to much time. :stuck_out_tongue:

Xan

When I was using the “IN” command for my IR sensors, it went from working to not working without changing any code. One day it worked and one day it didn’t.

To fix the unresponsive input pin, I had to uninstall the IDE and do a fresh install and wala! it fixed the problem. I would uninstall and reinstall your IDE to see if it fixes the problem. IF it doesn’t fix the problem, at least you can rule out the IDE issue.

If I remember correctly, My IDE was 8.0.1.7 also.

Hi Xan,

You also have the option to do it with this :confused: simple function:

[code]ButA var byte
PinToRead con 4

FSRPin var byte
mypins var word
Sound P9,[100\4400,80\3800,60\3200]

loop:
;mypins = INS
;butA = mypins.bit0(PinToRead)
;butA = (mypins.bit0(PinToRead) <> 0)
;ButA = ((INE & (1 <<PinToRead)) <> 0 )
gosub GetPinValue[PinToRead], ButA

if ButA=0 then 
  sound P9, [40\1000] 
endif 

goto loop

pinstate var byte
pinnum var byte

GetPinValue[pinnum]:
xor.l er0, er0
mov.b @PINNUM:16, r0l ; get our pinnumber
and.b #0x3f, r0l ;
shll.b r0l ; each element is 2 bytes in length
mov.w @(_PORTTABLE:16, er0), r2 ; OK R2 now has the two bytes high being the bit, and low being the port…
mov.b r2h, r3l ; Ok r3l now has the pin number
mov.b #0xff, r2h ; setup 32 bit address to port register PDRx

; make sure it is setup for input
mov.b	@(0x70-0xD0, er2), r0l	; get the current value for PCRx byte from the shadow location
bclr.b	r3l, r0l				; set the appropriate bit for our port
mov.b	r0l, @(0x70-0xD0, er2)	; save away the updated value to the shadow location
mov.b	r0l, @(0x10, er2)		; save away in the actual PCRx byte

bset.b	r3l, r0l			; set the appropriate bit for our port
mov.b	@er2, r0h			; now r0h has the 8 bit input value for the port associated with our pin.
and.b	r0l, r0h			; we issolate off our IO pit
add.b	#0xff, r0h			; will set carry if the bit was set.
subx.b	r0h, r0h			; should return 0 or FF
mov.b	r0h, @PINSTATE:16		; the state of the pin

return pinstate				; go back to basic and return the state.	
[/code]

Your millage may vary!
Kurt

Xan, are you using Studio or an older IDE? Lookup should be only a little slower than the other methods so if it works I’d recommend sticking with it for now. Kurts asm code is also an option.

Also, if you would cut and paste your code that isn’t working again, I’d appriciate it.

Hi All,

Aaarghh… I always hate those issues. I’m always looking in my code if I did something wrong. If the IDE or hardware fails this costs so much time…

Thanks for your “small” function! :stuck_out_tongue:
I’ll check the cost (in time) between both lookup and the ASM functions. And see what I’m going to use. The up side from using ASM is that it is most of the time pretty fast. Down side is that it is not easy to read for not “ASM guru’s” like you. :wink:

I’m using the older IDE. Version 8.0.1.7 to be precise. Does the 2nd method only work in the new Studio?

Sure:

[code]ButA var bit
PinToRead con 12

FSRPin var byte
mypins var word

loop:
mypins = INS ;Tried INE as well
butA = mypins.bit0(PinToRead)

if ButA=0 then
sound P9, [40\1000]
endif
goto loop[/code]
Thanks for looking at it!

Xan

Hi Xan,

I would go ahead and move up to Studio. As far as I know there are no known issues. We will be verifying our existing tutorials work with Studio as time permits till they are all moved to it.

I can’t garuntee the IN/OUT/DIR variables would work using this method in the older IDE. I made several changes to that system in 8.0.X.X. I would also recommend upgrading(or atleast trying out) Studio. I currently don’t have any major bug reports that would affect your code.