Easy IR


 Back To Index

This is a simple software routine showing how to generate 40KHz PWM on any I/O-pin. Use this for any PIC that doesn't have internal hardware PWM option or if you need to use PortB.3 for other purposes other than PWM generation. This example uses PortB.1, but can be changed to use any I/O-pin on the PIC that can output high & low signals.

Using IR_IN = PortB.0 immediately reads the status of the IR detector output pin connected to PortB.0 after the carrier generation routine has completed, and before the IR detector output pin changes state.

'****************************************************************
'*  Name    : Easy_IR.bas                                       *
'*  Author  : Bruce Reynolds                                    *
'*  Notes   : Generate IR carrier on any pin for remote control *
'*          : and object detection                              *
'****************************************************************
DEFINE OSC 20
TRISB=%00000001 ' PortB.0=input, rest=outputs
PortB=%00000010 ' LED=off on start
                   '                 430       IRLED
                   ' PortB.1 -------/\/\/\/------|<|----------+5V

NumCycles VAR BYTE ' Holds number of 40KHz carrier cycles
IR_IN     VAR BIT  ' IR detector input storage bit
IR_IN=1            ' Start with 1 | Indicates no object detected
GOTO Main          ' Jump over sub to Main routine

                  '                           ~47%
                  '                     High duty-cycle
Pulse:            '                +5V     ___________         ____
@   bcf portb,1   ; 200nS, LED=on          |         |         |
    PAUSEUS 13    ' 13uS                   | 11.8uS  | 13.2uS  |
@   bsf portb,1   ; 200nS, LED=off  0  ____|         |_________|
    PAUSEUS 11    ' 11uS                   |                   | 
@   decfsz _NumCycles,F ; 200nS            |<----  ~25uS  ---->|
@   goto _Pulse   ; 400nS, LED=off
    IR_IN=PortB.0 ' Read detector output
    RETURN        ' Return to Main    

Main:
    NumCycles=20  ' 20 carrier cycles
                  ' Most IR detectors require >=10 carrier cycles
                  ' Adjust as required to adjust time of 40KHz burst
    GOSUB Pulse   ' Go check for obstacle
    IF IR_IN=0 THEN Obstacle
    LOW Portb.2   ' No obstacle, indicator LED=off
    GOTO Main     ' Return to Main
                  '                  430         LED
Obstacle:         ' PortB.2 -------/\/\/\/\-------|>|-------GND
    IR_IN=1       ' Reset object detection indicator bit 
    HIGH Portb.2  ' Turn on LED when object is detected
    GOTO Main     ' Return to Main

You can also cut & paste the Pulse routine into other code. Then load the number of 40KHz carrier cycles required into byte variable NumCycles, use GOSUB to branch to the routine and generate the number of 40KHz carrier cycles loaded into NumCycles.

Useful for communicating with consumer infrared electronics such as televisions, vcrs, and much more.

Pulse:           
@   bcf portb,1   ; 200nS, LED=on      
   
PAUSEUS 13    ' 13uS              
@   bsf portb,1   ; 200nS, LED=off 
   
PAUSEUS 11    ' 11uS                
@   decfsz _NumCycles,F ; 200nS
@   goto _Pulse   ; 400nS, LED=off


 Back To Index

Copyright © 2007 Reynolds Electronics
http://www.rentron.com