PIC16f690, initialisation of ports, LEDS,reading from a pin

Status
Not open for further replies.
I have the hardware working(proximity detector circuit). All i have left is alot of programming. i did not post the specifications of my project on this forum yet. im trying to get the basic stuff working.
 
Code:
Doesnt IR travel way faster than you can measure?

if the proximity sensor is clipped at the side of a tank:

1. at the bottom of the tank(furthest distance from the proximity sensor)

i know the max distance which=height of tank, and i will know the time from reception till detection of the infrared light(since i will be using a timer )

hence i can find the velocity of ir light

2. the min distance is at the top of the tank were the sensor is placed
similarly i can find the velocity of ir light.


i will then average the velocity of infrared light at max and min height from the proximity circuit to get a more accurate value which i will then plug into
an equation. so now i know the velocity of infrared light which is a constant.


using distance=speed X time........(1)

subtracting (1) from the height of tank, i can find the level of the liquid.



well thats my plan.
 
Infrared liquid level detector
 
Last edited:
You can do it you blink the IR led and count the blinks you get back.
the closer the water the less blinks you'll get back to count

Here some code it's from the pickit2 Cd
Code:
  while (1 == 1)              //  Do Forever
    {
        NOP();
        for (i = 0; i < 3500; i++);  //  Delay 50 ms
        NOP();

        TMR2 = 0;               //  Setup for Sending IR Signal
        TMR2IF = 0;             //  Reset TMR2 Interrupt Request Flag
        T2CON = 0b01111100;     //  Enable TMR2 for 16 Cycles
        TRISC5 = 0;             //  Output to IR LED

        i = 0;                  //  Look for Input Signals
        while (!TMR2IF)         //  Wait for Completion of Output
            if (0 == RC4)
                i = i + 1;      //  Increment the Counter

        TRISC5 = 1;             //  Finished, Turn Off Output
        T2CON = 0b01111000;     //  Turn OFF TMR2

        if (i > 15)             //  Signal Received
            RC0 = 1;            //  LED On
        else
            RC0 = 0;            //  Nothing, LED Off

    }  //  elihw
}  //  End cIR
 
Last edited:
o thanks guys. i will use that info and get back to you'll. its 23:00 here, i got to wake at 5am to get ready for university. Thanks for all the help. see youll again.
 
Water bounces IR light so it you time your Ir blink like this
**broken link removed**
 
Code:
Presumably you've never heard of the 'speed of light'?


Wikipedia says "299,792,458 metres per second", so in 1uS it travels 300m.




this is from wiki as well:
The name means below red (from the Latin infra, "below"), red being the color of the longest wavelengths of visible light. IR light has a longer wavelength (a lower frequency) than that of red light.


i have heard of the speed of light. you get infrared light , ultra violet light...uhmmm i was thinking that since the wavelength is different for different types of lightt, then i cant apply
Wikipedia says "299,792,458 metres per second", so in 1uS it travels 300m.

because according to:

velocity=frequency x wavelength

different wavelengths would mean different velocity


I think i over think stuff. Thanks Nigel from what i read, you are right.
 
Why's that? Got a diagram?

I don't understand that either?, and I'm seriously dubious about it.

One way you CAN do it, used in the robot Cybot, is to send a digital word with varying powers in each of the bits, so only the ones which bounce back are read as one's, and the others read as zero's.
 
use of timer 1 to calculate distance.

What do you guys think about this method for calculating the distance?
Any simpler method?

Code:
//////////////////////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//////////////////////
 
Last edited:
How fast is your processor running, and how deep is your tank?.

Apply a little commonsense to the values - a reflection from 150m will take 1uS, from 15m it will take 0.1uS, and from 1.5m it will take 0.01uS.
 
How fast is your processor running, and how deep is your tank?.

1. OSCCON|=0x60; //set fosc to 4Mhz

2. I want to use a 50cm deep tank

It is a continuous level detetector.

3. the calculation of distance is in centimeters
 
Last edited:
Then do the maths - how long will it take light to travel 100cm?

1/300=0.003333333cm

1us--->300m

1us--->30000cm

hence
1us----->30000cm
?us------>100cm

?us=1/300us=0.003333333us

OOOpzzzzzz, i meant us NOT cm. So Sorry.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…