generating 40Khz pulses with a microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by fantabulous68, Aug 19, 2009.

  1. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    Software based ambient light detection.

    the sensor is controlled by a microcontroller to generate the low duty cycle pulses, you can benefit from the High and Low pulses to be able to detect any false readings due to ambient light. This is done by recording 2 different outputs of the sensor, one of them during the ON pulse (the sensor is emitting infra red light) and the other during the OFF time. and compare the results.

    Output recorded during:

    Software based deduction

    On pluse Off time
    1 0 There is definitely an Obstacle in front of the

    1 1 The sensor is saturated by ambient light, thus we
    can't know if there is an obstacle

    0 0 There is definitely Nothing in front of the sensor,
    the way is clear

    0 1 This reading is un logical, there is something wrong
    with the sensor.

    hardware connections:

    1)LED connected to PIN RC3 of micro controller;

    2)IR receiver IC TSOP48 has a digital output that can be directly modulated by a micro controller. The output of the IC is on pin 1 of the IC. The output of the receiver IC is connected to pin RC2 of micro controller.

    3) microcontroller generates low duty cycle pulses at pin RA2 at 40KHz.
    RA2 is connected to the CONTROL input which is linked to a transmitter

    the CONTROL input is connected to the source of low duty cycle pulses which could be from a 555timer but im not using a 555timer. i want to use the PIC16f690 to do that.


    The transmit burst is initiated from the microcontroller. The transmit burst is essentially a function that’s called whenever the microcontroller requests for it.
    The 40 kHz waveform has a period of:25us

    Software Algorithm
    A simple delay function will be used to toggle pin RA2 .I set the high part of the output to 5uS and the low part to 20uS.

    A variable is needed to keep track of the number of delays to indicate end of transmission. The total number of delays is equal to 20 (10*2). Ten is the number of pulses sent in a burst and 2 is the number of delays per pulse. Each time a delay occurs, a variable is incremented to keep track of the number of delays. Once this variable reaches 20 the transmission is complete.
    Pseudo code
    Set pin RA2 high. Run first delay of 5uS.
    Upon completion of delay set pin RA2 low. Run second delay for 20us.
    After each delay increment a variable. Keep track of variable till it reaches 20. At this point transmission is complete.

    here is my code:
    Code ( (Unknown Language)):
    2. #include <pic.h>
    3. #include "pic.h"
    4. #include "math.h"
    5. #include <stdio.h>
    6. #include <stdlib.h>
    7. #include "delay.h"
    9. bit ir1,ir2;       // 2 different outputs of sensor during On & Off pulse
    10. unsigned char ir; // stores final result
    15. void Transmit40(void)
    16. {  
    17.     ANSEL=0;        // Set inputs to digital
    18.     TRISC=0x04;     //making pin RC2 input
    20.     int t=0;
    21.     int cnt=0;
    23.     while(cnt<20)   //Tx 10 IR pulses/burst
    24.         {   if (t==0)              
    25.             {
    26.                 PORTA=PORTA^0x04;   //send IR, Toggles RA2
    27.                 DelayUs(5);
    28.                 ir1=RC2;// O/P IR bit1= O/P pin of receiver IC
    29.                 cnt++;
    30.                 t=1;
    32.             }
    33.             else if (t==1)             
    34.             {  
    36.                 PORTA=PORTA^0x04;   //stop IR, Toggles bit 2 of portA-IR control pin connected to RA2
    37.                 DelayUs(20);
    38.                 ir2=RC2;    // O/P IR bit2= O/P pin of receiver IC
    39.                 cnt++;
    40.                 t=0;
    41.             }
    44.             if ((ir1 == 1)&(ir2 == 0))  //Obstacle detected
    45.             {
    46.                 ir = 1;
    47.             }
    50.             else
    51.             {
    52.                 ir = 0;// The way is clear in front of the sensor
    53.             }
    56.             RC3=ir;   //light up led connected to RC3 if detect object;
    58.     }
    59. }
    61. void main(void)
    62. {
    65. Transmit40();
    67. }

    i am a beginner. need some assistance please for my project.

    i am generating low duty cycle pulses from the microcontroller to the transmitting infrared LED.

    i want my project "infra red proximity detector" to be immune to ambient light.


    when receiver detects an object then the LED must light up.

    when it does not detect an object, the LED must not light up.

    ambient light such as a desk lamp can falsely trigger the receiver.
    when i shine light onto the receiver i want the LED to remain off.

    when i programed the chip and put it into the circuit. The LED pulses but i dont want this.

    could some1 please assist me.
    Last edited: Aug 21, 2009
  2. nanovate

    Distinguished Member

    May 7, 2007
    Please use the Code tags --> [ CODE ][ /CODE ]

    so that:
    void main(void)



    looks like:
    Code ( (Unknown Language)):
    2. void main(void)
    3. {
    4.     // Formatted code is so much easier to read
    5.     // Now people can help you easier
    6.     Transmit40();
    7. }