FREQOUT - PWM question


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2007
    Posts
    8

    Question FREQOUT - PWM question

    Hello - I had a quick question and I was wondering if anyone would be able to help me out -

    Is there a way to generate a 38Khz square wave on any of the 16F877A pins? I am a bit confused because the PBP manual briefly talked about not being able to generate over 32Khz using the FREQOUT command - I know the 16F877A also has 2 PWM modules built in but I also wasn’t sure if they were capable of it - the manual from my understanding indicates no but I have a feeling that there is some way to do it.

    I am looking to use two IR LEDs pulsed out at 38Khz and two IR 38Khz receiver modules as simple object detection for a small autonomous robot - I don't have a whole lot of room on the PCB so I hope to avoid using the 555 Timer IC

    Any info regarding this problem would be great - thanks

    Oh one other question - Has anyone messed around with the radio shack 38Khz IR receiver module? All the ones I have gotten from them seem to not function so I ended up having to order a couple PNA4602M which do seem to work.

    Note -

    If I posted this in the wrong area it was by accident please don't flame

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Etcho View Post
    Hello - I had a quick question and I was wondering if anyone would be able to help me out -

    Is there a way to generate a 38Khz square wave on any of the 16F877A pins? I am a bit confused because the PBP manual briefly talked about not being able to generate over 32Khz using the FREQOUT command - I know the 16F877A also has 2 PWM modules built in but I also wasn’t sure if they were capable of it - the manual from my understanding indicates no but I have a feeling that there is some way to do it.

    I am looking to use two IR LEDs pulsed out at 38Khz and two IR 38Khz receiver modules as simple object detection for a small autonomous robot - I don't have a whole lot of room on the PCB so I hope to avoid using the 555 Timer IC

    Any info regarding this problem would be great - thanks

    Oh one other question - Has anyone messed around with the radio shack 38Khz IR receiver module? All the ones I have gotten from them seem to not function so I ended up having to order a couple PNA4602M which do seem to work.

    Note -

    If I posted this in the wrong area it was by accident please don't flame
    What is the clock on the '877?
    Assume 20mhz...

    High outputpin
    pauseus 13
    low outputpin
    pauseus 12
    do it again

    high outputpin takes less than 1us
    wait for 13us
    low outputpin takes less than 1us
    wait for 13us
    the jump takes a bit more time than the high and low commands...
    total = a bit more than 26us...
    26us for the loop gives you an output square wave of about 38461hz (actually a bit less), which should be close enough for an IR receiver.

    Of course this is only one way, a brute force method of doing it.
    One better way would be to either set up the PWM module to handle it (which it will), or use the TMR0 module to do some interrupts to toggle an output bit, and the list goes on.

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


    Did you find this post helpful? Yes | No

    Default

    Yes the internal CCP module set in PWM will handle it for you AND in background.

    Depending you OSC speed you'll have more or less DutyCycle resolution. But anyway, you want it 50% right?

    Unfortunately due to it's limitations, PBP HPWM won't help. You'll need to calculate and write the right value to the PIC registers.

    To do ALL related calcs, i will suggest to use my PicMultiCalc available bellow
    http://www.mister-e.org/pages/utilitiespag.html

    Once you have it, it's piece of cake.

    Here's something working @4MHZ
    Code:
            '
            '       38KHz PWM @ 4MHZ using a PIC16F877
            '       ----------------------------------
            @  __CONFIG _XT_OSC & _LVP_OFF
            '
            '
            TRISC = 0
            Duty        var byte
            
            '
            '       PWM SETUP
            '       =========
                    ' PicMultiCalc says..
                    '   Duty value for 50%=52
                    '   PR2 = 25
                    '   PRESCALLER 1:1
                    '   
                    '
            duty = 52                           ' duty value for 50% duty cycle
            PR2 = 25                            '
            T2CON = %00000100                   ' timer2 on, prescale 1:1
            CCPR1L = duty>>2                    ' MSB of duty cycle value
            CCP1CON=%00001100 | (dUTY<<5)       ' set PWM mode and store the 
                                                '   2 LSB of duty        
    SpinInRound:
            goto spininround
    also have a look to those...
    http://rentron.com/PicBasic/IR_Chips.htm
    http://rentron.com/IR_TO_RF.htm
    http://rentron.com/Infrared_Communication.htm
    Last edited by mister_e; - 25th February 2007 at 23:49.
    Steve

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

  4. #4
    Join Date
    Feb 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Thanks for all the help - I will look into doing it with PWM - The crystal I plan on using is 20MHZ (just to make sure - the crystals decoupling? caps should be around 22pf right?)

    One other thing which may be a tad off topic -

    I know I should go off the data sheet of the IR LEDs but I was wondering if either of you knew if the PIC would be strong enough to drive two IR LEDs in series or if they should be driven separately - one on each PWM pin or if I would need to use a transistor to drive them. The maximum current the PIC is supposed to source/sink per pin is 25mA but I was curious as to if it would be enough.

    I am not looking to have it pick up obstacles at a real long range maybe 3 - 6 inches tops.

    Thanks again for the help

    edit: thanks for the links mister e
    Last edited by Etcho; - 26th February 2007 at 00:13.

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Etcho View Post
    Thanks for all the help - I will look into doing it with PWM - The crystal I plan on using is 20MHZ (just to make sure - the crystals decoupling? caps should be around 22pf right?)

    One other thing which may be a tad off topic -

    I know I should go off the data sheet of the IR LEDs but I was wondering if either of you knew if the PIC would be strong enough to drive two IR LEDs in series or if they should be driven separately - one on each PWM pin or if I would need to use a transistor to drive them. The maximum current the PIC is supposed to source/sink per pin is 25mA but I was curious as to if it would be enough.

    I am not looking to have it pick up obstacles at a real long range maybe 3 - 6 inches tops.

    Thanks again for the help

    edit: thanks for the links mister e
    What's the forward voltage drop of the IR LEDs? If it's less than 2.5v each, yes, you can drive 2 in series. If it's less than 1.6v, you can drive 3 in series. However, total output will probably drop a bit. You might want to drive the gate of a MOSFET which in turn will drive the IR LEDs.

  6. #6
    Join Date
    Feb 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    What's the forward voltage drop of the IR LEDs? If it's less than 2.5v each, yes, you can drive 2 in series. If it's less than 1.6v, you can drive 3 in series. However, total output will probably drop a bit. You might want to drive the gate of a MOSFET which in turn will drive the IR LEDs.
    Well here is the data sheet to the LEDs-

    http://www.jameco.com/Jameco/Products/ProdDS/106526.pdf

    It says the forward voltage drop is usually 1.2V so I should be able to drive two in series along with a 150 or 200 Ohm resistor right?

    I just may not get a real far detection range.

    Thanks again for the help

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Etcho View Post
    Well here is the data sheet to the LEDs-

    http://www.jameco.com/Jameco/Products/ProdDS/106526.pdf

    It says the forward voltage drop is usually 1.2V so I should be able to drive two in series along with a 150 or 200 Ohm resistor right?

    I just may not get a real far detection range.

    Thanks again for the help
    Sounds like it's easily done, should be able to drive 3 or 4 in series without a problem. I think you're main problem will be ambient light and how to filter that out without killing your detection light.

  8. #8
    Join Date
    Feb 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Thanks again for all the help, the parts will get to my house next week - I will let you know how it goes.

Similar Threads

  1. Half-bridge PWM with a 16F684 ?
    By Byte_Butcher in forum General
    Replies: 7
    Last Post: - 17th January 2010, 23:18
  2. Pull Up enabled and PWM question
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th September 2009, 09:21
  3. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 07:03
  4. Hardware PWM Question?
    By jhorsburgh in forum General
    Replies: 1
    Last Post: - 18th August 2008, 04:07
  5. Tidying Up PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st February 2005, 01: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