Searching \ for '[PIC]: 16F628 CCP 40Khz IR Communication. Help ne' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/ios.htm?key=ir
Search entire site for: '16F628 CCP 40Khz IR Communication. Help ne'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: 16F628 CCP 40Khz IR Communication. Help ne'
2005\11\12@173612 by Scott Pierce

flavicon
face
part 1 2906 bytes content-type:text/plain; charset="us-ascii"; format=flowedHi All,

I'm trying to use the CCP module on a 16F628 to generate a 40Khz pulse in
order to communicate via IR.  The following test program I wrote using the
PICC compiler should set up the CCP module for a free running 40Khz, 50%
Duty pulse.  RB3, the output of the CCP module on the F628 is connected to
the Anode of an IR LED.  The Cathode of the IR LED is connected to
RB5.  So, test program should set up the CCP Module then go into an endless
loop of flashing the IR LED on and off using RB5.  Every time RB5 goes low,
a 40khz pulse should be output on the IR LED for a short period of time
(about a second).  When RB5 goes high, the IR LED shuts off.

On the receiving end, I have a Vishay TSOP1240.  This is a IR Receiver that
only responds to IR Light modulated at 40Khz.  When the receiver detects a
40Khz pulse, the output pin of the  receiver goes low.  I have a red LED's
Cathode pin connected to the output of the TSOP1240 and the Anode of the
red LED connected to a 330 Ohm resistor, which is in turn connected to
+5V.  The TSOP1240 also has a 4.7uF cap and a 100 Ohm resistor connected to
the Gnd and VCC pins as shown in the TSOP1240 Datasheet to suppress power
spikes.

Using an IR Light Sensor (the little reflector strip from Radio Shack) I
can visually see the IR LED turn on and off but my IR Receiver (TSOP1240)
doesn't seem to react to the modulated IR Pulse.  I would really appreciate
it if someone could look over my code and give me some pointers.  I have
included a simple Excel spreadsheet I made to help calculate the settings
for the CCP Module for the PIC 16F628, which I used to come up with my
values for the program.  Any help or comments are greatly appreciated!  Thanks.

Sincerely,
Scott Pierce



#include <pic.h>

__CONFIG (UNPROTECT & LVPDIS & BORDIS & MCLRDIS & PWRTEN & WDTDIS & INTIO);

void Setup();
void delay();

void main()
{

       Setup();

       while(1==1)
       {

               RB5 = 0;

               delay();

               RB5 = 1;

               delay();

       }//while

}//main


void delay()
{
       char i;
       char j;

       for (i = 0; i < 254; i++)
       {

               for (j = 0; j < 254; j++){}

       }//for

}

void Setup()
{

       TMR2                = 0;
       INTCON        = 0;
       PIE1                = 0;
       PIR1                = 0;

       TRISA                = 0b00000110;        // Set PORTA I/O Directions
       TRISB                = 0b00000000;        // Set PORTB I/O Directions
       OPTION        = 0b11000000;        // Set up Timer0 and other options
       T1CON                = 0b00000100;        // Turn off Timer1
       CMCON        = 0b00000111;        // Turn off Comparator on PORTA
       TXSTA                = 0b00000000;        // Turn off USART Transmitter
       RCSTA                = 0b00000000;        // Turn off USART Receiver
       SPBRG                = 0b00011001;        // 2400 BAUD

       PR2                = 24;                // 40khz PWM Period
       CCP1CON        = 0b00001100;        // Set CCP to PWM Mode
       CCPR1L        = 0b00001000;        // Set 50% Duty Cycle
       T2CON                = 0b00000100;        // Set Timer2 Prescalar
       CCP1CON        = 0b00001100;        // Set CCP to PWM Mode

       TMR2ON        = 1;
       
       PORTA        = 0;                // Initialize PORTA
       PORTB        = 0;                // Initialize PORTB

}//setup

part 2 14373 bytes content-type:application/octet-stream; name="PWM Calc.xls"; (decode)

part 3 35 bytes content-type:text/plain; charset="us-ascii"
(decoded 7bit)

2005\11\12@183536 by olin piclist

face picon face
Scott Pierce wrote:
> I'm trying to use the CCP module on a 16F628 to generate a 40Khz pulse
> in order to communicate via IR.  The following test program I wrote
> using the PICC compiler should set up the CCP module for a free running
> 40Khz, 50%
> Duty pulse.  RB3, the output of the CCP module on the F628 is connected
> to
> the Anode of an IR LED.  The Cathode of the IR LED is connected to
> RB5.

Not a good idea.  First, there is no need for a separate pin to
enable/disable the 40KHz pulse output.  You can do that just as easily in
software by enabling/disabling the CCP module.  Second, you need a resistor
in series with the LED if running it from 5V.  The forward voltage of an IR
LED is nowhere near 5V, so without a resistor something will have to give.
That will probably be one or both of the PIC pins.  Third, IR LEDs can take
a lot more current than most visible LEDs of the same size.  Check the data
sheet.  Most likely you can put at least 100mA thru it when it's on, for an
average current of 50mA.  Fourth, you can't drive that much current with a
PIC pin.  Connect the CCP output pin to a resistor, other end of resistor to
base of NPN transistor, emitter to ground, collector to LED cathode, LED
anode to another resistor, and the other end of that resistor to 5V.  Size
the LED resistor to get 100mA (depending on your LED) thru the LED when the
transistor is on.  Size the base resistor to make sure the transistor is
saturated when sinking the LED current.  With a jellybean transistor like
2N4401 you can count on a gain of at least 50.  That means you need at least
2mA base drive to support 100mA LED current.  A 2Kohm base resistor will get
you a little more than that, which is probably a good answer.

> a 40khz pulse should be output on the IR LED for a short period of time
> (about a second).

That's not a short period of time.  The IR detector will probably want to
see pulses from 10 to 50 cycles.  Much beyond that and it's automatic gain
circuitry will decide the pulse is ambient and set its threshold to filter
it out.

> I have included a simple Excel spreadsheet I made to help calculate
> the settings for the CCP Module for the PIC 16F628, which I used to
> come up with my values for the program.

That's silly.  What happens when you change the PIC oscillator frequency or
want to use a different IR modulation frequency?  These should be parameters
directly in your code, probably a top level include file, and everything
else should be computed from that at assembly time.


******************************************************************
Embed Inc, Littleton Massachusetts, (978) 742-9014.  #1 PIC
consultant in 2004 program year.  http://www.embedinc.com/products

2005\11\12@193151 by Scott Pierce

flavicon
face

>>a 40khz pulse should be output on the IR LED for a short period of time
>>(about a second).
>
>That's not a short period of time.  The IR detector will probably want to
>see pulses from 10 to 50 cycles.  Much beyond that and it's automatic gain
>circuitry will decide the pulse is ambient and set its threshold to filter
>it out.

That makes  sense, thank you for pointing that out.  I knew the receiver
would filter out sources it thought to be ambient light but I wasn't sure
what kind of time durations were acceptable.

>>I have included a simple Excel spreadsheet I made to help calculate
>>the settings for the CCP Module for the PIC 16F628, which I used to
>>come up with my values for the program.
>
>That's silly.  What happens when you change the PIC oscillator frequency or
>want to use a different IR modulation frequency?  These should be parameters
>directly in your code, probably a top level include file, and everything
>else should be computed from that at assembly time.

I'm not quite sure exactly what you mean by having the parameters directly
in my code.  I understand that changing the clock frequency and IR
modulation frequency would require calculating new parameters...but that's
what the spreadsheet is for.  You just plug in your clock and modulation
frequency, and prescalar and it tells you what parameters you need to plug
in.  I got the formulas directly from the Midrage Reference Manual and made
the spreadsheet so I wouldn't have to calculate them by hand each time I
wanted to change anything.  I also plugged in the Midrange Manual example
numbers to make sure my spreadsheet was correct.  The spreadsheet provided
the same results the manual showed.

Thank you for the suggestions for the transistor and not driving the IR LED
directly from the I/O pins.  Do you think an 2N3906 NPN would be
appropriate for driving an IR LED at these frequencies?

Also, my end goal is to do IR Serial Communication.  I haven't been able to
find very much information for this on the net when it comes to PIC
Micros.   Are there any things I should avoid when deciding on how to
implement this?

Thanks again.  All suggestions are welcome!

Sincerely,
Scott Pierce

2005\11\12@201910 by olin piclist

face picon face
Scott Pierce wrote:
> I'm not quite sure exactly what you mean by having the parameters
> directly in my code.  I understand that changing the clock frequency
> and IR modulation frequency would require calculating new
> parameters...but that's what the spreadsheet is for.  You just plug in
> your clock and modulation frequency, and prescalar and it tells you
> what parameters you need to plug in.

But that's exactly the problem since it requires manually changing something
outside the code and copying a new value into the code.  If the clock
frequency and the desired IR modulation frequency are symbolic constants
defined at the top of the main include file, then you can use assembly time
computation to determine the CCP module setup.  You should get used to
always doing this with the oscillator frequency anyway.  Lots of things are
derived from that, like UART baud rate, timer 2 period, etc.

As examples of this kind of thing, see my UART_BAUD and TIMER2_USEC macros
in STD.INS.ASPIC at http://www.embedinc.com/pic.

> Do you think an 2N3906 NPN would be
> appropriate for driving an IR LED at these frequencies?

Yes.


******************************************************************
Embed Inc, Littleton Massachusetts, (978) 742-9014.  #1 PIC
consultant in 2004 program year.  http://www.embedinc.com/products

2005\11\13@000703 by Dmitriy Kiryashov

picon face
Hi Scott.

> Thank you for the suggestions for the transistor and not driving the IR LED
> directly from the I/O pins.  Do you think an 2N3906 NPN would be
> appropriate for driving an IR LED at these frequencies?

2N3906 is PNP. 2N3904 is NPN.


WBR Dmitry.

2005\11\13@005836 by Dmitriy Kiryashov

picon face
Hi Olin.

CCP pin to base of NPN  ( 2N4401 or 2N2222 , Vcbo about 50v )
collector to +5v, emitter to current limiting resistor
2nd pin of current limiting resistor to anode of LED
cathode of LED to ground

One resistor less and smaller pcb estate.

Any objections ?


WBR Dmitry.

PS. 2N4401 with hFE = 50 on 100 mA sounds scary :)



Olin Lathrop wrote:
{Quote hidden}

> -

2005\11\13@053748 by michael brown

picon face
From: "Dmitriy Kiryashov"


> Hi Olin.
>
> CCP pin to base of NPN  ( 2N4401 or 2N2222 , Vcbo about 50v )
> collector to +5v, emitter to current limiting resistor
> 2nd pin of current limiting resistor to anode of LED
> cathode of LED to ground
>
> One resistor less and smaller pcb estate.
>
> Any objections ?
>
>
> WBR Dmitry.
>
> PS. 2N4401 with hFE = 50 on 100 mA sounds scary :)

With an NPN transistor (2n3904, 2n2222 etc), I'd connect the emitter to
ground and the collector to the cathode of the LED.  The LED's anode
should be connected to the dropping resistor and the other end of the
resistor goes to Vcc.  IOW, I'd use it as a low side switch.  I'd
probably use something like a 1K resistor to limit base current.

2005\11\13@061448 by Dmitriy Kiryashov

picon face
Hi Michael.

And what would be the benefit of connecting like that ?
( besides it's being described in every book how to handle LEDs :)

WBR Dmitriy.

PS. Olin already outlined that way.


michael brown wrote:
{Quote hidden}

> -

2005\11\13@063014 by Jinx

face picon face

> With an NPN transistor (2n3904, 2n2222 etc)

ZTX transistors are much better for driving IR LEDs

http://www.zetex.com/3.0/b1-2.asp

2005\11\13@075437 by olin piclist

face picon face
Dmitriy Kiryashov wrote:
> CCP pin to base of NPN  ( 2N4401 or 2N2222 , Vcbo about 50v )
> collector to +5v, emitter to current limiting resistor
> 2nd pin of current limiting resistor to anode of LED
> cathode of LED to ground
>
> One resistor less and smaller pcb estate.
>
> Any objections ?

Nope, sounds like a decent way to hook it up.

> PS. 2N4401 with hFE = 50 on 100 mA sounds scary :)

The data sheet I have (Fairchild) guarantees minimum Hfe of 80 at 10mA and
100 at 150mA.  50 at 100ma should be safe, but I can't argue with a little
more base current to be sure.  In your emitter follower arrangement this
isn't an issue since the base will grab whatever current it needs, although
it may glitch the PIC output pin a little.


******************************************************************
Embed Inc, Littleton Massachusetts, (978) 742-9014.  #1 PIC
consultant in 2004 program year.  http://www.embedinc.com/products

2005\11\13@090814 by michael brown

picon face

----- Original Message -----
From: "Olin Lathrop" <spam_OUTolin_piclistTakeThisOuTspamembedinc.com>
To: "Microcontroller discussion list - Public." <.....piclistKILLspamspam@spam@mit.edu>
Sent: Sunday, November 13, 2005 6:54 AM
Subject: Re: [PIC]: 16F628 CCP 40Khz IR Communication. Help needed!


{Quote hidden}

I don't understand.  What is the reason/advantage of hooking it up on
the high side?

> > PS. 2N4401 with hFE = 50 on 100 mA sounds scary :)
>
> The data sheet I have (Fairchild) guarantees minimum Hfe of 80 at 10mA
and
> 100 at 150mA.  50 at 100ma should be safe, but I can't argue with a
little
> more base current to be sure.  In your emitter follower arrangement
this
> isn't an issue since the base will grab whatever current it needs,
although
> it may glitch the PIC output pin a little.


2005\11\13@091954 by Jan-Erik Soderholm

face picon face
Scott Pierce wrote :

> I knew the receiver would filter out sources it thought
> to be ambient light but I  wasn't sure
> what kind of time durations were acceptable.

I'm close to 100% sure that any TSOPxxxx
data sheet specifies that.

Besides of that, the PWM module is perfect to drive
an IR link. Just switch a signle bit in some SFR to
generate the pulses...

Jan-Erik.



2005\11\13@214001 by Dmitriy Kiryashov

picon face
Hi Michael.

> I don't understand.  What is the reason/advantage of hooking
> it up on the high side?

1. One (base) resistor less compared to "standard" grounded emitter circuit
2. Transistor is biased as required with predictable output on emitter
  ( and current amplification does happen )
3. All LED cathodes are grounded ( good for common cathode config )
4. No current is "wasted" just for "control" after all :)
  ( everything goes in to enlite the LED :)

Proven approaches always good to be challendged :)


WBR Dmitriy.

More... (looser matching)
- Last day of these posts
- In 2005 , 2006 only
- Today
- New search...