Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280
DriverLCD.cpp@23:a6bb5298346c, 2018-01-10 (annotated)
- Committer:
- mwthewsey
- Date:
- Wed Jan 10 02:36:50 2018 +0000
- Revision:
- 23:a6bb5298346c
Added Custom LCD Driver
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mwthewsey | 23:a6bb5298346c | 1 | #include "DriverLCD.h" |
| mwthewsey | 23:a6bb5298346c | 2 | #include "mbed.h" |
| mwthewsey | 23:a6bb5298346c | 3 | |
| mwthewsey | 23:a6bb5298346c | 4 | DriverLCD::DriverLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7) : _rs(rs),_e(e), _d(d4, d5, d6, d7) |
| mwthewsey | 23:a6bb5298346c | 5 | { |
| mwthewsey | 23:a6bb5298346c | 6 | |
| mwthewsey | 23:a6bb5298346c | 7 | _e = 1; //Enable high, rs low to enter command mode |
| mwthewsey | 23:a6bb5298346c | 8 | _rs = 0; |
| mwthewsey | 23:a6bb5298346c | 9 | |
| mwthewsey | 23:a6bb5298346c | 10 | wait(0.015); //Wait 15ms for LCD to powerup |
| mwthewsey | 23:a6bb5298346c | 11 | |
| mwthewsey | 23:a6bb5298346c | 12 | for (int i=0; i<3; i++) { //Send command 3 times |
| mwthewsey | 23:a6bb5298346c | 13 | LCD_DATA(0x3,INIT); |
| mwthewsey | 23:a6bb5298346c | 14 | wait(0.002); //Wait 2ms, so wait for it |
| mwthewsey | 23:a6bb5298346c | 15 | } |
| mwthewsey | 23:a6bb5298346c | 16 | LCD_DATA(0x2,INIT); //4-bit mode |
| mwthewsey | 23:a6bb5298346c | 17 | wait(0.000040f); //Wait 40us |
| mwthewsey | 23:a6bb5298346c | 18 | |
| mwthewsey | 23:a6bb5298346c | 19 | LCD_DATA(0x28,CMD); //Function set. 2 lines, 5x8 dots, 1/16 dusy factor |
| mwthewsey | 23:a6bb5298346c | 20 | LCD_DATA(0x0C,CMD); //Turn Display on |
| mwthewsey | 23:a6bb5298346c | 21 | LCD_DATA(0x6,CMD); //Entry mode setting. Standard increment |
| mwthewsey | 23:a6bb5298346c | 22 | cls(); //Clear display |
| mwthewsey | 23:a6bb5298346c | 23 | } |
| mwthewsey | 23:a6bb5298346c | 24 | |
| mwthewsey | 23:a6bb5298346c | 25 | void DriverLCD::cls() |
| mwthewsey | 23:a6bb5298346c | 26 | { |
| mwthewsey | 23:a6bb5298346c | 27 | LCD_DATA(0x01,CMD); //Clear display command. |
| mwthewsey | 23:a6bb5298346c | 28 | wait(0.002); //Wait 2ms |
| mwthewsey | 23:a6bb5298346c | 29 | locate(0, 0); //Resets cursor to 0,0 |
| mwthewsey | 23:a6bb5298346c | 30 | } |
| mwthewsey | 23:a6bb5298346c | 31 | |
| mwthewsey | 23:a6bb5298346c | 32 | void DriverLCD::locate(int column, int row) |
| mwthewsey | 23:a6bb5298346c | 33 | { |
| mwthewsey | 23:a6bb5298346c | 34 | _column = column; //Set new column and row data to internal variables |
| mwthewsey | 23:a6bb5298346c | 35 | _row = row; |
| mwthewsey | 23:a6bb5298346c | 36 | } |
| mwthewsey | 23:a6bb5298346c | 37 | |
| mwthewsey | 23:a6bb5298346c | 38 | void DriverLCD::LCD_DATA(int data,int command) |
| mwthewsey | 23:a6bb5298346c | 39 | { |
| mwthewsey | 23:a6bb5298346c | 40 | if(command == 0) { //CMD = 0, enter command mode |
| mwthewsey | 23:a6bb5298346c | 41 | _rs = 0; |
| mwthewsey | 23:a6bb5298346c | 42 | } else if (command == 1) { //STR = 1 |
| mwthewsey | 23:a6bb5298346c | 43 | _rs = 1; |
| mwthewsey | 23:a6bb5298346c | 44 | } |
| mwthewsey | 23:a6bb5298346c | 45 | |
| mwthewsey | 23:a6bb5298346c | 46 | _d = data >> 4; //Send most significant nibble first |
| mwthewsey | 23:a6bb5298346c | 47 | wait(0.00004); //Wait 40us |
| mwthewsey | 23:a6bb5298346c | 48 | _e = 0; //Clock enable line to put in data |
| mwthewsey | 23:a6bb5298346c | 49 | wait(0.00004); |
| mwthewsey | 23:a6bb5298346c | 50 | _e = 1; |
| mwthewsey | 23:a6bb5298346c | 51 | |
| mwthewsey | 23:a6bb5298346c | 52 | _d = data >> 0; //Send least significant nibble seccond |
| mwthewsey | 23:a6bb5298346c | 53 | wait(0.00004); |
| mwthewsey | 23:a6bb5298346c | 54 | _e = 0; |
| mwthewsey | 23:a6bb5298346c | 55 | wait(0.00004); |
| mwthewsey | 23:a6bb5298346c | 56 | _e = 1; |
| mwthewsey | 23:a6bb5298346c | 57 | } |
| mwthewsey | 23:a6bb5298346c | 58 | |
| mwthewsey | 23:a6bb5298346c | 59 | void DriverLCD::character(int column, int row, int data) |
| mwthewsey | 23:a6bb5298346c | 60 | { |
| mwthewsey | 23:a6bb5298346c | 61 | int addr = 0x80 + (row * 0x40) + column; //Calculate address of segment |
| mwthewsey | 23:a6bb5298346c | 62 | LCD_DATA(addr,CMD); //Move cursor to new address |
| mwthewsey | 23:a6bb5298346c | 63 | LCD_DATA(data,STR); //Write data |
| mwthewsey | 23:a6bb5298346c | 64 | } |
| mwthewsey | 23:a6bb5298346c | 65 | |
| mwthewsey | 23:a6bb5298346c | 66 | |
| mwthewsey | 23:a6bb5298346c | 67 | int DriverLCD::_putc(int data) |
| mwthewsey | 23:a6bb5298346c | 68 | { |
| mwthewsey | 23:a6bb5298346c | 69 | if (data == '\n') { //If new line is found, move down a row |
| mwthewsey | 23:a6bb5298346c | 70 | _column = 0; //Reset to first column |
| mwthewsey | 23:a6bb5298346c | 71 | _row++; //inc row |
| mwthewsey | 23:a6bb5298346c | 72 | if (_row >= 2) {//Only two rows on this LCD, move up a row instead. |
| mwthewsey | 23:a6bb5298346c | 73 | _row = 0; |
| mwthewsey | 23:a6bb5298346c | 74 | } |
| mwthewsey | 23:a6bb5298346c | 75 | } else { |
| mwthewsey | 23:a6bb5298346c | 76 | character(_column, _row, data); //Write char |
| mwthewsey | 23:a6bb5298346c | 77 | _column++; //Then increment column and row to next location |
| mwthewsey | 23:a6bb5298346c | 78 | if (_column >= 16) { |
| mwthewsey | 23:a6bb5298346c | 79 | _column = 0; |
| mwthewsey | 23:a6bb5298346c | 80 | _row++; |
| mwthewsey | 23:a6bb5298346c | 81 | if (_row >= 2) { |
| mwthewsey | 23:a6bb5298346c | 82 | _row = 0; |
| mwthewsey | 23:a6bb5298346c | 83 | } |
| mwthewsey | 23:a6bb5298346c | 84 | } |
| mwthewsey | 23:a6bb5298346c | 85 | } |
| mwthewsey | 23:a6bb5298346c | 86 | return data; |
| mwthewsey | 23:a6bb5298346c | 87 | } |
| mwthewsey | 23:a6bb5298346c | 88 | |
| mwthewsey | 23:a6bb5298346c | 89 | int DriverLCD::_getc() |
| mwthewsey | 23:a6bb5298346c | 90 | { |
| mwthewsey | 23:a6bb5298346c | 91 | return -1; |
| mwthewsey | 23:a6bb5298346c | 92 | } |
| mwthewsey | 23:a6bb5298346c | 93 | |
| mwthewsey | 23:a6bb5298346c | 94 |