View Single Post
  #7   Spotlight this post!  
Unread 21-03-2006, 22:36
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Ultrasonic will not work with another Ultrasonic plz help

Here is a code fragment for managing 3 ultrasonic sensors:

Code:
int Service_Ultrasonic_Sensors (void)
{

  ultra_counter++;
  switch (ultra_counter)		// Process Ultrasonic Transducer
	{
	case 1:
	  for (pulsecount = 0 ; pulsecount < PULSE_WIDTH ; pulsecount++)
		{
		rc_dig_out10 = 1;		//	Make another pulse
		};
	  rc_dig_out10 = 0;			//	The pulse is over
	  break;
	case 2:
	  for (pulsecount = 0 ; pulsecount < PULSE_WIDTH ; pulsecount++)
		{
		rc_dig_out11 = 1;		//	Make another pulse
		};
	  rc_dig_out11 = 0;			//	The pulse is over
	  break;
	case 3:
	  for (pulsecount = 0 ; pulsecount < PULSE_WIDTH ; pulsecount++)
		{
		rc_dig_out12 = 1;		//	Make another pulse
		};
	  rc_dig_out12 = 0;			//	The pulse is over
	  break;
	case 4:
	  if (ultra_left_flag == TRUE)
		{
		INTCONbits.GIEL = 0;		//	Disable Low Priority Interrupts
		ultra_left_time = ultra_left_stop - ultra_left_start;
		INTCONbits.GIEL = 1;		//	Enable Low Priority Interrupts
		ultra_good_count++;
		}
	  else
		{
		ultra_left_time = -1;
		ultra_bad_count++;
		};
	  if (ultra_front_flag == TRUE)
		{
		INTCONbits.GIEL = 0;		//	Disable Low Priority Interrupts
		ultra_front_time = ultra_front_stop - ultra_front_start;
		INTCONbits.GIEL = 1;		//	Enable Low Priority Interrupts
		ultra_good_count++;
		}
	  else
		{
		ultra_front_time = -1;
		ultra_bad_count++;
		};
	  if (ultra_right_flag == TRUE)
		{
		INTCONbits.GIEL = 0;		//	Disable Low Priority Interrupts
		ultra_right_time = ultra_right_stop - ultra_right_start;
		INTCONbits.GIEL = 1;		//	Enable Low Priority Interrupts
		ultra_good_count++;
		}
	  else
		{
		ultra_right_time = -1;
		ultra_bad_count++;
		};
	  ultra_left_flag = FALSE;
	  ultra_front_flag = FALSE;
	  ultra_right_flag = FALSE;
	  ultra_counter = 0;
	  break;
	default:							// for any unexpected result
	  ultra_left_flag = FALSE;
	  ultra_front_flag = FALSE;
	  ultra_right_flag = FALSE;
	  ultra_counter = 0;
	  break;
	};
   
}
The code above can be called once per communications loop or via a timer interrupt, et cetera...

the variables ultra_bad_count and ultra_good_count were used during development and can be deleted.

An example interrupt routine is:

Code:
void Service_DIO4_Interrupt(unsigned char state)
{
	if (state == 1)						 // rising-edge interrupt
	{
	ultra_front_start = Read_Timer_3 ();	 // Read Start Time.
	}
	else									// falling-edge interrupt
	{
	ultra_front_flag = TRUE;
	ultra_front_stop = Read_Timer_3 ();	  // Read Stop Time
	}
}
The user routine has only to test for the time to be greater than zero to know that it is valid data.

Hopefully, this helps.


Mike
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...