12864 GLCD

Dependencies:   mbed GLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers myLCD.cpp Source File

myLCD.cpp

00001 #include "myLCD.h"
00002 /**
00003 myLCD::myLCD(PinName _DI, PinName _RW, PinName _EN, PinName _CS1, PinName _CS2, BusInOut *BUSLCD){
00004     DI = new DigitalOut(_DI);
00005     RW = new DigitalOut(_RW);
00006     EN = new DigitalOut(_EN);
00007     CS1 = new DigitalOut(_CS1);
00008     CS2 = new DigitalOut(_CS2);
00009     LCD_PORT = BUS_LCD;
00010     }*/
00011 myLCD::myLCD(PinName _DI, PinName _RW, PinName _EN, PinName _CS1, PinName _CS2, BusInOut *BUSLCD):DI(_DI),RW(_RW),EN(_EN),CS1(_CS1),CS2(_CS2){
00012     LCD_PORT = BUSLCD;
00013 }
00014     
00015 void myLCD::pulse_en(){
00016     EN = 1;
00017     wait_us(8);
00018     EN = 0;
00019     wait_us(8);
00020 }
00021     
00022 void myLCD::lcd_on(){
00023     DI = 0;
00024     RW = 0;
00025     EN = 0;
00026     CS1 = 0;
00027     CS2 = 0;
00028     LCD_PORT->output();
00029     wait_us(2);
00030     LCD_PORT->write(0x3F);
00031     wait_us(2);
00032     pulse_en();
00033 }
00034     
00035 void myLCD::write_byte(unsigned short byte){
00036     DI = 1; //high -> data
00037     RW = 0; //low -> write
00038     LCD_PORT->write(byte);
00039     wait_us(2);
00040     pulse_en();
00041 }
00042 
00043 //returns the value into @param &byte
00044 short myLCD::read_byte(unsigned short col, unsigned short page){
00045     unsigned short temp;
00046     set_xy(col, page); // this function reads data from current location
00047     LCD_PORT->input(); //set as input
00048     DI = 1; //high -> data
00049     RW = 1; //high -> read
00050     CS1 = (col>63);
00051     CS2 = !CS1;
00052     wait_us(1);
00053     EN = 1;     //latches data into output register
00054     wait_us(1);
00055     
00056     EN = 0;     //dummy read
00057     wait_us(5); //waits while lcd fetches data
00058     EN = 1;     //latch data from output register into data register
00059     wait_us(1);
00060     
00061     temp = LCD_PORT->read(); //data is saved onto byte from the caller
00062     EN = 0; //remove data from the bus
00063     wait_us(1);
00064     LCD_PORT->output(); //set bus as output again
00065     wait_us(2);
00066     return temp;
00067 }
00068 
00069 //@param col should be 0<=col<128
00070 void myLCD::set_col(unsigned short col){
00071     unsigned short col_data;
00072     DI = 0; //low -> instruction
00073     RW = 0; //low -> write
00074     
00075     //0-63
00076     if(col < 64){
00077         CS1 = 0; //select chip1
00078         CS2 = 1; //deselect chip2
00079         col_data = col;
00080     }
00081     else //64-127
00082     {
00083         CS1 = 1; //deselect chip1
00084         CS2 = 0; //select chip2
00085         col_data = col - 64;
00086     }
00087     //command formatting 01CCCCC -> C: column
00088     col_data = (col_data | 0x40) & 0x7F;
00089     LCD_PORT->write(col_data);
00090     wait_us(2);
00091     pulse_en();
00092 }
00093 
00094 void myLCD::set_row(unsigned short row){
00095     unsigned short row_data;
00096     DI = 0; //low -> instruction
00097     RW = 0; //low -> write
00098     //command formatting 10111PPP -> P: page
00099     row_data = (row | 0xB8) & 0xBF; //or with 10111000, and with 10111111. last 3 bits is the page
00100     LCD_PORT->write(row_data);
00101     wait_us(2);
00102     pulse_en();
00103 }
00104 
00105 void myLCD::set_xy(unsigned short col, unsigned short page){
00106     set_col(col);
00107     set_row(page);
00108 }
00109 
00110 void myLCD::clear(){
00111     for(short n = 0; n < 8; n++){
00112         set_xy(0,n);
00113         set_xy(64,n); //at this point, cs2 is selected
00114         CS1 = 1; //select cs1 as well
00115         for(short i = 0; i < 64; i++)
00116             write_byte(0);
00117     }
00118 }
00119 
00120 //@param color = true, draws white. clears otherwise
00121 void myLCD::draw_point(unsigned short x, unsigned short y, bool color){
00122     short data;
00123     short byte = read_byte(x, (y/8)); //byte has the point's 8bit data
00124     
00125     if(color){ //paint the point
00126         data = ~(1 << (y % 8)) & byte;
00127     }
00128     else{ //clear the point
00129         data = (1 << (y % 8)) | byte;
00130     }
00131     set_xy(x, y/8);
00132     write_byte(data);
00133 }