PDA

View Full Version : decimal input on portB



Sancerre
- 7th April 2015, 18:05
Hello from french wine town !

I'm newbie : it's first post here and I work on my second program :)

Thanks to Dogan's books and this forum to show what can be done with pbp.

Today I would like to divide portB inputs in two groups of 4 bits and get the two numbers in order to use them in branch loop.

Help would be appreciated.

have a nice day

Dave
- 7th April 2015, 18:28
Sancerre, I would:
1. read the port of choice into a single BYTE size variable.
2. AND port read variable with $F0 and place into separate variable to separate the upper nibble. (4 bits)
3. AND port read variable with $0F and place into separate variable to separate the lowerr nibble. (4 bits)
There you have it. I am assuming you are talking about BCD when you are talking about decimal?

Tabsoft
- 7th April 2015, 18:39
One way to accomplish this is this way.
This will copy the upper 4 bits of the value of PORTB into a new byte variable in the lower 4 bit positions and will copy the lower 4 bits of the value of PORTB into another byte variable in the lower 4 bit positions.



myPortB var byte
HighNib var byte
LowNib var byte


myPortB = PortB
HighNib = myPortB >> 4 'Right shift myPortB 4 bits, (moves bits 7:4 to bits 3:0 and then set bits 7:4 to 0)
LowNib = myPortB & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

So if PortB = %10010110 '$96
Then
HighNib = %00001001 '$09
LowNib = %00000110 '$06

Sancerre
- 8th April 2015, 07:37
Hi all,

Decimal or Hex ? It's depend of BRANCH syntax. Can I write something like
BRANCH PORTB [dog, cat,fish] ???


For nibbles, I can choose between
initial:
TRISB = 111111
SWVAR VAR BYTE
SWVAR1 VAR BYTE
SWVAR2 VAR BYTE

code1:
SWVAR = PORTB
SWVAR1 = SWVAR & $0F
SWVAR2 = SWVAR & $F0

or code2:
SWVAR1 = SWVAR >> 4 'Right shift myPortB 4 bits, (moves bits 7:4 to bits 3:0 and then set bits 7:4 to 0)
SWVAR2 = SWVAR & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

switches will be active when pulling down... Do I change something in my code ?

Thanks for your time. Have a nice day. Oleg

Tabsoft
- 8th April 2015, 13:08
What version of PBP are you using?

Sancerre
- 8th April 2015, 14:35
Bonjour


What version of PBP are you using?

PBP 2,5.

Have a nice day.

Tabsoft
- 8th April 2015, 15:26
Yes, you could use the BRANCH command as you have described, "BRANCH PORTB [dog, cat,fish]".
That being said, I don't think I would use it that way.

The BRANCH command has the following syntax, "BRANCH Index,[Label{,Label...}]"

One issue in using the BRANCH command this way is that using a byte variable or Port Alias for the "Index" means you can have 256 possible values (0-255).
Which might require you to create 256 Labels and add them to the Label list in the BRANCH command.
The "Index" is just a number pointing to the list of "Labels" in the BRANCH command. E.g. BRANCH Index, [Label_0, Label_1, Label_2, ...]
If Index = 0 then the program will Branch to Label_0, if Index = 1 then the program will Branch to Label_1, etc.

If you are using the value of PORTB, there can be up to 256 different values which would make for one very long Branch command.
"BRANCH Index,[Label_0, Label_1,... Label_255]"

If it were me I would do something like this.



myPortB var byte

myPortB = PORTB

if myPortB = 1 then GOTO Label_1
if myPortB = 2 then GOTO Label_2

etc.

Label_1:
'Do something

Label_2:
'Do something



This gives you the power to pick and choose which values of PORTB you want to act on.

You could also replace the GOTO portion above with GOSUB, which will jump to the Label_x, then at the end of your subroutine (Label_x) the "Return"
command will cause the program to return back to the next instruction after the GOSUB.



if myPortB = 1 then GOSUB Label1
if myPortB = 2 then GOSUB Label2
etc.

Label_1:
'Do something
return

Label_2:
'Do something
return



You could similarly use the Select Case command.

Art
- 9th April 2015, 04:50
This wouldn’t be needed if shift shifts in zero values which I think it does:


SWVAR2 = SWVAR & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

Sancerre
- 9th April 2015, 15:53
Hi all,

thanks for your valuable help. Following your advices, my code can look like :

TRISB = %11111111
TRISA = %00000
SWVAR VAR BYTE
SWVAR1 VAR BYTE
SWVAR2 VAR BYTE

SWVAR = PORTB
SWVAR1 = SWVAR >> 4 'Right shift myPortB 4 bits, (moves bits 7:4 to bits 3:0 and then set bits 7:4 to 0)
SWVAR2 = SWVAR & %00001111 'Mask off bits 7:4 to 0, Only get bits 3:0

IF SWVAR1 <>0 THEN GOTO SUITE

if SWVAR2 = 1 then GOTO sound1
if SWVAR2 = 2 then GOTO sound2
if SWVAR2 = 3 then GOTO sound3
...
ENDIF

SUITE:
if SWVAR1 = 1 then GOTO sound1
if SWVAR1 = 2 then GOTO sound2
if SWVAR1 = 3 then GOTO sound3
...

sound1:
FREQOUT PORTA.0,4000,500

sound2:
FREQOUT PORTA.0,4000,1000

sound3:
FREQOUT PORTA.0,4000,1500

sound4:
FREQOUT PORTA.0,4000,2000

Have a nice day.
Oleg