]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/mbed/freopen/TextLCD.cpp
Merge pull request #34 from jackhumbert/tmk-master
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / tests / mbed / freopen / TextLCD.cpp
1 /* mbed TextLCD Library
2  * Copyright (c) 2007-2009 sford
3  * Released under the MIT License: http://mbed.org/license/mit
4  */
5
6 #include "TextLCD.h"
7 #include "mbed.h"
8
9 /*
10  * useful info found at http://www.a-netz.de/lcd.en.php
11  *
12  * Initialisation
13  * ==============
14  *
15  * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
16  *
17  * - wait approximately 15 ms so the display is ready to execute commands
18  * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
19  * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command.
20  * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
21  * - Execute the "clear display" command
22  *
23  * Timing
24  * ======
25  *
26  * Nearly all commands transmitted to the display need 40us for execution.
27  * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
28  * These commands need 1.64ms for execution. These timings are valid for all displays working with an
29  * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
30  * can use the busy flag to test if the display is ready to accept the next command.
31  *
32  * _e is kept high apart from calling clock
33  * _rw is kept 0 (write) apart from actions that uyse it differently
34  * _rs is set by the data/command writes
35  */
36
37 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
38     PinName d2, PinName d3, const char *name) : TextDisplay(name), _rw(rw), _rs(rs),
39     _e(e), _d(d0, d1, d2, d3) {
40
41     _rw = 0;
42     _e  = 1;
43     _rs = 0; // command mode
44
45     // Should theoretically wait 15ms, but most things will be powered up pre-reset
46     // so i'll disable that for the minute. If implemented, could wait 15ms post reset
47     // instead
48     // wait(0.015);
49
50     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
51     for(int i=0; i<3; i++) {
52         writeByte(0x3);
53         wait(0.00164);      // this command takes 1.64ms, so wait for it
54     }
55     writeByte(0x2); // 4-bit mode
56
57     writeCommand(0x28);    // Function set 001 BW N F - -
58     writeCommand(0x0C);
59     writeCommand(0x6);  //  Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
60     cls();
61 }
62
63 void TextLCD::character(int column, int row, int c) {
64       int address = 0x80 + (row * 40) + column; // memory starts at 0x80, and is 40 chars long per row
65       writeCommand(address);
66       writeData(c);
67 }
68
69 int TextLCD::columns() {
70     return 16;
71 }
72
73 int TextLCD::rows() {
74     return 2;
75 }
76
77 void TextLCD::writeByte(int value) {
78     _d = value >> 4;
79     wait(0.000040f); // most instructions take 40us
80     _e = 0;
81     wait(0.000040f);
82     _e = 1;
83     _d = value >> 0;
84     wait(0.000040f);
85     _e = 0;
86     wait(0.000040f);  // most instructions take 40us
87     _e = 1;
88 }
89
90 void TextLCD::writeCommand(int command) {
91     _rs = 0;
92     writeByte(command);
93 }
94
95 void TextLCD::writeData(int data) {
96     _rs = 1;
97     writeByte(data);
98 }