1 Attachment(s)
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:
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.
Attachment 8110
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
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
1 Attachment(s)
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:
Attachment 8111
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...... :smile:
Thanks
Andy
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
1 Attachment(s)
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
Originally Posted by
mark_s
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!
Attachment 8112
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
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.
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
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
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.
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}
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
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
Originally Posted by
Art
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
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
Originally Posted by
richard
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
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!!
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
Originally Posted by
ardhuru
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!
2 Attachment(s)
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
Attachment 8116
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
- 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
Re: 38Khz IR carrier - best way to turn it on and off
Quote:
Originally Posted by
richard
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.
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