Sin


Closed Thread
Results 1 to 4 of 4

Thread: Sin

  1. #1
    AndyP's Avatar
    AndyP Guest

    Question Sin

    Does anyone have any example code or have a good explanation of how to use the SIN operator? In the manual it says that SIN starts with a value in binary radians as opposed to normal degrees. I have done a few debug tests and have got quite weird results.

    e.g
    SIN 0 =0
    SIN 255=65533
    SIN 127=3

    It says that the output is from -127 to 127 in two's compliment. Does that mean that -127 would be represented as 65408?

    Regards,

    Andy Peaple

  2. #2
    Join Date
    Jun 2005
    Location
    Surrey, England
    Posts
    35


    Did you find this post helpful? Yes | No

    Default Sin and Cos

    I realise the question was asked several years ago - I came across it on a search - the problem with SIN() and COS() in PICBASIC is that they return a twos complement number. Also they deal with "binary radians" (not the same as trig radians) where 0 is 0 degrees/radians and 255 is 360 degrees or 2PI radians.
    Here is my solution to displaying a barchart of the sine wave function 128+sin(i)
    (using http://www.picbasic.co.uk/forum/showthread.php?t=2359 to display):

    Value var word 'must use word variable for BARGRAPH
    twoC var byte 'must use byte variable
    include "LCDbar_INC.bas"
    'BARgraph Value, Row, Col, Width, Range, Style

    '........... I use in a loop and increment i etc............

    twoC = sin(i)
    if (twoc & %10000000)=0 then PosS 'else negative 2's complement
    ' value = 127-((twoc ^ %11111111) +1) 'restore negative value
    value = 126 - (twoc ^ %11111111) 'I wanted a 128 offset
    goto ShowS
    PosS: value = 128 + twoc
    ShowS:
    @ BARgraph _Value, 1, 0, 16, 256, lines

    '............ Fiddle/pause and go round the loop..........

    Peter Finch

  3. #3
    Join Date
    Apr 2006
    Location
    Alberta Canada
    Posts
    166


    Did you find this post helpful? Yes | No

    Default

    here is a link to a website i found a while ago, that helps with a lot of the math. the site was written for a basic stamp, but it all works out the same.

    http://www.emesystems.com/BS2math3.htm#arcCos

    and here has a bunch of other math functions for the stamp along with explaining some of the other functions that can be done with it..

    http://www.emesystems.com/BS2index.htm
    Last edited by dragons_fire; - 16th December 2007 at 16:04.

  4. #4


    Did you find this post helpful? Yes | No

    Default SIN example code

    Hello,
    The code below was used in an incubator. The user could set the mean temperature and the range. The day was divided into 96 fifteen minute segments and at 6 AM and 6 PM the temperature was set to the user commanded mean temperature. the "range" is a sinusoidal function that adds or subtracts from the mean. For example a mean of 24 with a range of 10 would cause the incubator to be at 24 degrees at 6 AM and 6 PM. At noon the temperature would be 24 + 10 = 34 and at midnight the temperature would be 24 - 10 = 14 Celsius.


    CalculateSetPoint:
    read 0, meantemp
    read 1, range
    ' SetPoint = 10* (meantemp + range * sine (timeslot * 255/96)) with allowances
    ' for the sign and integer truncation plus an 18 hour time shift.

    w = (255 * timeslot/96) ' 0 to 255 in 2.667 chunks
    b = sin w ' 0 to 127 in 2's complement
    if b.7 = 1 then negative
    positive:
    x = 10*meantemp + (20*b*range/255)
    goto timeslotdone
    negative:
    b = (b ^ %11111111) + 1 ' 2's complement to unsigned integer
    x = 10*meantemp - (20*b*range/255)
    TimeSlotDone:
    setpoint = x

    HTH
    BrianT

Similar Threads

  1. Cordic trig assembly code for PIC18f
    By ScaleRobotics in forum mel PIC BASIC Pro
    Replies: 54
    Last Post: - 8th September 2015, 05:36
  2. Simple Maths Going Wrong
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 43
    Last Post: - 27th May 2010, 09:01
  3. Can't send a Reserved Word to math coprocessor
    By bluesmoke in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th October 2009, 23:44
  4. How to simulate in EXCEL ?
    By Johan in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th June 2007, 18:08
  5. Sinus calculating !
    By Don Mario in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 28th November 2004, 23:56

Members who have read this thread : 1

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

Posting Permissions

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