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

Sine table code in c

Discussion in 'Microcontrollers' started by be80be, Mar 11, 2018.

  1. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    I was working on a sine table I have the sine table.
    so if I bit bang this no pwm or timer
    I would basically run a loop 256 times
    using the table as the on time and off time am a right or not even close?


    Code (text):

    uint8_t  sine_wave[256] = {
      0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
      0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
      0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
      0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
      0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
      0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4,
      0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC,
      0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
      0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD,
      0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6,
      0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB,
      0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC,
      0xDA, 0xD8, 0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xC9,
      0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3,
      0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C,
      0x99, 0x96, 0x93, 0x90, 0x8C, 0x89, 0x86, 0x83,
      0x80, 0x7D, 0x7A, 0x77, 0x74, 0x70, 0x6D, 0x6A,
      0x67, 0x64, 0x61, 0x5E, 0x5B, 0x58, 0x55, 0x52,
      0x4F, 0x4D, 0x4A, 0x47, 0x44, 0x41, 0x3F, 0x3C,
      0x39, 0x37, 0x34, 0x32, 0x2F, 0x2D, 0x2B, 0x28,
      0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18,
      0x16, 0x15, 0x13, 0x11, 0x10, 0x0F, 0x0D, 0x0C,
      0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
      0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
      0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
      0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A,
      0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x13, 0x15,
      0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24,
      0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37,
      0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D,
      0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64,
      0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D
    };
     
     
  2. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    7,545
    Likes:
    161
    I don't think that's right. Isn't that just a list of amplitudes? Shouldn't you be pre-calculating the on and off-times? So that If the list is [On1, Off1, On2, Off2, ....] where On_n+Off_n = your period, for all n.

    Or I guess you could just have them be just a list of all on (on1, on2, on3, ...) times or off times (off1, off2, off3...)and subtract it from the period on the fly to calculate the other half (Off1 = Period - On1) or (On1 = Period - Off1).
     
    • Like Like x 1
  3. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    I found lots about doing this with pwm the chip I was playing with don't have pwm I was just trying to get a sine wave I got part of one the down side slope looks good on a scope the upside looks like a clift LOL that was with a pwm and low pass filter.
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    How many spare pins have you got?

    I used three pins... A DAC and a filter and my sine was pretty good
    32 entries in the sine table for one cycle
     

    Attached Files:

  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    I got lots of pins lol could you share your code I'm just playing with my scope and how to make sine wave.

    I just tried with the chip I had hooked up it can't hold the table I'm think about a 24f it has a dac I think 10 bits
     
  7. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    You wont need the code.

    As it was three bit dac ( LOL three bits ) table was simple...

    char sinetable[] = {7,7,7,7,6,6,6,5,5,4,3,2,2,1,1,1,0,0,0,0,1,1,1,2,2,3,4,5,5,6,6,6};

    Oh yeah!! That was about 1khz... a delay of 100us between values so the filter was 1k and 0.1uf
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    So it hooks up like this
    Screenshot from 2018-03-12 10-48-06.png
     
  10. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    Yes... I have shown the circuit above.. BUT!! with a slower sine, ie less than 100hz , the filter doesn't do it justice.. The steps are too big!!
     
  11. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,615
    Likes:
    697
    Location:
    Derbyshire, UK
    I don't think the 24F generally has DAC (at least not of any decent resolution), but it's simple to use PWM as a DAC, and most PIC's have at least one PWM channel - what PIC are you using that doesn't have PWM?.

    I've recently used PWM for the contrast setting on an LCD module, just added a 1K resistor and 10uF as a lowpass filter.
     
  12. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    • Informative Informative x 1
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    Changed chips im not getting a nice sine wave don't think the cap is right I can't read them LOL
    I tried 3 two looked better these caps writing is so small hard to see whats what.
     
    Last edited: Mar 12, 2018
  14. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    Sounds like you ran out of RAM. Try putting it in program memory.

    Mike.
     
    • Like Like x 1
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    I was going to try that Mike but I gave up I'm still learning xc8 how would i put
    char sine[] = {7,7,7,7,6,6,6,5,5,4,3,2,2,1,1,1,0,0,0,0,1,1,1,2,2,3,4,5,5,6,6,6};
    In program memory the chip has lots of that but just 72 bytes of data tho.
     
  16. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    I'm try this out next
    Screenshot from 2018-03-12 22-42-50.png

    going to feed the 3 pins to the op amp
     
  17. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    Well I tried that not too good lol
    sine.jpg

    I cleaned it up a bit Ian looks nice now Thanks
    now to work on the next part LOL
    2018-03-13-014741.jpg
     
    Last edited: Mar 12, 2018
  18. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    To put the table in program memory just add "const" to the begining of char sinetable[]..
     
  19. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    I assume you've already worked out that to put your data in flash you just proceed it with const.

    const char sine[] = {7,7,7,7,6,6,6,5,5,4,3,2,2,1,1,1,0,0,0,0,1,1,1,2,2,3,4,5,5,6,6,6};

    Mike.
     
  20. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    I need some kind of Language Reference sheet I can do good if I can bring up and look at it.
    I can't think like i did 8 year's ago my doctor is slowly killing my brain with meds.
     
  21. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,802
    Likes:
    958
    Location:
    Rochdale UK
    With C compilers, its best to write you own stuff or derive it from working code... Libraries shipped with compilers are too generic and bloat code... I think it makes it harder to understand IFDEF this and IFDEF that... Gets on my left testicle...

    As for age.. Well I can't see my bench to well, let alone things on it!! I have now 3 different pairs of specs.. All with different focal lengths... All three are on my head so I can swap them out.. I look like a right nutjob..

    My shortest focal glasses have little magnifying lenses built into the already magnified lenses... Time to give up or what!!
     
    • Like Like x 1
  22. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,198
    Likes:
    159
    Location:
    morristown,tn
    Na i never give up hell i wear 3 pairs of glasses one over the next lol.
    My big peoblem is out of habit im good it nothing gets moved hell i can wire a house and cant even see the wire copper but i do look it over with glasses on
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice