38Khz IR carrier - best way to turn it on and off


Closed Thread
Results 1 to 17 of 17
  1. #1
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159

    Default 38Khz IR carrier - best way to turn it on and off

    I bought my wife a Roomba for Christmas. The one I got has one 'virtual wall' with it. The virtual wall allows you to place it inside a doorway, for example, and it emits an IR signal the the machine won't cross - you can stop it from going into the room. You can buy more of these virtual walls, but, of course, I would rather build one.

    In searching robotics forums, it would appear that the virtual wall is a 38Khz carrier, turned on and off at 1ms intervals.

    I'm just starting to experiment, so I may be WAY off here - but I just used an 18F2550 that I happen to have breadboarded, right now, and did the following:

    Code:
    HPWM 1, 254, 38000
    When I put the scope on it, I got 38KHz (a little over, actually) and a period of 26uS (38KHz is actually 26.3uS). So probably, that will be close enough to work - I can't imagine the receiver on the robot is too fussy, but I'll have to try it.

    Then, to get the 1mS on and off, I decided to just switch the pin from output to input and back again. Not sure why I have to use 500uS on one pause and 1000uS on the other pause, but the scope shows what I wanted for times. So I came up with:

    Code:
    MAIN:
    	HPWM 1, 254, 38000  
            pauseus 500
            TRISC.2 = 1
            pauseus 1000
            TRISC.2 = 0
    	GOTO MAIN
    And the attached picture is what the scope shows (not an expensive scope by ANY means). My question is after the image.

    Name:  IR1.JPG
Views: 916
Size:  86.6 KB

    Any idea what that slope is as the port is switched from output to input? Any way to get rid of it? Or am I just on the wrong track here?

    Thanks

    Andy
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Does it happen with x10 attenuation probes and setting on the scope?

    If the chip is doing nothing else, is it any better switching the whole port?
    Code:
    TRISC = $00
    TRISC = $FF

  3. #3
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    [QUOTE=Art;135608]Does it happen with x10 attenuation probes and setting on the scope?

    Yes. Here is the image:

    Name:  IR2.JPG
Views: 808
Size:  74.7 KB

    I also had the thought that I did not have a decoupling capacitor on Vdd - no difference. I also tried hooking a regular LED up to the port as I only had the scope hooked up to the port before. No difference.

    I also realized that I did not have to keep turning the PWM on and off, all I had to do was the HPWM command once, and then change the port back and forth. No difference, but here is the code now.

    Code:
    HPWM 1, 254, 38000
    MAIN:
        pauseus 1000
        TRISC = $FF
        pauseus 1000
        TRISC = $0
        GOTO MAIN


    You'll notice, I tried switching the whole port - no joy. This is getting frustrating, any other ideas?

    *Update* I had the bright idea that, maybe, if I turned a MOSFET on and off with another port and used the MOSFET to switch the pwm signal on and off, it would get better. Not really, its about the same, but as the MOSFET turns off, that slope has the pwm signal messing it up. So its worse. Back to the way it was - I guess I'll have to try it. Of course, I can't try it until after Christmas......

    Thanks

    Andy
    Last edited by andywpg; - 29th November 2015 at 16:43.
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    If you look in the manual under HPWM. You can see that pbp HPWM is only 8bits.
    In your code you have the duty set at 254, which is nearly 100%. I think you would want to set
    the duty to 50% or 127.

    untested
    Code:
    TRISC.2 = 0
    MAIN:
    	HPWM 1,127, 38000 ' duty cycle 50% 
                  pauseus 1000
                  HPWM 1, 0, 38000 ' duty cycle 0
                  pauseus 1000
    
    	GOTO MAIN

  5. #5
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Quote Originally Posted by mark_s View Post
    If you look in the manual under HPWM. You can see that pbp HPWM is only 8bits.
    In your code you have the duty set at 254, which is nearly 100%. I think you would want to set
    the duty to 50% or 127.
    Wow! You are SO right - looks MUCH better now. Thanks!

    Name:  IR3.JPG
Views: 768
Size:  83.7 KB

    Still has that slope on it though. I might try DT's Instant Interrupts and use a pin on and off to get it. Here's what the code is now:

    Code:
    HPWM 1, 127, 38000
    MAIN:
            pauseus 1000
            trisc.2 = 1
            pauseus 1000
            trisc.2 = 0
    	GOTO MAIN
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Have you tried turning it off and on again?
    Code:
    MAIN:
    	HPWM 1, 127, 38000
            pauseus 1000
    	HPWM 1, 0, 38000
            pauseus 1000
    	GOTO MAIN
    Strictly speaking of course, the HPWM takes some instructions so the loop takes longer than 2ms to execute,
    but you could make your own time wasting routine for accurate delay, so that shouldn’t be a problem.
    I mentioned that since I noticed you had more accurate timing before using HPWM only once.
    But you have a scope, you don’t even have to do the math to calculate a time waste routine.

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Frequency
    is the desired frequency of the PWM signal. On devices with 2
    channels, the
    Frequency must be the same on both channels. Not all frequencies
    are available at all oscillator settings. For the non-long versions of PBP (PBP and
    PBPW), the highest frequency at any oscillator speed is 32767Hz. The lowest
    usable HPWM
    Frequency at each oscillator setting is shown in the following table.
    quote from the pbp3 manual

    did you actually check the resulting freq, it can be done manually of course

    would a pull down resistor (say 1k) make the trace look better

  8. #8
    Join Date
    Oct 2004
    Posts
    448


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Hi,

    Here's what had worked for me. Richard is right, you cant use HPWM for frequencies above 32.768khz.

    Set the registers as follows.

    TRISC.2 = 0 ' CCP1 (PortC.2 = Output)
    PR2 = 25 ' Set PWM Period for approximately 38KHz
    CCPR1L = 13 ' Set PWM Duty-Cycle to 50%
    CCP1CON = %00001100 ' Mode select = PWM
    T2CON = %00000100 ' Timer2 ON + 1:1 prescale

    This would give you a continuous 38khz on the pwm pin, at 50% duty cycle.

    Now, toggle TRISC.2 between an input and an output at 1ms.

    Hope it works! If it does, thanks go to Bruce Reynolds. If doesnt, brickbats to me!

    Regards.

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    that would be for fosc=4
    for my money I'd say :
    CCPR1L = 52 ' Set PWM Duty-Cycle to 50%
    {mister_e pic multi-calc}

  10. #10


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    As Ardhuru said, credit to Bruce Reynolds for publishing some great projects.

    Maybe you can gets some ideas here.
    http://wayback.archive.org/web/20120...com/remote.htm

  11. #11
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Quote Originally Posted by Art View Post
    Have you tried turning it off and on again?
    Code:
    MAIN:
    	HPWM 1, 127, 38000
            pauseus 1000
    	HPWM 1, 0, 38000
            pauseus 1000
    	GOTO MAIN
    Strictly speaking of course, the HPWM takes some instructions so the loop takes longer than 2ms to execute,
    but you could make your own time wasting routine for accurate delay, so that shouldn’t be a problem.
    I mentioned that since I noticed you had more accurate timing before using HPWM only once.
    But you have a scope, you don’t even have to do the math to calculate a time waste routine.
    Hmm, tried it, but I couldn't get the frequency up high enough due to the 'setup time' of HPWM. See below though.....got it looking great now.

    Thanks Art,

    Andy
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  12. #12
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Quote Originally Posted by richard View Post
    did you actually check the resulting freq, it can be done manually of course
    Well, the scope has measurement functions. Again, this is NOT an expensive scope ($75), but it was reporting 38.46KHz and 26uS period......both right in the ballpark of what I was looking for.

    Quote Originally Posted by richard View Post
    would a pull down resistor (say 1k) make the trace look better
    And that was the answer! See my last post for images.

    Thank you!!
    Last edited by andywpg; - 1st December 2015 at 00:54.
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  13. #13
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Quote Originally Posted by ardhuru View Post
    Hi,

    Here's what had worked for me. Richard is right, you cant use HPWM for frequencies above 32.768khz.
    From what I understand of what was quoted, 32768Hz was maximum WITHOUT using longs. I'm using an 18F2550, and when I compile, I get the following message:

    '[MESSAGE]pic18f2550.pbpinc(285): LONG Variables enabled (PBPL used)'

    It seems like it used longs because of the frequency I specified, and it seemed happy. Anyway, the problem is solved, see my last post.

    Thanks for the help!
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  14. #14
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off - SOLVED

    Thanks Everyone!

    It worked!

    The answer was a pull down resistor. It got rid of that slope. I tried a few, and worked out that 4.7K was the highest value I could use. That gave me a final voltage about 4.2V which should be fine.

    Then I had to play with the frequency I specified to get exactly 38 pulses in 1mS. Even though the scope's measurement function still reports it as 38.46KHz - I told you it was a cheap scope!

    Here are two images of what I have now.

    Thanks again!

    Andy

    Attachment 8114

    Name:  IR5.JPG
Views: 778
Size:  78.3 KB
    Attached Images Attached Images  
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  15. #15
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    - I told you it was a cheap scope!
    the scope is actually correct.

    38.462KHz is as close as you can get to 38KHz hpwm with a 4MHz osc

  16. #16
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    Quote Originally Posted by richard View Post
    the scope is actually correct.

    38.462KHz is as close as you can get to 38KHz hpwm with a 4MHz osc
    I'm using the 8Mhz internal oscillator. I'm curious how you figured that out though, could you educate me?

    What if I used a 10 or 16Mhz crystal? I have both on hand.
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  17. #17
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: 38Khz IR carrier - best way to turn it on and off

    search the forum for PicMultiCalc by mistere

    @8m you can get 37.736 KHz , 38.462 KHz
    @16m 38.095 KHz , 38.462 KHz
    @10 37.879 KHz , 39.063 KHz

Similar Threads

  1. How to setup 38kHz PWM with PIC18F1330
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 7th March 2015, 12:35
  2. 38Khz Modulator
    By Gord11 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th September 2011, 08:14
  3. Turn me on
    By AvionicsMaster1 in forum General
    Replies: 8
    Last Post: - 16th November 2010, 14:04
  4. turn-off lcd and turn-on again
    By mehmetOzdemir in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 5th September 2009, 12:57
  5. Generated square wave 19Khz and 38Khz
    By blinkstar88 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th August 2007, 16:40

Members who have read this thread : 2

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