UART
To initialize the UART, please use this code. It is already done for you
by default in the Axon source code.
uartInit(); // initialize the UART (serial port)
uartSetBaudRate(0, 38400); //set UARTE speed
uartSetBaudRate(1, 115200); //set UARTD speed, for USB
uartSetBaudRate(2, 38400); //set UARTH speed
uartSetBaudRate(3, 38400); //set UARTJ speed
To declare which UART port to use, here is an example. The last UART to
get initialized is the one to be used:
rprintfInit(uart0SendByte);
rprintf(" UART 0");
rprintfInit(uart1SendByte);//USB
rprintf(" UART 1");
rprintfInit(uart2SendByte);
rprintf(" UART 2");
rprintfInit(uart3SendByte);
rprintf(" UART 3");
The Axon is set up to immediately send out analog data over
the USB at 115200bps. You can easily output data in code by using this line:
rprintf("Hello, World! Data: %d", variable);
where:
%d - decimal
%x - hex
%c - character
To send a single byte to the UART:
int m=5;
uart0SendByte(m);//send a byte
To receive a character from the UART:
temp=uart3GetByte();//get a single character
Use this code to send the Axon a command using HyperTerminal. By typing 'r',
you will command the Axon to do_stuff(). Use bluetooth and you got an instant
remote control robobt.
char temp;
temp=uart1GetByte();//returns -1 if no data present
if (temp == 'r')//if data received
do_stuff();
If you are using GPS or a robot camera, you will need to read in data streams.
Simply call the function Receive_Echo(counter), where counter is the number of bytes
you expect to receive. The function will also exit if the device sends a '\n' command.
In this example, the device is attached to UART 3, the datastream is being
forwarded to USB for debugging, and is being stored in response[]. To use the data,
just read it directly from response[] like a normal array.
//Receive data from device,
//store into an array of size response_counter
char response[100];
void Receive_Echo(int response_counter)
{
int temp;
int counter=0;
//echo output to USB for debugging
rprintfInit(uart1SendByte);
while(1)
{
temp=uart3GetByte();//returns -1 if no data
if (temp != -1)//if data received
{
rprintf("%c",temp);
//store values into an array
//&0x0F converts ascii to char
response[counter]=temp & 0x0F;
counter++;
if(counter>=response_counter || temp=='\n')
{
rprintf("\r\n\r\n");
//flush out receive buffer
uartFlushReceiveBuffer(3);
break;
}
}
}
}
The Axon can be used as a TTL to USB adapter. Attach your TTL device to any UART on the Axon,
and have the data outputted by USB. To do so, here is a code example:
int data;
rprintfInit(uart1SendByte);//output to USB
while(1)
{
data=uart3GetByte();//read UART3
if (data!=-1)//check for data
rprintf("%c",data);//use %d to output ints
}
Please view the rprintf.h and uart4.h source code for further documentation.