Traditional LCD library that has been modified to be used with a 16x4 LCD display along with using the 16bit Timer wait function used in the 'LCD_Wait' library to measure Pulsewidth

Dependents:   PulseWidthCapture_Program

Committer:
Ellor1
Date:
Thu Dec 11 08:59:57 2014 +0000
Revision:
1:6ad3406d5912
Parent:
0:473988c75437
Library to include 16x4 LCD

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ellor1 1:6ad3406d5912 1 /***********************************************************************************************************************
Ellor1 1:6ad3406d5912 2 This library is based upon the original TextLCD library. This library has been modified so it can be used with a 16x4 LCD
Ellor1 1:6ad3406d5912 3 The traditional 'wait' function has been chnaged to use 16 bit Timer 1 rather than 32 bit Timer 0 which is what is set up
Ellor1 1:6ad3406d5912 4 usually in the mbed library. The reason for this change is so that the new wait function and this library could be used
Ellor1 1:6ad3406d5912 5 alongside a PulseWidthCapture program which needs to use the 32 bit timer.
Ellor1 1:6ad3406d5912 6
Ellor1 1:6ad3406d5912 7 This program requires Library 'LCD_Wait' to work
Ellor1 1:6ad3406d5912 8 Callum Ellor
Ellor1 1:6ad3406d5912 9 **************************************************************************************************************************/
Ellor1 0:473988c75437 10
Ellor1 0:473988c75437 11 #include "TextLCD_16x4.h"
Ellor1 0:473988c75437 12 #include "LCD_Wait.h"
Ellor1 0:473988c75437 13 #include "mbed.h"
Ellor1 0:473988c75437 14
Ellor1 0:473988c75437 15
Ellor1 1:6ad3406d5912 16 LCD_Wait lcd_wait; // Use LCD_Wait library to replace standard wait function
Ellor1 0:473988c75437 17
Ellor1 0:473988c75437 18 TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5,
Ellor1 0:473988c75437 19 PinName d6, PinName d7, LCDType type) : _rs(rs),
Ellor1 0:473988c75437 20 _e(e), _d(d4, d5, d6, d7),
Ellor1 0:473988c75437 21 _type(type) {
Ellor1 0:473988c75437 22
Ellor1 0:473988c75437 23 _e = 1;
Ellor1 0:473988c75437 24 _rs = 0; // command mode
Ellor1 0:473988c75437 25
Ellor1 0:473988c75437 26 lcd_wait.Wait(0.015); // Wait 15ms to ensure powered up
Ellor1 0:473988c75437 27
Ellor1 0:473988c75437 28 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
Ellor1 0:473988c75437 29 for (int i=0; i<3; i++) {
Ellor1 0:473988c75437 30 writeByte(0x3);
Ellor1 0:473988c75437 31 lcd_wait.Wait(0.00164); // this command takes 1.64ms, so wait for it
Ellor1 0:473988c75437 32 }
Ellor1 0:473988c75437 33 writeByte(0x2); // 4-bit mode
Ellor1 0:473988c75437 34 lcd_wait.Wait(0.000040f); // most instructions take 40us
Ellor1 0:473988c75437 35
Ellor1 0:473988c75437 36 writeCommand(0x28); // Function set 001 BW N F - -
Ellor1 0:473988c75437 37 writeCommand(0x0C);
Ellor1 0:473988c75437 38 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
Ellor1 0:473988c75437 39 cls();
Ellor1 0:473988c75437 40 }
Ellor1 0:473988c75437 41
Ellor1 0:473988c75437 42 void TextLCD::character(int column, int row, int c) {
Ellor1 0:473988c75437 43 int a = address(column, row);
Ellor1 0:473988c75437 44 writeCommand(a);
Ellor1 0:473988c75437 45 writeData(c);
Ellor1 0:473988c75437 46 }
Ellor1 0:473988c75437 47
Ellor1 0:473988c75437 48 void TextLCD::cls() {
Ellor1 0:473988c75437 49 writeCommand(0x01); // cls, and set cursor to 0
Ellor1 0:473988c75437 50 lcd_wait.Wait(0.00164f); // This command takes 1.64 ms
Ellor1 0:473988c75437 51 locate(0, 0);
Ellor1 0:473988c75437 52 }
Ellor1 0:473988c75437 53
Ellor1 0:473988c75437 54
Ellor1 0:473988c75437 55 void TextLCD::locate(int column, int row) {
Ellor1 0:473988c75437 56 _column = column;
Ellor1 0:473988c75437 57 _row = row;
Ellor1 0:473988c75437 58 }
Ellor1 0:473988c75437 59
Ellor1 0:473988c75437 60 int TextLCD::_putc(int value) {
Ellor1 0:473988c75437 61 if (value == '\n') {
Ellor1 0:473988c75437 62 _column = 0;
Ellor1 0:473988c75437 63 _row++;
Ellor1 0:473988c75437 64 if (_row >= rows()) {
Ellor1 0:473988c75437 65 _row = 0;
Ellor1 0:473988c75437 66 }
Ellor1 0:473988c75437 67 } else {
Ellor1 0:473988c75437 68 character(_column, _row, value);
Ellor1 0:473988c75437 69 _column++;
Ellor1 0:473988c75437 70 if (_column >= columns()) {
Ellor1 0:473988c75437 71 _column = 0;
Ellor1 0:473988c75437 72 _row++;
Ellor1 0:473988c75437 73 if (_row >= rows()) {
Ellor1 0:473988c75437 74 _row = 0;
Ellor1 0:473988c75437 75 }
Ellor1 0:473988c75437 76 }
Ellor1 0:473988c75437 77 }
Ellor1 0:473988c75437 78 return value;
Ellor1 0:473988c75437 79 }
Ellor1 0:473988c75437 80
Ellor1 0:473988c75437 81 int TextLCD::_getc() {
Ellor1 0:473988c75437 82 return -1;
Ellor1 0:473988c75437 83 }
Ellor1 0:473988c75437 84
Ellor1 0:473988c75437 85 void TextLCD::writeByte(int value) {
Ellor1 0:473988c75437 86 _d = value >> 4;
Ellor1 0:473988c75437 87 lcd_wait.Wait(0.000040f); // most instructions take 40us
Ellor1 0:473988c75437 88 _e = 0;
Ellor1 0:473988c75437 89 lcd_wait.Wait(0.000040f);
Ellor1 0:473988c75437 90 _e = 1;
Ellor1 0:473988c75437 91 _d = value >> 0;
Ellor1 0:473988c75437 92 lcd_wait.Wait(0.000040f);
Ellor1 0:473988c75437 93 _e = 0;
Ellor1 0:473988c75437 94 lcd_wait.Wait(0.000040f); // most instructions take 40us
Ellor1 0:473988c75437 95 _e = 1;
Ellor1 0:473988c75437 96 }
Ellor1 0:473988c75437 97
Ellor1 0:473988c75437 98 void TextLCD::writeCommand(int command) {
Ellor1 0:473988c75437 99 _rs = 0;
Ellor1 0:473988c75437 100 writeByte(command);
Ellor1 0:473988c75437 101 }
Ellor1 0:473988c75437 102
Ellor1 0:473988c75437 103 void TextLCD::writeData(int data) {
Ellor1 0:473988c75437 104 _rs = 1;
Ellor1 0:473988c75437 105 writeByte(data);
Ellor1 0:473988c75437 106 }
Ellor1 0:473988c75437 107
Ellor1 0:473988c75437 108 int TextLCD::address(int column, int row) {
Ellor1 0:473988c75437 109 switch (_type) {
Ellor1 0:473988c75437 110 case LCD20x4:
Ellor1 0:473988c75437 111 switch (row) {
Ellor1 0:473988c75437 112 case 0:
Ellor1 0:473988c75437 113 return 0x80 + column;
Ellor1 0:473988c75437 114 case 1:
Ellor1 0:473988c75437 115 return 0xc0 + column;
Ellor1 0:473988c75437 116 case 2:
Ellor1 0:473988c75437 117 return 0x94 + column;
Ellor1 0:473988c75437 118 case 3:
Ellor1 0:473988c75437 119 return 0xd4 + column;
Ellor1 0:473988c75437 120 }
Ellor1 1:6ad3406d5912 121 case LCD16x4: // 16x4 LCD added
Ellor1 0:473988c75437 122 switch (row) {
Ellor1 0:473988c75437 123 case 0:
Ellor1 0:473988c75437 124 return 0x80 + column;
Ellor1 0:473988c75437 125 case 1:
Ellor1 0:473988c75437 126 return 0xc0 + column;
Ellor1 0:473988c75437 127 case 2:
Ellor1 1:6ad3406d5912 128 return 0x90 + column; //changed to work with 16x4
Ellor1 0:473988c75437 129 case 3:
Ellor1 1:6ad3406d5912 130 return 0xd0 + column; //changed to work with 16x4
Ellor1 0:473988c75437 131 }
Ellor1 0:473988c75437 132 case LCD16x2B:
Ellor1 0:473988c75437 133 return 0x80 + (row * 40) + column;
Ellor1 0:473988c75437 134 case LCD16x2:
Ellor1 0:473988c75437 135 case LCD20x2:
Ellor1 0:473988c75437 136 default:
Ellor1 0:473988c75437 137 return 0x80 + (row * 0x40) + column;
Ellor1 0:473988c75437 138 }
Ellor1 0:473988c75437 139 }
Ellor1 0:473988c75437 140
Ellor1 0:473988c75437 141 int TextLCD::columns() {
Ellor1 0:473988c75437 142 switch (_type) {
Ellor1 0:473988c75437 143 case LCD20x4:
Ellor1 0:473988c75437 144 case LCD20x2:
Ellor1 0:473988c75437 145 return 20;
Ellor1 0:473988c75437 146 case LCD16x2:
Ellor1 0:473988c75437 147 case LCD16x2B:
Ellor1 1:6ad3406d5912 148 case LCD16x4: //added to work with 16x4
Ellor1 0:473988c75437 149 default:
Ellor1 0:473988c75437 150 return 16;
Ellor1 0:473988c75437 151 }
Ellor1 0:473988c75437 152 }
Ellor1 0:473988c75437 153
Ellor1 0:473988c75437 154 int TextLCD::rows() {
Ellor1 0:473988c75437 155 switch (_type) {
Ellor1 0:473988c75437 156 case LCD20x4:
Ellor1 1:6ad3406d5912 157 case LCD16x4: //added to work with 16x4
Ellor1 0:473988c75437 158 return 4;
Ellor1 0:473988c75437 159 case LCD16x2:
Ellor1 0:473988c75437 160 case LCD16x2B:
Ellor1 0:473988c75437 161 case LCD20x2:
Ellor1 0:473988c75437 162 default:
Ellor1 0:473988c75437 163 return 2;
Ellor1 0:473988c75437 164 }
Ellor1 0:473988c75437 165 }