Home     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   

Author Topic: SIN and COS for PIC  (Read 9631 times)

0 Members and 1 Guest are viewing this topic.

Supernova

  • Full Member
  • ***
  • Posts: 149
SIN and COS for PIC
« 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.

Erkeli

  • Guest
Re: SIN and COS for PIC
« Reply #1 on: March 02, 2008, 01:32:52 AM »
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

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: SIN and COS for PIC
« Reply #2 on: March 02, 2008, 02:41:27 AM »
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:


tpone

  • Guest
Re: SIN and COS for PIC
« Reply #3 on: March 02, 2008, 10:27:26 AM »
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?

kostas

  • Guest
Re: SIN and COS for PIC
« Reply #4 on: March 02, 2008, 13:13:47 PM »
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?

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: SIN and COS for PIC
« Reply #5 on: March 02, 2008, 15:02:07 PM »
Multiplication

Code: [Select]
;***************************************************************************
;**  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

Code: [Select]
;***********************************************
;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
;***********************************************

ScubaCap

  • Guest
Re: SIN and COS for PIC
« Reply #6 on: March 02, 2008, 16:12:57 PM »
I believe a page that you will gather all these routines would be a great deal. dont you think so?

tpone

  • Guest
Re: SIN and COS for PIC
« Reply #7 on: March 02, 2008, 16:29:02 PM »
Or a project with a mathematic co-processor?

manley_V

  • Guest
Re: SIN and COS for PIC
« Reply #8 on: March 04, 2008, 23:56:16 PM »
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:



Did anybody check this thing? Does it work?

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: SIN and COS for PIC
« Reply #9 on: March 05, 2008, 08:14:56 AM »
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

Alex

  • Guest
Re: SIN and COS for PIC
« Reply #10 on: March 05, 2008, 19:34:44 PM »
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.

manley_V

  • Guest
Re: SIN and COS for PIC
« Reply #11 on: March 05, 2008, 23:04:00 PM »
Maybe his is the best way.  :(
I really do not like it though...

Alex

  • Guest
Re: SIN and COS for PIC
« Reply #12 on: March 06, 2008, 01:51:05 AM »
Neither do i. But this seems to be the most popular way  >:(

manley_V

  • Guest
Re: SIN and COS for PIC
« Reply #13 on: March 17, 2008, 23:58:41 PM »
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:



Did anybody check this thing? Does it work?

No matter how much i try, i cannot make it work  :-\ :-\ :-\

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: SIN and COS for PIC
« Reply #14 on: March 18, 2008, 21:28:54 PM »
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.