Difference between revisions of "PIC32MX: XBee Wireless Round-trip Latency"

From Mech
Jump to navigationJump to search
Line 13: Line 13:
Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.
Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.


= Code =
= Include a Program =


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.
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.
Line 37: Line 37:
// ****** Includes
// ****** Includes
#include "HardwareProfile.h"
#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
#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();
}
}

Revision as of 17:02, 13 February 2010

Original Assignment

Do not erase this section!

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

Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.

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

  1. include "HardwareProfile.h"