cycyy

Dependencies:   MCP23017 WattBob_TextLCD

Committer:
mihaidd
Date:
Wed May 08 03:58:47 2019 +0000
Revision:
0:a9b4ee4ed395
ccc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mihaidd 0:a9b4ee4ed395 1 /* draft mbed TextLCD
mihaidd 0:a9b4ee4ed395 2 * (c) 2007/8, sford
mihaidd 0:a9b4ee4ed395 3 * Modified jherd
mihaidd 0:a9b4ee4ed395 4 */
mihaidd 0:a9b4ee4ed395 5
mihaidd 0:a9b4ee4ed395 6 #include "WattBob_TextLCD.h"
mihaidd 0:a9b4ee4ed395 7 #include "MCP23017.h"
mihaidd 0:a9b4ee4ed395 8
mihaidd 0:a9b4ee4ed395 9 #include "mbed.h"
mihaidd 0:a9b4ee4ed395 10
mihaidd 0:a9b4ee4ed395 11 /*
mihaidd 0:a9b4ee4ed395 12 * Initialisation
mihaidd 0:a9b4ee4ed395 13 * ==============
mihaidd 0:a9b4ee4ed395 14 *
mihaidd 0:a9b4ee4ed395 15 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
mihaidd 0:a9b4ee4ed395 16 *
mihaidd 0:a9b4ee4ed395 17 * - wait approximately 15 ms so the display is ready to execute commands
mihaidd 0:a9b4ee4ed395 18 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
mihaidd 0:a9b4ee4ed395 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.
mihaidd 0:a9b4ee4ed395 20 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
mihaidd 0:a9b4ee4ed395 21 * - Execute the "clear display" command
mihaidd 0:a9b4ee4ed395 22 *
mihaidd 0:a9b4ee4ed395 23 * Timing
mihaidd 0:a9b4ee4ed395 24 * ======
mihaidd 0:a9b4ee4ed395 25 *
mihaidd 0:a9b4ee4ed395 26 * Nearly all commands transmitted to the display need 40us for execution.
mihaidd 0:a9b4ee4ed395 27 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
mihaidd 0:a9b4ee4ed395 28 * These commands need 1.64ms for execution. These timings are valid for all displays working with an
mihaidd 0:a9b4ee4ed395 29 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
mihaidd 0:a9b4ee4ed395 30 * can use the busy flag to test if the display is ready to accept the next command.
mihaidd 0:a9b4ee4ed395 31 *
mihaidd 0:a9b4ee4ed395 32 * _e is kept high apart from calling clock
mihaidd 0:a9b4ee4ed395 33 * _rw is kept 0 (write) apart from actions that uses it differently
mihaidd 0:a9b4ee4ed395 34 * _rs is set by the data/command writes
mihaidd 0:a9b4ee4ed395 35 *
mihaidd 0:a9b4ee4ed395 36 * RS = 7
mihaidd 0:a9b4ee4ed395 37 * RW = 6
mihaidd 0:a9b4ee4ed395 38 * E = 5
mihaidd 0:a9b4ee4ed395 39 * Back light = 4
mihaidd 0:a9b4ee4ed395 40 * D4 = 0
mihaidd 0:a9b4ee4ed395 41 * D5 = 1
mihaidd 0:a9b4ee4ed395 42 * D6 = 2
mihaidd 0:a9b4ee4ed395 43 * D7 = 3
mihaidd 0:a9b4ee4ed395 44 */
mihaidd 0:a9b4ee4ed395 45
mihaidd 0:a9b4ee4ed395 46 WattBob_TextLCD::WattBob_TextLCD(MCP23017 *port) {
mihaidd 0:a9b4ee4ed395 47 //
mihaidd 0:a9b4ee4ed395 48 // Initialise pointer to MCP23017 object
mihaidd 0:a9b4ee4ed395 49 //
mihaidd 0:a9b4ee4ed395 50 par_port = port;
mihaidd 0:a9b4ee4ed395 51 par_port->config(0x0F00, 0x0F00, 0x0F00);
mihaidd 0:a9b4ee4ed395 52
mihaidd 0:a9b4ee4ed395 53 _rows = 2;
mihaidd 0:a9b4ee4ed395 54 _columns = 16;
mihaidd 0:a9b4ee4ed395 55
mihaidd 0:a9b4ee4ed395 56 //
mihaidd 0:a9b4ee4ed395 57 // Time to allow unit to initialise
mihaidd 0:a9b4ee4ed395 58 //
mihaidd 0:a9b4ee4ed395 59 wait(DISPLAY_INIT_DELAY_SECS);
mihaidd 0:a9b4ee4ed395 60
mihaidd 0:a9b4ee4ed395 61 _rw(0);
mihaidd 0:a9b4ee4ed395 62 _e(0);
mihaidd 0:a9b4ee4ed395 63 _rs(0); // command mode
mihaidd 0:a9b4ee4ed395 64
mihaidd 0:a9b4ee4ed395 65 //
mihaidd 0:a9b4ee4ed395 66 // interface defaults to an 8-bit interface. However, we need to ensure that we
mihaidd 0:a9b4ee4ed395 67 // are in 8-bit mode
mihaidd 0:a9b4ee4ed395 68 //
mihaidd 0:a9b4ee4ed395 69 for(int i=0; i<3; i++) {
mihaidd 0:a9b4ee4ed395 70 writeNibble(CMD4_SET_8_BIT_INTERFACE);
mihaidd 0:a9b4ee4ed395 71 wait(0.00164); // this command takes 1.64ms, so wait for it
mihaidd 0:a9b4ee4ed395 72 }
mihaidd 0:a9b4ee4ed395 73
mihaidd 0:a9b4ee4ed395 74 writeNibble(CMD4_SET_4_BIT_INTERFACE); // now force into 4-bit mode
mihaidd 0:a9b4ee4ed395 75
mihaidd 0:a9b4ee4ed395 76 // writeCommand(CMD_NULL);
mihaidd 0:a9b4ee4ed395 77 // writeCommand(CMD_NULL);
mihaidd 0:a9b4ee4ed395 78 // writeCommand(CMD_NULL);
mihaidd 0:a9b4ee4ed395 79 // writeCommand(CMD_NULL);
mihaidd 0:a9b4ee4ed395 80 // writeCommand(CMD_NULL);
mihaidd 0:a9b4ee4ed395 81
mihaidd 0:a9b4ee4ed395 82 writeCommand(CMD_FUNCTION_SET | INTERFACE_4_BIT | TWO_LINE_DISPLAY | FONT_5x8 | ENGL_JAPAN_FONT_SET); // 0x28
mihaidd 0:a9b4ee4ed395 83 writeCommand(CMD_DISPLAY_CONTROL | DISPLAY_ON | CURSOR_OFF | CURSOR_CHAR_BLINK_OFF); // 0xC0
mihaidd 0:a9b4ee4ed395 84 cls();
mihaidd 0:a9b4ee4ed395 85 writeCommand(CMD_RETURN_HOME);
mihaidd 0:a9b4ee4ed395 86 writeCommand(CMD_ENTRY_MODE | CURSOR_STEP_RIGHT | DISPLAY_SHIFT_OFF ); // 0x06
mihaidd 0:a9b4ee4ed395 87 }
mihaidd 0:a9b4ee4ed395 88
mihaidd 0:a9b4ee4ed395 89 int WattBob_TextLCD::_putc(int value) {
mihaidd 0:a9b4ee4ed395 90 if(value == '\n') {
mihaidd 0:a9b4ee4ed395 91 newline();
mihaidd 0:a9b4ee4ed395 92 } else {
mihaidd 0:a9b4ee4ed395 93 writeData(value);
mihaidd 0:a9b4ee4ed395 94 }
mihaidd 0:a9b4ee4ed395 95 return value;
mihaidd 0:a9b4ee4ed395 96 }
mihaidd 0:a9b4ee4ed395 97
mihaidd 0:a9b4ee4ed395 98 int WattBob_TextLCD::_getc() {
mihaidd 0:a9b4ee4ed395 99 return 0;
mihaidd 0:a9b4ee4ed395 100 }
mihaidd 0:a9b4ee4ed395 101
mihaidd 0:a9b4ee4ed395 102 void WattBob_TextLCD::newline() {
mihaidd 0:a9b4ee4ed395 103 _column = 0;
mihaidd 0:a9b4ee4ed395 104 _row++;
mihaidd 0:a9b4ee4ed395 105 if(_row >= _rows) {
mihaidd 0:a9b4ee4ed395 106 _row = 0;
mihaidd 0:a9b4ee4ed395 107 }
mihaidd 0:a9b4ee4ed395 108 locate(_column, _row);
mihaidd 0:a9b4ee4ed395 109 }
mihaidd 0:a9b4ee4ed395 110
mihaidd 0:a9b4ee4ed395 111 void WattBob_TextLCD::locate(int row, int column) {
mihaidd 0:a9b4ee4ed395 112 if(column < 0 || column >= _columns || row < 0 || row >= _rows) {
mihaidd 0:a9b4ee4ed395 113 // error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
mihaidd 0:a9b4ee4ed395 114 return;
mihaidd 0:a9b4ee4ed395 115 }
mihaidd 0:a9b4ee4ed395 116
mihaidd 0:a9b4ee4ed395 117 _row = row;
mihaidd 0:a9b4ee4ed395 118 _column = column;
mihaidd 0:a9b4ee4ed395 119 int address = 0x80 + (_row * 0x40) + _column; // memory starts at 0x80, and internally it is 40 chars per row (only first 16 used)
mihaidd 0:a9b4ee4ed395 120 writeCommand(address);
mihaidd 0:a9b4ee4ed395 121 }
mihaidd 0:a9b4ee4ed395 122
mihaidd 0:a9b4ee4ed395 123 void WattBob_TextLCD::cls() {
mihaidd 0:a9b4ee4ed395 124 writeCommand(CMD_CLEAR_DISPLAY); // 0x01
mihaidd 0:a9b4ee4ed395 125 wait(DISPLAY_CLEAR_DELAY); //
mihaidd 0:a9b4ee4ed395 126 locate(0, 0);
mihaidd 0:a9b4ee4ed395 127 }
mihaidd 0:a9b4ee4ed395 128
mihaidd 0:a9b4ee4ed395 129 void WattBob_TextLCD::reset() {
mihaidd 0:a9b4ee4ed395 130 cls();
mihaidd 0:a9b4ee4ed395 131 }
mihaidd 0:a9b4ee4ed395 132
mihaidd 0:a9b4ee4ed395 133 void WattBob_TextLCD::clock() {
mihaidd 0:a9b4ee4ed395 134 wait(0.000040f);
mihaidd 0:a9b4ee4ed395 135 _e(1);
mihaidd 0:a9b4ee4ed395 136 wait(0.000040f); // most instructions take 40us
mihaidd 0:a9b4ee4ed395 137 _e(0);
mihaidd 0:a9b4ee4ed395 138 }
mihaidd 0:a9b4ee4ed395 139
mihaidd 0:a9b4ee4ed395 140 void WattBob_TextLCD::writeNibble(int value) {
mihaidd 0:a9b4ee4ed395 141 _d(value);
mihaidd 0:a9b4ee4ed395 142 clock();
mihaidd 0:a9b4ee4ed395 143 }
mihaidd 0:a9b4ee4ed395 144
mihaidd 0:a9b4ee4ed395 145 void WattBob_TextLCD::writeByte(int value) {
mihaidd 0:a9b4ee4ed395 146 writeNibble((value >> 4) & 0x000F);
mihaidd 0:a9b4ee4ed395 147 writeNibble((value >> 0) & 0x000F);
mihaidd 0:a9b4ee4ed395 148 }
mihaidd 0:a9b4ee4ed395 149
mihaidd 0:a9b4ee4ed395 150 void WattBob_TextLCD::writeCommand(int command) {
mihaidd 0:a9b4ee4ed395 151 _rs(0);
mihaidd 0:a9b4ee4ed395 152 writeByte(command);
mihaidd 0:a9b4ee4ed395 153 }
mihaidd 0:a9b4ee4ed395 154
mihaidd 0:a9b4ee4ed395 155 void WattBob_TextLCD::writeData(int data) {
mihaidd 0:a9b4ee4ed395 156 _rs(1);
mihaidd 0:a9b4ee4ed395 157 writeByte(data);
mihaidd 0:a9b4ee4ed395 158 _column++;
mihaidd 0:a9b4ee4ed395 159 if(_column >= _columns) {
mihaidd 0:a9b4ee4ed395 160 newline();
mihaidd 0:a9b4ee4ed395 161 }
mihaidd 0:a9b4ee4ed395 162 }
mihaidd 0:a9b4ee4ed395 163
mihaidd 0:a9b4ee4ed395 164 void WattBob_TextLCD::_rs(int data) {
mihaidd 0:a9b4ee4ed395 165 par_port->write_bit(data, RS_BIT);
mihaidd 0:a9b4ee4ed395 166 }
mihaidd 0:a9b4ee4ed395 167
mihaidd 0:a9b4ee4ed395 168 void WattBob_TextLCD::_rw(int data) {
mihaidd 0:a9b4ee4ed395 169 par_port->write_bit(data, RW_BIT);
mihaidd 0:a9b4ee4ed395 170 }
mihaidd 0:a9b4ee4ed395 171
mihaidd 0:a9b4ee4ed395 172 void WattBob_TextLCD::_e(int data) {
mihaidd 0:a9b4ee4ed395 173 par_port->write_bit(data, E_BIT);
mihaidd 0:a9b4ee4ed395 174 }
mihaidd 0:a9b4ee4ed395 175
mihaidd 0:a9b4ee4ed395 176 void WattBob_TextLCD::_d(int data) {
mihaidd 0:a9b4ee4ed395 177 par_port->write_mask((unsigned short)data, (unsigned short)0x000F);
mihaidd 0:a9b4ee4ed395 178 }