View previous topic :: View next topic |
Author |
Message |
Lion Guest
|
Really Really Need Help on Ping Ultrasonic sensor |
Posted: Thu May 07, 2009 8:23 pm |
|
|
Hello everyone,
I have already search thie forum and google it but I do not find the answer.
I am using Ping Ultrasonic sensor(three Pins)
I try to use 7 ultrasonics at the same time. Is it possible?
I am using PIC 16F877 microcontroller and 2840 development board from Basic Micro.
I can use one Ping Ultrasonic. It is work perfectly but how to use 7 Pings at the same time?
I mean that I use timer1 to count the time (how long the echo come back)and to caculate the distance by echo.
There are 7 Ping Ultrasonics but I do not have 7 timers so how should I do ?
Your help will be appreciated.
Thank you very much. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
got code ??? |
Posted: Thu May 07, 2009 8:30 pm |
|
|
got any working code to show of what you did so far?
there are several ways to do what you want to do - i think
BUT
U R not giving much info on what you did already -
so a good start would be to show what YOU Have accomplished
and what your limits are now.
then help may be possible |
|
|
Lion Guest
|
This is my code |
Posted: Thu May 07, 2009 9:06 pm |
|
|
hello,
Thank you for your reply.
This is my code
Code: |
#include <16F877.h>
#device ADC=10
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include<stdio.h>
#include <math.h> // for temperature sensor
#include <stdlib.h>
int16 overflow_count;
#int_timer1
void timer1_isr()
{
overflow_count++;
}
void main()
{
int16 time;
//unsigned char time;
float distance;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
while(1)
{
delay_ms(500);
output_high(PIN_B4);
delay_us(5);
output_low(PIN_B4);
while(!input(PIN_B4))
set_timer1(0); //The problem is here. If there are 7 Ultrasonic, I
// need 7 timers to count the time but I just got three
overflow_count=0; // Is there any way to do that???
while(input(PIN_B4))
disable_interrupts(global);
time=get_timer1(); // count the time
time=time+(overflow_count*65535);//
}
} |
|
|
|
Guest
|
|
Posted: Thu May 07, 2009 10:33 pm |
|
|
1- post your FUSES ??
2- I'm missing where in your Timer1 ISR you RESET TMR1IF
3 another approach to your desire
is to zero out timer 1 ,
then record a count for each ping return in 7 different variables - and combine 7 ping returns to ONE external Or gate that fires a B port interrupt
then do the math on each variable -
a bit more coding - but maybe doable
BTW; those fixed delays need to be done using timer0
- so the focus of the main () routine can do something useful.
beyond count cycles. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Thu May 07, 2009 11:44 pm |
|
|
You can multiplex your sensors. So you are not measuring all 7 sensors at once, but one after another. So you use the same timer every time and you do not need to wait after one measurement, but you fire the next sensor. And if the obstacles are all 10m away you will need ~233ms to process all sensors... less if there are obstacles closer.
I think that if the sensors are pointed in same direction there will be interference from others, so it is not a good way to fire them all at once. |
|
|
Lion Guest
|
|
Posted: Fri May 08, 2009 1:49 am |
|
|
bungee- wrote: | You can multiplex your sensors. So you are not measuring all 7 sensors at once, but one after another. So you use the same timer every time and you do not need to wait after one measurement, but you fire the next sensor. And if the obstacles are all 10m away you will need ~233ms to process all sensors... less if there are obstacles closer.
I think that if the sensors are pointed in same direction there will be interference from others, so it is not a good way to fire them all at once. |
Thanks for you guys' reply.
I got one more question is that how to change the PIN.
I mean that I follow the suggestion to try to fire each sensor one by one and I also use Array to save each value but how to change the pin from one PIN to another PIN. For example, I use Port B from RB1~RB7 to accept the signal but how to change the PIN for PIN_B0 to PIN_B7
your help will be appreciated.
Thank you so much. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Fri May 08, 2009 9:07 am |
|
|
You could do that in couple diferent ways. One simple and straight forward is to use switch statment.
Code: |
switch (Sensor)
{
case 0: output_high(Pin_b1); Delay_us(5); Output_low(Pin_b1);
break;
case 1: output_high(pin_b2); Delay_us(5); Output_low(Pin_b2);
break;
.......
}
|
Something like that. On other hand you could write to whole port at once. You would just shift the current sensor.
Code: |
sensor=1;
While (sensor!=0b10000000)
{
output_B(sensor);
delay_us(5);
output_b(0);
while(!input_a()&sensor)
set_timer1(0);
.......
sensor=sensor<<1;
}
|
A couple of rough ideas, I think that you'll manage from here. |
|
|
Lion Guest
|
I still can not solve the problem |
Posted: Sat May 09, 2009 9:36 pm |
|
|
Hello everyone.
I tried that way which you guys' suggestion but it does not work.
My code is following.
Code: |
for (x=1;x<5;x++)
{
z=1;
switch(z)
{
case 1:
ultrasonic code
z++;
break;
case 2:
ultrasonic code
z++
break;
case 3:
ultrasonic code
z++
break
case 4
ultrasonic code
break;
}
} |
But it still does not work.
I saw that there is a code which was written like following.
Code: | int32 pin = PIN_B3
output_high(pin);
output_low(pin);
while(!input(pin))
......function code......
while(input(pin))
.......function code..........
pin++ |
But my compiler do not accept this kine of syntax like
or
By the way, I am using 16f877, MPLAB 8.0. I am not sure about my compiler version.
Anyone can get me a hand?
Any help will be really appreciated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 09, 2009 10:33 pm |
|
|
Quote: | I am not sure about my compiler version. |
Look at the top of the .LST file. The version number is given there.
It's a 4-digit number in this format: x.xxx
The .LST file is in your project directory. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
Re: I still can not solve the problem |
Posted: Sun May 10, 2009 3:44 am |
|
|
Instead of defining Pin variable again, you could do it like that:
Code: |
int8 current_pin;
output_high(PIN_B0+current_pin);
output_low(PIN_B0+current_pin);
while(!input(PIN_B0+current_pin))
......function code......
while(input(PIN_B0+current_pin))
.......function code..........
|
Where variable current pin goes from 0 to 7 |
|
|
Lion Guest
|
|
Posted: Mon May 11, 2009 5:53 pm |
|
|
Hello Bungee, PCM Programmer,
Thank you for you guys' reply.
My network was out of order so I can not reply during past days.
I am sorry about that.
Hello PCM programmer,
My compiler version is 3.222. Your suggestions is always useful for me.
Hello Bungee,
I will try your suggestion tonight and let you know the result.
Again. Thank you for your help.
I hope that it can work.
Thank you so much. |
|
|
Lion Guest
|
Hello, I come again |
Posted: Sat May 16, 2009 3:28 am |
|
|
Hello everyone,
I get the problem. I tried two codes but it still can not work.
Any help will be appreciated.
Thank you so much.
My First code:
For this code, I tried to use 'z' to get in switch loop but it always print out 'default'. Any one can get me some suggetion?
Code: |
for(x=0;x<4;x++)
{
z=1;
switch (z)
{
case 1:
delay_ms(500);
output_high(PIN_B0);
delay_us(5);
output_low(PIN_B0);
while(!input(PIN_B0))
set_timer1(0);
overflow_count=0;
while(input(PIN_B0))
disable_interrupts(global);
z++;
break;
case 2:
delay_ms(500);
output_high(PIN_C0);
delay_us(5);
output_low(PIN_C0);
while(!input(PIN_C0))
set_timer1(0);
overflow_count=0;
while(input(PIN_C0))
disable_interrupts(global);
z++;
break;
case 3:
delay_ms(500);
output_high(PIN_D0);
delay_us(5);
output_low(PIN_D0);
while(!input(PIN_D0))
set_timer1(0);
overflow_count=0;
while(input(PIN_D0))
disable_interrupts(global);
z++;
break;
case 4:
delay_ms(500);
output_high(PIN_D2);
delay_us(5);
output_low(PIN_D2);
while(!input(PIN_D2))
set_timer1(0);
overflow_count=0;
while(input(PIN_D2))
disable_interrupts(global);
break;
default:
printf("%d do not get in switch\n\r",x);
|
For Bungee,
I tried your suggetion but it does not work.
I got four errors==> "Error 27: Expression must evaluate to a constant"
The code is following:
Code: |
int8 current_pin;
while(1)
{
for(x=0;x<4;x++)
{
current_pin=0;
enable_interrupts(global);
delay_ms(500);
output_high(PIN_B0+current_pin); ==> Error 27
delay_us(5);
output_low(PIN_B0+current_pin); ==> Error 27
while(!input(PIN_B0+current_pin)) ==> Error 27
set_timer1(0);
overflow_count=0;
while(input(PIN_B0+current_pin)) ==> Error 27
disable_interrupts(global); |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 16, 2009 10:31 am |
|
|
Quote: | For Bungee,
I tried your suggetion but it does not work.
I got four errors==> "Error 27: Expression must evaluate to a constant"
My compiler version is 3.222.
|
Version 3.xxx only accepts CCS pin constants for the input() function
and other i/o functions. It does not allow a variable for the parameter. |
|
|
Lion Guest
|
|
Posted: Sat May 16, 2009 2:32 pm |
|
|
PCM programmer wrote: | Quote: | For Bungee,
I tried your suggetion but it does not work.
I got four errors==> "Error 27: Expression must evaluate to a constant"
My compiler version is 3.222.
|
Version 3.xxx only accepts CCS pin constants for the input() function
and other i/o functions. It does not allow a variable for the parameter. |
Hello PCM programmer,
If I want to do this function, which version I should use?
Do you know how much it will be cost?
Thank you so much |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 16, 2009 2:49 pm |
|
|
Here is a function for vs. 3.xxx that allows a variable for the pin value:
http://www.ccsinfo.com/forum/viewtopic.php?t=25280&start=4
The pin parameter must be still a CCS value, as listed in 16F877.h,
(eg. PIN_B0, PIN_B1, etc.) but it can be in a variable instead of being a
constant. |
|
|
|