Difference between revisions of "PIC32MX: XBee Wireless Round-trip Latency"
| Line 12: | Line 12: | ||
The arrows symbolize an elementary view of the signal flow during the timing process. |
The arrows symbolize an elementary view of the signal flow during the timing process. |
||
[[Image:Lab5.2010.Crct.png|Center]] |
|||
== Code == |
== Code == |
||
Revision as of 20:38, 13 February 2010
Original Assignment
Your assignment is to set up one PIC32 to send data to another using XBees for wireless RS-232 comm. One PIC will send a series of numbers; the other PIC will receive them and echo them back; and the first PIC will time the total time to send and receive back. To time, the first PIC can set a digital pin high, then set it low again when the original sent data is correctly received. It could also keep track of the count of a counter-timer. You can time how long it takes for the round-trip using an oscilloscope or by reading the counter-timer. You should try this for different baud rates and a couple of different sizes (in bytes) of the message. How low can you get the total round-trip time while guaranteeing low data loss?
Overview
Summarize briefly what the page is about.
Circuit
The following diagram depicts the circuit we used for our experiments. The theory behind the circuit is simple. The transmitter PIC TX pin is connected to the XBee RX pin. The XBee broadcasts the signal. The receiver XBee's RX pin is connected to the PIC's TX pin. The receiver PIC's RX pin is then connected to the XBee's TX pin.
The arrows symbolize an elementary view of the signal flow during the timing process.
Code
Transmitter Code
The code below is for the transmitter XBee chip. Note that the message string sent included only one period and was placed at the end. The program scans the message it receives looking for that period, so it knows that the whole message was received.
/**********************************************************************
Transmitter Code
Lab 5: High Speed XBee Latency
George Randolph
Nathan Hirsch
10 February 2010
This code is for the TRANSMITTER XBee chip. It takes a string of characters and
sends them from the PIC to the XBee and broadcasts it out into the world. The
reciever chip should receive the message and echo it back to the transmitter. At
the point when the transmitter first broadcasts the message, a pin on the PIC is
set to high. When the message is successfully echoed back, that pin is set low
so the whole process can be timed using an oscilloscope.
**********************************************************************/
// ****** Includes
#include "HardwareProfile.h"
// ****** Constants
#define DESIRED_BAUDRATE (115200) // The desired BaudRate Note: This must be changed when changing the XBee chip BaudRate
#define PIN_D1 LATDbits.LATD1 // These commands format pins D1-D4 as digital outputs.
#define PIN_D2 LATDbits.LATD2
#define PIN_D3 LATDbits.LATD3
#define PIN_D4 LATDbits.LATD4
// ****** Variables
unsigned int Time;
char RS232_Out_Buffer[64]; // The buffer may be changed if lots of data is being sent at a high BaudRate
char message;
// ****** Function Declarations
void initInterruptController();
// ****** Main Function
int main(void)
{
int pbClk;
//initUART2(pbClk);
// Configure the system performance
pbClk = SYSTEMConfigPerformance(SYS_FREQ);
mInitAllLEDs();
// define setup Configuration 2 for OpenUARTx
// IrDA encoded UxTX idle state is '0'
// Enable UxRX pin
// Enable UxTX pin
// Interrupt on transfer of every character to TSR
// Interrupt on every char received
// Disable 9-bit address detect
// Rx Buffer Over run status bit clear
#define config2 UART_TX_PIN_LOW | UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR | UART_ADR_DETECT_DIS | UART_RX_OVERRUN_CLEAR
// Open UART2 with config1 and config2
OpenUART2(config1, config2, pbClk/16/DESIRED_BAUDRATE-1); // calculate actual BAUD generate value.
// Configure UART2 RX Interrupt with priority 7
ConfigIntUART2(UART_INT_PR7 | UART_RX_INT_EN);
// Must enable glocal interrupts - in this case, we are using multi-vector mode
INTEnableSystemMultiVectoredInt();
//Set D1, D2, D3, and D4 as a digital output
LATD |= 0x001E; TRISD &= 0xFFE1;
while(1) //let interrupt handle the UART
{
if (swUser) // swUser NOT pressed
{
// Turn off all the lights to show that the message has not been sent
mLED_0_Off();
mLED_1_Off();
mLED_2_Off();
mLED_3_Off();
PIN_D1 = 0;
}
else
{
// Turn on all the lights to show that the message was sent
mLED_0_On();
mLED_1_On();
mLED_2_On();
mLED_3_On();
PIN_D1 = 1; // Set the pin high to begin the clocking
putsUART2("1234.\r\n"); // This is where the message goes. Note the period at the end of the message
break; // Code only sends the stuff in UART2 ONE time.
}
}
return 0;
}
// **** Interrupts
// UART 2 interrupt handler and is set at priority 7
void __ISR(_UART2_VECTOR, ipl7) IntUart2Handler(void)
{
// Is this an RX interrupt?
if(mU2RXGetIntFlag())
{
// Clear the RX interrupt Flag
mU2RXClearIntFlag();
message = ReadUART2();
mLED_3_On();
// Toggle LED to indicate UART activity
if (message == '.') // Note this is the last portion of the message we sent.
{
PIN_D1 = 0; // Put the Pin to low to finish the clocking
// Toggle some LEDs to show that the message was received correctly.
mLED_0_Off();
mLED_1_Off();
}
}
// We don't care about TX interrupt
if ( mU2TXGetIntFlag() )
{
mU2TXClearIntFlag();
}
}
Receiver Code
The code below is for the receiver XBee. It takes the message sent by the transmitter and echoes it back to the transmitter.
/****************************************************
Receiver Code
Lab 5: High Speed XBee Latency
George Randolph
Nathan Hirsch
10 February 2010
***************************************************/
//*** Includes
#include "HardwareProfile.h"
//*** Constants
#define DESIRED_BAUDRATE (115200) // The desired BaudRate. Note this must change if the XBee chip's BaudRate is changed
//*** Main Program
int main(void)
{
int pbClk;
unsigned char data; // The variable that will assing the things to when it reads the UART
pbClk = SYSTEMConfigPerformance(SYS_FREQ);
mInitAllLEDs();
#define config1 UART_EN | UART_IDLE_CON | UART_RX_TX | UART_DIS_WAKE | UART_DIS_LOOPBACK | UART_DIS_ABAUD | UART_NO_PAR_8BIT | UART_1STOPBIT | UART_IRDA_DIS |
UART_DIS_BCLK_CTS_RTS| UART_NORMAL_RX | UART_BRGH_SIXTEEN
#define config2 UART_TX_PIN_LOW | UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR | UART_ADR_DETECT_DIS | UART_RX_OVERRUN_CLEAR
//OpenUART1( config1, config2, pbClk/16/DESIRED_BAUDRATE-1); // calculate actual BAUD generate value.
OpenUART2( config1, config2, pbClk/16/DESIRED_BAUDRATE-1); // calculate actual BAUD generate value.
while (1)
{
while(!DataRdyUART2()); // Wait for data in the UARTRx
data = (char)ReadUART2(); // Read data from Rx and assign it to "data"
while(BusyUART2()); // Wait till the UART transmitter is free
putcUART2(data); // Write data into Tx.
if(data == '.') // If the data string matches what we sent, turn the lights on
{
mLED_0_On();
mLED_1_On();
mLED_2_On();
mLED_3_On();
}
else // If the received data doesn't match what was actually sent, only turn on two lights to show that some data was received, just not the right kind
{
mLED_0_On();
mLED_1_On();
mLED_2_Off();
mLED_3_Off();
}
}
}
Notes
The message string that was sent ended in a period and there was only one period in the whole message string. We used this to assure that the entire string was read before assigning the digital pin that clocked the entire transfer process to low.
