|
|
View previous topic :: View next topic |
Author |
Message |
rahi_044
Joined: 08 Nov 2011 Posts: 3
|
Ultrasonic Distance Finder using 4MHz Oscillator |
Posted: Sun Dec 11, 2011 1:35 am |
|
|
I am trying to measure distance using 4MHz external oscillator and PIC16F877A. But the timer value is not stable. Here is my code:
Code: |
#include <16F877A.h>
#device adc=8
#FUSES NOWDT, XT, NOPUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=4000000)
#include <lcd.c>
int16 flg = 0;
int16 flgTemp = 0;
int16 distance = 0, time = 0;
#int_ccp1
void isr()
{
/*
rise = CCP_1;
//CCP_1 is the time the pulse went high
fall = CCP_2;
//CCP_2 is the time the pulse went low
pulse_width = fall - rise;
//pulse width
*/
}
#int_EXT
void EXT_isr(void)
{
flgTemp = flg;
flg = 25;
}
#int_TIMER0
void TIMER0_isr(void)
{
flg ++;
if(flg > 20) flg = 0;
}
void main()
{
set_tris_a(0b00000001);
lcd_init(); // initiating timer
printf(LCD_PUTC, "\fTest 2");
delay_ms(1000);
setup_timer_2(T2_DIV_BY_1, 24, 1); // For 4 MHz Oscillator
set_pwm1_duty(12);
setup_CCP1(CCP_OFF);
output_low(PIN_C2);
delay_ms(10);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8); // (RTCC_EXT_L_TO_H|RTCC_DIV_32); //(T1_INTERNAL|T1_DIV_BY_8); // initiating timer
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // initiating timer
enable_interrupts(INT_EXT); // Enable Interrupts
enable_interrupts(GLOBAL);
while(true)
{
switch(flg)
{
case 0: // 0 ms
setup_ccp1(CCP_PWM);
set_timer1(0); // setting timer zero
break;
case 1: // 2 ms
setup_CCP1(CCP_OFF);
output_low(PIN_C2);
break;
case 25: // EXT_INT
disable_interrupts(INT_EXT); // Disable Interrupts
//disable_interrupts(GLOBAL);
//while(input(PIN_A0)) // Wait for high state of echo pin
//{ }
time = get_timer1(); // Getting the time
distance = time*0.028 + 1.093 ; // Calculating the distance
printf(lcd_putc,"\fTime :%Lu \nDistance :%Lu",time,distance);
delay_ms(2000);
flg = flgTemp;
enable_interrupts(INT_EXT); // Enable Interrupts
//enable_interrupts(GLOBAL);
break;
default:
break;
}
}
}
|
Can anyone give me an implemented CCS code please, I will convert it on my demand. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sun Dec 11, 2011 6:20 am |
|
|
Several comments, a lot relating to 'how does your hardware work':
1) You talk about an external oscillator, but have 'XT' selected, which is for driving a crystal.
2) Why are you looking at a pulse width?. Normally for ultrasonic, you have a pulse 'burst' being sent, and a similar burst being received. The hardware needs to have a narrowband filter detecting the signal train, followed by an peak detector circuit to give an 'envelope' corresponding to the signal from the transducer. Normally with an AGC circuit so that the sensitivity increases when signals are not seen. The code first, needs to ignore signals for a short time during and after the transmission burst - otherwise you will detect this, not the reflection. Then look for the rising edge on the envelope.
You can't directly look for pulses from the transducer, or you _will_ see every percussive signal in range....
3) Why fiddle with interrupts?. At the end of the day, the code is doing nothing while you wait for a return, so do this in the main loop. Just set a timer to zero when you send the pulse burst, and read it when you see the return. If it overflows, then you have almost certainly not had a successful reflection, so need to try again.
4) Why waste processor time using int16 for flg, and flgtemp. Values seem to always be 30 or less...
5) What on earth is flgtemp for?. Seems to be wasting time and space.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 11, 2011 1:22 pm |
|
|
1. Post the manufacturer and model number of your Ultrasonic Range Finder.
2. Post a link to the webpage that has the data sheet for it.
3. Post your CCS compiler version. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|