Lab Goals:

q  Getting Familiar with USB display functions

Equipment needed:

q  Personal computer equipped with MPLAB IDE

q  Robot Platform

Lab Prerequisite:

q  User need to be familiar with the concept of functions in C programming language.

Pre Lab Activities:

q  Study the tutorial for this lab and do the sample project

Safety concerns

There are no safety concerns

Tips:

q  Make sure the following line is included before main () routine.

#include "System.h"

q  Make sure following line is the first line of code in the main() routine

InitRobot();

Required Tasks and Exercises:

q  First task:

Modify your digital clock code form lesson two so the user can set the clock to current time using the PC keyboard as a user interface for data entry.

Displaying Information on USB

Displaying information on MPLAB:

Sometimes it is helpful to be able to display information on PC or get some input from PC keyboard for that matter. This is especially helpful when debugging your application.

Note: To use the functions listed in this session the microcontroller board shall be connected to computer with the debugger USB cable as if you are going to program the board. Once MPLAB recognize the board you can print on MPLAB output window. For receiving the keyboard inputs the MPLAB would pop up a window. The data entry is complete when you hit ENTER on the keyboard. At that point all the received input would be send to the microcontroller.

There are several functions that could be used to print on MPLAB output window. The easiest one to use is the standard printf function.

printf

printf function work much like the standard printf function on PC . You can use the all formatting feature available for this function.

Note: The MPLAB would not display the information on screen until it receives at least one end of line character (‘\n’).

printf(“Hello World”);

printf(“Lesson 2 Demo\n”);

The above code would display the following string on the MPLAB output window

Hello World.Lesson 2 Demo

You can also use the allowed formatting for printf as usual.

int myvalue = -200;

printf(“The value = %d”, myvalue);

The above code would display the following string on the MPLAB output window

The value = -200

Here is the rest of function used for printing on MPLAB output window. This function does not possess the flexibility of the printf function and can only deal with simple strings or character information.

USB.Puts

Description: This function would print the given string on MPLAB output window

Prototype: void Puts (char String[])

Arguments: String: desired string that needs to be printed

Return Value: None

Example: The following example print the LESSON 3 DEMO on output screen

USB.Puts(“LESSON3 DEMO”);

There is two functions that could be used to receive keyboard entry from user as follows.

USB.Gets

Description: This function would accept a string of character for the given length from MPLAB output window

Prototype: void Puts (char String[], unsigned char StringLength)

Arguments: String: The input string would be stored in this variable

StringLength : This indicate the maximum length of string to be accepted

Return Value: None

Remark: This function would pop up a new window and halt the program execution until a ENTER is pressed on PC keyboard.

Example: The following example waits for the user to enter a string of maximum length of 10 char and then store in InputString variable.

char InputString[10];

USB.Gets(InputString,10);

USB.Getc

Description: This function would accept a single character from MPLAB output window

Prototype: void Puts (char * InputChar)

Arguments: InputChar is a pointer to the variable where the result should be stored

Return Value: None

Remark: This function would pop up a new window and halt the program execution until a ENTER is pressed on PC keyboard.

Remark: Please note that for this function you should pass the address of the variable (use &) not the variable itself.

Example: The following example waits for the user to enter a single char and then store in InputChar variable.

char InputChar;

USB.Gets(InputChar);

Example: The following example asks for the users name and stores it in variable Name. It then presents the user with a menu list and then executes the given option accordingly as described by the menu item list.

#include "System.h"

#define NAME_LENGTH 20

int main(void)

{

/* This variable hold the menu option selected by user from keyboard */

char MenuChoice;

char Name[NAME_LENGTH] = "";

InitRobot();

MenuChoice = '\0'; /* Initialize the menu option */

USB.Puts ("ENTER YOUR NAME: \n");

/* get a string of length 20 and store it

The programm stalls until user hit ENTER on keyboard*/

USB.Gets(Name,NAME_LENGTH);

/* print a new line on screen */

USB.Puts ("\n");

while (1)

{

printf ("\n");

printf ("[C] Change your name\n");

printf ("[S] Clear LCD screen\n");

printf ("[D] Display your name on LCD\n");

printf ("[Q] Quit Program\n");

printf ("\n Select an option from the menu: \n");

// Clear the menu option before getting the new one

MenuChoice = '\0';

/* Get a char of and store it. Note that in this case the

address of the variable should be passed to function

The programm stalls until user hit ENTER on keyboard*/

USB.Getc(MenuChoice);

if ((MenuChoice == 'C') || (MenuChoice == 'c'))

{

/* Clear the char already stored in Name variable

this is a standard c function*/

memset(Name,'\0',NAME_LENGTH);

USB.Gets(Name,NAME_LENGTH);

}

if ((MenuChoice == 'S') || (MenuChoice == 's'))

{

LCD.Clear();

}

if ((MenuChoice == 'D') || (MenuChoice == 'd'))

{

LCD.Puts(LINE1,Name);

}

else if ((MenuChoice == 'Q') || (MenuChoice == 'q'))

{

printf ("\n************************************\n");

printf ("Goodbye %s\n",Name);

printf ("**************************************\n");

/* Jump out of while loop */

break;

}

else

{

printf ("\n Invalid Entry. Try again.\n");

}

}

}

Robot Training Manual – Lesson 7 Page 2