1. Introduction


USB stands for Universal Serial Bus.
This tutorial will teach you how to use the USB-comport. To use the example a driver installation is required. The driver can be found in the USBVcom example. For this tutorial the usbvcom library is required, this library can be downloaded from our repository.

 
2. Includes


First it’s needed to include the libraries.

#include "usbvcom.h"
#include "stdlib.h"
#include "string.h"             
#include "usbapi.h"
 
The libraries for the microcontroller and the variables have to be included to:

#include "LPC17xx.h"
#include "lpc_types.h"
 

3. Explanation


The Simplecortex communicates on a baud rate of 19200. To define the baud rate use the command:

#define BAUD_RATE           19200                                                                    

Any baud rate is possible to use, the max baud rate is 12Mbaud but speeds above 1Mbaud are not recommended.
The following instructions are needed to set up USB in the main program.

USB_init();
VCOM_init();
USBHwConnect(TRUE);
 
To write one character the command VCOM_putchar(); is used.
For example:
the command send the letter A is: VCOM_putchar(65);
65 is the ascii value for the letter A.
Ofter it is easier to write a full sentence, to do this the command VCOM_putstring(); is used. For example: to send the sentence "Hello world": VCOM_putstring("Hello world"); To make a new line use this code: VCOM_putstring("\r\n");
It is also possible to put \r\n behind Hello. Example: VCOM_putstring("Hello \r\n");
This will send "Hello" and the next sentence that is sended will start at a new line.
 
To read data from the serial port the first thing to do is check if there is data available.
The command to check if there is data available is VCOM_available(); This command returns the amount of bytes of available data. This has to be stored in a variable. the working code is:

             uint32_t dataavailable;
             dataavailable = VCOM_available();
 
This command reads one character from VCOM port: VCOM_getchar();
The command returns the ascii code of the character, it has to be stored in a variable.
It it also possible to read a full string from the serial port. to do this the command VCOM_RecieveString(); is used. To store the sting a pointer variable has to be used:

                char *stringtoread = NULL;
                stringtoread = VCOM_RecieveString();
 
Strings are a bit difficult in C, it is not possible to compare strings like this:

if (string1 == string2){}

To compare two string the command strcmp(string1, string2); is used. This command returns a 0 if the strings are equal. A working code to compare strings is:

if (!strcmp(string1, string2)){}

It is also possible to do this:

if (!strcmp("Hello", string2)){}

All string related commands are in the string.h library, some more info about it can be found here:

 

 

 
Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics