Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

math function pic

Status
Not open for further replies.

neelam29

New Member
hi friends
can anyone please help me out to how to wrtite the code (asm ) for solving this math function of finding out the square root of three for a pic microcontroller.
i want to find out the volatges between two phases in which i have to use the equation- [(va + vb)/2] *square root of 3, where va and vb are two voltages of each phase.
 
What are va and vb? 8 bit unsigned? What precision do you need?

The simplest method would be to use a math library that provides 16 or 32 bit multiplication, then multiply by sqrt(3) in fixed point format, which would be a constant you would precalculate.

Here is psuedocode for a 16.16 bit fixed point version, which would require 32 bit multiplication and shift routines.

Code:
tmp = va + vb
tmp = tmp >> 1 //Shift right by one, which divides by 2
tmp = tmp * 113511 //Multiply by sqrt(3) in 16.16 fixed point
tmp = tmp >> 16 //Shift right by sixteen to convert from 16.16 to 32 bit
In place of that last step you could also just access the most significant 16 bits directly.

You need to provide more information on precision in and out if you want help with the actual asm.

Dan East
 
neelam29 said:
hi friends
can anyone please help me out to how to wrtite the code (asm ) for solving this math function of finding out the square root of three for a pic microcontroller.
i want to find out the volatges between two phases in which i have to use the equation- [(va + vb)/2] *square root of 3, where va and vb are two voltages of each phase.

I'm confused. The sq root of 3 is a constant and you would not calculate it with code.
There are many routines out there for sq root and cube root on variables though, they usually do successive iterations and then test if the iterations are producing a different end number yet.
You need to determine what the largest numbers you'll encounter are and what accuracy you need. For example the sq root of 3 is 1.73205 but only 1 in integer math. But if you multiply 3x100 then take sq root, you get an extra decimal place and get 17 which is probably a more useful result.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top