PDA

View Full Version : Exponential Operator



andrewroz
- 25th October 2007, 22:46
Hello,
Is there a math operator in PicBasic Pro for raising one number to the power of another?
I can't seem to find one.

I would expect it to be:

a = 2^4 ' answer is 16
or EXP()
or POW()

but none of these seem to be documented for PBP.
Thanks
-Andrew

BrianT
- 25th October 2007, 23:51
Microcontrollers have limited resources and generally are not efficient for floating point or exponential operations. PBP is an integer basic that makes good use of limited devices. You can get integer exponential functions by repeated multiplication and with the new LONG variables on PBPL you can probably get reasonable results by scaling but you will need to devise the functions yourself.

Working with floating point (in Mikroelktronica or Proton+) or longs with PBPL will devour code space.

What are you doing that needs exponentials? There may be an alternative solution.

HTH
Brian

andrewroz
- 26th October 2007, 01:34
Thank you for confirming my fear.

I'm using a PIC16F877 to control some MAX7219 LED display chips.
I'm trying to access 6 different rows of LED's in succession.

To light up an LED in a column you do this:
row 1 = %0001
row 2 = %0010
row 3 = %0100
row 4 = %1000
etc.

I just wanted to convert between #4 and %1000 with a single line of code.
Ideally, I wanted to make it a part of the FOR statement in a loop.
You can see below the code I ended up using instead of the invalid: STEP 2^Row

PBP code fragment follows:
********************
Row VAR BYTE
Light1 VAR WORD
Column VAR WORD

FOR Row = 1 to 64 STEP Row*2-row
Light1 = Column + Row
LOW Load
SHIFTOUT DOUT, CLK, 1,[Light1\16] 'send command to LED chip
HIGH Load
NEXT Row
****************

-Andrew

sinoteq
- 26th October 2007, 02:43
Have you looked at the shift functions >> and << around page 32 in the PBP manual?
row 1 = %0001
row 2 = %0010
row 3 = %0100
row 4 = %1000
If you want a byte or word to do the above thing this might work:

X var Byte
Y var Byte

Y=1 'Now Y=%00000001
For x=0 to 6 Step 1
Y=Y << 1 'Shift Y left one time, Y=%00000010,Y=%00000100,Y=%00001000
Next x

This command is also usefull when combining nibbles and other stuff, shift happily left or right as you see fit.

paul borgmeier
- 26th October 2007, 05:30
>> I just wanted to convert between #4 and %1000 with a single line of code.

B0 = DCD 3 ‘ Sets B0 to %00001000

(see PBP manual for full details)

andrewroz
- 26th October 2007, 07:36
Thank you Sinoteq
I checked that out.
I think I can even do this:

Y=1 'Now Y=%00000001
x = 6
Y=Y << x

And thank you Paul,
I'll look into DCD.

By the way, what is PBPL?

-Andrew

MarioC
- 26th October 2007, 08:22
It comes with the new PBP 2.50 upgrade.

PBPL is the new .exe file that lets you use 32 bit variables (only on the 18FXXX series).


You can use negative numbers without having to use two's complement; you can use * + / - operators with 32 bit.

DIG and other instrucctions are "enhanced" by it.

andrewroz
- 30th October 2007, 06:09
Now what I want to do is go backwards from what I did before.

What would be a good equation/function for:

Given a = %00100
b should equal 2


%00001 = 0
%00010 = 1
%00100 = 2
%01000 = 3
%10000 = 4

Dave
- 30th October 2007, 10:33
andrewroz, Use the NCD function. It will give you the value of the most significant bit set.
It's in the manual.....

Dave Purola,
N8NTA

andrewroz
- 30th October 2007, 13:46
Cool,
Thanks Dave.
-Andrew

RodSTAR
- 31st October 2007, 03:53
Thank you Sinoteq
I checked that out.
I think I can even do this:

Y=1 'Now Y=%00000001
x = 6
Y=Y << x

And thank you Paul,
I'll look into DCD.

By the way, what is PBPL?

-Andrew

PBPL = PicBasic Pro Libraries ???

MarioC
- 31st October 2007, 08:00
PBPL it's the name of the compiler that uses long format:

PBPL = PIC Basic Pro LONG