PDA

View Full Version : 40 kHz wave



RUBiksCUbe
- 10th December 2005, 01:28
I need to pulse an LED signal at 40 kHz for an IR project, but
PULSOUT A1, 40
didn't work on a 4 MHz 12F683, and the manual said that a pic running at 4 MHz has a PULSOUT period of 10 us. The datasheet (http://pdf.alldatasheet.com/datasheet-pdf/view/14338/PANASONIC/PNA4614M.html) of the IR detector I am using said that the typical signal width is 200 us.
Does the PIC not actually pulse out at 10 us intervals?

Acetronics2
- 10th December 2005, 11:07
Hi, Rubik

a 555 running at 40 khz would easily solve your headache ...without any program timing difficulties ...

Alain

milestag
- 10th December 2005, 15:50
Just use the PIC12F683's internal hardware PWM peripheral. This way you can easily generate the 40KHz, turn it on and off, adjust the duty cycle and it runs in the background. The datasheet will show you exactly how to set it up.

Personally I don't like using the HPWM command in PB Pro because its too limited. It's easier to do it by manually setting the registers.

This is exactly how I generate the 40KHz IR for my lasertag systems.

'register setup (this is for a 16F684, so it may need to be modified slightly for your chip)

PR2 = 49 'PWM Period 40K=49
CCPR1L = 10 'PWM Duty Cycle
T2CON = 4 'Timer2 = ON; Prescale = 1:1
CCP1CON = 0 'PWM module off

'generate 1ms 40KHz pulse

CCP1CON = 12 'PWM ON
PauseUS 1000
CCP1CON = 0 'PWM OFF

RUBiksCUbe
- 11th December 2005, 01:09
Woah I didn't know about the HPWM command. I'm still learning PBP. It seems like it will do the trick in my case.

I don't like using 555s because they heat up and their output changes.

I think that for a 40kHz square wave I need to say:
HPWM 1,127,40000
Right?

mister_e
- 11th December 2005, 09:14
Nop! HPWM will not do the job in this case... look in the PBP manual for the specific limitation on different crystal speed.

in your case you must write directly to the internal register related by the PWM module. look section 11 of the datasheet and look to the Milestag's example. I'm not a fan of HPWM myself too.

555's are not as this bad.. it's working great but it's still a RC oscillator ... like the PIC internal one. So both will shift a litlle bit with the temperature variation.

There's still some easy circuit to build with some CMOS inverter OR Route the CLKOUT of the PIC to a frequency divider OR.... OR...

RUBiksCUbe
- 11th December 2005, 15:19
How do you write stuff to the internal registers? I've tried to set up my PIC as a counter before but I didn't know how to, and the COUNT command didn't really work too well. I'm looking through my PICs datasheet (12F683) and I don't understand what it's talking about. What is the CCP1CON register? It mentions it a lot in the PWM section.

It says that in order to set the PWM period I need to the PR2 register. How/What do I write?

milestag
- 11th December 2005, 15:31
Read my earlier post again. I already showed you exactly how to write to those registers, and the values you need for 40KHz.

Just make sure that those are the identical register names.

RUBiksCUbe
- 11th December 2005, 17:32
I tried your code (the register names are the same) but it didn't work. The datasheet tells me to "Make the CCP1 pin an output by clearing the
TRISIO<2> bit." and your code didn't do that. How do I clear a bit?

mister_e
- 11th December 2005, 18:53
clear bit n => bit n=0
set bit n => bit n=1

clear bit 2 of TRISIO => TRISIO.2=1

the Register value will be different for each crystal speed. You can run the F683 with the internal 8 MHZ or lower, 4MHZ is rthe default speed. Refer to the OSCCON table

RUBiksCUbe
- 11th December 2005, 19:36
I think it really helps when making writing code for a PIC when the PIC actually works. >:0. I was using a dead PIC. When I saw that A.0 was putting out a constant .5 volts and A.1 was putting out around 4 I realized that it might be the PICs fault. I tried a simple blinking program and the PIC is indeed dead. I'm switching over to the more powerfull (and faster) 18F252 running at 20 MHz.

milestag
- 11th December 2005, 20:34
I think it really helps when making writing code for a PIC when the PIC actually works. >:0. I was using a dead PIC. When I saw that A.0 was putting out a constant .5 volts and A.1 was putting out around 4 I realized that it might be the PICs fault. I tried a simple blinking program and the PIC is indeed dead. I'm switching over to the more powerfull (and faster) 18F252 running at 20 MHz.

I am using the 18F2525 at 8MHz, so you will have to change some of those Timer2 values. The formula for calculating the values is in the PIC datasheet. Otherwise it should work. Don't let those formulas scare you off, it's really pretty simple.

good luck

Jim

RUBiksCUbe
- 11th December 2005, 21:31
I tried calculating it but I did somthing wrong.

PWM Period = (PR2 + 1) * 4 * Oscilator * (TMR2 Prescale)

I'm assuming that PWM Period and Oscillator are in hertz, and since PR2 is the unknown in my case it should be:

40,000 = (PR2 + 1) * 4 * 20,000,000 * 1

And when I did it all out to solve for PR2 I got:

PR2 = -.995

I really dont think thats right.

RUBiksCUbe
- 14th December 2005, 20:01
Alright I didnt read over the datasheet enough but I still don't understand how I know what "Tosc" is in the PWM period equation. I found a confusing chart on page 273 that says that in HS mode, which is what my 18F252 is in with a 20 MHz oscillator, Tosc has a range of 40 to 250 ns. How do I find what it is? Once I know how to correctly find Tosc I can solve the equation and calculate what I need to set PR2 to.
I know that in order to generate a 40 kHz signal I need to generate a square wave with a pulse duration of 25 microseconds.

Ok,
The formula in the datasheet for the PIC18FXX2:

PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

and 1/PWM period = frequency so in my case:

1/PWM period = 40,000
PWM period = .000025 (25 microseconds)

So my equation should be

.000025 = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

Since TMR2 prescale is 1, I can rearrange it so that (PR2), the unknown, is on one side:

(20MHz / (4 * TMR2 prescale value * 40KHz)) - 1= PR2.

Solve it all out...

PR2 = 124

...
And it works!

Thanks everyone!


In case anyone in the future needs to use the hardware PWMs and has no idea what to set PR2 to, use this equation:
(Oscillator speed / (4 * TMR2 prescale value * Desired Output)) - 1= PR2.