PDA

View Full Version : Math for numbers > 65535?



Scottacus
- 23rd December 2003, 17:55
I am a hobbyist who new to PIC BASIC and have written a little beginner program that displays values from a pot to an LCD screen using the "pot" command. I have put a subroutine in that displays the ohm values when a switch is pressed and had a question about the math. Since the "pot" command displays from 0 to 255 and the rating of the pot is 10k ohms I divided the two to get each increment equal to 39.21 ohms. I would like to use three significant figures so I changed 39.21 to 392 and divided the result by 10 but the issue is that a word sized variable can only go to 65,535 and I need 100,000. I realize that I could scale back on the numbers (i.e. 196 instead of 392 and divide by 5 rather then 10) to stay in range. Is there a way of getting a variable of greater than word sized 16 bits (i.e. 32bits) with PIC BASIC?

Thanks in advance,
Scott

Darrel Taylor
- 28th December 2003, 09:31
Hi Scott,

As long as a 31 bit number will do, there is a way to deal with larger numbers with PBP. Multiplying 2 word sized variables together gives a 32-bit result. You can't use the result directly, but if you immediately follow it with a DIV32 statement you can work with some pretty big numbers.


PotValue Var byte
Dummy Var Word
W1 Var Word

Dummy = 39216 ' 10,000/255 * 1000 rounded
W1 = PotValue ' Copy to a Word variable
Dummy = Dummy * W1 ' 32-bit result is in PBP system vars
Dummy = Div32 1000 ' divide 32 bit result by 1000
' ** Dummy now holds the resistance value. **
10000/255 = 39.21568627    So round that to 3 decimal places and multiply by 1000 = 39216

Now multiply that number times the POT value (0-255)
39216 * 255 = 10,000,080  This is the largest number, so it fits easily in 31-bits.

Then in the very next statement divide by 1000 with the Div32
10,000,080 / 1000 = 10,000 integer

Granted with only 256 steps, accuracy is probably not very important, but it should do the job.

For more info on the Div32 operator, see page 33 in the PicBasic manual.

Another way to approach it might be to use the A/D convertor to read the Pot's position. This would increase the resolution to 10-bit or 1024 steps = 9.775 ohms per step.

Best regards,
   Darrel

Scottacus
- 29th December 2003, 15:44
Dear Darrel,

Thank you very much for your reply. Your help is greatly appreciated!

Scott Miller

Darrel Taylor
- 30th December 2003, 09:43
My pleasure Scott,

It's kinda slow around here, got any more probs?

Anyone?

Hello -- ello -- llo -- lo

Echo -- echo -- cho

:)

mat janssen
- 30th December 2003, 14:21
Darrel,
Yes there is not so much to do here. Because PBP is so good and there are not so much problems to make solutions.
I use PBP for 5 years now and every time I update and upto now everything works within a few hours.
I made several applications with PIC's. form cable checkers to parrallel to serial convertors to fully automatical controlled dual fuel engines with data collection for after tests to look with a labtop if everything went good. Al programs are written in PBP (2.44) for PIC and the applications for the pc I wrote in Qbasic.
So data communication is also no problem.
I think its a to good compiler !!
Greetings and happy new year
Mat