Home
Up
I2C library
Serial Interface

 

 

Serial Interface


This is a terminal interface program.  I love it.  Simply  connect to your device through a level shifter and a serial COM port.  Type commands in the terminal terminated by the enter key.  Commands may be as long as you like (simply change the szCommand buffer length).  If you enter a bad command, the program informs you of valid commands.  Backspace is functional to correct typing mistakes, characters are echoed back to the terminal so you can see what you are typing.

Code:

Serial_interface.c - contains main, initializations, and useful functions for sending and receiving information from the terminal.

Serial_interface.h

SerialCommand.c - Command interface, details given below

SerialCommand.h

GenDefs.h - includes common definitions used in this program

Serial_interface.hex - compiled for ATmega64 with USART 1, internal 8mhz crystal (with the calibration value loaded into the EEPROM at address 0x000) and the baud rate set to 57600 baud. No parity, 8 data bits, 1 stop bit.

 

instructions: 

To add commands, first add the command to your list of commands (found in SerialCommand.c):

char * arszCommands[CCOMMANDS] = {"Command 1" , "Command 2" , "More Commands" };

Then indicate the number of valid commands by changing:

#define CCOMMANDS 2  (becomes->)  #define CCOMMANDS 3

Add an "else if" statement inside ExecuteCommand() to deal with your new command:

else if(strcmp(szMessage,"More Commands") == 0)
{
tx_string("More Commands have been executed");
ResetCommand();
}

Thats all it takes to add a new command to your program interface.

To get user input once inside a command, you must first reset the fStringWaiting flag by calling the ResetCommand(); function.  This indicates that you wish to receive user input.  Simply wait for the fStringWaiting flag to become fTrue like so:

while(fStringWaiting != fTrue)
{
}

Now the user input resides in the szCommand character array as a null terminated string.  You may do anything you wish with that null terminated string, such as:

tx_string(szCommand);

which simply echoes what you typed right back to the screen.  Make sure to call ResetCommand(); when you are finished.

a complete example that would inform the user to type something, then echo it back when they hit enter would look like this:

else if(strcmp(szMessage,"More Commands") == 0)
{
tx_string("More Commands have been executed");
tx_string("Please enter a string and hit enter");

ResetCommand();

while(fStringWaiting != fTrue)
{
}

tx_string("you typed:");
tx_string(szCommand);


ResetCommand();
}

Thats pretty much it, I also included a function to convert a string to a BYTE (uint8_t) from three different formats.  To enter a Hex number, prefix the number with an 'x', binary prefix with a 'b' and decimal needs no prefix.  An example is given below:

else if(strcmp(szMessage,"numbers") == 0)
{

BYTE temp;

tx_string("Please Enter Data: prefix b->Binary, x->Hex, none->decimal");

ResetCommand();

while(fStringWaiting != fTrue)
{
}

temp = sz_to_int(szCommand);

sprintf(szBuf,"You entered %d", temp);
tx_string(szBuf);

ResetCommand();
}

On the terminal, this would look like:

>numbers
>Please Enter Data: prefix b->Binary, x->Hex, none->decimal
>xFF
>You entered 255

That should get you started.  The UART used can be changed by commenting or uncommenting one of:

// choose usart 1 or 0
#define USART_ONE
//#define USART_ZERO

You can get into the hardware init function and change the baud rate of the serial port.  The current version uses the internal 8mhz crystal (with the calibration value loaded into the EEPROM at address 0x000) and the baud rate set to 57600 baud. No parity, 8 data bits, 1 stop bit.  E-mail me if you have any questions.  I realize this tutorial is lacking, but honestly, how many people are going to actually use this?

Hit Counter