PDA

View Full Version : FREQOUT - PWM question



Etcho
- 25th February 2007, 22:14
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

skimask
- 25th February 2007, 22:36
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.

mister_e
- 25th February 2007, 22:39
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


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

Etcho
- 25th February 2007, 23:11
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

skimask
- 25th February 2007, 23:15
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.

Etcho
- 25th February 2007, 23:24
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

skimask
- 25th February 2007, 23:39
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.

Etcho
- 25th February 2007, 23:51
Thanks again for all the help, the parts will get to my house next week - I will let you know how it goes.