PDA

View Full Version : How do you convert an algorithm to code?



boroko
- 29th March 2009, 00:25
HI all,

After overworking the few functional brain cells that I have left. I was able to come up with a formula in Excel that accomplishes getting measurement numbers from a 16 bit timer into a value from 1 to 1000 for use in a Power Control PWM module.
The 16 bit upper and lower limits can be changed to adjust the PWM to different situations.

Now, I'm at a bit of a blank stare about how to convert it into code. Anyone think of search terms that might get me started on my homework?

formula: ((Echo-Min)/(Max-Min))*1000
Echo= captured value from 16 bit TMR1
Min= low setting for "0" PWM, (can be changed ) 16 bit
Max= high setting for 100% PWM (can also be changed) 16 bit

Thanks
Bo

HYETİK
- 29th March 2009, 11:05
If you have an algorithm,you can write it in PicBasic easily. Decide what you need how you do processes and use logic comparison statement (if then etc.)
and create neccessary labels to minimize your code.

Pic Basic Forum community will help you, if you start writing your code;)

timmers
- 29th March 2009, 18:16
How about..

RESULT VAR WORD
ECHO VAR WORD
MIN VAR WORD
MAX VAR WORD

RESULT = ((ECHO - MIN) / (MAX - MIN)) * 1000

Its hardly rocket science.

Tim.

BigWumpus
- 29th March 2009, 23:00
RESULT = ((ECHO - MIN) / (MAX - MIN)) * 1000


I think, it won't work:

(ECHO - MIN) / (MAX - MIN) will be between 0 and 1 and will be truncated to 0.

Try:

ZW VAR WORD
ZW=MAX-MIN:RESULT= (ECHO-MIN)*1000:RESULT=DIV32 ZW

That will work with 16 Bit-Variables and don't use to big numbers.

Bruce
- 29th March 2009, 23:37
MIN & MAX are PBP keywords. You'll want to rename these.

boroko
- 30th March 2009, 01:27
Thanks for all of the input. After working on it a while, I ran into the same problems that you guys pointed out. Here is where I ended up. It required LONGs for me to get it to work right. When I started to look at it, I had no idea how to do it. Why is it that some projects seem to make you need every function or process that you are new at?


<font color="#000000"><b>DEFINE </b>OSC 8
<b>DEFINE </b>DEBUG_REG PORTA
<b>DEFINE </b>DEBUG_BIT 0
<b>DEFINE </b>DEBUG_BAUD 2400
<b>DEFINE </b>DEBUG_MODE 0

TRISA = %00100000 <font color="#0000FF"><i>' PortA : All outputs except RA.1 for INT_INT
</i></font>TRISB = %01000100 <font color="#0000FF"><i>' PWM1,3,5 outputs,RB2=INT, RB0,2,3: DIO
</i></font>ADCON1 = %00001111 <font color="#0000FF"><i>' Set up ADCON1
</i></font>OSCCON = %11111110 <font color="#0000FF"><i>' INTOSC, 8MHz
</i></font>EchoTime <b>VAR WORD
</b>Term1 <b>VAR WORD
</b>Term2 <b>VAR WORD
</b>Term3 <b>VAR LONG </b><font color="#0000FF"><i>' making this LONG solved the problem
</i></font>Duty0 <b>VAR WORD
</b>Near0 <b>VAR WORD
</b>Far0 <b>VAR WORD
</b>EchoTime = 28750 <font color="#0000FF"><i>' test value for the measurement
</i></font>Near0 = 6500 <font color="#0000FF"><i>' test setting for the minimum reading
</i></font>Far0 = 51000 <font color="#0000FF"><i>' test setting for the maximum
</i></font><b>DEBUG </b><font color="#FF0000">&quot;1330-2l&quot;</font>,13,10 <font color="#0000FF"><i>' splash

</i></font>Main:
Term1 = Far0 - Near0 <font color="#0000FF"><i>'51000 - 6500 = 44500
</i></font>Term2 = EchoTime-Near0 <font color="#0000FF"><i>'25500 - 6500 = 19000
</i></font>Term3 = Term2*1000 <font color="#0000FF"><i>'19000 * 1000 = 19000000
</i></font>Duty0 = Term3/Term1 <font color="#0000FF"><i>'19000000/44500 = 426.996
' ' Word won't work: divisor&gt;32767, Need Long

' Duty0 = (EchoTime-Near0)/(Far0-Near0)*1000
</i></font><b>DEBUG </b>#Term1,<font color="#FF0000">&quot; &quot;</font>,#Term2,<font color="#FF0000">&quot; &quot;</font>,#Duty0, 13,10
<b>PAUSE </b>700
<b>GOTO </b>Main

Thank you all for looking at it.
Bo

BigWumpus
- 30th March 2009, 22:34
Ähhh....

What's about the DIV32-command ?

boroko
- 1st April 2009, 02:22
Thought about the DIV32 way, but I hadn't done either before and figured that the LONGs were the newest, so that was where I would concentrate my efforts unless pointed otherwise.

I'm sure it would work, but I had to have a reason to justify my recent upgrade :-)

Bo