Calculating value with Cos and Sin function on pic16f877


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    12

    Post Calculating value with Cos and Sin function on pic16f877

    Hi everyone,
    I'm working on f877 and want to display cos or sin value of an angle.
    i could not find any examples about cos function but as i read it says that i have to input angle value between 0-255 .
    That means i have to input 63 for 90 degrees.
    If i calculate for 63, LCD displays Cos = 3 and Sin = 127 .
    Or if i do it for 90, it displays Cos = 65460 and Sin = 102 .

    I'd appreciate if anyone could help me with these commands with simple explaination.

    Thansk for your helps.

    MyCodes:

    PORTD = 0
    TRISD = 0

    DEGREE VAR WORD
    COSVALUE VAR WORD
    SINVALUE VAR WORD

    DEFINE LCD_DREG PORTD
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_RSBIT 2
    DEFINE LCD_EREG PORTD
    DEFINE LCD_EBIT 3
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2

    DEGREE = 63
    COSVALUE = COS DEGREE
    lcdout $fe,1
    lcdout #COSVALUE
    pause 2000
    SINVALUE = SIN DEGREE
    lcdout $fe,$c0
    lcdout #SINVALUE

    end

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    The result of sin and cos are twos complement. This is a bit hard to describe. Here it is displayed:

    Name:  twos-complement.PNG
Views: 1618
Size:  5.8 KB

    The sin and cos functions are made for byte variables. If you use word variables, it will throw your numbers WAY off, like you saw in the cos example you shared. You need to change those back to bytes. Other than that, it sounds like you got it, as you are converting degrees to "binary radians" correctly. (degrees * 255/360)

    The results -127 to 127 represent -1 to 1. If you do sin of 270 degrees (270*255/360) = 191 ,sin of 191 would displays an answer of 129 on your LCD. That is actually -127 , or -1.
    http://www.scalerobotics.com

  3. #3
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    thanks scalerobotics,

    i understood the logic, RealCosvalue = Cosvalue/127 . Also i work for angles <90 and it's much more easy to apply proramming

    you are a life saver. it was making me mad thinking about how that function works.

    Have a nice day.

    Quote Originally Posted by scalerobotics View Post
    The result of sin and cos are twos complement. This is a bit hard to describe. Here it is displayed:

    Attachment 4878

    The sin and cos functions are made for byte variables. If you use word variables, it will throw your numbers WAY off, like you saw in the cos example you shared. You need to change those back to bytes. Other than that, it sounds like you got it, as you are converting degrees to "binary radians" correctly. (degrees * 255/360)

    The results -127 to 127 represent -1 to 1. If you do sin of 270 degrees (270*255/360) = 191 ,sin of 191 would displays an answer of 129 on your LCD. That is actually -127 , or -1.

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts