40 kHz wave


Closed Thread
Results 1 to 13 of 13

Thread: 40 kHz wave

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    clear bit n => bit n=0
    set bit n => bit n=1

    clear bit 2 of TRISIO => TRISIO.2=1

    the Register value will be different for each crystal speed. You can run the F683 with the internal 8 MHZ or lower, 4MHZ is rthe default speed. Refer to the OSCCON table
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  2. #2
    RUBiksCUbe's Avatar
    RUBiksCUbe Guest


    Did you find this post helpful? Yes | No

    Default

    I think it really helps when making writing code for a PIC when the PIC actually works. >:0. I was using a dead PIC. When I saw that A.0 was putting out a constant .5 volts and A.1 was putting out around 4 I realized that it might be the PICs fault. I tried a simple blinking program and the PIC is indeed dead. I'm switching over to the more powerfull (and faster) 18F252 running at 20 MHz.

  3. #3
    Join Date
    Sep 2005
    Location
    Dayton, Ohio
    Posts
    72


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RUBiksCUbe
    I think it really helps when making writing code for a PIC when the PIC actually works. >:0. I was using a dead PIC. When I saw that A.0 was putting out a constant .5 volts and A.1 was putting out around 4 I realized that it might be the PICs fault. I tried a simple blinking program and the PIC is indeed dead. I'm switching over to the more powerfull (and faster) 18F252 running at 20 MHz.
    I am using the 18F2525 at 8MHz, so you will have to change some of those Timer2 values. The formula for calculating the values is in the PIC datasheet. Otherwise it should work. Don't let those formulas scare you off, it's really pretty simple.

    good luck

    Jim

  4. #4
    RUBiksCUbe's Avatar
    RUBiksCUbe Guest


    Did you find this post helpful? Yes | No

    Default

    I tried calculating it but I did somthing wrong.

    PWM Period = (PR2 + 1) * 4 * Oscilator * (TMR2 Prescale)

    I'm assuming that PWM Period and Oscillator are in hertz, and since PR2 is the unknown in my case it should be:

    40,000 = (PR2 + 1) * 4 * 20,000,000 * 1

    And when I did it all out to solve for PR2 I got:

    PR2 = -.995

    I really dont think thats right.

  5. #5
    RUBiksCUbe's Avatar
    RUBiksCUbe Guest


    Did you find this post helpful? Yes | No

    Default

    Alright I didnt read over the datasheet enough but I still don't understand how I know what "Tosc" is in the PWM period equation. I found a confusing chart on page 273 that says that in HS mode, which is what my 18F252 is in with a 20 MHz oscillator, Tosc has a range of 40 to 250 ns. How do I find what it is? Once I know how to correctly find Tosc I can solve the equation and calculate what I need to set PR2 to.
    I know that in order to generate a 40 kHz signal I need to generate a square wave with a pulse duration of 25 microseconds.

    Ok,
    The formula in the datasheet for the PIC18FXX2:

    PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

    and 1/PWM period = frequency so in my case:

    1/PWM period = 40,000
    PWM period = .000025 (25 microseconds)

    So my equation should be

    .000025 = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

    Since TMR2 prescale is 1, I can rearrange it so that (PR2), the unknown, is on one side:

    (20MHz / (4 * TMR2 prescale value * 40KHz)) - 1= PR2.

    Solve it all out...

    PR2 = 124

    ...
    And it works!

    Thanks everyone!


    In case anyone in the future needs to use the hardware PWMs and has no idea what to set PR2 to, use this equation:
    (Oscillator speed / (4 * TMR2 prescale value * Desired Output)) - 1= PR2.
    Last edited by RUBiksCUbe; - 14th December 2005 at 22:33.

Similar Threads

  1. To Generate Sine Wave Using PCPWM of PIC18F4331 (Issue)
    By Cyborg in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd March 2014, 14:39
  2. 40 KHz burst
    By aherrera in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th November 2009, 23:36
  3. What's the best way to output 30 to 40 kHz from a 12F683 pin?
    By fizyxman in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st June 2009, 01:08
  4. Sine wave
    By Darrenmac in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 18th May 2009, 04:31
  5. PIC 16F877 I2C communications
    By sigmoideng in forum General
    Replies: 7
    Last Post: - 13th July 2007, 11:28

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