Ben Parkes / Mbed OS Thread_Communication_V5

Dependencies:   BMP280

Fork of Thread_Communication_V4_fortest by BDG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD.cpp Source File

LCD.cpp

00001 #include "mbed.h"
00002 
00003 #include "LCD.h"
00004 #define busy_mask 0x8000
00005 
00006 
00007 LCD::LCD(PinName RS, PinName E, PinName d4, PinName d5,
00008                  PinName d6, PinName d7) : _RS(RS),
00009         _E(E), _DBUS(d4, d5, d6, d7) {
00010 
00011     _E  = 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 
00018     wait(0.015);        // Wait 15ms to ensure powered up
00019 
00020     for (int i=0; i<3; i++) {
00021         DATA(0x30, CMD);
00022         wait(0.00164);  // this command takes 1.64ms, so wait for it
00023     }
00024     DATA(0x02, CMD);     // 4-bit mode
00025     wait(0.000040f);    // most instructions take 40us
00026 
00027     DATA(0x28,CMD); // Function set 001 BW N F - -
00028     DATA(0x0C,CMD);
00029     DATA(0x06,CMD);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00030     Clear();
00031     DATA(0x01,CMD); // cls, and set cursor to 0
00032     wait(0.00164f);     // This command takes 1.64 ms
00033     Write("Constructed");
00034 }
00035 
00036 
00037 
00038 
00039 /*int main(){
00040      //(PF_13, PE_9, PF_14, PF_15)
00041      //(PF_15, PF_14, PE_9,PF_13)
00042      LCD_DBUS = new BusInOut(PF_13, PE_9, PF_14, PF_15) ;
00043      LCD_Init();
00044      while(1){
00045      write_string("Hi");
00046      wait(0.1);
00047      }
00048     }
00049 */
00050 // Initialise LCD Pins //
00051 /*void LCD_Init(void){                                        
00052         LCD_E = 0;;          //clear enable
00053         LCD_RW = 0;                 // write
00054         LCD_RS = 0;         // command
00055 
00056         wait_ms(3);           //delay for LCD to initialise.
00057 
00058         LCD_DATA(0x28,CMD);     //set to 4 bit interface, 2 line and 5*8 font
00059         LCD_DATA(0x0f,CMD);     //cursor on, cursor position blink
00060         LCD_DATA(0x10,CMD);
00061         LCD_CLR;                //clear display
00062         LCD_DATA(0x06,CMD);     //move cursor right after write
00063         LCD_HOME;                   //return home
00064 
00065     LCD_E  = 1;
00066     LCD_RS = 0;            // command mode
00067     LCD_RW = 0;
00068     wait(0.015);        // Wait 15ms to ensure powered up
00069 
00070     for (int i=0; i<3; i++) {
00071         LCD_DATA(0x03, TXT);
00072         wait(0.00164);  // this command takes 1.64ms, so wait for it
00073     }
00074     LCD_DATA(0x02, TXT);     // 4-bit mode
00075     wait(0.000040f);    // most instructions take 40us
00076 
00077     LCD_DATA(0x28,CMD); // Function set 001 BW N F - -
00078     LCD_DATA(0x0C,CMD);
00079     LCD_DATA(0x06,CMD);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00080     LCD_DATA(0x01,CMD); // cls, and set cursor to 0
00081     wait(0.00164f);     // This command takes 1.64 ms
00082 }*/
00083 /*---------------------------------------------------------------------*/
00084 void LCD::Clear(void){
00085     DATA(CLEAR,CMD);
00086     }
00087     
00088 void LCD::RowSelect(int row){
00089     switch(row){
00090         case 0:
00091             DATA(LINE1,CMD); 
00092             break;
00093         case 1:
00094             DATA(LINE2,CMD); 
00095             break;
00096         case 2:
00097             DATA(LINE3,CMD);
00098             break;
00099         case 3:
00100             DATA(LINE4,CMD);
00101             break;
00102         default:
00103             DATA(LINE1,CMD);
00104             break;
00105         }
00106     }
00107     
00108 void LCD::Busy(void)
00109 {
00110         wait_ms(1);
00111 }
00112 /*---------------------------------------------------------------------*/
00113 void LCD::DATA(char data,char type){
00114 
00115         Busy();                  //TEST LCD FOR BUSY 
00116         
00117         _DBUS = (data>>4);
00118 
00119         wait(0.00040f); // most instructions take 40us
00120     
00121         if(type == CMD)
00122         {
00123             _RS = 0;            //COMMAND MODE
00124         } 
00125         else 
00126         {
00127             _RS = 1;            //CHARACTER/DATA MODE
00128         }
00129         wait(0.00040f);
00130 
00131         _E = 1;                 //ENABLE LCD DATA LINE
00132         wait(0.00040f);         // most instructions take 40us
00133         _E = 0;                 //DISABLE LCD DATA LINE
00134             
00135         _DBUS = 0;
00136       
00137         _DBUS = (data);
00138         
00139         _E = 1;                 //ENABLE LCD DATA LINE
00140         wait(0.00040f);         // most instructions take 40us
00141         _E = 0;                 //DISABLE LCD DATA LINE
00142 }
00143 /*---------------------------------------------------------------------*/
00144 void LCD::Write(char text[80]){   
00145     int i = 0;
00146       
00147     
00148     while((text[i] != 0))
00149     {
00150         
00151         char character = text[i];
00152         DATA (character,TXT);      // Write text "a" to the LCD
00153         
00154         i++;
00155     }
00156 }