1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Electronics Computing SINE

Discussion in 'Modding' started by Kipman725, 19 Jun 2007.

  1. Kipman725

    Kipman725 When did I get a custom title!?!

    Joined:
    1 Nov 2004
    Posts:
    1,753
    Likes Received:
    0
    Hello I have need to have a PIC16F84 output values of sine multiplied by 255 on it's 8bit output ports so that they can be put into a DAC and used as a function generator. Programing it in assembler achiving square and triangle waves was quite easy but sine waves are proving most difficult.
    So far I have found the cordic algorithm which is frankly a bit beyod my level of maths ability to figure out how to impliment and the taylor series (which works kind of eraticly) I also looked into just bunging a table of 256 values into the memory at the start of the program. This however feels like I would be cheating slightly, although it would save computation time.
    Anyone know a bettter solution?
     
  2. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    Creating a table it a standard and straightforward method.

    It's a regularly used method since you can program a table into a standalone EEPROM and it'll fire out the answers without any computation or processor.

    Note: Beware of table boundaries if you choose to implement a table by manipulating the program counter. Make sure you correctly modify the high byte of the program counter!
     
  3. Macaba

    Macaba What's a Dremel?

    Joined:
    4 Mar 2006
    Posts:
    107
    Likes Received:
    1
    Using a lookup table is a standard method, don't feel like you are cheating!

    If you need help with the lookup table code, don't hesitate to ask. With all the different ways I've implemented lookup tables, I'm quite a little guru on LUT's.
     
  4. Moriquendi

    Moriquendi Bit Tech Biker

    Joined:
    3 Nov 2005
    Posts:
    1,691
    Likes Received:
    58
    I think cheating is the whole point in programming, its one of the reasons I enjoy it. At the end of the day it dosnt matter how it works as long as it does work.

    Moriquendi
     
  5. Kipman725

    Kipman725 When did I get a custom title!?!

    Joined:
    1 Nov 2004
    Posts:
    1,753
    Likes Received:
    0
    well I will need some help as I only learnt how to program pics a few days ago :p
    (although I'm finding it a lot easier than any computer programing I ever tried with C++ etc)

    My first question is how do I deal with numbers greater than $FF negative numbers and fractions?
     
  6. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    It doens't matter so long as you keep to the same convention throughout your program.

    Numbers bigger than 0xFF are usually spread over several RAM addresses, or use floating point.

    Negative numbers might have a flag in a separate register or use signed numbers.

    For fractions you might want to save your numbers in fixed point format. Doesn't really matter so long as you use the same method throughout your program.
     
  7. ConKbot of Doom

    ConKbot of Doom What's a Dremel?

    Joined:
    2 Jul 2003
    Posts:
    2,160
    Likes Received:
    6
    Also, you could half, or even quarter the size of your LUT if you have enough clock cycles to do the math... the 64 spots of the LUT would be a 1/4 sine wave ranging from 127 to 255. Just increment your way up the LUT till you reach spot 64, then decrement your way back down till you reach 0 again, giving you 1/2 your sine wave. then simply increment your way back up and make your output equal to (128 - X) where x is what is in the LUT. Then decrement your way back down the LUT to finish the inverted half of the sine.

    Do you understand what I mean?
     
  8. Kipman725

    Kipman725 When did I get a custom title!?!

    Joined:
    1 Nov 2004
    Posts:
    1,753
    Likes Received:
    0
    not yet... I only started learning how to use these things just over a week ago. Whats LUT?

    AS collage is finishing for the summer soon I need to get hold of a PIC programmer. Linux compatibility is essential for home use and I think serial port interface would be ideal as my laptop will only have a few USB ports. The PIC's I'm using at the moment are 16F84A although I want a programmer that will be compatible with bigger chips aswell. Is this one any good?
    http://www.electronic-kits-and-projects.com/3117.htm
     
  9. Kipman725

    Kipman725 When did I get a custom title!?!

    Joined:
    1 Nov 2004
    Posts:
    1,753
    Likes Received:
    0
    Another problem I am having is MPLAB... it won't compile any of my programs even though the complier I use at collage can. here is an example of a program that does nothing but would compile fine at collage but brings up lots of errors on MPLAB:
     
  10. ConKbot of Doom

    ConKbot of Doom What's a Dremel?

    Joined:
    2 Jul 2003
    Posts:
    2,160
    Likes Received:
    6
    LUT= look up table


    If you are going to be outputting a sinewave from 0-255, centered around 127, you only need the first 1/4 of the sine in the lookup table. after that, you just mirror it, flip it and mirror it, and then mirror it again to get the remainder of the sine wave. I dont know about linux programming suites, but a JDM programmer should work on a serial port if you can get direct access to it via software, so that would be your cheapest and easiest route.
     
  11. thecrownles

    thecrownles What's a Relix?

    Joined:
    27 Feb 2004
    Posts:
    733
    Likes Received:
    0
    OK I would not normally post but since you sound pretty intelligent, I have to say something... why are you writing "collage" instead of "college"? It's just not right and it's utterly annoying.
     
  12. Kipman725

    Kipman725 When did I get a custom title!?!

    Joined:
    1 Nov 2004
    Posts:
    1,753
    Likes Received:
    0
    I can't spell although It's getting better and used to be a lot worse. Quite a lot of the time what I right is illegible as I get so excited and just scribble it down. It's quite common for intelligent people not to be able to spell ;)

    I am posting again because I have found some example code and thought that others may be interested:
    http://www.brouhaha.com/~eric/pic/sine.html

    Quite interesting :D
    At the moment I'm exploring different ideas for my A2 electronics project and have made my own simple serial interface which is behaving every oddly which I think is a clocking issue (as both transmitter and receiver work perfectly in isolation and malfunction when run from the same clock.)
     

Share This Page