liamgrazier lcd library 16x02

Fork of LGLCD by Liam Grazier

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 void lglcd::setline(int row,int column)
00033 {   
00034     if(row == 1) { // switch statement for placing the cursor
00035                     writedata(LINE1|column,CMD); //set lcd to line one and correct position
00036             wait(0.005); //2ms delay to stop empty screen timing error
00037     }
00038     if(row == 2){
00039             writedata(LINE2|column,CMD); //set lcd to line two and correct position
00040             wait(0.005); //2ms delay to stop empty screen timing error
00041       }
00042     }
00043 
00044 /*---------------------------------------------------------------------*/
00045 void lglcd::writedata(unsigned char info, unsigned char type)
00046 {
00047     if(type == CMD) 
00048     {
00049         _lcdrs=0;              //COMMAND MODE
00050     }
00051      else
00052       {
00053         _lcdrs=1;          //CHARACTER/DATA MODE
00054     }
00055     
00056     _lcdinfo = info >> 4;
00057     wait(0.000040f); // most instructions take 40us
00058     _lcde = 0;
00059     wait(0.000040f);
00060     _lcde = 1;
00061     _lcdinfo = info >> 0;
00062     wait(0.000040f);
00063     _lcde = 0;
00064     wait(0.000040f);  // most instructions take 40us
00065     _lcde = 1;
00066 }
00067 
00068 void lglcd::write(char charq[])
00069 { 
00070  for (int i = 0; i < strlen(charq); i++)
00071   {
00072     wait(0.003);
00073     int count = 0;
00074     count++;
00075     writedata(charq[i], TXT);
00076     if (i == 15) {
00077       writedata(LINE2 | 0, CMD);
00078     }
00079     if (i == 32) {
00080       clear();
00081       writedata(LINE1, CMD);
00082    }
00083   }
00084 }