1 /**************************************************************************/
3 @file samsung_20T202DA2JA.c
4 @author Original source : Limor Fried/ladyada (Adafruit Industries)
5 LPC1343 port : K. Townsend
9 Driver for Samsung 20T202DA2JA 20x2 VFD display module.
11 This driver is based on the SPI_VFD Library from Limor Fried
12 (Adafruit Industries) at: https://github.com/adafruit/SPI_VFD
14 The displays can be purchased at:
15 https://www.adafruit.com/products/347
19 Software License Agreement (BSD License)
21 Copyright (c) 2012 Adafruit Industries
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26 1. Redistributions of source code must retain the above copyright
27 notice, this list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright
29 notice, this list of conditions and the following disclaimer in the
30 documentation and/or other materials provided with the distribution.
31 3. Neither the name of the copyright holders nor the
32 names of its contributors may be used to endorse or promote products
33 derived from this software without specific prior written permission.
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
36 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
39 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 /**************************************************************************/
49 #include "samsung_20T202DA2JA.h"
51 #include "core/gpio/gpio.h"
52 #include "core/systick/systick.h"
54 /**************************************************************************/
56 /**************************************************************************/
58 uint8_t samsungvfd_displayfunction
;
59 uint8_t samsungvfd_displaycontrol
;
60 uint8_t samsungvfd_displaymode
;
61 uint8_t samsungvfd_initialized
;
62 uint8_t samsungvfd_numlines
, samsungvfd_currline
;
64 /**************************************************************************/
66 @brief Low-level SPI bit-banging routing
68 /**************************************************************************/
69 static inline void samsungvfd_sendByte(uint8_t c
) {
72 // Make sure clock pin starts high
73 gpioSetValue(SAMSUNGVFD_SCK_PORT
, SAMSUNGVFD_SCK_PIN
, 1);
75 // Write from MSB to LSB
79 gpioSetValue(SAMSUNGVFD_SCK_PORT
, SAMSUNGVFD_SCK_PIN
, 0);
80 // Set data pin high or low depending on the value of the current bit
81 gpioSetValue(SAMSUNGVFD_SIO_PORT
, SAMSUNGVFD_SIO_PIN
, c
& (1 << i
) ? 1 : 0);
83 gpioSetValue(SAMSUNGVFD_SCK_PORT
, SAMSUNGVFD_SCK_PIN
, 1);
87 /**************************************************************************/
89 @brief Sends a command to the VFD display
91 /**************************************************************************/
92 void samsungvfd_command(uint8_t value
)
94 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 0);
95 samsungvfd_sendByte(SAMSUNGVFD_SPICOMMAND
);
96 samsungvfd_sendByte(value
);
97 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 1);
100 /**************************************************************************/
102 @brief Initialises the VFD character display
104 /**************************************************************************/
105 void samsungvfd_begin(uint8_t cols
, uint8_t lines
, uint8_t brightness
)
107 // set number of lines
109 samsungvfd_displayfunction
= SAMSUNGVFD_2LINE
;
111 samsungvfd_displayfunction
= SAMSUNGVFD_1LINE
;
113 if (brightness
>SAMSUNGVFD_BRIGHTNESS25
) //catch bad values
114 brightness
= SAMSUNGVFD_BRIGHTNESS100
;
116 // set the brightness and push the linecount with VFD_SETFUNCTION
117 samsungvfdSetBrightness(brightness
);
119 samsungvfd_numlines
= lines
;
120 samsungvfd_currline
= 0;
122 // Initialize to default text direction (for romance languages#include "SPI_VFD.h"
123 samsungvfd_displaymode
= SAMSUNGVFD_ENTRYLEFT
| SAMSUNGVFD_ENTRYSHIFTDECREMENT
;
124 // set the entry mode
125 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET
| samsungvfd_displaymode
);
127 samsungvfd_command(SAMSUNGVFD_SETDDRAMADDR
); // go to address 0
129 // turn the display on with no cursor or blinking default
130 samsungvfd_displaycontrol
= SAMSUNGVFD_DISPLAYON
;
137 /**************************************************************************/
139 /**************************************************************************/
141 /**************************************************************************/
143 @brief Initialises the VFD character display
145 /**************************************************************************/
146 void samsungvfdInit(uint8_t brightness
)
148 // Set all pins to output
149 gpioSetDir(SAMSUNGVFD_SIO_PORT
, SAMSUNGVFD_SIO_PIN
, gpioDirection_Output
);
150 gpioSetDir(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, gpioDirection_Output
);
151 gpioSetDir(SAMSUNGVFD_SCK_PORT
, SAMSUNGVFD_SCK_PIN
, gpioDirection_Output
);
153 // Set strobe and clock pins high by default
154 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 1);
155 gpioSetValue(SAMSUNGVFD_SCK_PORT
, SAMSUNGVFD_SCK_PIN
, 1);
157 // Default to 2x20 display (SAMSUNG 20T202DA2JA)
158 samsungvfd_begin(20, 2, brightness
);
161 /**************************************************************************/
163 @brief Writes a single character to the VFD display
165 /**************************************************************************/
166 void samsungvfdWrite(uint8_t value
)
168 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 0);
169 samsungvfd_sendByte(SAMSUNGVFD_SPIDATA
);
170 samsungvfd_sendByte(value
);
171 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 1);
174 /**************************************************************************/
176 @brief Writes a full string to the VFD display
178 /**************************************************************************/
179 void samsungvfdWriteString(const char * str
)
183 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 0);
184 samsungvfd_sendByte(SAMSUNGVFD_SPIDATA
);
185 samsungvfd_sendByte(*str
++);
186 gpioSetValue(SAMSUNGVFD_STB_PORT
, SAMSUNGVFD_STB_PIN
, 1);
190 /**************************************************************************/
192 @brief Sets the display brightness
194 /**************************************************************************/
195 void samsungvfdSetBrightness(uint8_t brightness
)
197 // set the brightness (only if a valid value is passed
198 if (brightness
<= SAMSUNGVFD_BRIGHTNESS25
)
200 samsungvfd_displayfunction
&= ~SAMSUNGVFD_BRIGHTNESS25
;
201 samsungvfd_displayfunction
|= brightness
;
203 samsungvfd_command(SAMSUNGVFD_FUNCTIONSET
| samsungvfd_displayfunction
);
207 /**************************************************************************/
209 @brief Quickly turn the display off
211 /**************************************************************************/
212 void samsungvfdNoDisplay(void)
214 samsungvfd_displaycontrol
&= ~SAMSUNGVFD_DISPLAYON
;
215 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL
| samsungvfd_displaycontrol
);
218 /**************************************************************************/
220 @brief Quickly turn the display on
222 /**************************************************************************/
223 void samsungvfdDisplay(void)
225 samsungvfd_displaycontrol
|= SAMSUNGVFD_DISPLAYON
;
226 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL
| samsungvfd_displaycontrol
);
229 /**************************************************************************/
231 @brief Turns the cursor off
233 /**************************************************************************/
234 void samsungvfdNoCursor(void)
236 samsungvfd_displaycontrol
&= ~SAMSUNGVFD_CURSORON
;
237 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL
| samsungvfd_displaycontrol
);
240 /**************************************************************************/
242 @brief Enables the cursor
244 /**************************************************************************/
245 void samsungvfdCursor(void)
247 samsungvfd_displaycontrol
|= SAMSUNGVFD_CURSORON
;
248 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL
| samsungvfd_displaycontrol
);
251 /**************************************************************************/
253 @brief Disable cursor blinking
255 /**************************************************************************/
256 void samsungvfdNoBlink(void)
258 samsungvfd_displaycontrol
&= ~SAMSUNGVFD_BLINKON
;
259 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL
| samsungvfd_displaycontrol
);
262 /**************************************************************************/
264 @brief Enable cursor blinking
266 /**************************************************************************/
267 void samsungvfdBlink(void)
269 samsungvfd_displaycontrol
|= SAMSUNGVFD_BLINKON
;
270 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL
| samsungvfd_displaycontrol
);
273 /**************************************************************************/
275 @brief Scroll the display left without changing the RAM
277 /**************************************************************************/
278 void samsungvfdScrollDisplayLeft(void)
280 samsungvfd_command(SAMSUNGVFD_CURSORSHIFT
| SAMSUNGVFD_DISPLAYMOVE
| SAMSUNGVFD_MOVELEFT
);
283 /**************************************************************************/
285 @brief Scroll the display right without changing the RAM
287 /**************************************************************************/
288 void samsungvfdScrollDisplayRight(void)
290 samsungvfd_command(SAMSUNGVFD_CURSORSHIFT
| SAMSUNGVFD_DISPLAYMOVE
| SAMSUNGVFD_MOVERIGHT
);
293 /**************************************************************************/
295 @brief Cause text to flow left to right
297 /**************************************************************************/
298 void samsungvfdLeftToRight(void)
300 samsungvfd_displaymode
|= SAMSUNGVFD_ENTRYLEFT
;
301 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET
| samsungvfd_displaymode
);
304 /**************************************************************************/
306 @brief Cause text to flow from right to left
308 /**************************************************************************/
309 void samsungvfdRightToLeft(void)
311 samsungvfd_displaymode
&= ~SAMSUNGVFD_ENTRYLEFT
;
312 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET
| samsungvfd_displaymode
);
315 /**************************************************************************/
317 @brief Right justify text from the cursor
319 /**************************************************************************/
320 void samsungvfdAutoscroll(void)
322 samsungvfd_displaymode
|= SAMSUNGVFD_ENTRYSHIFTINCREMENT
;
323 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET
| samsungvfd_displaymode
);
326 /**************************************************************************/
328 @brief Left justify text from the cursor
330 /**************************************************************************/
331 void samsungvfdNoAutoscroll(void)
333 samsungvfd_displaymode
&= ~SAMSUNGVFD_ENTRYSHIFTINCREMENT
;
334 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET
| samsungvfd_displaymode
);
337 /**************************************************************************/
339 @brief Clears the display
341 /**************************************************************************/
342 void samsungvfdClear(void)
344 samsungvfd_command(SAMSUNGVFD_CLEARDISPLAY
); // clear display, set cursor position to zero
345 systickDelay(2); // this command takes a long time!
348 /**************************************************************************/
350 @brief Places the cursor at 0, 0
352 /**************************************************************************/
353 void samsungvfdHome(void)
355 samsungvfd_command(SAMSUNGVFD_RETURNHOME
); // Set cursor position to zero
356 systickDelay(2); // this command takes a long time!
359 /**************************************************************************/
361 @brief Sets the cursor to the specified row and column position
363 /**************************************************************************/
364 void samsungvfdSetCursor(uint8_t row
, uint8_t col
)
366 int row_offsets
[] = { 0x00, 0x40, 0x14, 0x54 };
367 if ( row
> samsungvfd_numlines
)
369 row
= samsungvfd_numlines
-1; // we count rows starting w/0
372 samsungvfd_command(SAMSUNGVFD_SETDDRAMADDR
| (col
+ row_offsets
[row
]));