PDA

View Full Version : 38Khz IR carrier - best way to turn it on and off



andywpg
- 28th November 2015, 21:48
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:


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:


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.

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

Art
- 29th November 2015, 12:32
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?


TRISC = $00
TRISC = $FF

andywpg
- 29th November 2015, 16:21
[QUOTE=Art;135608]Does it happen with x10 attenuation probes and setting on the scope?

Yes. Here is the image:

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.


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

mark_s
- 29th November 2015, 16:55
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


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

GOTO MAIN

andywpg
- 29th November 2015, 21:52
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!

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:


HPWM 1, 127, 38000
MAIN:
pauseus 1000
trisc.2 = 1
pauseus 1000
trisc.2 = 0
GOTO MAIN

Art
- 30th November 2015, 01:35
Have you tried turning it off and on again?



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.

richard
- 30th November 2015, 03:30
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

ardhuru
- 30th November 2015, 07:18
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.

richard
- 30th November 2015, 09:52
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}

mark_s
- 30th November 2015, 15:26
As Ardhuru said, credit to Bruce Reynolds for publishing some great projects.

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

andywpg
- 1st December 2015, 00:37
Have you tried turning it off and on again?



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

andywpg
- 1st December 2015, 00:40
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.



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!!

andywpg
- 1st December 2015, 00:44
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!

andywpg
- 1st December 2015, 00:54
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

8114

8116

richard
- 1st December 2015, 01:55
- 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

andywpg
- 1st December 2015, 02:43
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.

richard
- 1st December 2015, 04:14
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