Measuring Time between two IR sensor

Status
Not open for further replies.

Mohamed Slama

Member level 4
Joined
Nov 8, 2013
Messages
72
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Visit site
Activity points
443
Hi

I have 2 IR sensor(with its receiver) the distance between is 1Cm and i have a coin pass through them

i want to calculate the time from the coin hit the first IR sensor to leave the second IR sensor .

using MicroC Pro , 16f877A

Thanks
 

Just make the first event as interrupt and run the timer from there till the second event occurs. its simple

thank u for reply

can u write MicroC Code for interrupt and timer .because my major knowledge in image processing and c# >
 

i used one ir led connected to portb.fo

but the code didnt work
Code:
// LCD module connections
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC7_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISC7_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections


//variables
char Freq_txt[11];
unsigned int tmr1_Val=0;
float  Time_elapsed=0;
unsigned int OverNumb=0;
unsigned long Freq=0;
unsigned long Time=0;
unsigned short tmr0_cnt=100;
bit flag;
char TPer_txt[15];
float TPer=0;

void _Init()
{
  trisd.f0=0;
  lcd_init();
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Cmd(_LCD_CLEAR);




  //timer1 config
  intcon.f7=1;   //enable global interrupt
  intcon.f6=1;  //enable pref interrupt
  PIE1.f0=1; //Enables the TMR1 overflow interrupt
  //t1con.f1=0; //internal clock (f/4)
  //t1con.f3=1; //Oscillator is shut-off
  t1con=0b10111001;
 // t1con.f2=1; //Synchronize external clock input
  tmr1l=0; tmr1h=0; //clear timer1
}


//ISR
void interrupt()
{



 if(pir1.f0==1) //if timer1 is overlfowed
 {
   OverNumb++;  //increment number of overflow
   pir1.f0=0; //reset timer1 flag
 }

}




void main()
{
 trisb=1;
       _Init();





   // intcon.f7=0; t1con.f0=0;  //reenabling counting and global interrupts
   lcd_out(1,1, "Time Counter");
   delay_ms(100);
   for(;;)
   {

    if (portb.f0==0){

           t1con.f0=1;
          while(portb.f0==0);
            intcon.f7=0; t1con.f0=0;  // glopal interrupt off - stop timer 1
                      }

               tmr1_Val = (TMR1H << 8) + TMR1L; //get high and low from timer1
                Time_elapsed =(OverNumb*65536)+10 ;
                            Time_elapsed/=100;

                floatToStr(Time_elapsed,TPer_txt);   //convert Period to string
              lcd_out(4,1, TPer_txt); lcd_out_cp("S");




           tmr1l=0; tmr1h=0; tmr1_Val=0;  OverNumb=0; Freq=0; TPer=0; //reset variables
           Time_elapsed=0;

           intcon.f7=1; t1con.f0=1;  //reenabling counting and global interrupts
        }
   }





/*
To count more than 65536Hz using timer1 16bit use this formula:

Freq = (2^16  * OverflowNumber ) + timer1 count

*/

/*
To combine to 8bit into a 16 bit use:

TMR1_value = (TMR1H << 8) + TMR1L;

*/



/*
To calculate the PRELOAD value use this:

PRELOAD = 256 - ( Time you need * Frequency ) / ( 4 * Prescaler)

Example: if 10ms is needed we do this: 256-(0.010*16000000)/(4*256)= 99
so  99 should be the RTCC_PRELOAD value.

*/
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…