PDA

View Full Version : Shuffling variables



ERMEGM
- 29th November 2014, 06:14
OK, spent the better part of the night sorting through threads, but haven't found exactly what I was looking for. The process seems simple, but the pic finds it very complex. Here's what I'm trying to do. I have a large amount of code that pulses 4 outputs. Two outputs vs. the other two outputs. What I would like to do is be able to change the pairings of these outputs (i.e. 1 and 2 vs. 3 and 4, to 1 and 4 vs. 2 and 3 to 1 and 3 vs. 2 and 4).

I've tried using variables, variable on variables, if..then statements, select case and even include files. The pics don't like this. I even looked at Darrel's virtualport code, but unless I'm mistaken, it's only for bits to ports. How can I change the ports, or their assignments around?

Thanks,
Tony

towlerg
- 29th November 2014, 09:47
Could you draw a picture of what you're try to achieve and approx speed of pulses?

George

Amoque
- 29th November 2014, 13:32
Yes, I too think more description, and perhaps, some code would help. For one thing there are many ways to do the same thing and by knowing which path you've taken it might be less obtrusive to follow along rather than reinvent the wheel, so to speak. Also, are you designing - and might have room or to add external circuitry (say a logic gate or mux chip) - or must this be exclusively software?

ERMEGM
- 29th November 2014, 17:09
It must be strictly code. The following code is just a small, simple example of pulsing four outputs.



a var portb.0
b var portb.1
c var portb.2
d var portb.3

action:

for x = 1 to 5

a = 1
b = 1
c = 0
d = 0

pause 200

a = 0
b = 0
c = 0
d = 0

pause 200

next x

for x = 1 to 5

a = 0
b = 0
c = 1
d = 1

pause 200

a = 0
b = 0
c = 0
d = 0

pause 200

next x

goto action


What I want to do with the code is be able to change the assignment of the ports when an external input is received, therefore to be able to change the assignment as follows:



a var portb.3
b var portb.2
c var portb.1
d var portb.0

or


a var portb.0
b var portb.3
c var portb.2
d var portb.1

or


a var portb.0
b var portb.2
c var portb.1
d var portb.3


This is the only part I'm struggling with. I can do the rest of the code, but to change the assignment, I am having great difficulty.

As I said, I tried using if..then statements, select case, the above variables won't work once they've been set. I even tried using other variable to substitute for the port aliases. My setup didn't like it. To me it looks like it should work, but the pic might not see it that way.

Don't judge where the returns are and missing code. That was done just so you know that they didn't flow into each other. The way it works is that when action is run, it runs through its code continuously until a button is pressed. Once pressed, it jumps to an on..gosub command where it advances the pairing change option by one. When the pairs have been reassigned, it goes back to action and runs the code with the new pairing assignment.




a var portb.0
b var portb.1
c var portb.2
d var portb.3
e var word
f var word
g var word
h var word

pair1:
' 1 and 2 vs. 3 and 4

e = a
f = b
g = c
h = d

return ' return to pairing change

pair2:
' 1 and 4 vs. 2 and 3

e = a
f = d
g = b
h = c

return ' return to pairing change

action:
' alternating flash

for x = 1 to 5

e = 1
f = 1
g = 0
h = 0

pause 200

e = 0
f = 0
g = 0
h = 0

pause 200

next x

for x = 1 to 5

e = 0
f = 0
g = 1
h = 1

pause 200

e = 0
f = 0
g = 0
h = 0

pause 200

next x


Does any of this make any sense and am I at all on the right track or at least headed in the right direction or did I hit a dead end?

towlerg
- 29th November 2014, 21:24
I'm not sure that helped. All I got from that was at different times the mapping between the bit variables A,B,C and D and the physical bits 0 thro 3 of PortB varies according to some rule.
Is that correct?

George

rsocor01
- 29th November 2014, 21:44
This is the only part I'm struggling with. I can do the rest of the code, but to change the assignment, I am having great difficulty.

Well, you are having great difficulty because you cannot reassign ports like that in the program. Ports are only assigned to variables at the beginning of the program.

richard
- 29th November 2014, 21:49
Does any of this make any sense and am I at all on the right track or at least headed in the right direction or did I hit a dead end?

looks like a dead end to me . try this way
code is for a 12f1822 so uses porta and porta.3 is input only . but you get the idea


'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/11/2014 *
'* Version : 1.0 *
'* Notes : *
'* : pic12f1822 *
'************************************************* ***************
#CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_ON
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
__CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
__CONFIG _CONFIG2, cfg2
#ENDCONFIG

osccon=$6a '4 mhz
anselA=0
trisa=000

a var byte
b var byte
c var byte
d var byte
pin_mode var byte
x var byte

a=0
b=1
c=2
d=4
pin_mode=0

action:
for x = 1 to 5
lata.0[a] = 1
lata.0[b] = 1
lata.0[c]= 0
lata.0[d]= 0
pause 200
lata.0[a] = 0
lata.0[b] = 0
lata.0[c]= 0
lata.0[d]= 0
pause 200
next x
for x = 1 to 5
lata.0[a] = 0
lata.0[b] = 0
lata.0[c]= 1
lata.0[d]= 1
pause 200
lata.0[a] = 0
lata.0[b] = 0
lata.0[c]= 0
lata.0[d]= 0
pause 200
next x
gosub shuffle
goto action

shuffle :
pin_mode=pin_mode+1
select case pin_mode
case 1
a=4 ;a var portb.3
b=2 ;b var portb.2
c=1 ;c var portb.1
d=0 ;d var portb.0
case 2
a=0 ;a var portb.0
b=4 ;b var portb.3
c=2 ;c var portb.2
d=1 ;d var portb.1
case 3
a=0 ;a var portb.0
b=2 ;b var portb.2
c=1 ;c var portb.1
d=4 ;d var portb.3
case 4
a=0 ;a var portb.0
b=1 ;b var portb.1
c=2 ;c var portb.2
d=4 ;d var portb.3
pin_mode=0
end select
return

richard
- 29th November 2014, 23:16
another approach

'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/11/2014 *
'* Version : 1.0 *
'* Notes : *
'* : pic12f1822 *
'************************************************* ***************
#CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_ON
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
__CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
__CONFIG _CONFIG2, cfg2
#ENDCONFIG

osccon=$6a '4 mhz
anselA=0
trisa=000

a var byte
b var byte
c var byte
d var byte
pin_mode var byte
x var byte
all_off var byte

pins_on var byte





a=1 ;portb.0
b=2 ;portb.1
c=4 ;portb.2
d=16 ;portb.4
pin_mode=0

all_off=!(a+b+c+d)

lata = lata & all_off
action:
pins_on=a+b
for x = 1 to 5
lata = lata | pins_on


pause 200
lata = lata & all_off

pause 200
next x
pins_on=c+d
for x = 1 to 5
lata = lata | pins_on
pause 200
lata = lata & all_off
pause 200
next x
gosub shuffle
goto action

shuffle :
pin_mode=pin_mode+1
select case pin_mode
case 1
a=16 ;a var portb.3
b=4 ;b var portb.2
c=2 ;c var portb.1
d=1 ;d var portb.0
case 2
a=1 ;a var portb.0
b=16 ;b var portb.3
c=4 ;c var portb.2
d=2 ;d var portb.1
case 3
a=1 ;a var portb.0
b=2 ;b var portb.2
c=2 ;c var portb.1
d=16 ;d var portb.3
case 4
a=1 ;a var portb.0
b=2 ;b var portb.1
c=4 ;c var portb.2
d=16 ;d var portb.3
pin_mode=0
end select
return

SUNFLOWER
- 30th November 2014, 00:23
It is possible to use variables to name ports then change variable values to change ports. copied from my program --

http://www.harbornet.com/sunflower/PCB.pbp


' The PORTL and PORTH aliases are used for the PIN numbers (0-15)

' PORTL is 0-7 and PORTH is 8-15.

' Look in the .bas and .bal files for the chip you are using.

' If it's a 18F4620 then open c:\pbp\18F4620.bas


' PORTL VAR PORTB

' PORTH VAR PORTD

' TRISL VAR TRISB

' TRISH VAR TRISD

'
' PORTB AND PORTD can now be referenced by variables.

' http://www.picbasic.co.uk/forum/showthread.php?t=14493&

'

The default, I believe, is PORTB AND PORTC. The above changes in .bas and .bal changed that to ports B and D. The link explains in more detail

SUNFLOWER
- 30th November 2014, 01:10
if azMotor then

azMotor_A = m1_ina '14 PORTD.6 ' Digital I/O PORT D.0 - D.7 NUMBERED 8 - 15

azMotor_B = m1_inb '13 PORTD.5

else

azMotor_A = m1_inb ' Software will reverse wires if motor or sensor installed backwards. 0 to 7 = PORTB.0 to PORTB.7

azMotor_B = m1_ina ' Configured in Install subroutine. Used in Set_Motors HBridge subroutine. 8 to 15 = PORTD.0 to PORTD.7

endif

low azmotor_a ' Brake azimuth motor

low azmotor_b

richard
- 30th November 2014, 05:24
sunflower that's very interesting , but I suspect it may fail if attempted on latx registers which can still leave you exposed to rmw issues . has anybody tried it on latx ?
I could be wrong but most pbp "functions" find the tris reg by using a fixed offset from the port reg

ERMEGM
- 30th November 2014, 06:35
Well, you are having great difficulty because you cannot reassign ports like that in the program. Ports are only assigned to variables at the beginning of the program.
Yes I know this. I was having difficulty because I was trying to think of a way to do it in a manner that the pic would understand. Our brains can easily process this and substitute that values, but with the pic, it's not that easy.


looks like a dead end to me . try this way
code is for a 12f1822 so uses porta and porta.3 is input only . but you get the idea


'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/11/2014 *
'* Version : 1.0 *
'* Notes : *
'* : pic12f1822 *
'************************************************* ***************
#CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_ON
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
__CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
__CONFIG _CONFIG2, cfg2
#ENDCONFIG

osccon=$6a '4 mhz
anselA=0
trisa=000

a var byte
b var byte
c var byte
d var byte
pin_mode var byte
x var byte

a=0
b=1
c=2
d=4
pin_mode=0

action:
for x = 1 to 5
lata.0[a] = 1
lata.0[b] = 1
lata.0[c]= 0
lata.0[d]= 0
pause 200
lata.0[a] = 0
lata.0[b] = 0
lata.0[c]= 0
lata.0[d]= 0
pause 200
next x
for x = 1 to 5
lata.0[a] = 0
lata.0[b] = 0
lata.0[c]= 1
lata.0[d]= 1
pause 200
lata.0[a] = 0
lata.0[b] = 0
lata.0[c]= 0
lata.0[d]= 0
pause 200
next x
gosub shuffle
goto action

shuffle :
pin_mode=pin_mode+1
select case pin_mode
case 1
a=4 ;a var portb.3
b=2 ;b var portb.2
c=1 ;c var portb.1
d=0 ;d var portb.0
case 2
a=0 ;a var portb.0
b=4 ;b var portb.3
c=2 ;c var portb.2
d=1 ;d var portb.1
case 3
a=0 ;a var portb.0
b=2 ;b var portb.2
c=1 ;c var portb.1
d=4 ;d var portb.3
case 4
a=0 ;a var portb.0
b=1 ;b var portb.1
c=2 ;c var portb.2
d=4 ;d var portb.3
pin_mode=0
end select
return

That's exactly what I was envisioning in my head, but I couldn't get it out. Thank you Richard. I'll try to plug this into my code and see how it works.

richard
- 30th November 2014, 10:29
up until now I never found a use for "swap" this makes even smaller and more elegant code


'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/11/2014 *
'* Version : 1.0 *
'* Notes : *
'* : pic12f1822 *
'************************************************* ***************
#CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_ON
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
__CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
__CONFIG _CONFIG2, cfg2
#ENDCONFIG

osccon=$6a '4 mhz
anselA=0
trisa=000

a var byte
b var byte
c var byte
d var byte
pin_mode var bit
x var byte
all_off var byte

pins_on var byte





a=1 ;portb.0
b=2 ;portb.1
c=4 ;portb.2
d=16 ;portb.4
pin_mode=0

all_off=!(a+b+c+d)

lata = lata & all_off
action:
pins_on=a+b
for x = 1 to 5
lata = lata | pins_on


pause 200
lata = lata & all_off

pause 200
next x
pins_on=c+d
for x = 1 to 5
lata = lata | pins_on
pause 200
lata = lata & all_off
pause 200
next x
gosub shuffle
goto action

shuffle :
pin_mode =!pin_mode
if pin_mode then
swap a,d
swap b,c
else
swap a ,c
swap b ,d
endif





return

richard
- 30th November 2014, 11:25
shuffle wrong this is better


shuffle :
pin_mode =!pin_mode
if pin_mode then
swap a,d
swap b,c
else
swap b ,d
swap c ,d
endif

Amoque
- 30th November 2014, 12:13
...this is better...

Better? I'd say it's AWESOME! I had thought to suggest (as your original answer) using array values or various mask patterns perhaps, but - to my humble skills - there is simply no improvement possible to "swap". Somewhere, in the back of my mind, I remember seeing it, but had forgotten... I'll remember now!

ERMEGM
- 2nd December 2014, 04:30
That's awesome, however, I have one question. What is (=!)? Can you please translate to English what the line "pin_mode =!pin_mode" means. That's the only part I'm having a problem with right now. The good book references it at !=, but that is used when you have two expressions, not the same expression. The same expression will always be equal to itself.

Thanks,
Tony

richard
- 2nd December 2014, 05:05
! is the logical not operator afaik , anyway that's the way I use it (its a C thing but works in pbp too)

I think its the same as the "tilde" ~ but my laptop keyboard makes it hard to find. maybe someone can confirm this


pin_mode = ! (pin_mode) is the way to read it , if pin_mode is 1 then ! pin_mode becomes 0 or if pin_mode is 0 then ! pin_mode becomes 1
not to be confused with != which is not equal to

ps
just tried it . seems "~ ! not" are all the same thing (on bit and byte vars anyway) for what it worth

Amoque
- 2nd December 2014, 11:54
In binary, where there are two states, "NOT" or any variation thereof (~ ! etc) reverses the bit value, so... NOT 1 = 0. It makes sense on a bit level... if it's NOT a zero, it must be a one, yes? Now, of course if it is not, not a one, then it was all along a zero. In comedy, the "double negative" can lead to all sorts of Don Knott's style of humor - in computer science... not so much.