1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Sin table in PIC....

Discussion in 'Microcontrollers' started by koolguy, May 16, 2013.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    HI,

    I am testing the sin function on simulator here is my code it is running but don't showing any value in PORTB register why???

    Code (text):

    #include <htc.h>
    #include <math.h>
    #include <stdio.h>

    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000


    void main (void){
    TRISB=0x00;
    {
    int i, a;
    while(1)
    a = sin(20);
    PORTB=a;
    }
    }
     
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Sometimes I wonder about you..... If sin(20) = 0.9294 radians or 0.34202 degrees... How the hell is an int going to display it...
     
  3. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE
    sin(x) is not in radians nor degrees, it is just a number between -1.0 and 1.0
    Anyway the math.h defines the function as double sin(double); so unless you do a sin of pi/4 you will allways get 0 on the output.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE

    Also, didnt the compile give you some warning like possible loss of precision?
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Point being!! He has variable a as an int!!!
     
    • Like Like x 1
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Just checked it out..... sin() returns radians....

    sin(20) returns.. 0.9131
    sin(30) returns..-0.9880
    sin(45) returns.. 0.8513
    sin(60) returns..-0.3049
     
  8. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,510
    Likes:
    189
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE
    Sin() doesn´t return radians, it allways returns a fraction without any units, but it is true that the parameter is in radians.
    (sinus of an angle is equal to a ratio of two sides in a triangle, so it can´t have any units)
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    What am I on...... Don't know my head from my arse today.....Of course you are correct... Sorry for the argument Kubeek......

    I need a lie down or something
     
  10. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,512
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Hahaha, don't worry about it Ian. I've had days like that ;)

    But yeah, the value that is put into sin() has units of radians or degrees. i.e. sin(35 degrees) or sin(pi/2 radians).

    [MODNOTE]Unhelpfull Post.[/MODNOTE]
     
    Last edited by a moderator: May 17, 2013
    • Like Like x 3
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    The code never reach the line "PORTB=a;" because of this:

    while(1)
    a = sin(20);
     
    • Like Like x 1
  12. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Thanks that what i want to know.....
     
  13. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    That is not what you need to know.
     
    • Like Like x 1
  14. WTP Pepper

    WTP Pepper Active Member

    Joined:
    Jan 24, 2012
    Messages:
    648
    Likes:
    41
    Location:
    UK
    Why can't you just multiply by a scaling factor, converting to a uchar and passing that to the port?
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    yes i will do that..
    but how to make look up table for sin wave 180 degree.....
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Remember the math library I gave you in another post!! This has a sine wave table ( actually cosine, but its the same ).
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Can you explain what r u doing and how to make discrete value?
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    How fast is your sine wave???
     
  19. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
  20. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,049
    Likes:
    961
    Location:
    NJ
    Hello,

    If you already have the sin(a) function you can easily generate it using that. Just multiply by a constant to form the table value. The highest value is 1 and that is at 90 degree, and the lowest value is -1 and that is at 270 degrees (convert degrees to radians as needed) but we usually only need the values for 0 to 90 degrees which returns a float value 0 to 1. So if 0x0FFF is the highest value you need at 90 degrees, then multiply all the return values of sin(a) by 0x0FFF.

    For example in a simplified language:

    for k=0 to 19 do
    a=sin(k/19*pi/4)
    y=sin(a)
    Y=y*0x0FFF
    Table[k]=Y
    end for

    That creates a table that is entered into ram that covers 0 to 90 degrees, and that provides enough values to cover the full range of 0 to 360 degrees.
     
  21. Brian Griffin

    Brian Griffin Member

    Joined:
    May 23, 2013
    Messages:
    49
    Likes:
    8
    Location:
    Kuala Lumpur
    A sine table in a microcontroller is straightforward.

    Since microcontrollers don't read or store decimal numbers, these sine values must be scaled. Precalculate these sine wave values as a microcontroller will have a tough time working on it.

    Then discretize the sine values for one period. Example, for 256-value sine table, sin(x/256*2pi*127) + 127 and rounded. So after getting these 256 values, dump them inside the array. The values should be 0 - 255 and plotting this in excel will get you a sine wave.

    For a controllable synthesis of sine wave, it's advisable to read up on Digital Direct Synthesis (DDS). It is a simple algorithm to generate a sine wave on a microcontroller. However, some filtering is required on the output.

    I made my own sine wave generators (it's for the toy music boxes) all using this method, without involving math.h or whatsoever.
     

Share This Page