4d-an-p5003 serial connecting a 4d display to an arduino host r 1 00

Upload: ion-iovi

Post on 09-Oct-2015

52 views

Category:

Documents


1 download

TRANSCRIPT

  • Application Note: 4D-AN-P5003 Serial Connecting a 4D Display to an Arduino Host

    APPLIC

    ATI

    ON N

    OTE

    Document Date: 31st October 2013

    Document Revision: 1.0

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 2 of 15 www.4dsystems.com.au

    Description

    This Application Note explores the possibilities provided by the Serial

    environment in Workshop for a 4D display module to work with an

    Arduino host. In this example, the host is an AVR ATmega328

    microcontroller-based Arduino Uno board. The host can also be an Arduino

    Mega 2560 or Due. Ideally, the application described in this document

    should work with any Arduino board with at least one UART serial port.

    See specifications of Aduino boards here.

    Before getting started, the following are required:

    Any of the following 4D Picaso display modules:

    uLCD-24PTU

    uLCD-28PTU

    uLCD-32PTU

    uLCD-32WPTU

    uLCD-43(P/PT/PCT)

    uVGA-III

    other superseded modules which support the Serial

    environment

    4D Programming Cable or USB-PA5

    Workshop 4 IDE (installed according to the installation

    document) and Arduino IDE

    An Arduino board with a UART serial port

    4D Arduino Adaptor Shield (optional) or connecting wires

    The user has read 4D-AN-P5002 Serial Getting Started The

    Serial Platform Environment Application or has an

    understanding of the Serial Protocol

    Content

    Description ............................................................................................. 2

    Content .................................................................................................. 2

    Application Overview .............................................................................. 3

    Configure the Display as a Serial Slave ..................................................... 4

    Program the Arduino Host....................................................................... 4

    Understanding the Arduino Demo Sketch.......................................... 4

    Open a Serial Port and Set the Baud Rate ......................................... 4

    Reset the Arduino Host and the Display ............................................ 5

    Let the Display Start Up.................................................................... 5

    Clear the Screen ............................................................................... 6

    The Command Bytes 6

    The GetAck ( ) Routine...................................................................... 6

    Set the Time Limit 6

    Read and Evaluate the Reply 7

    The Error-handling Routine .............................................................. 7

    Draw a Line...................................................................................... 8

    The Command Bytes 8

    The Parameter Bytes 8

    Send a String.................................................................................... 8

    The Command Bytes 8

    The Parameter Bytes 8

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 3 of 15 www.4dsystems.com.au

    The Null Byte 9

    Receive a Reply from the Display ...................................................... 9

    The GetWord ( ) Routine ................................................................... 9

    Set the Time Limit 9

    Read and Return the Reply Bytes 9

    Send the String Length back to the Display...................................... 10

    Connect the 4D Display Module to the Arduino Host .............................. 10

    Using the New 4D Arduino Adaptor Shield (Rev 2.00) ...................... 10

    Using the Old 4D Arduino Adaptor Shield (Rev 1)............................. 13

    Connection Using Jumper Wires ...................................................... 13

    Proprietary Information ........................................................................ 15

    Disclaimer of Warranties & Limitation of Liability................................... 15

    Application Overview

    In 4D-AN-P5002 Serial Getting Started The Serial Platform Environment

    Application the user was introduced to how a 4D display module is

    configured as a serial slave device and to the basics of the Serial Protocol.

    This application note now discusses the steps on how to interface a 4D

    display module serial slave to an Arduino host. First the user configures the

    4D display module to act as a serial slave by loading the SPE (Serial

    Platform Environment) application. The procedure for doing this was

    discussed in 4D-AN-P5002 Serial Getting Started The Serial Platform

    Environment Application. The Arduino host, on the other hand, is

    programmed using the Arduino IDE.

    This application note does not make use of the Arduino-Picaso Serial

    Library. Instead it deals directly with the low level serial data bytes. The

    objective is to demonstrate the basics of implementing the Serial Protocol

    as described in the Picaso Serial Command Set Reference Manual. Users

    who intend to create their own libraries (in C) for interfacing a 4D display

    serial slave device to other host controllers may use this application note

    as a reference. Likewise, users who wish to start looking inside the

    Arduino-Picaso Serial Library may find the sketch in this application note

    useful. In the end, before the sketch gets very complicated and explaining

    gets even more difficult, the user is now directed to the document 4D-AN-

    P5004 Serial Connecting a 4D Display to an Arduino Host using a Library,

    which explains the use of the Arduino-Picaso Serial Library.

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 4 of 15 www.4dsystems.com.au

    Configure the Display as a Serial Slave

    See pages 3 to 8 of the application note 4D-AN-P5002 Serial Getting

    Started The Serial Platform Environment Application.

    Program the Arduino Host

    This section discusses the source code for the Arduino host so it can work

    with the display module. It is assumed that the user has a basic

    understanding of how the Arduino host works and how to program in the

    Arduino IDE. Inexperienced users may need to frequently refer to the

    Arduino website for more information.

    Understanding the Arduino Demo Sketch

    Open, compile, and download to the Arduino host the attached sketch.

    Note that comments have been added to the code for the benefit of the

    user. Additional explanations now follow.

    Open a Serial Port and Set the Baud Rate

    In the function setup( ), port Serial0 is set to communicate with the display

    module at 9600 bps.

    Logically, the 4D display should also communicate with the Arduino host at

    the same baud rate. To check the baud rate of the display module check

    the baud rate indicated on the Serial Platform Environment (SPE)

    application splash screen.

    Comms 9600

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 5 of 15 www.4dsystems.com.au

    Reset the Arduino Host and the Display

    To make resetting more convenient, the code below resets the display

    module when the program is restarted.

    If using the new 4D Arduino Adaptor Shield (Rev 2)

    Note that the GPIO pin D4 of the Arduino host is used here for resetting

    the display. When using the new 4D Arduino Adaptor Shield (Rev 2.00

    written on the PCB), simply connect RES to AR in jumper J1. See the section

    Connect the 4D Display Module to the Arduino Host. If using the old 4D

    Arduino Adaptor Shield (Rev 1), simply change the code above. Use pin 2

    instead of pin 4.

    If using the old 4D Arduino Adaptor Shield (Rev 1)

    If using jumper connecting wires, connect the RESET pin of the display

    module to the D4 pin of the Arduino with a 1kohm series resistor in

    between (see the section Connect the 4D Display Module to the Arduino

    Host), and modify the code as shown below.

    If using jumper wires

    Note that the logic state for resetting the display is now 0 instead of 1. This

    is because the display modules RESET pin is directly connected to D4 via a

    1kohm resistor. If using a 4D Arduino Adaptor Shield, the display modules

    RESET pin is switched by the D4 pin via a transistor.

    Let the Display Start Up

    The five second delay below waits for the display module to start up.

    Note that on section 2.5 Power-Up and Reset of the Picaso Serial

    Command Set Reference Manual it says:

    When the PICASO Display Module comes out of a

    power-up or external reset, a sequence of events is

    executed internally. The user should wait at least 3

    seconds for the start-up to take place before

    attempting to communicate with the module.

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 6 of 15 www.4dsystems.com.au

    Clear the Screen

    Section 5.2.1 Clear Screen of the Picaso Serial Command Set Reference

    Manual describes in detail the command for clearing the screen.

    The Command Bytes

    The display module will receive these bytes, execute the command, and

    will send back an acknowledgment byte (0x06). It is very important that

    the Arduino host must wait for the acknowledgment byte before sending

    another command. Section 2.4 Introduction and Guidelines to the Serial

    Protocol of the Picaso Serial Command Set Reference Manual emphasizes

    this point.

    This now explains the need for the following lines in the code:

    The code shown above will call on the function GetAck ( ). This function

    will return a value depending on the response of the display module.

    Proceed to the next section for more details. If the reply is not what is

    expected (the ACK byte), then the function mycallback ( ) is called.

    The GetAck ( ) Routine

    The purpose of this routine is to wait for the ACK byte from the display

    module. A time limit must therefore be defined.

    Set the Time Limit

    The five-second time limit is defined at the beginning of the sketch.

    The while loop below will execute while nothing is received from the

    display yet and the waiting time has not yet exceeded the specified time

    limit.

    Commands should only be sent and their response

    received, before another command is sent. If two

    commands are sent before the first response is

    received, incorrect operation may follow.

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 7 of 15 www.4dsystems.com.au

    When using the Serial Commander, the user has also the option of setting

    the time limit.

    Read and Evaluate the Reply

    If a reply has been received, it will be read

    and evaluated if it is equal to the ACK byte. The variable Error4D is then set

    depending on the results and its value returned to the main function.

    The Error-handling Routine

    The function mycallback ( ) will execute when an error occurs (i.e., the

    display has taken too long to respond or it has sent back a reply other than

    the expected ACK byte).

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 8 of 15 www.4dsystems.com.au

    Note that mycallback ( ) will only blink the LED at pin 13 indefinitely. The

    user may also use this function to reset the display and re-establish

    communications. When an error occurs, it would be improper for the

    program to proceed further without resetting the display. Again,

    Draw a Line

    Section 5.2.5 Draw Line of the Picaso Serial Command Set Reference

    Manual describes in detail the command for drawing a line on the screen.

    The Command Bytes

    The Parameter Bytes

    Send a String

    Section 5.1.2 Put String of the Picaso Serial Command Set Reference

    Manual describes in detail the command for printing a string on the

    display.

    The Command Bytes

    The Parameter Bytes

    Commands should only be sent and their response

    received, before another command is sent. If two

    commands are sent before the first response is

    received, incorrect operation may follow.

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 9 of 15 www.4dsystems.com.au

    The Null Byte

    A note from the manual:

    Receive a Reply from the Display

    After receiving the put string command and parameter bytes from the

    Arduino host, the display will print the string and will send back an ACK

    byte and the length of the received string. In this example the string "Hello

    World!\nHello Picaso!" has twenty-six characters in it, so the display will

    send back the bytes: 0x06 0x00 0x1A (ACK, string length MSB, and string

    length LSB). To receive the ACK byte, the Arduino host program uses the

    function GetAck ( ). After verifying that the ACK byte has been received,

    the program now uses the function GetWord( ) to receive the string length

    bytes.

    The GetWord ( ) Routine

    Similar to the GetAck ( ) function, GetWord ( ) waits for the reply bytes

    from the display module for a specified duration of time.

    Set the Time Limit

    The five-second time limit is also used.

    Read and Return the Reply Bytes

    The reply bytes are read

    and returned to the main function.

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 10 of 15 www.4dsystems.com.au

    Send the String Length back to the Display

    Going back to the main function, note that the program sends back to the

    display module the string length to be printed.

    The display module should now display the string length in hex.

    To make programming easier, 4D Systems provides a library for interfacing

    a 4D display serial slave to an Arduino host controller. Refer to the

    document 4D-AN-P5004 Serial Connecting a 4D Display to an Arduino Host

    using a Library for more information.

    Connect the 4D Display Module to the

    Arduino Host

    This section discusses how to connect the display module to the Arduino

    host. The user has the option of using a 4D Arduino Adaptor Shield or

    simply connecting the Tx0, Rx0, RESET, and GND pins of the display module

    to the corresponding pins of the Arduino host.

    Using the New 4D Arduino Adaptor Shield (Rev 2.00)

    Jumpers

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 11 of 15 www.4dsystems.com.au

    To reset the display using the D4 pin of the Arduino and to power the

    display from the Arduino host, set jumpers J1 and J2 as shown below.

    By default, jumpers J3 and J4 are configured to connect RX (RX of display

    module) to D0 (TX0 of the Arduino) and TX (TX of display module) to D1

    (RX0 of the Arduino). Communication in this case is thru Serial0. To use the

    other serial ports of the Mega or Due, remove the jumper connectors of J3

    and J4 and connect the display TX and RX pins to the desired serial port

    using jumper wires.

    To the right are images of a complete setup wherein the display module

    and the Arduino host have a common power supply. Note that the power

    supply must be able to provide enough current for both the display module

    and the Arduino host. Refer to the display modules datasheet for the

    specified supply current.

    Using the USB cable:

    Using the jack:

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 12 of 15 www.4dsystems.com.au

    To power the display using a 4D USB Programming Cable or a USB-PA5

    Programming Adaptor, set J2 as shown below.

    H2 is for the 4D USB Programming Cable or USB-PA5, and H1 is for the

    display module. The following image shows how the Arduino host and the

    4D display are connected when they are powered separately.

    Complete setup:

    Note that the display module cannot be programmed thru the USB-PA5 in

    this setup since H2 transfers power only. Before programming the display

    module, disconnect it first from the Arduino Adaptor Shield. Likewise,

    before programming the Arduino, make sure that it is not connected to the

    display module. Do this only when the Arduino host and the 4D display

    communicate thru Serial0. Always double check the orientation of the

    connectors.

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 13 of 15 www.4dsystems.com.au

    Using the Old 4D Arduino Adaptor Shield (Rev 1)

    Connection Using Jumper Wires

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 14 of 15 www.4dsystems.com.au

  • 4D SYSTEMS 4D-AN-P5003

    2013 4D Systems Page 15 of 15 www.4dsystems.com.au

    Proprietary Information

    The information contained in this document is the property of 4D Systems Pty. Ltd. and may be the subject of patents pending or granted, and must not be

    copied or disclosed without prior written permission.

    4D Systems endeavours to ensure that the information in this document is correct and fairly stated but does not accept liability for any error or omission. The

    development of 4D Systems products and services is continuous and published information may not be up to date. It is importan t to check the current

    position with 4D Systems.

    All trademarks belong to their respective owners and are recognised and acknowledged.

    Disclaimer of Warranties & Limitation of Liability

    4D Systems makes no warranty, either expresses or implied with respect to any product, and specifically disclaims all other warranties, including, without

    limitation, warranties for merchantability, non-infringement and fitness for any particular purpose.

    Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates.

    It is your responsibility to ensure that your application meets with your specifications.

    In no event shall 4D Systems be liable to the buyer or to any third party for any indirect, incidental, special, consequential, punitive or exemplary damages

    (including without limitation lost profits, lost savings, or loss of business opportunity) arising out of or relating to any product or service provided or to be

    provided by 4D Systems, or the use or inability to use the same, even if 4D Systems has been advised of the possibility of such damages.

    4D Systems products are not fault tolerant nor designed, manufactured or intended for use or resale as on line control equipment in hazardous environments

    requiring fail safe performance, such as in the operation of nuclear facilities, aircraft navigation or communication systems, air traffic c ontrol, direct life

    support machines or weapons systems in which the failure of the product could lead directly to death, personal injury or severe physical or environmental

    damage (High Risk Activities). 4D Systems and its suppliers specifically disclaim any expressed or implied warranty of fitness for High Risk Activities.

    Use of 4D Systems products and devices in 'High Risk Activities' and in any other application is entirely at the buyers risk, and the buyer agrees to defend,

    indemnify and hold harmless 4D Systems from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or

    otherwise, under any 4D Systems intellectual property rights.