![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Wave-Player with TLV320
Dependencies: FatFileSystemCpp I2SSlave TLV320 mbed
LCD/LCD.cpp@0:3087f1924901, 2015-12-09 (annotated)
- Committer:
- HB9GAA
- Date:
- Wed Dec 09 20:58:55 2015 +0000
- Revision:
- 0:3087f1924901
Wave-Player with TLV320
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HB9GAA | 0:3087f1924901 | 1 | /* mbed LCD Library, for a 4-bit LCD based on HD44780 |
HB9GAA | 0:3087f1924901 | 2 | * Copyright (c) 2007-2012, hb9gaa |
HB9GAA | 0:3087f1924901 | 3 | */ |
HB9GAA | 0:3087f1924901 | 4 | |
HB9GAA | 0:3087f1924901 | 5 | #include "LCD.h" |
HB9GAA | 0:3087f1924901 | 6 | #include "mbed.h" |
HB9GAA | 0:3087f1924901 | 7 | |
HB9GAA | 0:3087f1924901 | 8 | TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw), _e(e), _d(d0, d1, d2, d3), _type(type) |
HB9GAA | 0:3087f1924901 | 9 | { |
HB9GAA | 0:3087f1924901 | 10 | _e = 1; |
HB9GAA | 0:3087f1924901 | 11 | _rw = 0; |
HB9GAA | 0:3087f1924901 | 12 | _rs = 0; // command mode |
HB9GAA | 0:3087f1924901 | 13 | |
HB9GAA | 0:3087f1924901 | 14 | wait(0.015); // Wait 15ms to ensure powered up |
HB9GAA | 0:3087f1924901 | 15 | |
HB9GAA | 0:3087f1924901 | 16 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
HB9GAA | 0:3087f1924901 | 17 | for (int i=0; i<3; i++) |
HB9GAA | 0:3087f1924901 | 18 | { |
HB9GAA | 0:3087f1924901 | 19 | writeByte(0x3); |
HB9GAA | 0:3087f1924901 | 20 | wait_ms(1.64); // this command takes 1.64ms, so wait for it |
HB9GAA | 0:3087f1924901 | 21 | } |
HB9GAA | 0:3087f1924901 | 22 | writeByte(0x2); // 4-bit mode |
HB9GAA | 0:3087f1924901 | 23 | wait_ms(0.1); // most instructions take 100us |
HB9GAA | 0:3087f1924901 | 24 | |
HB9GAA | 0:3087f1924901 | 25 | writeCommand(0x28); // Function set 001 BW N F - - |
HB9GAA | 0:3087f1924901 | 26 | writeCommand(0x0C); |
HB9GAA | 0:3087f1924901 | 27 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
HB9GAA | 0:3087f1924901 | 28 | cls(); |
HB9GAA | 0:3087f1924901 | 29 | } |
HB9GAA | 0:3087f1924901 | 30 | |
HB9GAA | 0:3087f1924901 | 31 | |
HB9GAA | 0:3087f1924901 | 32 | void TextLCD::character(int column, int row, int c) |
HB9GAA | 0:3087f1924901 | 33 | { |
HB9GAA | 0:3087f1924901 | 34 | int a = address(column, row); |
HB9GAA | 0:3087f1924901 | 35 | writeCommand(a); |
HB9GAA | 0:3087f1924901 | 36 | writeData(c); |
HB9GAA | 0:3087f1924901 | 37 | } |
HB9GAA | 0:3087f1924901 | 38 | |
HB9GAA | 0:3087f1924901 | 39 | |
HB9GAA | 0:3087f1924901 | 40 | void TextLCD::cls() |
HB9GAA | 0:3087f1924901 | 41 | { |
HB9GAA | 0:3087f1924901 | 42 | writeCommand(0x01); // cls, and set cursor to 0 |
HB9GAA | 0:3087f1924901 | 43 | wait_ms(4.1); // This command takes 4.1 ms |
HB9GAA | 0:3087f1924901 | 44 | locate(0, 0); |
HB9GAA | 0:3087f1924901 | 45 | } |
HB9GAA | 0:3087f1924901 | 46 | |
HB9GAA | 0:3087f1924901 | 47 | |
HB9GAA | 0:3087f1924901 | 48 | void TextLCD::locate(int column, int row) |
HB9GAA | 0:3087f1924901 | 49 | { |
HB9GAA | 0:3087f1924901 | 50 | _column = column; |
HB9GAA | 0:3087f1924901 | 51 | _row = row; |
HB9GAA | 0:3087f1924901 | 52 | } |
HB9GAA | 0:3087f1924901 | 53 | |
HB9GAA | 0:3087f1924901 | 54 | |
HB9GAA | 0:3087f1924901 | 55 | int TextLCD::_putc(int value) |
HB9GAA | 0:3087f1924901 | 56 | { |
HB9GAA | 0:3087f1924901 | 57 | if (value == '\n') |
HB9GAA | 0:3087f1924901 | 58 | { |
HB9GAA | 0:3087f1924901 | 59 | _column = 0; |
HB9GAA | 0:3087f1924901 | 60 | _row++; |
HB9GAA | 0:3087f1924901 | 61 | if (_row >= rows()) |
HB9GAA | 0:3087f1924901 | 62 | { |
HB9GAA | 0:3087f1924901 | 63 | _row = 0; |
HB9GAA | 0:3087f1924901 | 64 | } |
HB9GAA | 0:3087f1924901 | 65 | } |
HB9GAA | 0:3087f1924901 | 66 | else |
HB9GAA | 0:3087f1924901 | 67 | { |
HB9GAA | 0:3087f1924901 | 68 | character(_column, _row, value); |
HB9GAA | 0:3087f1924901 | 69 | _column++; |
HB9GAA | 0:3087f1924901 | 70 | if (_column >= columns()) |
HB9GAA | 0:3087f1924901 | 71 | { |
HB9GAA | 0:3087f1924901 | 72 | _column = 0; |
HB9GAA | 0:3087f1924901 | 73 | _row++; |
HB9GAA | 0:3087f1924901 | 74 | if (_row >= rows()) |
HB9GAA | 0:3087f1924901 | 75 | { |
HB9GAA | 0:3087f1924901 | 76 | _row = 0; |
HB9GAA | 0:3087f1924901 | 77 | } |
HB9GAA | 0:3087f1924901 | 78 | } |
HB9GAA | 0:3087f1924901 | 79 | } |
HB9GAA | 0:3087f1924901 | 80 | return value; |
HB9GAA | 0:3087f1924901 | 81 | } |
HB9GAA | 0:3087f1924901 | 82 | |
HB9GAA | 0:3087f1924901 | 83 | |
HB9GAA | 0:3087f1924901 | 84 | int TextLCD::_getc() |
HB9GAA | 0:3087f1924901 | 85 | { |
HB9GAA | 0:3087f1924901 | 86 | return -1; |
HB9GAA | 0:3087f1924901 | 87 | } |
HB9GAA | 0:3087f1924901 | 88 | |
HB9GAA | 0:3087f1924901 | 89 | |
HB9GAA | 0:3087f1924901 | 90 | void TextLCD::writeByte(int value) |
HB9GAA | 0:3087f1924901 | 91 | { |
HB9GAA | 0:3087f1924901 | 92 | _d = value >> 4; |
HB9GAA | 0:3087f1924901 | 93 | wait(0.0001); // most instructions take 100us |
HB9GAA | 0:3087f1924901 | 94 | _e = 0; |
HB9GAA | 0:3087f1924901 | 95 | wait(0.0001); |
HB9GAA | 0:3087f1924901 | 96 | _e = 1; |
HB9GAA | 0:3087f1924901 | 97 | _d = value >> 0; |
HB9GAA | 0:3087f1924901 | 98 | wait(0.0001); |
HB9GAA | 0:3087f1924901 | 99 | _e = 0; |
HB9GAA | 0:3087f1924901 | 100 | wait(0.0001); |
HB9GAA | 0:3087f1924901 | 101 | _e = 1; |
HB9GAA | 0:3087f1924901 | 102 | } |
HB9GAA | 0:3087f1924901 | 103 | |
HB9GAA | 0:3087f1924901 | 104 | |
HB9GAA | 0:3087f1924901 | 105 | void TextLCD::writeCommand(int command) |
HB9GAA | 0:3087f1924901 | 106 | { |
HB9GAA | 0:3087f1924901 | 107 | _rs = 0; |
HB9GAA | 0:3087f1924901 | 108 | writeByte(command); |
HB9GAA | 0:3087f1924901 | 109 | } |
HB9GAA | 0:3087f1924901 | 110 | |
HB9GAA | 0:3087f1924901 | 111 | |
HB9GAA | 0:3087f1924901 | 112 | void TextLCD::writeData(int data) |
HB9GAA | 0:3087f1924901 | 113 | { |
HB9GAA | 0:3087f1924901 | 114 | _rs = 1; |
HB9GAA | 0:3087f1924901 | 115 | writeByte(data); |
HB9GAA | 0:3087f1924901 | 116 | } |
HB9GAA | 0:3087f1924901 | 117 | |
HB9GAA | 0:3087f1924901 | 118 | |
HB9GAA | 0:3087f1924901 | 119 | int TextLCD::address(int column, int row) |
HB9GAA | 0:3087f1924901 | 120 | { |
HB9GAA | 0:3087f1924901 | 121 | switch (_type) |
HB9GAA | 0:3087f1924901 | 122 | { |
HB9GAA | 0:3087f1924901 | 123 | case LCD20x4: |
HB9GAA | 0:3087f1924901 | 124 | switch (row) |
HB9GAA | 0:3087f1924901 | 125 | { |
HB9GAA | 0:3087f1924901 | 126 | case 0: |
HB9GAA | 0:3087f1924901 | 127 | return 0x80 + column; |
HB9GAA | 0:3087f1924901 | 128 | case 1: |
HB9GAA | 0:3087f1924901 | 129 | return 0xc0 + column; |
HB9GAA | 0:3087f1924901 | 130 | case 2: |
HB9GAA | 0:3087f1924901 | 131 | return 0x94 + column; |
HB9GAA | 0:3087f1924901 | 132 | case 3: |
HB9GAA | 0:3087f1924901 | 133 | return 0xd4 + column; |
HB9GAA | 0:3087f1924901 | 134 | } |
HB9GAA | 0:3087f1924901 | 135 | case LCD16x2B: |
HB9GAA | 0:3087f1924901 | 136 | return 0x80 + (row * 40) + column; |
HB9GAA | 0:3087f1924901 | 137 | case LCD16x2: |
HB9GAA | 0:3087f1924901 | 138 | case LCD20x2: |
HB9GAA | 0:3087f1924901 | 139 | default: |
HB9GAA | 0:3087f1924901 | 140 | return 0x80 + (row * 0x40) + column; |
HB9GAA | 0:3087f1924901 | 141 | } |
HB9GAA | 0:3087f1924901 | 142 | } |
HB9GAA | 0:3087f1924901 | 143 | |
HB9GAA | 0:3087f1924901 | 144 | |
HB9GAA | 0:3087f1924901 | 145 | int TextLCD::columns() |
HB9GAA | 0:3087f1924901 | 146 | { |
HB9GAA | 0:3087f1924901 | 147 | switch (_type) |
HB9GAA | 0:3087f1924901 | 148 | { |
HB9GAA | 0:3087f1924901 | 149 | case LCD20x4: |
HB9GAA | 0:3087f1924901 | 150 | case LCD20x2: |
HB9GAA | 0:3087f1924901 | 151 | return 20; |
HB9GAA | 0:3087f1924901 | 152 | case LCD16x2: |
HB9GAA | 0:3087f1924901 | 153 | case LCD16x2B: |
HB9GAA | 0:3087f1924901 | 154 | default: |
HB9GAA | 0:3087f1924901 | 155 | return 16; |
HB9GAA | 0:3087f1924901 | 156 | } |
HB9GAA | 0:3087f1924901 | 157 | } |
HB9GAA | 0:3087f1924901 | 158 | |
HB9GAA | 0:3087f1924901 | 159 | |
HB9GAA | 0:3087f1924901 | 160 | int TextLCD::rows() |
HB9GAA | 0:3087f1924901 | 161 | { |
HB9GAA | 0:3087f1924901 | 162 | switch (_type) |
HB9GAA | 0:3087f1924901 | 163 | { |
HB9GAA | 0:3087f1924901 | 164 | case LCD20x4: |
HB9GAA | 0:3087f1924901 | 165 | return 4; |
HB9GAA | 0:3087f1924901 | 166 | case LCD16x2: |
HB9GAA | 0:3087f1924901 | 167 | case LCD16x2B: |
HB9GAA | 0:3087f1924901 | 168 | case LCD20x2: |
HB9GAA | 0:3087f1924901 | 169 | default: |
HB9GAA | 0:3087f1924901 | 170 | return 2; |
HB9GAA | 0:3087f1924901 | 171 | } |
HB9GAA | 0:3087f1924901 | 172 | } |
HB9GAA | 0:3087f1924901 | 173 | |
HB9GAA | 0:3087f1924901 | 174 | |
HB9GAA | 0:3087f1924901 | 175 | void TextLCD::buildChar(unsigned char *p) |
HB9GAA | 0:3087f1924901 | 176 | { |
HB9GAA | 0:3087f1924901 | 177 | unsigned char i, j; |
HB9GAA | 0:3087f1924901 | 178 | |
HB9GAA | 0:3087f1924901 | 179 | for(j=0;j<8;j++) //CGRAM Adresse |
HB9GAA | 0:3087f1924901 | 180 | { |
HB9GAA | 0:3087f1924901 | 181 | writeCommand(0x40+(j*8)); //Write to CGRAM |
HB9GAA | 0:3087f1924901 | 182 | for(i=0;i<8;i++) |
HB9GAA | 0:3087f1924901 | 183 | writeData(p[i+(j*8)]); //Write the character pattern to CGRAM |
HB9GAA | 0:3087f1924901 | 184 | } |
HB9GAA | 0:3087f1924901 | 185 | writeCommand(0x80); //shift back to DDRAM location 0 |
HB9GAA | 0:3087f1924901 | 186 | } |