Baseline for testing

Revision:
0:c428f1d60316
Child:
1:0f1dffc6103a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD.cpp	Thu Jul 25 00:43:40 2019 +0000
@@ -0,0 +1,575 @@
+#include "mbed.h"
+#include "LCD.h"
+
+LCD::LCD(): 
+    LCD_E1(p17),
+    LCD_E2(p18),
+    LCD_RS(p19),
+    LCD_RW(p20),
+    //LCD_D4(p24),
+    //LCD_D5(p23),
+    //LCD_D6(p22),
+    //LCD_D7(p21),
+    //bus constructor is the reverse order of the pins in the byte order. 
+    // d0 - d7
+    LCD_DATA( p24, p23, p22, p21)
+{
+    
+    
+    LCD_DATA.output();
+    LCD_DATA.mode(OpenDrain);
+    LCD_E1.mode(OpenDrain);
+    LCD_E2.mode(OpenDrain);
+    LCD_RS.mode(OpenDrain);
+    LCD_RW.mode(OpenDrain);
+    isOutput=1;
+    
+}
+/*
+ * Destructor the LCD.
+ */
+LCD::~LCD()
+{
+}
+
+
+
+void LCD::command1(char i) //Top half of the display 
+{
+   LCD_DATA.output();
+   LCD_DATA = i;
+   LCD_RW = 0;
+   LCD_RS = 0;
+   LCD_E1 = 1;
+   wait_us(40); // wait 40 us for most commands
+   LCD_E1 = 0;
+   
+   //printf("C1:%x\n\r", i);
+}
+
+void LCD::command2(char i) //Bottom half of the display 
+{
+   LCD_DATA.output();
+   LCD_DATA = i;
+   LCD_RW = 0;
+   LCD_RS = 0;
+   LCD_E2 = 1;
+   wait_us(40); // wait 40 us for most commands
+   LCD_E2 = 0;
+   
+   //printf("C2:%x\n\r", i);
+}
+
+void LCD::writedata1(char i) //Top half of the display 
+{;
+   LCD_DATA.output();
+   LCD_DATA = i;
+   LCD_RW = 0;
+   LCD_RS = 1;
+   LCD_E1 = 1;
+   wait_us(40); // wait 40 us for most commands
+   LCD_E1 = 0;
+   //printf("D1:%x\n\r", i);
+}
+
+void LCD::writedata2(char i) //Bottom half of the display 
+{
+   LCD_DATA.output();
+   LCD_DATA = i;
+   LCD_RW = 0;
+   LCD_RS = 1;
+   LCD_E2 = 1;
+   wait_us(40); // wait 40 us for most commands
+   LCD_E2 = 0;
+   //printf("D2:%x\n\r", i);
+}
+
+void LCD::nextline1(){
+    writeCommand(0xc0,1,0);         //set DDRAM address to 40 (line 2)
+}
+void LCD::nextline2(){
+    writeCommand(0xc0,0,1);         //set DDRAM address to 40 (line 4)
+}
+
+void LCD::display(char *show){  //show character data on display
+    char datum;
+    int i;
+    for (i=0;i<40;i++){
+        datum=*show;            //point to 1st line data
+        writeData(datum,1,0);          //write to 1st line
+        ++show;                 //next letter
+    }
+    nextline1();                //move address to line 2
+    for (i=0;i<40;i++){
+        datum=*show;
+        writeData(datum,1,0);          //write to 2nd line
+        ++show;
+    }    
+    for (i=0;i<40;i++){
+        datum=*show;
+        writeData(datum,0,1);          //write to 3rd line
+        ++show;
+    }
+    nextline2();                //move address to line 4
+    for (i=0;i<40;i++){
+        datum=*show;
+        writeData(datum,0,1);          //write to 4th line
+        ++show;
+    }
+}
+
+
+void LCD::writeByte1(int value) {
+    
+    // -------------------------------------------------
+    LCD_DATA.output();
+    LCD_DATA = value >> 4;
+    
+    LCD_RW = 0;
+    LCD_E1 = 1;
+    wait_ms(5); // wait 40 us for most commands
+    __nop();
+    LCD_E1 = 0;
+    
+    // -------------------------------------------------
+    wait_ms(1); // wait 40 us for most commands
+    
+    // -------------------------------------------------
+    LCD_DATA = value >> 0;
+    LCD_E1 = 1;
+    wait_ms(5); // wait 40 us for most commands
+    __nop();
+    LCD_E1 = 0;
+    
+    //printf("x1:%x\n\r", value);
+}
+
+
+void LCD::writeByte2(int value) {
+    
+    // -------------------------------------------------
+    LCD_DATA.output();
+    LCD_DATA = value >> 4;
+    LCD_RW = 0;
+    LCD_E2 = 1;
+    wait_ms(5); // wait 40 us for most commands
+    __nop();
+    LCD_E2 = 0;
+    
+    // -------------------------------------------------
+    wait_ms(1); // wait 40 us for most commands
+    
+    // -------------------------------------------------
+    LCD_DATA = value >> 0;
+    LCD_E2 = 1;
+    wait_ms(5); // wait 40 us for most commands
+    __nop();
+    LCD_E2 = 0;
+    
+   //printf("x2:%x\n\r", value);
+}
+
+
+
+/************************************************************************/
+void LCD::init(void)
+{
+     
+   wait_ms(100);
+   
+   LCD_E1 = 0;
+   LCD_E2 = 0;
+   
+   for (int i=0;i<3;i++) {
+       command1(0x3);//Wake up 
+       command2(0x3);
+       wait_ms(1); // wait 40 us for most commands
+    }
+    command1(0x2);// Set 4 bit mode
+    command2(0x2);
+    wait_ms(1); // wait 40 us for most commands
+
+  
+    LCD_RW = 0;
+    LCD_RS = 0;
+    
+    writeCommand(0x28,1,1); //Turn on display 
+    
+    writeCommand(LCD_setDisplayOff,1,1); //Turn on display 
+    //writeCommand2(LCD_setDisplayOff);
+    
+    writeCommand(LCD_clearDisplay,1,1); //Clear display 
+    //writeCommand2(LCD_clearDisplay);
+
+    writeCommand(LCD_entryModeSet,1,1); //Entry mode cursor increment 
+    //writeCommand2(LCD_entryModeSet);
+    
+    writeCommand(LCD_setDisplayOn,1,1); //Turn on display; no cursor 
+    //writeCommand2(LCD_setDisplayOn);
+    
+    
+   LCD_E1 = 0;
+   LCD_E2 = 0;
+      
+}
+
+
+void LCD::locate(int row, int column) {
+     _row = row;
+    _column = column;
+      
+}
+    
+void LCD::clear(int e1, int e2 ){
+        
+    writeCommand(LCD_clearDisplay,e1,e2);
+    _row = 0;
+    _column = 0;
+    
+    // for debugging, write spaces to each cell
+    memset( displayBuffer, 0x85, sizeof(displayBuffer));
+}
+
+
+
+int LCD::isBusy1(void)
+{
+    int input  = 0;
+    int hi  = 0;
+    int lo  = 0;
+    int result = 0;
+    
+    LCD_RW = 1;
+    LCD_RS = 0;
+    LCD_DATA.input();      // switch port back to output 
+    
+    LCD_E1 = 1;
+    __nop();
+    lo = LCD_DATA.read() ;  // read low bit      
+    LCD_E1 = 0;
+    wait_ms(1);
+    LCD_E1 = 1;
+    hi = LCD_DATA.read() ;    // read high bit     
+    __nop();
+    LCD_E1 = 0;
+        
+    //printf("isBusy1? = 0x%02x 0x%02x \n\r", lo,hi );     
+        
+    if ((0x8 & input));  // wait until display is ready
+    {
+        result = 1;
+    }
+    result =0;
+    LCD_RW = 0;
+    LCD_DATA.output();      // switch port back to output 
+    return result;
+}
+
+int LCD::isBusy2(void) 
+{
+    
+    int input  = 0;
+    int result = 0;
+    
+    LCD_RW = 1;
+    LCD_RS = 0;
+
+    LCD_E2 = 1;
+    __nop();
+    input = LCD_DATA.read()  >> 0;  // read low bit      
+    LCD_E2 = 0;
+    __nop();
+    LCD_E2 = 1;
+    input = LCD_DATA.read() >> 4;    // read high bit     
+    __nop();
+    LCD_E2 = 0;
+        
+    //printf("isBusy1? = 0x%02x \n\r", input );     
+    
+    if ((0x8 & input));  // wait until display is ready
+    {
+        result = 1;
+    }
+    
+    LCD_RW = 0;
+    LCD_DATA.output();      // switch port back to output 
+    
+    return result;
+}
+
+
+
+int LCD::isBusy(int e1, int e2)
+{
+    int input  = 0;
+    int result = 0;
+    
+    LCD_RW = 1;
+    LCD_RS = 0;
+    LCD_DATA.input();      // switch port back to output 
+    
+    if (e1) {
+        LCD_E1 = 1;
+        __nop();
+        input = LCD_DATA.read()  >> 0;  // read low bit      
+        LCD_E1 = 0;
+        __nop();
+        LCD_E1 = 1;
+        input = LCD_DATA.read() >> 4;    // read high bit     
+        __nop();
+        LCD_E1 = 0;
+        
+        //printf("0x%02x \n\r", input );     
+        
+        if ((0x8 & input));  // wait until display is ready
+        {
+            result = 1;
+        }
+    }
+    
+    
+    if (e2 && result==0) {
+        
+        LCD_E2 = 1;
+        __nop();
+        input = LCD_DATA.read()  >> 0;  // read low bit      
+        //printf("0x%02dFF \n\r", input );     
+        LCD_E2 = 0;
+        __nop();
+        LCD_E2 = 1;
+        input = LCD_DATA.read() >> 4;    // read high bit     
+        __nop();
+        LCD_E2 = 0;
+        
+        if ((0x8 & input));  // wait until display is ready
+        {
+            result = 1;
+        }
+    }    
+    LCD_RW = 0;
+    LCD_DATA.output();      // switch port back to output 
+    
+    return result;
+}
+        
+void LCD::waitBusy()
+{     
+    unsigned char statusBit;
+    statusBit = isBusy(1,1);     
+    //int i=0;
+    while (statusBit)
+    {
+        statusBit = isBusy(1,1);     
+        wait_us(5);
+    } 
+}
+
+void LCD::writeData(char value, int e1, int e2 ){
+    
+    
+    int hi=( value & 0xF0 ) >> 4;
+    int lo=( value & 0x0F ) >> 0;
+    
+    LCD_DATA.output();
+        
+    if(e1) {
+                
+        LCD_DATA  = hi;
+        LCD_E1 = 1;
+        wait_us(1000); // wait 40 us for most commands
+        LCD_E1 = 0;
+        
+        wait_us(1000); // wait 40 us for most commands
+        
+        LCD_DATA   = lo;
+        LCD_E1 = 1;
+        wait_us(1000); // wait 40 us for most commands
+        LCD_E1 = 0;
+    }
+        
+    if(e2)
+    {
+        LCD_DATA  = hi;
+        LCD_E2= 1;
+        wait_us(1000); // wait 40 us for most commands
+        LCD_E2= 0;
+        
+        wait_us(1000); // wait 40 us for most commands
+        
+        LCD_DATA   = lo;
+        LCD_E2= 1;
+        wait_us(1000); // wait 40 us for most commands
+        LCD_E2= 0;
+    }
+}
+
+void LCD::writeCommand(char value, int e1, int e2){
+    
+    LCD_RS = 0;
+    writeData(value,e1,e2);
+}
+
+void LCD::writeChar(char value, int e1, int e2){
+    LCD_RS = 1;
+    writeData(value,e1,e2);
+}
+
+
+void LCD::putc(const char c)
+{
+    character(_row, _column, c);
+}
+
+void LCD::printf(const char *message, ...)
+{
+    char buffer[128];
+    va_list argptr;
+    va_start(argptr, message);
+
+    vsprintf( buffer, message, argptr);  
+    write (buffer); 
+
+    va_end(argptr);    
+}
+
+void LCD::write(const char* text) {
+    //_row = 0;
+    //_column = 0;
+    int characterAmount =0;
+    
+    for(int i = 0; text[i] != '\0'; i++)
+        characterAmount++;
+        
+    for (int i = 0; i < characterAmount && i < rows() * columns(); i++){
+        character(_row, _column, text[i]);
+    }
+}
+
+void LCD::writeLine(int line, const char* text ) {
+    
+    _row = line;
+    _column = 1;
+    
+    int characterAmount =strlen(text);
+    
+    int i=0;
+    locate(_row,_column);
+    
+    while(i < columns()-1)
+    {
+        if ( i < characterAmount )
+            putc( text[i]);
+        else
+            putc ( ' ');
+            
+        i++;
+    }
+}
+
+void LCD::writeCharacter(const char c, int row, int column) {
+    locate(row, column);
+    character(_row, _column, c);
+}
+
+void LCD::writeCharacters(const char* text, int row, int column) {
+    locate(row, column);
+    
+    int characterAmount =0;
+    
+    for(int i = 0; text[i] != '\0'; i++)
+        characterAmount++;
+        
+    for (int i = 0; i < characterAmount  && i < columns() - column; i++){
+        character(_row, _column, text[i]);
+    }
+}
+
+int LCD::address(int row, int column){
+    
+    int a=0x80;
+    
+    if(row < rows() && column < columns())
+    {
+        switch (row){
+            case 0:
+            case 1:
+                a = 0x80 + (row * 0x40) + column;
+                break;
+            case 2:
+            case 3:
+                a = 0x80 + ((row-2) * 0x40) + column;
+                break;
+            default:
+                break;
+        }
+    }
+    
+    return a;
+}
+
+void LCD::character(int row, int column,  char c) {
+    uint8_t a = address(row, column);
+            
+    // ------------------------------------
+    // write to internal buffer
+    // ------------------------------------
+    displayBuffer [row][column] = c;
+    
+    if(row<2){
+        writeCommand(a,1,0);
+        writeChar(c,1,0);
+    }
+    else
+    {
+        writeCommand(a,0,1);
+        writeChar(c,0,1);
+    }
+    
+    //Update position
+    if(_column < columns())
+        _column++;
+    if (_column >= columns()){
+        #if 0
+        if (_row == 0){
+            _column = 0;
+            _row++;
+        }
+        else{
+            _row = 0;
+            _column = 0;
+        }
+        #endif
+    }
+}
+
+void LCD::setCursorMode( int mode )
+{
+    if (mode) 
+    {
+        if(_row < 2)
+            writeCommand(LCD_setDisplayBlink,1,0); 
+        else
+            writeCommand(LCD_setDisplayBlink,0,1); 
+        
+    } 
+    else
+    {
+        writeCommand(LCD_setDisplayOn,1,1);  
+    }
+}
+
+void LCD::dump(Serial * pc) {
+    
+    fprintf(stdout,"\n\r");
+    fprintf(stdout,"----------------------------------------------\n\r");
+    for (int i=0;i<rows();i++)
+    {
+        fprintf(stdout,"[%d]",i);
+        for (int j=0;j<columns();j++)
+        {
+            fprintf(stdout,"%c",displayBuffer[i][j]);
+        }
+        fprintf(stdout,"\n\r");
+    }
+    fprintf(stdout,"\n\r");
+}
\ No newline at end of file