PCB Heaven
General Category => Digital discussion => Topic started by: Supernova on March 01, 2008, 23:58:39 PM

Is it really possible? I have manage to some other math functions like multiply and div but i cannot generate these numbers and so i cannot work very good with triangles.
Basically, i need to convert a length to angle. This length is one side of a normal triangle. And i need to calculate the angle that the other side is raised.
Is it possible.

you may have a trigonometric table for some degrees that you are about to use and find every time the closest one.
I know this is a crappy method and without precision at all but it's a good start i would say! ;D

well, i know this Taylor's series. I have not test it yet on a pic but it works. The accuracy of the result depends on the amount of calculations you make on the series. Try some on a PC based simulation and you will see how many calculations fits for your need.
In general, the series goes like:
(http://a330.g.akamai.net/7/330/2540/20011017142225/www.edn.com/articles/images/EDN/20011025/24di1q1.gif)

This might be a stupid question but i really do not get it. How can someone do a division with pic and how can raise a power of a number?

Actually, a raise power is the number multiplied by itself
A multiplication for example 21 X 15 is number 21 added 15 times (21+21+21+21.....+21)
A division is a little bit harder. I do several divisions by 2 until i get 0 or 1. If result is 0, then the division has an even number otherwise there is a carry. I do not know any other way to calculate the carry with more precise.
Tip: A division by 2 can be achieved by simply rotate right the bits of a number! This is also possible for division by 4 (2 rotates) by 8 (3 rotates) by 16 (4 rotates) etc etc etc.
If you rotate left, you get a multiplication. Cool eh?

Multiplication
;***************************************************************************
;** time efficient multiplication 8 bit x 8 bit = 16 bit (unsigned)
;**
;** company: elektronik 21 GmbH
;** programmer: Martin Schaefer (idea from Andrew Warren)
;**
;** execution time: fixed 38 cycles (with jump in and jump out) !!!
;** code length: 35 words
;** multiplier: w
;** multiplicand: resultlo
;** result: resulthi:resultlo
;***************************************************************************
MUL8X8 CODE
Mul8x8 ;* 2 cycles for call  instruction
GLOBAL Mul8x8, resulthi, resultlo
mult MACRO
btfsc STATUS,C
addwf resulthi,F
rrf resulthi,F
rrf resultlo,F
ENDM
clrf resulthi ;* 1 cycle
rrf resultlo,F ;* 1 cycle
mult ;* 4 cycles
mult ;* 4 cycles
mult ;* 4 cycles
mult ;* 4 cycles
mult ;* 4 cycles
mult ;* 4 cycles
mult ;* 4 cycles
mult ;* 4 cycles
retlw 0 ;* 2 cycles
;***************************************************************************
Division
;***********************************************
;Floating point division of two unsigned integer
;8 bit variables
;
;Input: AARGB0  dividend (nominator)
; BARGB0  divisor (denominator)
;Output:
; AEXP, AARGB0, AARGB1  quotient
; (MICROCHIP format  AN575)
; w = 0 on success
; w = 1 error: divide by zero
;Temporaries:
; BARGB1
;
;RAM  5 bytes
;ROM  41 words
;Speed  7 + 4 + 8 * 9 + 4 + 14 * 23 + 4 = 413 instruction
; cycles worst case (including call and return)
;***********************************************
fdiv24_8_8
movfw BARGB0
skpnz
retlw 1 ;divide by zero
clrf AEXP
movfw AARGB0
skpnz
retlw 0 ;zero result
;loop to use all 8 bits of dividend (integer 8 by 8 divide)
movwf AARGB1
clrf BARGB1
movlw 0x08
movwf AARGB0 ;aargb0 is used as loop counter
fdiv24_8_8b
rlf AARGB1, f
rlf BARGB1, f
movfw BARGB0
subwf BARGB1, w
skpnc
movwf BARGB1
decfsz AARGB0, f
goto fdiv24_8_8b
rlf AARGB1, f ;aargb1 is the integer quotient so far
;loop to fill all the bits of 16bit mantissa
clrf AARGB0
movlw 0x8E
movwf AEXP
clrc
fdiv24_8_8c
rlf BARGB1, f
movfw BARGB0
skpc ;check carry (9th bit)
goto fdiv24_8_8d
subwf BARGB1, f
setc
goto fdiv24_8_8e
fdiv24_8_8d
subwf BARGB1, w
skpnc
movwf BARGB1
fdiv24_8_8e
rlf AARGB1, f
rlf AARGB0, f
decf AEXP, f
btfss AARGB0, 7
goto fdiv24_8_8c
bcf AARGB0, 7 ;replace explicit msb with sign
retlw 0
;***********************************************

I believe a page that you will gather all these routines would be a great deal. dont you think so?

Or a project with a mathematic coprocessor?

well, i know this Taylor's series. I have not test it yet on a pic but it works. The accuracy of the result depends on the amount of calculations you make on the series. Try some on a PC based simulation and you will see how many calculations fits for your need.
In general, the series goes like:
(http://a330.g.akamai.net/7/330/2540/20011017142225/www.edn.com/articles/images/EDN/20011025/24di1q1.gif)
Did anybody check this thing? Does it work?

I am still testing it but i do not get any good results yet. I also check the cordic method.
http://www.dspguru.com/info/faqs/cordic.htm (http://www.dspguru.com/info/faqs/cordic.htm)

All around i checked and noone uses anything like this for finding trigonometric numers for Pics. They use a predetermined array of numbers. I do not know exactly what as i was not looking for this, but no one refers to series.

Maybe his is the best way. :(
I really do not like it though...

Neither do i. But this seems to be the most popular way >:(

well, i know this Taylor's series. I have not test it yet on a pic but it works. The accuracy of the result depends on the amount of calculations you make on the series. Try some on a PC based simulation and you will see how many calculations fits for your need.
In general, the series goes like:
(http://a330.g.akamai.net/7/330/2540/20011017142225/www.edn.com/articles/images/EDN/20011025/24di1q1.gif)
Did anybody check this thing? Does it work?
No matter how much i try, i cannot make it work :\ :\ :\

Maybe you are right. I just have no time at the moment to check it. Sorry. I promise i will return with a better answer than a raw formula.