//////////////////////CALCULATING DISTANCE///////////////////////
void enable_interrupt(void)
//This function is basically designed to set the specific bits of the timer and capture registers
{
CCP1CON=0x05; //bits 3-0 of the capture control register are set to 0101.
//This mode is to capture every rising edge of the pulse being
//transmitted and then received
TMR1IF = 0; //The interrupt flag bit of the PIR1 register is cleared before
//enabling the interrupt.
CCP1IF = 0; //Before a capture is made, the Interrupt Request Flag bit
//CCP1IF of the PIR1 register is cleared.
CCP1IE = 1; //The capture interrupt enable bit is set to enable the interrupt
TMR1IE = 1;
//An interrupt will occur immediately provided that the GIE and PEIE bits of the INTCON
//register are set.
PEIE = 1;
GIE = 1;
}
void interrupt ISR(void)
{
if (TMR1IF) //if there is an overflow
{
TMR1IF = 0; //and the flag is then cleared
TMR1_OF++;
}
if (CCP1IF) //if there is a capture
{
CCP1IF = 0; //Zero Capture flag
if (Cap_1st) //allow only 1 capture to be taken
{
Cap_1st=0; //Cap_1st is cleared as if another value is captured before the previous value is read
//then the
t_capL=CCPR1L; //lower byte of the capture register
t_capH=CCPR1H; //higher byte of the capture register
capture_status=1; //signal that a capture occured is enabled.
}
}
}
void calc_distance(void)
{
long sample=0;
i=1; j=0;
Cap_1st = 1;
capture_status=0; //signal that a capture occured is disabled
TMR1ON = 0; //stop timer
//tH=TMR1H; tL=TMR1L;
tH=0; tL=0; t_capL=0; t_capH=0; //initialise capture
TMR1_OF=0;
Transmit40khz();
DelayUs(100);
enable_interrupt(); //Interrupt is enabled to capture time when the rising edge occurs
TMR1ON = 1; // start timer 1
//when timer 1 starts, the enable_interrupt function will be called to
//capture the time from the 40kHz signal once the first rising edge of the transmitted
//pulse occurs
//A continuous loop is run such that when the signal to state that a capture has occurred
// is 0, then the 40 kHz will be transmitted and received when sending out 10 pulses.
//Once this condition is satisfied, the global interrupt enable bit will be cleared to
//prevent the interrupt from occurring when an interruption is in progress.
//The timer is then stopped by disabling TMR1ON and TMR1IE.
while((capture_status==0)&&(TMR1_OF<1));
GIE = 0; //disable global interrupt
TMR1ON = 0; //stop timer 1
TMR1IE = 0; // disable timer 1 interrupts on overflow//
//Another condition arises such that if the signal to state that a capture has occurred
// is then enabled, then the sample time is calculated and stored in an array. The sample
// time is calculated via the difference between high byte of the capture time and the high
// byte of Timer1 added to the low byte of the capture time and the low byte of Timer1.
//The index is incremented. This then deduces the end of the function to get the samples.
//However, if the index is less than 20, then the function will continue.
if (capture_status==1)
{
sample = ((t_capH)*0x100)+(t_capL);
i = 2;
GIE=0; //Global interrupt is disabled to avoid another interrupt from occurring while
// an interrupt is taking place
t_capH = 0;
t_capL = 0;
}
if (i==1)
distance = 0;
//if and only if a capture is made will the process of distance calculation continue.
if (i==2)
{
time = sample * 1e-6; //since each count takes 1us to complete, this value is multiplied
//to the sample time.
//Distance is calculated via multiplying the time and distance light travels
//light travels 300m in 1 us.This result is divided by 2
//as speed travels twice the measured distance.
distance=(time*30000)/2; //[B]calculation in cm[/B]
//liquidLEVEL=heightOFtank-distance im still working on this part
}
return;
}
////////////////////END CALCULATING DISTANCE//////////////////////