Liam Grazier / LGLCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lglcd.cpp Source File

lglcd.cpp

00001 #include "mbed.h"
00002 #include "lglcd.h"
00003 
00004 lglcd::lglcd(PinName rs, PinName e, PinName d4, PinName d5,PinName d6, PinName d7) : _lcdrs(rs),_lcde(e), _lcdinfo(d4, d5, d6, d7)
00005 {
00006     cline=1; //set the current line to the top line
00007     cpos=0; //sets the current position to the left most CHARACTER
00008     _lcde=1;              //clear enable
00009     _lcdrs=0;             // command
00010     l = 0;
00011     writedata(_lcde,CMD);
00012     wait(0.000004);              //delay for LCD to initialise.
00013     writedata(0x28,CMD);         //set to 4 bit interface, 2 line and 5*8 font
00014     wait(0.000004);
00015     writedata(0x0C,CMD);         //cursor on, cursor position blink
00016     wait(0.000004);
00017     writedata(0x10,CMD);
00018     wait(0.000004);
00019     LCD_CLR;                        //clear display
00020     wait(0.000004);
00021     writedata(0x06,CMD);         //move cursor right after write
00022     wait(0.000004);
00023     LCD_HOME;                                   //return home
00024 
00025 }
00026 /*---------------------------------------------------------------------*/
00027 void lglcd::clear(void)//set a function to clear the lcd since just calling LCD_CLR can throw error if called to quickly
00028 {
00029     LCD_CLR;
00030     wait(0.002); //2ms delay to stop timing error
00031 }
00032 
00033 /*---------------------------------------------------------------------*/
00034 void lglcd::writedata(unsigned char info, unsigned char type)
00035 {
00036     if(type == CMD) {
00037         _lcdrs=0;              //COMMAND MODE
00038     } else {
00039         _lcdrs=1;          //CHARACTER/DATA MODE
00040     }
00041     
00042     _lcdinfo = info >> 4;
00043     wait(0.000040f); // most instructions take 40us
00044     _lcde = 0;
00045     wait(0.000040f);
00046     _lcde = 1;
00047     _lcdinfo = info >> 0;
00048     wait(0.000040f);
00049     _lcde = 0;
00050     wait(0.000040f);  // most instructions take 40us
00051     _lcde = 1;
00052 }
00053 
00054 void lglcd::write(char charq[])
00055 { 
00056  for (int i = 0; i < strlen(charq); i++)
00057   {
00058     wait(0.003);
00059     int count = 0;
00060     count++;
00061     writedata(charq[i], TXT);
00062     if (i == 15) {
00063       writedata(LINE2 | 0, CMD);
00064     }
00065     if (i == 32) {
00066       clear();
00067       writedata(LINE1, CMD);
00068    }
00069   }
00070 }