Roland Elmiger / Mbed 2 deprecated AudioCODEC_6

Dependencies:   FatFileSystemCpp I2SSlave TLV320 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD.cpp Source File

LCD.cpp

00001 /* mbed LCD Library, for a 4-bit LCD based on HD44780
00002  * Copyright (c) 2007-2012, hb9gaa
00003  */
00004 
00005 #include "LCD.h"
00006 #include "mbed.h"
00007 
00008 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) 
00009     {
00010     _e  = 1;
00011     _rw = 0;
00012     _rs = 0;                // command mode
00013 
00014      wait(0.015);           // Wait 15ms to ensure powered up
00015 
00016     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00017     for (int i=0; i<3; i++) 
00018         {
00019         writeByte(0x3);
00020         wait_ms(1.64);      // this command takes 1.64ms, so wait for it
00021         }
00022     writeByte(0x2);         // 4-bit mode
00023     wait_ms(0.1);           // most instructions take 100us
00024 
00025     writeCommand(0x28);     // Function set 001 BW N F - -
00026     writeCommand(0x0C);
00027     writeCommand(0x6);      // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00028     cls();
00029     }
00030 
00031 
00032 void TextLCD::character(int column, int row, int c) 
00033     {
00034     int a = address(column, row);
00035     writeCommand(a);
00036     writeData(c);
00037     }
00038 
00039 
00040 void TextLCD::cls() 
00041     {
00042     writeCommand(0x01);     // cls, and set cursor to 0
00043     wait_ms(4.1);           // This command takes 4.1 ms
00044     locate(0, 0);
00045     }
00046 
00047 
00048 void TextLCD::locate(int column, int row) 
00049     {
00050     _column = column;
00051     _row = row;
00052     }
00053 
00054 
00055 int TextLCD::_putc(int value) 
00056     {
00057     if (value == '\n') 
00058         {
00059         _column = 0;
00060         _row++;
00061         if (_row >= rows()) 
00062             {
00063             _row = 0;
00064             }
00065         } 
00066     else 
00067         {
00068         character(_column, _row, value);
00069         _column++;
00070         if (_column >= columns()) 
00071             {
00072             _column = 0;
00073             _row++;
00074             if (_row >= rows()) 
00075                 {
00076                 _row = 0;
00077                 }
00078             }
00079         }
00080     return value;
00081     }
00082 
00083 
00084 int TextLCD::_getc() 
00085     {
00086     return -1;
00087     }
00088 
00089 
00090 void TextLCD::writeByte(int value) 
00091     {
00092     _d = value >> 4;
00093     wait(0.0001);       // most instructions take 100us
00094     _e = 0;
00095     wait(0.0001);
00096     _e = 1;
00097     _d = value >> 0;
00098     wait(0.0001);
00099     _e = 0;
00100     wait(0.0001);
00101     _e = 1;
00102     }
00103 
00104 
00105 void TextLCD::writeCommand(int command) 
00106     {
00107     _rs = 0;
00108     writeByte(command);
00109     }
00110 
00111 
00112 void TextLCD::writeData(int data) 
00113     {
00114     _rs = 1;
00115     writeByte(data);
00116     }
00117 
00118 
00119 int TextLCD::address(int column, int row) 
00120     {
00121     switch (_type) 
00122         {
00123         case LCD20x4:
00124             switch (row) 
00125                 {
00126                 case 0:
00127                     return 0x80 + column;
00128                 case 1:
00129                     return 0xc0 + column;
00130                 case 2:
00131                     return 0x94 + column;
00132                 case 3:
00133                     return 0xd4 + column;
00134                 }
00135         case LCD16x2B:
00136             return 0x80 + (row * 40) + column;
00137         case LCD16x2:
00138         case LCD20x2:
00139         default:
00140             return 0x80 + (row * 0x40) + column;
00141         }
00142     }
00143 
00144 
00145 int TextLCD::columns() 
00146     {
00147     switch (_type) 
00148         {
00149         case LCD20x4:
00150         case LCD20x2:
00151             return 20;
00152         case LCD16x2:
00153         case LCD16x2B:
00154         default:
00155             return 16;
00156         }
00157     }
00158 
00159 
00160 int TextLCD::rows() 
00161     {
00162     switch (_type) 
00163         {
00164         case LCD20x4:
00165             return 4;
00166         case LCD16x2:
00167         case LCD16x2B:
00168         case LCD20x2:
00169         default:
00170             return 2;
00171         }
00172     }
00173 
00174 
00175 void TextLCD::buildChar(unsigned char *p)
00176     {
00177      unsigned char i, j;
00178      
00179      for(j=0;j<8;j++)                    //CGRAM Adresse
00180          {
00181          writeCommand(0x40+(j*8));       //Write to CGRAM
00182          for(i=0;i<8;i++)
00183             writeData(p[i+(j*8)]);       //Write the character pattern to CGRAM
00184          }
00185         writeCommand(0x80);              //shift back to DDRAM location 0
00186     }