A basic library for the Freetronics arduino LCD shield.
Fork of freetronicsLCDShield by
freetronicsLCDShield.cpp@4:3a0c74cef8e8, 2014-12-13 (annotated)
- Committer:
- KKempeneers
- Date:
- Sat Dec 13 09:18:36 2014 +0000
- Revision:
- 4:3a0c74cef8e8
- Parent:
- 1:ddcefddda4a7
- Child:
- 6:ac481535732f
The author's mail address is changed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KKempeneers | 1:ddcefddda4a7 | 1 | /* mbed freetronicsLCDShield Library, written by Koen J.F. Kempeneers |
KKempeneers | 4:3a0c74cef8e8 | 2 | * kkempeneers(at)skynet.be |
KKempeneers | 1:ddcefddda4a7 | 3 | * |
KKempeneers | 1:ddcefddda4a7 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
KKempeneers | 1:ddcefddda4a7 | 5 | * of this software and associated documentation files (the "Software"), to deal |
KKempeneers | 1:ddcefddda4a7 | 6 | * in the Software without restriction, including without limitation the rights |
KKempeneers | 1:ddcefddda4a7 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
KKempeneers | 1:ddcefddda4a7 | 8 | * copies of the Software, and to permit persons to whom the Software is |
KKempeneers | 1:ddcefddda4a7 | 9 | * furnished to do so, subject to the following conditions: |
KKempeneers | 1:ddcefddda4a7 | 10 | * |
KKempeneers | 1:ddcefddda4a7 | 11 | * The above copyright notice and this permission notice shall be included in |
KKempeneers | 1:ddcefddda4a7 | 12 | * all copies or substantial portions of the Software. |
KKempeneers | 1:ddcefddda4a7 | 13 | * |
KKempeneers | 1:ddcefddda4a7 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
KKempeneers | 1:ddcefddda4a7 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
KKempeneers | 1:ddcefddda4a7 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
KKempeneers | 1:ddcefddda4a7 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
KKempeneers | 1:ddcefddda4a7 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
KKempeneers | 1:ddcefddda4a7 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
KKempeneers | 1:ddcefddda4a7 | 20 | * THE SOFTWARE. |
KKempeneers | 1:ddcefddda4a7 | 21 | */ |
KKempeneers | 1:ddcefddda4a7 | 22 | |
KKempeneers | 0:01f3d38f8b6d | 23 | #include "mbed.h" |
KKempeneers | 0:01f3d38f8b6d | 24 | #include "freetronicsLCDShield.h" |
KKempeneers | 0:01f3d38f8b6d | 25 | |
KKempeneers | 0:01f3d38f8b6d | 26 | #define PULSE_E wait(0.000001f); \ |
KKempeneers | 0:01f3d38f8b6d | 27 | _e = 0; \ |
KKempeneers | 0:01f3d38f8b6d | 28 | wait(0.000001f); \ |
KKempeneers | 0:01f3d38f8b6d | 29 | _e = 1; |
KKempeneers | 0:01f3d38f8b6d | 30 | |
KKempeneers | 0:01f3d38f8b6d | 31 | |
KKempeneers | 0:01f3d38f8b6d | 32 | freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) |
KKempeneers | 0:01f3d38f8b6d | 33 | : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) { |
KKempeneers | 0:01f3d38f8b6d | 34 | // Class constructor |
KKempeneers | 0:01f3d38f8b6d | 35 | // Init the display, wait 15ms to insure the power is up |
KKempeneers | 0:01f3d38f8b6d | 36 | _e = true; |
KKempeneers | 0:01f3d38f8b6d | 37 | _rs = false; |
KKempeneers | 0:01f3d38f8b6d | 38 | wait(0.015f); |
KKempeneers | 0:01f3d38f8b6d | 39 | |
KKempeneers | 0:01f3d38f8b6d | 40 | for (int i = 0; i < 3; i++){ |
KKempeneers | 0:01f3d38f8b6d | 41 | writeByte(0x3); |
KKempeneers | 0:01f3d38f8b6d | 42 | wait(0.00164f); // this command takes 1.64ms, so wait for it |
KKempeneers | 0:01f3d38f8b6d | 43 | } |
KKempeneers | 0:01f3d38f8b6d | 44 | |
KKempeneers | 0:01f3d38f8b6d | 45 | writeByte(0x2); // 4-bit mode |
KKempeneers | 0:01f3d38f8b6d | 46 | writeCommand(0x28); // Function set 001 BW N F - - |
KKempeneers | 0:01f3d38f8b6d | 47 | writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off |
KKempeneers | 0:01f3d38f8b6d | 48 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
KKempeneers | 0:01f3d38f8b6d | 49 | cls(); |
KKempeneers | 0:01f3d38f8b6d | 50 | |
KKempeneers | 0:01f3d38f8b6d | 51 | // Set the PWM period to 20ms |
KKempeneers | 0:01f3d38f8b6d | 52 | _bl.period_ms(1); |
KKempeneers | 0:01f3d38f8b6d | 53 | _bl.write(0.0); |
KKempeneers | 0:01f3d38f8b6d | 54 | } |
KKempeneers | 0:01f3d38f8b6d | 55 | |
KKempeneers | 0:01f3d38f8b6d | 56 | void freetronicsLCDShield::setCursorPosition (int line, int col) { |
KKempeneers | 0:01f3d38f8b6d | 57 | // Set the new cursorposition |
KKempeneers | 0:01f3d38f8b6d | 58 | writeCommand(0x80 + (line * 0x40) + col); |
KKempeneers | 0:01f3d38f8b6d | 59 | } |
KKempeneers | 0:01f3d38f8b6d | 60 | |
KKempeneers | 0:01f3d38f8b6d | 61 | void freetronicsLCDShield::setBackLight (bool blStatus) { |
KKempeneers | 0:01f3d38f8b6d | 62 | // Switch the backlight on (true) or off (false) |
KKempeneers | 0:01f3d38f8b6d | 63 | _bl = (blStatus) ? 1.0 : 0.0; |
KKempeneers | 0:01f3d38f8b6d | 64 | } |
KKempeneers | 0:01f3d38f8b6d | 65 | |
KKempeneers | 0:01f3d38f8b6d | 66 | void freetronicsLCDShield::setBackLight (float blIntensity) { |
KKempeneers | 0:01f3d38f8b6d | 67 | // Switch the backlight on (true) or off (false) |
KKempeneers | 0:01f3d38f8b6d | 68 | _bl = blIntensity; |
KKempeneers | 0:01f3d38f8b6d | 69 | } |
KKempeneers | 0:01f3d38f8b6d | 70 | |
KKempeneers | 0:01f3d38f8b6d | 71 | void freetronicsLCDShield::setCursor (bool cStatus, bool blink) { |
KKempeneers | 0:01f3d38f8b6d | 72 | int tmp = 0; |
KKempeneers | 0:01f3d38f8b6d | 73 | |
KKempeneers | 0:01f3d38f8b6d | 74 | if (blink) tmp = 0x01; |
KKempeneers | 0:01f3d38f8b6d | 75 | if (cStatus) tmp |= 0x02; |
KKempeneers | 0:01f3d38f8b6d | 76 | writeCommand(0x0C + tmp); |
KKempeneers | 0:01f3d38f8b6d | 77 | } |
KKempeneers | 0:01f3d38f8b6d | 78 | |
KKempeneers | 1:ddcefddda4a7 | 79 | void freetronicsLCDShield::shift(bool direction) { |
KKempeneers | 1:ddcefddda4a7 | 80 | if(direction == LEFT) shiftLeft(); |
KKempeneers | 1:ddcefddda4a7 | 81 | else shiftRight(); |
KKempeneers | 1:ddcefddda4a7 | 82 | } |
KKempeneers | 1:ddcefddda4a7 | 83 | |
KKempeneers | 1:ddcefddda4a7 | 84 | void freetronicsLCDShield::shiftLeft(void) { |
KKempeneers | 1:ddcefddda4a7 | 85 | writeCommand(0x18 + 0x04); |
KKempeneers | 1:ddcefddda4a7 | 86 | } |
KKempeneers | 1:ddcefddda4a7 | 87 | |
KKempeneers | 1:ddcefddda4a7 | 88 | void freetronicsLCDShield::shiftRight(void) { |
KKempeneers | 1:ddcefddda4a7 | 89 | writeCommand(0x18); |
KKempeneers | 0:01f3d38f8b6d | 90 | } |
KKempeneers | 0:01f3d38f8b6d | 91 | |
KKempeneers | 0:01f3d38f8b6d | 92 | void freetronicsLCDShield::cls(void) { |
KKempeneers | 0:01f3d38f8b6d | 93 | // Clear the display and place the cursor at 0, 0 |
KKempeneers | 0:01f3d38f8b6d | 94 | writeCommand(0x01); |
KKempeneers | 0:01f3d38f8b6d | 95 | wait(0.00164f); |
KKempeneers | 0:01f3d38f8b6d | 96 | } |
KKempeneers | 0:01f3d38f8b6d | 97 | |
KKempeneers | 0:01f3d38f8b6d | 98 | void freetronicsLCDShield::home(void) { |
KKempeneers | 0:01f3d38f8b6d | 99 | // Undo shift operations and place cursor at 0,0 |
KKempeneers | 0:01f3d38f8b6d | 100 | writeCommand(0x02); |
KKempeneers | 0:01f3d38f8b6d | 101 | wait(0.00164f); |
KKempeneers | 0:01f3d38f8b6d | 102 | } |
KKempeneers | 0:01f3d38f8b6d | 103 | |
KKempeneers | 1:ddcefddda4a7 | 104 | void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) { |
KKempeneers | 1:ddcefddda4a7 | 105 | // Write the address only once, it is autoincremented |
KKempeneers | 1:ddcefddda4a7 | 106 | writeCommand(0x40 | address); |
KKempeneers | 1:ddcefddda4a7 | 107 | |
KKempeneers | 1:ddcefddda4a7 | 108 | // Write the data |
KKempeneers | 1:ddcefddda4a7 | 109 | for(int i = 0; i < nbytes; i++) { |
KKempeneers | 1:ddcefddda4a7 | 110 | writeData(*ptr++); |
KKempeneers | 1:ddcefddda4a7 | 111 | } |
KKempeneers | 1:ddcefddda4a7 | 112 | } |
KKempeneers | 1:ddcefddda4a7 | 113 | |
KKempeneers | 0:01f3d38f8b6d | 114 | // Low level output functions |
KKempeneers | 0:01f3d38f8b6d | 115 | void freetronicsLCDShield::writeByte (int byte) { |
KKempeneers | 0:01f3d38f8b6d | 116 | // Split the byte in high and low nibble, write high nibble first |
KKempeneers | 0:01f3d38f8b6d | 117 | _d = byte >> 4; |
KKempeneers | 0:01f3d38f8b6d | 118 | PULSE_E; |
KKempeneers | 0:01f3d38f8b6d | 119 | _d = byte >> 0; |
KKempeneers | 0:01f3d38f8b6d | 120 | PULSE_E; |
KKempeneers | 0:01f3d38f8b6d | 121 | // Most instructions take 40us |
KKempeneers | 0:01f3d38f8b6d | 122 | wait(0.000040f); |
KKempeneers | 0:01f3d38f8b6d | 123 | } |
KKempeneers | 0:01f3d38f8b6d | 124 | |
KKempeneers | 0:01f3d38f8b6d | 125 | void freetronicsLCDShield::writeData (int data) { |
KKempeneers | 0:01f3d38f8b6d | 126 | _rs = true; |
KKempeneers | 0:01f3d38f8b6d | 127 | writeByte(data); |
KKempeneers | 0:01f3d38f8b6d | 128 | } |
KKempeneers | 0:01f3d38f8b6d | 129 | |
KKempeneers | 0:01f3d38f8b6d | 130 | void freetronicsLCDShield::writeCommand (int command) { |
KKempeneers | 0:01f3d38f8b6d | 131 | _rs = false; |
KKempeneers | 0:01f3d38f8b6d | 132 | writeByte(command); |
KKempeneers | 0:01f3d38f8b6d | 133 | } |
KKempeneers | 0:01f3d38f8b6d | 134 | |
KKempeneers | 0:01f3d38f8b6d | 135 | float freetronicsLCDShield::readButton(void) { |
KKempeneers | 0:01f3d38f8b6d | 136 | return(_a0.read()); |
KKempeneers | 0:01f3d38f8b6d | 137 | } |
KKempeneers | 0:01f3d38f8b6d | 138 | |
KKempeneers | 0:01f3d38f8b6d | 139 | // Virtual functions for stream |
KKempeneers | 0:01f3d38f8b6d | 140 | int freetronicsLCDShield::_putc(int value) { |
KKempeneers | 0:01f3d38f8b6d | 141 | writeData(value); |
KKempeneers | 0:01f3d38f8b6d | 142 | return value; |
KKempeneers | 0:01f3d38f8b6d | 143 | } |
KKempeneers | 0:01f3d38f8b6d | 144 | |
KKempeneers | 0:01f3d38f8b6d | 145 | int freetronicsLCDShield::_getc() { |
KKempeneers | 0:01f3d38f8b6d | 146 | return -1; |
KKempeneers | 0:01f3d38f8b6d | 147 | } |