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

Return to SDK Examples for Microchip Studio

1/*
2 * Title: SpiSerialDisplay
3 *
4 * Objective:
5 * This example demonstrates how to write data to an SPI device.
6 *
7 * Description:
8 * This example will set up SPI communications on COM-0 then write various
9 * data to the device.
10 *
11 * Requirements:
12 * ** A NHD-0420D3Z LCD display in SPI 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 80000
32
33// Select the clock polarity to match the target device. The clock polarity
34// setting indicates whether the device expects a low signal when idle or a
35// high signal when idle. It also indicates whether the leading or trailing
36// edge of the clock cycle are rising or falling edges.
37// Selecting SCK_LOW indicates that SCK is low when idle and the leading edge
38// of the clock cycle is a rising edge while the trailing edge is a falling
39// edge.
40// Selecting SCK_HIGH indicates that SCK is high when idle and the leading edge
41// of the clock cycle is a falling edge while the trailing edge is a rising
42// edge.
43// The default value for a COM connector's clock polarity is SCK_LOW.
44#define clockPolarity SerialDriver::SCK_HIGH
45
46// Select the clock phase setting to match the target device. The clock phase
47// setting indicates whether data is sampled or changed on the leading or
48// trailing edge in the clock cycle.
49// Selecting LEAD_SAMPLE indicates that data is sampled on the leading edge and
50// changed on the trailing edge.
51// Selecting LEAD_CHANGE indicates that data is sampled on the trailing edge
52// and changed on the leading edge.
53// The default value for a COM connector's clock phase is LEAD_CHANGE.
54#define clockPhase SerialDriver::LEAD_CHANGE
55
56// Define which COM serial port connector to use: ConnectorCOM0 or ConnectorCOM1
57#define SpiPort ConnectorCOM0
58
59// Sample data to write to the display
60const uint8_t line1[21] = "abcdefghijklmnopqrst";
61const uint8_t line2[21] = "ABCDEFGHIJKLMNOPQRST";
62const uint8_t line3[21] = "01234567890123456789";
63const uint8_t line4[21] = "98765432109876543210";
64
65// Declare our helper functions so that they may be used before they are defined
66void SetBrightness(uint8_t level);
67void SetCursor(uint8_t row, uint8_t column);
68
69int main() {
70 // Configure the COM port for our SPI device then open the port.
71 SpiPort.Mode(Connector::SPI);
72 SpiPort.Speed(baudRate);
73 SpiPort.DataOrder(SerialDriver::COM_MSB_FIRST);
74 SpiPort.SpiClock(clockPolarity, clockPhase);
75 SpiPort.PortOpen();
76
77 // The COM port is now configured and ready to send commands and
78 // data to the display.
79
80 // Set the display brightness level.
81 // The maximum value for full brightness is 8.
82 SetBrightness(4);
83
84 // Set the cursor position to the top-left corner.
85 SetCursor(0, 0);
86
87 // Open the SPI port on ConnectorCOM0.
88 SpiPort.SpiSsMode(SerialDriver::LINE_ON);
89
90 // Send the lines "out of order" (1, 3, 2, 4) to the display.
91 // Without resetting the cursor position for each line, this is the order
92 // in which lines must be sent to be displayed correctly.
93 SpiPort.SpiTransferData(line1, NULL, 20);
94 SpiPort.SpiTransferData(line3, NULL, 20);
95 SpiPort.SpiTransferData(line2, NULL, 20);
96 SpiPort.SpiTransferData(line4, NULL, 20);
97
98 // Close the port.
99 SpiPort.SpiSsMode(SerialDriver::LINE_OFF);
100}
101
102/*------------------------------------------------------------------------------
103 * SetBrightness
104 *
105 * Sends a short SPI transaction to control the brightness of the attached
106 * LCD screen. See the device's datasheet for a full set of commands and
107 * syntax.
108 *
109 * Parameters:
110 * uint8_t level - The brightness level to be set
111 *
112 * Returns: None
113 */
114void SetBrightness(uint8_t level) {
115 SpiPort.SpiSsMode(SerialDriver::LINE_ON);
116 SpiPort.SpiTransferData(0xfe);
117 SpiPort.SpiTransferData(0x53);
118 SpiPort.SpiTransferData(level);
119 SpiPort.SpiSsMode(SerialDriver::LINE_OFF);
120}
121//------------------------------------------------------------------------------
122
123/*------------------------------------------------------------------------------
124 * SetCursor
125 *
126 * Sends a short SPI transaction to control the position of the device's
127 * internal cursor that controls where characters are printed on the LCD
128 * screen. See the device's datasheet for a full set of commands and syntax.
129 *
130 * Parameters:
131 * uint8_t row - The character row to move the cursor to.
132 * uint8_t column - The character column to move the cursor to.
133 *
134 * Returns: None
135 */
136void SetCursor(uint8_t row, uint8_t column) {
137 // Bounds-check the passed-in row and column
138 if (row >= NUM_ROWS) {
139 row = 0;
140 }
141 if (column >= NUM_COLUMNS) {
142 column = 0;
143 }
144
145 uint8_t position = row * NUM_COLUMNS + column;
146 SpiPort.SpiSsMode(SerialDriver::LINE_ON);
147 SpiPort.SpiTransferData(0xfe);
148 SpiPort.SpiTransferData(0x45);
149 SpiPort.SpiTransferData(position);
150 SpiPort.SpiSsMode(SerialDriver::LINE_OFF);
151}
152//------------------------------------------------------------------------------