Sinewaves using interrupts.


Closed Thread
Results 1 to 26 of 26

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Ok, I'll give part of it a go:

    For a 5K signal with 32 samples, thats 32 x 5000 = 160,000/sec times to hit the interupt. With no real numbers to use here, lets say it takes 100 instructions to "hit" the int. So thats 1,600,000 ins/sec or 1.6mips. so to start off, i would think at a min 8 meg clock (2mips)

    The rest of the math depends on how the signal is generated. for instance using hpwm or maybe old school R2R D/A.

    Yes i get tired of the question
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  2. #2
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Someone told me 16 PWM outputs per cycle could generate a decent filtered sine wave. If PBP can handle a much lower 20-KHz (50-usec) interrupt (with 20-MHz clock), maybe we could get Hank to check out the waveforms on his scope? For 16 outputs per cycle he could use an Fout frequency of 20000/16=1250-Hz. For 20 outputs per cycle he could use an Fout frequency of 20000/20=1000-Hz.

    So how would you PBP gurus convert this DDS engine for Hank's 20-MHz 16F690 to PBP (Accum and Phase are 24-bit variables)?

    Code:
    //
    //  setup CCP in PWM mode for 20-KHz (50-usecs) with prescale 1,
    //  postcale 1, and PR2=250-1 (20-MHz clock)
    //
    //  Fdds = 1 / 0.00005 = 20000 Hz
    //  Fres = 20000 / 2^24 = 0.0011920928955078125
    //  Phase (1250-Hz) = INT(1250 / 0.0011920928955078125 + 0.5) = 1048576
    //  Phase (1000-Hz) = INT(1000 / 0.0011920928955078125 + 0.5) =  838861
    //
    void interrupt()               //
    { pir1.TMR2IF = 0;             // clear TMR2 interrupt flag
      Accum = Accum + Phase;       // add phase offset to Accum
      ccpr1l = sine[Accum >> 16];  // setup next PWM duty cycle
    }
    We'd also need a 256 element sine table with PWM values of 0..250. Here's the output from my handy dandy Sine PWM Table Generator;

    Code:
    Number of steps: 256
       Output range: 250
     Table or Array: a
    
            125,128,131,134,137,140,143,146
            149,152,155,158,161,164,167,169
            172,175,178,181,183,186,189,191
            194,196,199,201,204,206,208,211
            213,215,217,219,221,223,225,227
            228,230,232,233,235,236,237,239
            240,241,242,243,244,245,246,246
            247,248,248,249,249,249,249,249
            250,249,249,249,249,249,248,248
            247,246,246,245,244,243,242,241
            240,239,237,236,235,233,232,230
            228,227,225,223,221,219,217,215
            213,211,208,206,204,201,199,196
            194,191,189,186,183,181,178,175
            172,169,167,164,161,158,155,152
            149,146,143,140,137,134,131,128
            124,121,118,115,112,109,106,103
            100,097,094,091,088,085,082,080
            077,074,071,068,066,063,060,058
            055,053,050,048,045,043,041,038
            036,034,032,030,028,026,024,022
            021,019,017,016,014,013,012,010
            009,008,007,006,005,004,003,003
            002,001,001,000,000,000,000,000
            000,000,000,000,000,000,001,001
            002,003,003,004,005,006,007,008
            009,010,012,013,014,016,017,019
            021,022,024,026,028,030,032,034
            036,038,041,043,045,048,050,053
            055,058,060,063,066,068,071,074
            077,080,082,085,088,091,094,097
            100,103,106,109,112,115,118,121
    Last edited by Mike, K8LH; - 1st October 2010 at 18:31.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Sure, I'd be more than happy to run any test code posted up here, and respond with related scope screen shots.

    But please note, I only have a PIC16F690 & 20Mhz resonator at my disposal

    (that said I think I have a 20Mhz Xtal i my drawer somewhere if thats significant!)

    Quote Originally Posted by mackrackit View Post
    We could change it so the answers would be nice and long like:

    a pesticide used to kill lice?
    DICHLORODIPHENYLTRICHLOROETHANE
    When I buy the stuff every other week, I just ask for the shortned name - DDT ...my chemist normally charges me one thousand, one hundred & thirty three pence per bottle
    Last edited by HankMcSpank; - 1st October 2010 at 18:50.

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Hi Hank

    If you're planning to use the digitally synthesised sine wave for audio purposes, please re-consider the number of samples. 16 samples are good enough to generate a sine wave for the CRO, but for the ear,.... well, it's a different game altogether.

    I had used the DDS technique, albeit with a PSOC, to generate pure tones (sinewaves of fixed frequency) for a clinical audiometer. You will get a clean sine wave on the scope, but, the distortion present in the wave will make it unsuitable for any audio purist. So, I had to switch over to the AD9832 which should be quite similar in function to the 9835(I haven't checked)

    The AD9832 is a 1024 point sampled wave DDS IC. The results I obtained from this are just excellent. Before this, I had used a ML2036 which is now obsolete.

    You might achieve the tones, but, you may not like the sound you get using a 16 point DDS. Let me not discourage your collaborative effort, but look at it as a gentle helmet I've gifted you to save the remaining hair on your head LoL

    Cheers

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Jerson View Post
    The AD9832 is a 1024 point sampled wave DDS IC. The results I obtained from this are just excellent. Before this, I had used a ML2036 which is now obsolete.

    You might achieve the tones, but, you may not like the sound you get using a 16 point DDS. Let me not discourage your collaborative effort, but look at it as a gentle helmet I've gifted you to save the remaining hair on your head LoL
    Hi Jerson,

    Weirdly the AD9835 is available much cheaper on Ebay than the AD9832 - http://cgi.ebay.com.sg/AD9835BRU-Com...item35ac588755 )

    Quote Originally Posted by cncmachineguy View Post
    Ok, I'll give part of it a go:

    For a 5K signal with 32 samples, thats 32 x 5000 = 160,000/sec times to hit the interupt. With no real numbers to use here, lets say it takes 100 instructions to "hit" the int. So thats 1,600,000 ins/sec or 1.6mips. so to start off, i would think at a min 8 meg clock (2mips)
    And what about generating such to two decimal places (eg 4999.22Hz) - what impact does that have on the number (100x more?)


    Actually my intentions aren't particularly music driven, but while on that vein, here's someone pushing the PIC DDS (ADSR) envelope...



    I actually totally agree about the moderation (a big high five from me)...it was just a bit of tongue in cheek - I too normally forget to answer the question (& for a good while, whenever I got presented with the coffee question I always got the answer wrong - I was just putting in the missing letter as the answer .....a case of RTFQ Hank!)
    Last edited by HankMcSpank; - 1st October 2010 at 19:55.

  6. #6
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    I agree that if you're doin' audio you should look at one of the many DDS chips from Analog Devices. They used to be very nice about samples too, which I took advantage of when building a DDS Ham Radio project a few years ago.

    Regards, Mike

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    I want to get clear about something interupt related here. If I have a pure ASM isr, does that mean I don't have to worry about PBP stuff at all when I enter and exit? So for instance, if I set PIE for timer 1 and GIE, I know my interupt will fire any time. I also know the PIC will tend to W,STATUS, and PCLATH, (more or less depending on device). So I don't need to worry about anything else? And if I address a user defined variable in my ISR, I do that with _VariableName? If its that easy, thats wonderful.

    Sorry Hank, not trying to hijack your thread, just seems a good place to ask. I am cooking up something related to sine wave generator for you.

    @Dave and all the other kind folks that moderate for us, Thank You. I HAVE noticed this is one of the best spam free forums I have seen. I do know thats takes a LOT of work, and if my having to answer a question everytime I post helps, its the least I can do. Have to admit though, I preview my post most times, then forget to answer again. But I hope sooner or later I will learn. I have telsa spelled tesla almost every time now
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by cncmachineguy View Post
    I want to get clear about something interupt related here. If I have a pure ASM isr, does that mean I don't have to worry about PBP stuff at all when I enter and exit? So for instance, if I set PIE for timer 1 and GIE, I know my interupt will fire any time. I also know the PIC will tend to W,STATUS, and PCLATH, (more or less depending on device). So I don't need to worry about anything else? And if I address a user defined variable in my ISR, I do that with _VariableName? If its that easy, thats wonderful.
    I can answer that for you. The ISR only needs to save as many registers as you are going to use in the ISR. So, if you plan on using W, Status, FSR, and any other registers that are used in the main program code, you need to save them on entry to the ISR and restore them while getting out. The moment you mix in PBP in an ASM ISR, you need to worry about the variables that PBP starts using.

    PBP uses a lot of internal variables called T1, T2, R0, R1, etc
    The moment you start using PBP math, logical and, if x and y then type constructs, these variables are used. So, you are better off not using PBP inside interrupts if you mark the ISR as ASM code with DT_ints The best way is to look at the generated LST file for the ISR code; you will learn a lot by just inspecting it.

    A simple x = y type assignment does not need any PBP variables and it will most certainly work in ASM.

    Regards

  9. #9
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Sinewaves using interrupts.

    Ok, old thread of mine & hands up...short of dabbling with scalerobotics cool sinewave generation program I never actually progressed much further - but I keep coming back to this DDS issue....I reckon I can do so much cool stuff once I've mastered the art of generating sine waves under my control

    Now I'm never the brightest button in the sewing box, but I do have dogged determination (rough translation "a pest")...I'll often kludge my way through without necessary understanding every part of the process - So with this in mind, I hope to start soon again on this pesky DDS melarky.

    So some questions.....

    all this talk of a 24 bit accumulator - how do I achieve that on a 12LF1822 with PICbasic?!!!

    Would up to 4.8Khz be do-able in HPWM for the DAC aspect?

    Can I really expect to get granular with setting the frequency without recourse to floating point? (I want to be able to set the frequency via another PIC by fiielding an Eusart interrupt to the DDS PIC)

    There are those on the net, that have done this with other processors. The Atmel AT90S2313 seems to figure large due to the lower number of clock cycles it takes per instruction. This chap has posted a great webpage...


    http://www.myplace.nu/avr/minidds/index.htm

    Including code....

    http://www.myplace.nu/avr/minidds/minidds.asm

    for those that have a clue (rules me out), how much of an ordeal would it be to move that over to a PIC?

    Is this a project where I really ought to start learning assembly?
    Last edited by HankMcSpank; - 15th July 2011 at 23:03.

  10. #10


    Did you find this post helpful? Yes | No

    Default Re: Sinewaves using interrupts.

    Hi Hank,

    Note that the avr project needs at least 8 I/O pins to drive the resistor ladder(DAC). So with a PIC12f series you are stuck using PWM, serial DAC or a dedicated DDS chip.

    I tried a spi DAC, too slow. Couldn't get over 600hz. It wouldn't be too hard to do the avr dds with a pic. Use a look up table and a loop and write each value to an 8bit port. Not sure how the speed would compare with an AVR?

    This is the best, but it is expensive
    http://www.sparkfun.com/products/9169

    I wrote some code for this in PBP if you are interested.

    cheers

Members who have read this thread : 0

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