ClearCore Library
Loading...
Searching...
No Matches
TtlSerialDisplay.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: TtlSerialDisplay
3 *
4 * Objective:
5 * This example demonstrates how to write data to a TTL device.
6 *
7 * Description:
8 * This example will set up TTL communications on COM-0 then write various
9 * data to the device.
10 *
11 * Requirements:
12 * ** A NHD-0420D3Z LCD display in TTL mode connected to COM-0.
13 * Datasheet: http://www.newhavendisplay.com/specs/NHD-0420D3Z-NSW-BBW-V3.pdf
14 *
15 * Links:
16 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
17 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
18 *
19 *
20 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
21 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
22 */
23
24#include "ClearCore.h"
25
26// Data bounds of the device
27#define NUM_ROWS 4
28#define NUM_COLUMNS 20
29
30// Select the baud rate to match the target device.
31#define baudRate 57600
32
33// Specify which serial to use: ConnectorCOM0 or ConnectorCOM1
34#define SerialPort ConnectorCOM0
35
36// Sample data to write to the display
37const uint8_t line1[21] = "abcdefghijklmnopqrst";
38const uint8_t line2[21] = "ABCDEFGHIJKLMNOPQRST";
39const uint8_t line3[21] = "01234567890123456789";
40const uint8_t line4[21] = "98765432109876543210";
41
42// Declare our helper functions so that they may be used before they are defined
43void SetBrightness(uint8_t level);
44void SetCursor(uint8_t row, uint8_t column);
45
46int main() {
47 SerialPort.Mode(Connector::TTL);
48 SerialPort.Speed(baudRate);
49 SerialPort.PortOpen();
50
51 // The COM port is now configured and ready to send commands and
52 // data to the display.
53
54 // Set the display brightness level.
55 // The maximum value for full brightness is 8.
56 SetBrightness(4);
57
58 // Set the cursor position to the top-left corner.
59 SetCursor(0, 0);
60
61 // Send the lines "out of order" (1, 3, 2, 4) to the display.
62 // Without resetting the cursor position for each line, this is the order
63 // in which lines must be sent to be displayed correctly.
64 SerialPort.Send((char *)line1);
65 SerialPort.Send((char *)line3);
66 SerialPort.Send((char *)line2);
67 SerialPort.Send((char *)line4);
68}
69
70/*------------------------------------------------------------------------------
71 * SetBrightness
72 *
73 * Sends a short group of data to control the brightness of the attached
74 * LCD screen. See the device's datasheet for a full set of commands and
75 * syntax.
76 *
77 * Parameters:
78 * uint8_t level - The brightness level to be set
79 *
80 * Returns: None
81 */
82void SetBrightness(uint8_t level) {
83 SerialPort.SendChar(0xfe);
84 SerialPort.SendChar(0x53);
85 SerialPort.SendChar(level);
86}
87//------------------------------------------------------------------------------
88
89/*------------------------------------------------------------------------------
90 * SetCursor
91 *
92 * Sends a short group of data to control the position of the device's
93 * internal cursor that controls where characters are printed on the LCD
94 * screen. See the device's datasheet for a full set of commands and syntax.
95 *
96 * Parameters:
97 * uint8_t row - The character row to move the cursor to.
98 * uint8_t column - The character column to move the cursor to.
99 *
100 * Returns: None
101 */
102void SetCursor(uint8_t row, uint8_t column) {
103 // Bounds-check the passed-in row and column
104 if (row >= NUM_ROWS) {
105 row = 0;
106 }
107
108 if (column >= NUM_COLUMNS) {
109 column = 0;
110 }
111
112 uint8_t position = row * NUM_COLUMNS + column;
113 SerialPort.SendChar(0xfe);
114 SerialPort.SendChar(0x45);
115 SerialPort.SendChar(position);
116}
117//------------------------------------------------------------------------------