PDA

View Full Version : Math question...



atomski
- 11th June 2004, 07:14
Hi all,

I need to represent a 3 digit decimal number spanning
from 000-777 (e.g. 023) like a 9-bit binary word consisting
of 3 x 3 bytes (e.g. %000-010-011). Is there an easy way
of converting this using PBP math? Thanks in advance.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Dwayne
- 11th June 2004, 15:41
Hello Atomsky

Atomsky>>I need to represent a 3 digit decimal number spanning
from 000-777 (e.g. 023) like a 9-bit binary word consisting
of 3 x 3 bytes (e.g. %000-010-011). Is there an easy way
of converting this using PBP math? Thanks in advance.<<

Yes, there is a way...

First you use the "Dig" command... this will "Dig out each individual number...

Number var byte
DigNumber var byte
counter var byte
BinNumber var byte

For counter=2 to 0 step -1

DigNumber=Number Dig counter '(I don't have the book in front
of me, so syntax may be wrong)
' Now DigNumber is the first number you want.
' We now take that Dignumber and turn it into Binary

'I am not sure if this will work...you will have to see, but i saw this
'littel shortcut...it may not take a "assigned" value
'If you are ambitious, use a case statement and use up code <g>
'case DigNumber=1 BinNumber = DCD 1
'case DigNumber=2 BinNumber = DCD 2
'etc... <g>.

BinNumber = DCD DigNumber


LCDout BinNumber
Next counter


This is untested code, and I am not very diversed with PBP compiler commands. I have not learned all the syntex of the language yet...I am am *sure* there are probably easier ways to do such things... or a more "Proper" way.

Dwayne

Dwayne
- 11th June 2004, 16:17
Hello Atomski,

First of all... Please forgive me for misspelling your name on the last post...I did not mean to make that error...

Here is some code that will switch Your Dig number to a binary...
Remember, I am *NOT* a PBP pro <g> I do not know the language that well, Now if you put me in Borlands c/c++ compilers...You are in my field...<g>

But I think the following should compile without much changing at all.



BinNumber=0;
Power=100;
for (BinCounter=2;BinCounter>=0;BinCounter--)
{
if((BinCounter*2)<=DigNumber)
{
DigNumber=DigNumber-(BinCounter*2);
BinNumber=BinNumber+((BinCounter*2)* Power);
BinNumber=BinNumber;
}
Power=Power/10;
}
if (DigNumber>0)BinNumber=BinNumber+1;


This equates to PBP code as the following::




BinNumber=0;
Power=100;
for BinCounter=2 to 0 stop -1)

if((BinCounter*2)<=DigNumber)
{
DigNumber=DigNumber-(BinCounter*2);
BinNumber=BinNumber+((BinCounter*2)* Power);
BinNumber=BinNumber;
}
Power=Power/10;
Next BinCounter

if (DigNumber>0)BinNumber=BinNumber+1;



The Entire code will look somehting like this....



Number var byte
DigNumber var byte
counter var byte
BinNumber var byte
BinCounter var byte
Power Var byte

For counter=2 to 0 step -1
DigNumber=Number Dig counter 'Dig first number out

**************
'One way of doing it..if it works
BinNumber = DCD DigNumber '
*************************
'Another way of doing it...
BinNumber=0;
Power=100;
for BinCounter=2 to 0 stop -1)

if((BinCounter*2)<=DigNumber)
{
DigNumber=DigNumber-(BinCounter*2);
BinNumber=BinNumber+((BinCounter*2)* Power);
BinNumber=BinNumber;
}
Power=Power/10;
Next BinCounter

if (DigNumber>0)BinNumber=BinNumber+1;
*************************************
'Another way of doing it..with case statements...
'although I saw someone use a case statement here in th forum
'I did a quick search and didn't find one one the first try...
'look it up (if PBP has one) it will go somthing like...

Switch (DigNumber)
case 0: BinNumber = DCD DigNumber
break
case 1 : BinNumber = DCD DigNumber
break
case 2 : BinNumber = DCD DigNumber
break
.....
.....
case 7:BinNumber = DCD DigNumber

LCDout BinNumber


***************************************
'if you want zero's (front zeros printed) check length of 'BinNumber and add leading zeros.

LCDout BinNumber
LCDout "-"
Next counter

atomski
- 12th June 2004, 16:26
Hello Dwayne,

Hey no problem for the typo, either way is fine with me. My nickname is not related to sky or ski it's just a word in my language that would translate into nuclear or radio-active man ;)

BTW the examples you gave me look pretty complex in syntax and I'm not sure I'll be able to cope with 'em. Here's what I've done and it gave me something to be happy about, but not quite... I've converted an OCTAL number 023 into a decimal number 19 using the following formula:

W = Octal_Num DIG 0 *(8^2) + Octal_Num DIG 1 *(8^1) + Octal_Num DIG 2 *(8^0)

hoping to be able to send it out with the following code:

Loop:
Dec_Num = W
FOR Bit_Counter = 0 TO 8
IF Dec_Num.8 = 1 THEN
GOSUB Send_One
ELSE
GOSUB Send_Zero
ENDIF
Dec_Num = Dec_Num << 1
NEXT Bit_Counter
GOTO Loop

Everything works just fine if my formula yealds a full 9-bit binary number with MSB 1, however any preceding zeroes get automatically discarted and my loop gets uncomlete binary number (non 9-bit) and yealds gaps. Any ideas how to get around this and get a full 9-bit binary number no matter if it's all zeroes and just one 1 (LSB)? Thanks.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

anj
- 13th June 2004, 23:29
Gday Atomski
If you will never go above 777, you can use a little trick by adding 1000, to ensure you always get yr leading zeroes for the DIG cmd

Numsend = 17
Numsend = 1000 + 17 ' ie now you have 1017 and DIG gets the zero in pos 2 no probs

output = %1 ' so you will always have a 1 in MSB
output = ( output << 3 ) + numsend dig 2 ' the leading zero
' ie %1000 + %000 = %1000
output = ( output << 3 ) + numsend dig 1 ' the 1
' ie %1000000 + %001 = %1000001
output = ( output << 3 ) + numsend dig 0 ' the 7
' ie %1000001000 + %111 = %1000001111

output shld now be in yr required format but will always have 10bits
dont process the MSB bit and yr away
Andrew

atomski
- 14th June 2004, 07:10
anj,

Thanks for the tip! Great idea using 10-bits and processing just 9. The simplest ideas are usually the best :) I'll give it a try and let you guys know how it works.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Dwayne
- 14th June 2004, 14:50
Hello atomsky,


atomski>>BTW the examples you gave me look pretty complex in syntax and I'm not sure I'll be able to cope with 'em. Here's what I've done and it gave me something to be happy about, but not quite... I've converted an OCTAL number 023 into a decimal number 19 using the following formula:

W = Octal_Num DIG 0 *(8^2) + Octal_Num DIG 1 *(8^1) + Octal_Num DIG 2 *(8^0)
<<

Great one!... Didn't know PBP has power functions... I looked for it a few minutes, ... couldn't find it. <g>.

I wish I knew more of the language of PBP, but...I think in time I will get used to it. Sometimes it is a pain in the 6 Oclock.. Some things that should be simple, are not....

For example (maybe you could help me with this one...

IF (ABS(But1-1000))<50 Then But3=1 Else But3=0 EndIf

compiler says syntax error..

I have tried (it seems) every combination of parenthesis, but it still says syntax error. You know why?


Looks like ANJ answered your other question...

Great seeing ya..

Hey, do you run 20 meters up there in the 14.2 area? Its been a while since I have chatted to Yugoslavia <g>

Dwayne

atomski
- 15th June 2004, 08:26
Guys, I've done some experementing and here's how the my final code snippet looks like after correcting some bugs:

Dec_Num = Octal_Num DIG 2 *(8*8) + Octal_Num DIG 1 *(8*1) + Octal_Num DIG 0 'Converts octal to decimal no.

Bin_Num = %1000000000 + Dec_Num 'gives 10-bit result
W = Bin_Num 'temporary holds 10-bit binary no.
Loop:
FOR Bit_Counter = 1 TO 9 'process just 9-bits starting with
IF Bin_Num.8 = 1 THEN 'bit 8 MSB and discart MSB bit 9
GOSUB Send_One 'jump to subroutine for sending 1's
ELSE
GOSUB Send_Zero 'jump to subroutine for sending 0's
ENDIF
Bin_Num = Bin_Num << 1
NEXT Bit_Counter
Bit_Counter = 0
Bin_Num = W 'restores 10-bit binary value
GOTO Loop

PS Dwayne, I think you can't go that way. You first have to do all your math, store results into variables and then use IF... THEN...ELSE statements.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Melanie
- 15th June 2004, 11:15
DecimalNum=((OctalNum DIG 2)*64)+((OctalNum DIG 1)*8)+(OctalNum DIG 0)

Sometimes it's fun just hanging around to watch the pain....

oh... and if you now wish to transmit three sets of three Binary digits with leading zero's (in the form 000-000-000 - per your original post) then do the same thing...

SEROut BIN3(DecimalNum DIG 2),"-",BIN3(DecimalNum DIG 1),"-",BIN3(DecimalNum DIG 0)

Melanie

Melanie
- 15th June 2004, 12:03
Dwayne...

---snip---

For example (maybe you could help me with this one...

IF (ABS(But1-1000))<50 Then But3=1 Else But3=0 EndIf

compiler says syntax error..

I have tried (it seems) every combination of parenthesis, but it still says syntax error. You know why?

---snip---

Two things here...

Firstly, endif is a command all on it's own, to be used when you break up an if-then-else statement onto separate lines...

IF (ABS(But1-1000))<50 Then
But3=1
Else
But3=0
EndIf

Also, you have an unnescessary set of brackets... try...

IF ABS(But1-1000)<50 Then

Finally, If it still errors, make sure you have correctly defined But1 and But3.

There's no code overhead penalty in using multiple lines within if-then-else statements and it makes code much more readable.

Melanie

atomski
- 15th June 2004, 13:06
Mel, thanks for the tip. I guess it must be fun watching the fish fry, huh? :D Sending the data out is a different matter since I have to use exact timing of 7446uS for each 1 and 0. Also, I need to send 11 + 3 + 9 bits where the first 11 bits change according to another formula, the 3 following bits are fixed to %100, followed by the 9-bit word we just mentioned. So using Serout is out of the question, but thanks for the thought anyway, I appreciate it.

--
Sincerest regards,

YZ7REA Vladimir M Skrbic
4N7ATV Repeater Administrator
YU7GHZ Radio Club President

Dwayne
- 15th June 2004, 14:28
Hello Melanie!


Melanie>>
Firstly, endif is a command all on it's own, to be used when you break up an if-then-else statement onto separate lines...

IF (ABS(But1-1000))<50 Then
But3=1
Else
But3=0
EndIf

Also, you have an unnescessary set of brackets... try...

IF ABS(But1-1000)<50 Then

Finally, If it still errors, make sure you have correctly defined But1 and But3.
<<

Thanks a million... you helped me solve the problem... I had to break down every little statement onto separate lines, and then it finally compiled through... It now looks like the following...

I just love fighing these little parannahs... <g>. I guess that is part of the learning curve of a different compiler...


IF ((ABS(But1-1000))<50) Then
But3=1
Else
But3=0
EndIF

IF ABS(But1-600)<50 Then
But2=1
Else
But2=0
EndIF

IF ABS(But1-300)<50 Then
But1=1
Else
But1=0
EndIF

IF(But3=1 AND StickyButton1=0) Then
StickyButton1=1
Else
IF(But3=1 AND STickyButton1=1) Then
StickyButton1=0
EndIF
EndIF

anj
- 15th June 2004, 21:55
Gday Mel
Just saw your use of the BIN prefix so went to the book for a squizz. Atomski is sending data MSB to LSB in blocks of three, and order/pacing of bits is important.
With the serout/serin qualifiers, i understand they will fill a buffer with a set no of bits from the incoming stream, and then format as reqd. But does it fill as MSB to LSB or LSB to MSB? Ie what order are the bits actually transmitted in. I cant find that in the book anywhere yet?
Andrew

Melanie
- 15th June 2004, 22:03
Hi Andrew

BIN produces bits MSB to LSB.

Example...

MyVar=3
LCDOut BIN5 MyVar

LCD displays 00011

Melanie