Using RS-232 and Printing to LCD
1.Plug the Seetron BPI-216 LCD display into the COM1 serial port of the target PC, and power the display (see the LCD display’s user’s guide for more information) If you wish to use COM2, see “Changing COM Ports” later in this article.
2.Copy the file RS232Async_Messages.m into your work folder. This file contains a struct that holds configuration parameters for the RS232_Send block within the BPI_216_Send subsystem. Open the BPI_216_Send.mdl file and copy the BPI_216_Send block into your own model. The other blocks in BPI_216_Send.mdl are just for demonstration purposes.
3.To output text to the BPI-216 LCD display using the BPI_216_Send block, the system that is deciding which message to output needs to output a number of data type “double” (see “double” in the MATLAB help files for more information on data types). The BPI_216_Send subsystem will read this number and display the corresponding message. (Note: the default time step of BPI_216_Send is 0.01 seconds. BPI_216_Send’s time step must be an integer multiple of your main system’s time step.)
Figure 1 shows how to hook up the system:
File:RS232 LCD connections
Displaying Text Messages
Open BPI_216_Send, and then open Message Bank/Selector. Find the switch-case control structure in the embedded MATLAB function. This is where we will store the messages. Messages must be entered in the form:
The MATLAB function "uint8()" makes sure that each letter of your text is converted into data type uint8(unsigned 8 bit integer, also known as an unsigned char). If ommitted, the program might not display your messages correctly.
See Figure 2:
File:RS232 LCD messageselect
The number outputted by your main system will be read in as the variable msgID. The switch-case control structure will find the case that matches the value of msgID, and print out the message corresponding message, displaying the message under otherwise if no match if found. Replace message_1, message_2, and message_default with your own message, preserving the single quotes. You may add or remove as many cases as you need, but case "otherwise" must be defined.
If your message is too long to display on a single screen, your message will be automatically split up into multiple screens, and displayed one at a time; after the last screen is displayed, it will loop back to the first screen and keep looping. Extra spaces on the last screen will be padded with black triangles; this will help you identify the last screen. The default time between screens is 3 seconds.
Displaying Numeric Data
If you wish to display numberic data you must add an input port to the message_select m-function to accept the incoming data. Add as many input ports as you need, and beware that you may need to insert a "rate transation" block between the data source and message_select.
File:RS232 LCD add data.jpg
Use the subfunction numtostr to convert it into a uint8 vector (because Simulink does not support string data types, we cannot simply use num2str for this). Simly insert numtostr(yourdata) in between strings, as shown in the figure below (Note:be sure to put blank spaces around numtostr(), or you will get a compiler error):
File:RS232 lcd blankspaces
You must manually set how many digits numtostr will display. Open the message_select m-function block, and scroll towards the bottom until you find the function header for numtostr. Set number of integer places(int_p) and decimal places(dec_p) that you want displayed. If the inputted number has more integer places than int_p, the offending most significant digits will be lost. If the number is negative, an additional integer place will be automatically added to hold the negative sign. Unused integer spaces will be filled in by blank spaces. File:RS232 lcd set numtostr
Changing COM Ports
To change the COM port, open
Change Port from COM1 to the COM port you want. Then open
Change Port from COM1 to the COM port you want.
Changing Screen Change Rate
To change the rate at which successive screens are displayed when the message is broken up into multiple screens, open
Set the sample time to the time you want between screen changes, in seconds. ]]