Calculating value with Cos and Sin function on pic16f877


Closed Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    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: 1727
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

  2. #2
    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