The IR Range Sensor: Software

Program Download:

Main C File

IR Range Sensor Control
           The program we'll write will blink the led on the P40 board with faster frequencies the closer an object gets to the sensor. This is why we used the AN1 pin on the P40 board as the AN0 is connected to the led.

------------« Begin Code »------------
Chris @
Date: 2/5/2008
//Initial Include Libraries
#include <p18f452.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>
int result; //ADC Result Stored Here
void main(void)
//Initialize PORTA where the led is attached.
TRISA = 0x00;
PORTA = 0x00;
     PORTA = 0x00; //Turn the LED Off

     if(result == 0) //if result is 0, make it 1
           result = 1; //This avoids any 'false' led blinks

     Delay10KTCYx(result*2); //Pause the Program This Much Time
     PORTA = 0x01; //Turn the LED on
     Delay10KTCYx(result*2); //Pause the Program This Much Time

     OpenADC( ADC_FOSC_32 //Configure & Turn on ADC
                & ADC_RIGHT_JUST
                & ADC_8ANA_0REF,
                ADC_CH1 & ADC_INT_OFF );

     Delay10TCYx(5); //Pause for AD Conversion
     ConvertADC(); //Convert the Value
     while( BusyADC() ); //When ADC isn't busy get the adc value
           result = ReadADC();

     CloseADC(); //turn off adc's
------------« End Code »------------

           The code is fairly straight forward. There are port intiailizations at first, then a while loop that goes forever. The while loop blinks the led then gets a new ADC value using the c18 ADC library functions (i.e. openadc, closeadc, etc..). That new value named result then determines the new frequency to blink. The reason we multiply the frequency by 2 is that most of the numbers we encounter will be small and inorder to amplify the ir range effect we multiply the number and the change in distance becomes more visible.

           Take a look at the code, it's available for download at the top of the page. As always, it is heavily commented to make it as easy as possible for you guys to understand. If you have any questions about functionality just head over to the forums and ask. So, now let's see how well the radar system works!