PDA

View Full Version : There has to be an easier way!



Christopher4187
- 27th May 2006, 01:19
I don't have much experience with the lookup command and I don't understand what the manual states. I will post the code here and someone let me know if I can clean it up please.

B1=8
B2=6
B3=11
B4=15

'I NEED TO TAKE THESE INTO BINARY, BUT I CAN'T FIGURE OUT HOW TO DO IT. WITHIN B1, I NEED R1, R2, R3 AND R4 TO EQUAL THE BINARY OF B1, LIKE THIS

'THE OUTPUT OF B1 LOOKS LIKE THIS:
R1=0
R2=0
R3=0
R4=0

'THE OUTPUT OF B2 LOOKS LIKE THIS:
R5=0
R6=1
R7=1
R8=0

'AND SO ON BUT THE PROBLEM IS THAT I HAVE TO SCAN 16 DIFFERENT SIGNALS AND IF THERE WAS A WAY TO SAY SOMETHING LIKE

B1,B2,B3,B4=R1 THAT WOULD BE GREAT. SO, R1 EQUALS 8 AND B1=0, B2=0, B3=0 AND B4=1. IS THIS POSSIBLE?




'I HAVE TO WRITE MY CODE LIKE THIS

IF B1=1 THEN

Christopher4187
- 27th May 2006, 01:25
I re-read what I wrote and it may be a bit confusing. Here is the short story:

b=7 now I need to extrapolate to binary


r1=1
r2=1
r3=1
r4=0


If it's still not clear, let me know.

mister_e
- 27th May 2006, 01:58
maybe i don't understand what you need but lets assume
B1= 11110000 bin => F0 hex => 240 decimal
B2=00001111 bin => 0F hex => 15 decimal

even if you define
B1=$F0
OR
B1=%11110000
OR
B1=240

It do the same thing.

IF B1=$F0 then....
IF b1=%11110000 then ....
IF B1=240 then .....

Maybe i need to sleep, or it's really what you need ;)

mister_e
- 27th May 2006, 02:00
HONK! i think i got it.

Let's say B1=%11110000

R0 should be Bit 0 of B1, R7 should be Bit7 of B1... i'm i right?
If so...
R0=B1.0
R1=B1.1
R2=B1.2
R3=B1.3
..
...
R7=B1.7

Christopher4187
- 27th May 2006, 02:08
Steve,

You solved another one of my problems! Yes, to answer your question, I want to have a decimal number and convert that to binary. In that binary number, take each bit and it represents another number. So, within one number I extract 8 different boolean numbers. I'll try your suggestion but I do think it will work.

Thanks,

Chris

Christopher4187
- 27th May 2006, 02:12
I see one small issue.


For example, I know that b1=9 which is 00001011.

If b1=9 (the decimal number), how can I let PBP know that it is actually 00001011 (the binary number).

Or, can I simply go

R0=B1.0
R1=B1.1
R2=B1.2
R3=B1.3

BobK
- 27th May 2006, 03:10
Hi Christopher,

Decimal 9 in binary is %00001001 not 00001011.

Binary representation is from right to left meaning the digit on the far right is the first is 1 then 2 then 4 then 8 then 16 then 32 then 64 then 128.

I have been reading your posts and just don't quite get what it is you are trying to do.

"I don't have much experience with the lookup command and I don't understand what the manual states. I will post the code here and someone let me know if I can clean it up please.

B1=8
B2=6
B3=11
B4=15

B1 in binary is %00001000
B2 in binary is %00000110
B3 in binary is %00001011
B4 in binary is %00001111

Hope this helps!

BobK

SteveB
- 27th May 2006, 03:16
Chris,
I think you are missing the point of exactly how PB and the PIC store any variable. When a variable is stored, it is stored as a binary number in a byte-wide file register.
Simple example:


B1 VAR BYTE
B1 = 172

Now, this is oversimplified, but this is what is happening. The compiler :
1)Allocates a register in memory for B1
2)Generates Assemby code to Store "10101100" in that register
3)Remembers that any time you reference that variable again, it will actually be referencing the allocated register, and the value therein.


Now, lets add some more code:

R0 VAR BIT
R1 VAR BIT
'...
R7 VAR BIT

R0 = B1.0
R1 = B1.1
'...
R7 = B1.7
Now the compiler looks for a specific bit in the file register for B1, and assigns the value of that bit to the variable.

That Help?

Steve

Christopher4187
- 27th May 2006, 11:30
Actually, Steve (Mister e) understood what I was referring to. Here is the long story. I have a 16F870 monitoring 16 inputs and I am sending the status of all inputs wirelessly to a 18F4550. Then, the 18F4550 outputs the status through USB into VB6. Anyway, because of my limitation in PBP, I can't send word variables wirelessly so I am sending 4 bytes which covers the 16 inputs. On the transmitter side, my code looks like this...it's a portion of the code...and don't forget, it may not be efficient because I am not very proficient in PBP.

if PORTA.5=0 THEN
B1=0
ELSE
B1=1
ENDIF
if PORTA.2=0 THEN
B2=0
ELSE
B2=1
ENDIF
if PORTA.1=0 THEN
B3=0
ELSE
B3=1
ENDIF
if PORTA.0=0 THEN
B4=0
ELSE
B4=1
ENDIF
C1=(B1*1)+(B2*2)+(B3*4)+(B4*8)

This was the easy part. One the receiver side, it looks like this:

in1=c1.0
in2=c1.1
in3=c1.2
in4=c1.3

And then from here it sends the data to VB6. I wasn't aware that PBP stored the numbers as binary and that is where my confusion was. When sending the numbers over the data link, it is simply sent as (C1=9) or (C1=15), not (c1=%00001011). Furthermore, I never knew that you can use a c1.0 or c1.3 to take a portion of the number.

Previously, I was sending 4 bytes for each specific number so it was taking 96 bytes (when you include the start and end bit) to update all 16 inputs. Now, it only takes 6 bytes to send that data.

Thanks to all who helped.

Chris

Acetronics2
- 27th May 2006, 12:37
if PORTA.5=0 THEN
B1=0
ELSE
B1=1
ENDIF
if PORTA.2=0 THEN
B2=0
ELSE
B2=1
ENDIF
if PORTA.1=0 THEN
B3=0
ELSE
B3=1
ENDIF
if PORTA.0=0 THEN
B4=0
ELSE
B4=1
ENDIF
C1=(B1*1)+(B2*2)+(B3*4)+(B4*8)

Chris

> Hi, Christopher

> Let's do :

> C1 = PORTA & % 00100111
> C1.3 = C1.5 : C1.5 = 0
> C1 = C1 REV 4

That's all !!!

See manual $ 4.10 and 4.17.11 ...

Alain

paul borgmeier
- 27th May 2006, 23:13
Another Option - although more lines of code, this compiles to less code space -

temp var byte
C1 var byte

C1=0 ; set C1 to 0
temp=PORTA ; in case PORTA changes while “processing”
C1.0=temp.5
C1.1=temp.2
C1.2=temp.1
C1.3=temp.0

Paul Borgmeier
Salt Lake City, Utah
USA

paul borgmeier
- 27th May 2006, 23:30
or this

temp var byte
C1 var byte

C1=0 ; set C1 to 0
temp=PORTA ; in case PORTA changes while “processing”
if temp.5=1 then C1.0=1
if temp.2=1 then C1.1=1
if temp.1=1 then C1.2=1
if temp.0=1 then C1.3=1

Paul