HD44780 compatible 16x2 character LCD driver with HC595 shiftregister

Dependents:   mbed_3pinLCD

Committer:
k4zuki
Date:
Fri Jun 20 01:07:06 2014 +0000
Revision:
0:9cf8e8a3a8d6
porting from arduino library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
k4zuki 0:9cf8e8a3a8d6 1 /** this is for HD44780 compatible 16x2 charachter LCD with HC595 by bitbanging
k4zuki 0:9cf8e8a3a8d6 2 * by Kazuki Yamamoto, or _K4ZUKI_
k4zuki 0:9cf8e8a3a8d6 3 */
k4zuki 0:9cf8e8a3a8d6 4
k4zuki 0:9cf8e8a3a8d6 5 #ifndef __TRIPINLCD_H__
k4zuki 0:9cf8e8a3a8d6 6 #define __TRIPINLCD_H__
k4zuki 0:9cf8e8a3a8d6 7
k4zuki 0:9cf8e8a3a8d6 8 #include <inttypes.h>
k4zuki 0:9cf8e8a3a8d6 9 #include "mbed.h"
k4zuki 0:9cf8e8a3a8d6 10 #include "ShiftOut.h"
k4zuki 0:9cf8e8a3a8d6 11
k4zuki 0:9cf8e8a3a8d6 12 // commands
k4zuki 0:9cf8e8a3a8d6 13 #define LCD_CLEARDISPLAY 0x01
k4zuki 0:9cf8e8a3a8d6 14 #define LCD_RETURNHOME 0x02
k4zuki 0:9cf8e8a3a8d6 15 #define LCD_ENTRYMODESET 0x04
k4zuki 0:9cf8e8a3a8d6 16 #define LCD_DISPLAYCONTROL 0x08
k4zuki 0:9cf8e8a3a8d6 17 #define LCD_CURSORSHIFT 0x10
k4zuki 0:9cf8e8a3a8d6 18 #define LCD_FUNCTIONSET 0x20
k4zuki 0:9cf8e8a3a8d6 19 #define LCD_SETCGRAMADDR 0x40
k4zuki 0:9cf8e8a3a8d6 20 #define LCD_SETDDRAMADDR 0x80
k4zuki 0:9cf8e8a3a8d6 21
k4zuki 0:9cf8e8a3a8d6 22 // flags for display entry mode
k4zuki 0:9cf8e8a3a8d6 23 #define LCD_ENTRYRIGHT 0x00
k4zuki 0:9cf8e8a3a8d6 24 #define LCD_ENTRYLEFT 0x02
k4zuki 0:9cf8e8a3a8d6 25 #define LCD_ENTRYSHIFTINCREMENT 0x01
k4zuki 0:9cf8e8a3a8d6 26 #define LCD_ENTRYSHIFTDECREMENT 0x00
k4zuki 0:9cf8e8a3a8d6 27
k4zuki 0:9cf8e8a3a8d6 28 // flags for display on/off control
k4zuki 0:9cf8e8a3a8d6 29 #define LCD_DISPLAYON 0x04
k4zuki 0:9cf8e8a3a8d6 30 #define LCD_DISPLAYOFF 0x00
k4zuki 0:9cf8e8a3a8d6 31 #define LCD_CURSORON 0x02
k4zuki 0:9cf8e8a3a8d6 32 #define LCD_CURSOROFF 0x00
k4zuki 0:9cf8e8a3a8d6 33 #define LCD_BLINKON 0x01
k4zuki 0:9cf8e8a3a8d6 34 #define LCD_BLINKOFF 0x00
k4zuki 0:9cf8e8a3a8d6 35
k4zuki 0:9cf8e8a3a8d6 36 // flags for display/cursor shift
k4zuki 0:9cf8e8a3a8d6 37 #define LCD_DISPLAYMOVE 0x08
k4zuki 0:9cf8e8a3a8d6 38 #define LCD_CURSORMOVE 0x00
k4zuki 0:9cf8e8a3a8d6 39 #define LCD_MOVERIGHT 0x04
k4zuki 0:9cf8e8a3a8d6 40 #define LCD_MOVELEFT 0x00
k4zuki 0:9cf8e8a3a8d6 41
k4zuki 0:9cf8e8a3a8d6 42 // flags for function set
k4zuki 0:9cf8e8a3a8d6 43 #define LCD_3WIERMODE 0x20
k4zuki 0:9cf8e8a3a8d6 44 #define LCD_8BITMODE 0x10
k4zuki 0:9cf8e8a3a8d6 45 #define LCD_4BITMODE 0x00
k4zuki 0:9cf8e8a3a8d6 46 #define LCD_2LINE 0x08
k4zuki 0:9cf8e8a3a8d6 47 #define LCD_1LINE 0x00
k4zuki 0:9cf8e8a3a8d6 48 #define LCD_5x10DOTS 0x04
k4zuki 0:9cf8e8a3a8d6 49 #define LCD_5x8DOTS 0x00
k4zuki 0:9cf8e8a3a8d6 50
k4zuki 0:9cf8e8a3a8d6 51 //#define SHIFT_RS (0x02) //B00000010
k4zuki 0:9cf8e8a3a8d6 52 #define SHIFT_RS (0x01) //B00000010
k4zuki 0:9cf8e8a3a8d6 53 #define SHIFT_EN (0x08) //B00001000
k4zuki 0:9cf8e8a3a8d6 54
k4zuki 0:9cf8e8a3a8d6 55 #define DATA_BYTE (1)
k4zuki 0:9cf8e8a3a8d6 56 #define CMND_BYTE (0)
k4zuki 0:9cf8e8a3a8d6 57
k4zuki 0:9cf8e8a3a8d6 58 /** tripinLcd
k4zuki 0:9cf8e8a3a8d6 59 * mbed library for HD44780 compatible charachter LCD with HC595 by bitbanging
k4zuki 0:9cf8e8a3a8d6 60 *
k4zuki 0:9cf8e8a3a8d6 61 * Example:
k4zuki 0:9cf8e8a3a8d6 62 * @code
k4zuki 0:9cf8e8a3a8d6 63 #include "mbed.h"
k4zuki 0:9cf8e8a3a8d6 64 #include "TriPinLcd.h"
k4zuki 0:9cf8e8a3a8d6 65
k4zuki 0:9cf8e8a3a8d6 66 Timer t;
k4zuki 0:9cf8e8a3a8d6 67 TriPinLcd lcd(p5,p7,p6);//clk = p7, data = p5, latch = p6
k4zuki 0:9cf8e8a3a8d6 68
k4zuki 0:9cf8e8a3a8d6 69 int main()
k4zuki 0:9cf8e8a3a8d6 70 {
k4zuki 0:9cf8e8a3a8d6 71
k4zuki 0:9cf8e8a3a8d6 72 lcd.begin(16, 2);
k4zuki 0:9cf8e8a3a8d6 73 lcd.noBlink();
k4zuki 0:9cf8e8a3a8d6 74
k4zuki 0:9cf8e8a3a8d6 75 lcd.printf("LCD3WIRE mbed");
k4zuki 0:9cf8e8a3a8d6 76 t.start();
k4zuki 0:9cf8e8a3a8d6 77 while(1) {
k4zuki 0:9cf8e8a3a8d6 78 lcd.setCursor(0, 1);
k4zuki 0:9cf8e8a3a8d6 79 // lcd.printf("%.0f", t.read());
k4zuki 0:9cf8e8a3a8d6 80 lcd.printf("%d", t.read_us());
k4zuki 0:9cf8e8a3a8d6 81 }
k4zuki 0:9cf8e8a3a8d6 82 }
k4zuki 0:9cf8e8a3a8d6 83
k4zuki 0:9cf8e8a3a8d6 84 * @endcode
k4zuki 0:9cf8e8a3a8d6 85 */
k4zuki 0:9cf8e8a3a8d6 86
k4zuki 0:9cf8e8a3a8d6 87 class TriPinLcd : public Stream
k4zuki 0:9cf8e8a3a8d6 88 {
k4zuki 0:9cf8e8a3a8d6 89
k4zuki 0:9cf8e8a3a8d6 90 public:
k4zuki 0:9cf8e8a3a8d6 91 /** Constructor
k4zuki 0:9cf8e8a3a8d6 92 * @param data data input
k4zuki 0:9cf8e8a3a8d6 93 * @param clock clock input
k4zuki 0:9cf8e8a3a8d6 94 * @param latch latches HC595 and sends data to LCD
k4zuki 0:9cf8e8a3a8d6 95 */
k4zuki 0:9cf8e8a3a8d6 96 TriPinLcd(PinName data, PinName clock, PinName latch);
k4zuki 0:9cf8e8a3a8d6 97
k4zuki 0:9cf8e8a3a8d6 98 void begin(uint8_t cols, uint8_t rows, uint8_t dotsize = LCD_5x8DOTS);
k4zuki 0:9cf8e8a3a8d6 99
k4zuki 0:9cf8e8a3a8d6 100 /**
k4zuki 0:9cf8e8a3a8d6 101 clears display, set cursor position to zero
k4zuki 0:9cf8e8a3a8d6 102 */
k4zuki 0:9cf8e8a3a8d6 103 void clear();
k4zuki 0:9cf8e8a3a8d6 104
k4zuki 0:9cf8e8a3a8d6 105 /**
k4zuki 0:9cf8e8a3a8d6 106 set cursor position to zero
k4zuki 0:9cf8e8a3a8d6 107 */
k4zuki 0:9cf8e8a3a8d6 108 void home();
k4zuki 0:9cf8e8a3a8d6 109 /**
k4zuki 0:9cf8e8a3a8d6 110 Turn the display off
k4zuki 0:9cf8e8a3a8d6 111 */
k4zuki 0:9cf8e8a3a8d6 112 void noDisplay();
k4zuki 0:9cf8e8a3a8d6 113 /**
k4zuki 0:9cf8e8a3a8d6 114 Turn the display on
k4zuki 0:9cf8e8a3a8d6 115 */
k4zuki 0:9cf8e8a3a8d6 116 void display();
k4zuki 0:9cf8e8a3a8d6 117 /**
k4zuki 0:9cf8e8a3a8d6 118 Turns off the blinking cursor
k4zuki 0:9cf8e8a3a8d6 119 */
k4zuki 0:9cf8e8a3a8d6 120 void noBlink();
k4zuki 0:9cf8e8a3a8d6 121 /**
k4zuki 0:9cf8e8a3a8d6 122 Turns on the blinking cursor
k4zuki 0:9cf8e8a3a8d6 123 */
k4zuki 0:9cf8e8a3a8d6 124 void blink();
k4zuki 0:9cf8e8a3a8d6 125 /**
k4zuki 0:9cf8e8a3a8d6 126 Turns the underline cursor off
k4zuki 0:9cf8e8a3a8d6 127 */
k4zuki 0:9cf8e8a3a8d6 128 void noCursor();
k4zuki 0:9cf8e8a3a8d6 129 /**
k4zuki 0:9cf8e8a3a8d6 130 Turns the underline cursor on
k4zuki 0:9cf8e8a3a8d6 131 */
k4zuki 0:9cf8e8a3a8d6 132 void cursor();
k4zuki 0:9cf8e8a3a8d6 133 /**
k4zuki 0:9cf8e8a3a8d6 134 scroll the display to left without changing the RAM
k4zuki 0:9cf8e8a3a8d6 135 */
k4zuki 0:9cf8e8a3a8d6 136 void scrollDisplayLeft();
k4zuki 0:9cf8e8a3a8d6 137 /**
k4zuki 0:9cf8e8a3a8d6 138 scroll the display to right without changing the RAM
k4zuki 0:9cf8e8a3a8d6 139 */
k4zuki 0:9cf8e8a3a8d6 140 void scrollDisplayRight();
k4zuki 0:9cf8e8a3a8d6 141 /**
k4zuki 0:9cf8e8a3a8d6 142 This is for text that flows Left to Right
k4zuki 0:9cf8e8a3a8d6 143 */
k4zuki 0:9cf8e8a3a8d6 144 void leftToRight();
k4zuki 0:9cf8e8a3a8d6 145 /**
k4zuki 0:9cf8e8a3a8d6 146 This is for text that flows Right to Left
k4zuki 0:9cf8e8a3a8d6 147 */
k4zuki 0:9cf8e8a3a8d6 148 void rightToLeft();
k4zuki 0:9cf8e8a3a8d6 149 /**
k4zuki 0:9cf8e8a3a8d6 150 This will 'right justify' text from the cursor
k4zuki 0:9cf8e8a3a8d6 151 */
k4zuki 0:9cf8e8a3a8d6 152 void autoscroll();
k4zuki 0:9cf8e8a3a8d6 153 /**
k4zuki 0:9cf8e8a3a8d6 154 This will 'left justify' text from the cursor
k4zuki 0:9cf8e8a3a8d6 155 */
k4zuki 0:9cf8e8a3a8d6 156 void noAutoscroll();
k4zuki 0:9cf8e8a3a8d6 157
k4zuki 0:9cf8e8a3a8d6 158 /**
k4zuki 0:9cf8e8a3a8d6 159 Allows us to fill the first 8 CGRAM locations
k4zuki 0:9cf8e8a3a8d6 160 with custom characters
k4zuki 0:9cf8e8a3a8d6 161 */
k4zuki 0:9cf8e8a3a8d6 162 void createChar(uint8_t, uint8_t[]);
k4zuki 0:9cf8e8a3a8d6 163 /**
k4zuki 0:9cf8e8a3a8d6 164 set cursor position to (x=col,y=row); (0,0) is left-top
k4zuki 0:9cf8e8a3a8d6 165 @param col X position
k4zuki 0:9cf8e8a3a8d6 166 @param row Y position
k4zuki 0:9cf8e8a3a8d6 167 */
k4zuki 0:9cf8e8a3a8d6 168 void setCursor(uint8_t, uint8_t);
k4zuki 0:9cf8e8a3a8d6 169
k4zuki 0:9cf8e8a3a8d6 170
k4zuki 0:9cf8e8a3a8d6 171 private:
k4zuki 0:9cf8e8a3a8d6 172 ShiftOut::ShiftOut _lcd;
k4zuki 0:9cf8e8a3a8d6 173 void _write(uint8_t);
k4zuki 0:9cf8e8a3a8d6 174 void _command(uint8_t);
k4zuki 0:9cf8e8a3a8d6 175
k4zuki 0:9cf8e8a3a8d6 176 virtual int _putc(int value);
k4zuki 0:9cf8e8a3a8d6 177 virtual int _getc();
k4zuki 0:9cf8e8a3a8d6 178
k4zuki 0:9cf8e8a3a8d6 179 void _send(uint8_t, uint8_t);
k4zuki 0:9cf8e8a3a8d6 180 void _write4bits(uint8_t);
k4zuki 0:9cf8e8a3a8d6 181
k4zuki 0:9cf8e8a3a8d6 182 uint8_t _displayfunction;
k4zuki 0:9cf8e8a3a8d6 183 uint8_t _displaycontrol;
k4zuki 0:9cf8e8a3a8d6 184 uint8_t _displaymode;
k4zuki 0:9cf8e8a3a8d6 185
k4zuki 0:9cf8e8a3a8d6 186 uint8_t _numlines,_currline;
k4zuki 0:9cf8e8a3a8d6 187
k4zuki 0:9cf8e8a3a8d6 188 };
k4zuki 0:9cf8e8a3a8d6 189
k4zuki 0:9cf8e8a3a8d6 190 #endif