Wireless XBee Pan/Tilt System

Program Download:

Project Archives

Xbee Pan/Tilt Transmitter

Xbee Pan/Tilt Receiver

The Software
           The full project used to create the firmware hex file in included for both the transmitter and the receiver in the downloadable archive files. Let's take a quick look at snippets of code from the two programs:
    -The Data Output via Transmitter
    -The Data Input via Receiver


           The transmitter's primary job is to take A/D input from the two trimpots and digital input from the button. All this input is input into an 8 bit variable called data. The variable data is split into chunks representing the 3 devices in 8 bits: bits 7-6 are reserved for button presses. Bits 5-3 represent the voltage value of the first trimpot. Bits 2-0 represent the voltage value of the second trimpot. After the variable is assembled, it's sent via the hardware serial peripheral and then does the same thing again.

Snippet of Xbee Tranmistter Code:

------------« Begin Code »------------
..
...
while(1){

data = 0;

/************* Convert Channel 0 *****************/
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH0 & 
ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0);
//Convrt A/D
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
//Read A/D
while( BusyADC()); // Wait for completion
	result = (ReadADC()); // Read result
Delay10TCYx(5);
CloseADC(); // Disable A/D converter 
/************* ***************** *****************/

data += (result>>7); // 3 Most Significant Bits Into Data

/************* Convert Channel 1 *****************/
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH1 & 
ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0);
//Convrt A/D
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
//Read A/D
while( BusyADC() ); // Wait for completion
	result = (ReadADC()); // Read result
Delay10TCYx(5);
CloseADC(); // Disable A/D converter 
/************* ***************** *****************/

data += (result&0b1110000000)>>4; // 3 Most Significant Bits Into Data

//If Button Pressed, Set Bit 6
if(PORTDbits.RD7 == 0)
	data += 0b01000000;
else
	data += 0b00000000;

Delay10KTCYx(5);
putcUSART( data );
}
...
..
------------« End Code »------------

           The receiver's main job is to decode the received 8 bit command and to tell which of the two servos what angle they should be turned, and also to tell the LED whether it should be on or off to prove that we can do remote switching with XBee modules.

Two Interrupt Service Routines

------------« Begin Code »------------
..
...
while(1){	
	if(data&0b11000000)
		PORTA = 0xFF;
	else
		PORTA = 0x00;

	if(DataRdyUSART())
		data = getcUSART();

	switch((data&0b00111000)>>3){
		case 0	:	servo1 = speed_0;
					break;
		case 1	:	servo1 = speed_1;
					break;
		case 2	:	servo1 = speed_2;
					break;
		case 3	:	servo1 = speed_3;
					break;
		case 4	:	servo1 = speed_4;
					break;
		case 5	:	servo1 = speed_5;
					break;
		case 6	:	servo1 = speed_6;
					break;
		case 7	:	servo1 = speed_7;
					break;
		default :
					break;
	}
	switch(data&0b00000111){
		case 0	:	servo0 = speed_0;
					break;
		case 1	:	servo0 = speed_1;
					break;
		case 2	:	servo0 = speed_2;
					break;
		case 3	:	servo0 = speed_3;
					break;
		case 4	:	servo0 = speed_4;
					break;
		case 5	:	servo0 = speed_5;
					break;
		case 6	:	servo0 = speed_6;
					break;
		case 7	:	servo0 = speed_7;
					break;
		default :
					break;
	}
	Delay1KTCYx(1);
}
...
..
------------« End Code »------------

           The software for this article is not meant to be overly complex in anyway. The main purpose is to give you a skeleton core of code to start with and use for your own project. So take a look through the code and let's see how it works in the system.



;