Generating tones with decimals using PWM?


Closed Thread
Results 1 to 6 of 6
  1. #1
    atomski's Avatar
    atomski Guest

    Question Generating tones with decimals using PWM?

    Here's another one,

    How would one generate tones using
    PWM if decimal numbers are involved
    (e.g. 123.4 Hz)? Cute, huh?

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Sometimes, I think people ask too much of the PIC and expect it to do things when other external hardware exists that would do it better. But since you mailed me off-list to give you a solution, then here’s an obvious one you really should have thought of yourself... which is one reason I didn't reply to this thread originally.

    Why not just calculate the period time of your requirement and write a routine to produce it?

    123.4Hz has a period of 8.103mS, therefore it has a High of 4.052mS and a Low similarly. There will then be 1000/8.103=123.4 cycles produced in a second.

    CounterA var Byte
    TonePin var PortB.0
    .. ..

    ‘ Approximate 1 second burst of 123.4Hz

    BurstLoop:
    For CounterA=0 to 123
    High TonePin
    PauseUS 4052
    Low TonePin
    PauseUS 4048
    ‘ slightly lower to account for approximate
    ‘ propogation delay through For-Next Loop
    Next CounterA

    Accuracy will depend on how good your PIC’s clock is. Use a Scope to ensure linearity of High and Low Pulses along with a Frequency Counter to check the result. Adjustments are made by obviously increasing or decreasing the appropriate pauses. I’ll let you do that.

    Melanie

  3. #3
    atomski's Avatar
    atomski Guest


    Did you find this post helpful? Yes | No

    Talking

    Mel,

    Thank you very much for the reply.
    Sometimes, the most obvious solution
    is the one that seems least probable to
    the person with little experience. I did
    think about this matter a lot and even
    considered using a 10x the output freq.
    from the PIC dividing it with an ext.
    IC to the original frequency. However
    my project is confined to small area of
    16 x 12 mm and this solution wasn't
    possible. I like your suggestion and I
    will most definitely use it for my project
    with a slight modification. I will need to
    make a lookup table for the generation
    of tones to cover all standard freq. From
    67 - 250.3 Hz (CTCSS) & non-standard
    EIA tones in that range. Mel, I really appreciate your help and apologize for
    any inconvenience I might have caused
    by my persistence.

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  4. #4
    atomski's Avatar
    atomski Guest


    Did you find this post helpful? Yes | No

    Question

    Mel,

    I've implemented your solution and it
    worked 100% (why shouldn't it, right?).
    However as I suspected, dividing the
    signal of e.g. 10ms (100Hz) into 2 half-
    cycles would produce a square wave
    shaped signal not a 256 step sinewave.
    I did use a double RC filter (1K, 100n)
    at the output, however while observing
    the signal with an oscilloscope and a
    spectrum analyzer all odd harmonics
    were still there looking pretty strong,
    thus deforming the overall signal shape.
    I was wondering if you could elaborate
    on how would I implement PWM in order
    to generate already mentioned freq. in
    the 67.0 - 250.3Hz range. This way I
    could generate a sine-wave-like signal
    right from the pic and filter out all the
    harmonics with a double RC filter in
    order to get a "true sinewave". Thank
    you very much in advance...

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You're not listening when I say that there are times a PIC alone is unsuitable for an application.

    Hardware PWM will not go down to the frequencies you required (consult your PIC's Datasheet if you don't believe me). PBP's software PWM doesn't allow for frequency changes. However if you insist on the PWM route, you may like to do a search for 'Slow Software PWM' or 'SSPWM' in the MeLabs archives that I directed you to on another thread. I cannot comment on the software or it's suitability, but I'd calculate that generating nice sine waves will have a major software overhead.

    Why don't you just use a CTCSS encoder/decoder chip (like an MX165CP) and have the PIC control that? A simple web search will give you heaps of ways of generating CTCSS tones cheaply and most can be interfaced to a PIC quite easily... example...

    http://www.xs4all.nl/~pa0nhc/Project...038encoder.htm

    Melanie

  6. #6
    atomski's Avatar
    atomski Guest


    Did you find this post helpful? Yes | No

    Talking

    Mel,

    I understand. I will try something else and
    post it in this thread if it should work...

    PS MX IC's are sweet but as I said my
    project is limited to a 16 X 12 X 3 mm area
    plus it has to be done on a very tight
    buget

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

Similar Threads

  1. Half-bridge PWM with a 16F684 ?
    By Byte_Butcher in forum General
    Replies: 7
    Last Post: - 17th January 2010, 22:18
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. PWM setting PIC16F887
    By Gevo in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th June 2008, 07:24
  4. help generating low freq tones
    By Darrenmac in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd June 2007, 08:45
  5. Tidying Up PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st February 2005, 00:26

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