Baseline for testing
Diff: LCD.cpp
- Revision:
- 2:73721d9e0056
- Parent:
- 1:0f1dffc6103a
diff -r 0f1dffc6103a -r 73721d9e0056 LCD.cpp --- a/LCD.cpp Thu Sep 12 11:27:29 2019 +0000 +++ b/LCD.cpp Tue Sep 17 13:48:43 2019 +0000 @@ -1,8 +1,37 @@ +/************************************************************************** + * @file LCD.cpp + * @brief Base class for wrapping the interface with the New Haven 40x4 + * LCD monitor. + * @version: V1.0 + * @date: 9/17/2019 + + * + * @note + * Copyright (C) 2019 E3 Design. All rights reserved. + * + * @par + * E3 Designers LLC is supplying this software for use with Cortex-M3 LPC1768 + * processor based microcontroller for the ESCM 2000 Monitor and Display. + * * + * @par + * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED + * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. + * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR + * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. + * + ******************************************************************************/ #include "mbed.h" #include "LCD.h" #include <stdio.h> #include <ctype.h> + +#define WAIT_TIME 100 + +#define wait_ms(x) ThisThread::sleep_for(x) + +/*****************************************************************************/ LCD::LCD(): LCD_E1(p17), LCD_E2(p18), @@ -27,15 +56,15 @@ isOutput=1; } -/* - * Destructor the LCD. - */ + +/*****************************************************************************/ LCD::~LCD() { } +/*****************************************************************************/ void LCD::command1(char i) //Top half of the display { LCD_DATA.output(); @@ -49,6 +78,7 @@ //printf("C1:%x\n\r", i); } +/*****************************************************************************/ void LCD::command2(char i) //Bottom half of the display { LCD_DATA.output(); @@ -62,6 +92,7 @@ //printf("C2:%x\n\r", i); } +/*****************************************************************************/ void LCD::writedata1(char i) //Top half of the display {; LCD_DATA.output(); @@ -71,9 +102,9 @@ 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(); @@ -83,16 +114,19 @@ 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; @@ -121,6 +155,7 @@ } +/*****************************************************************************/ void LCD::writeByte1(int value) { // ------------------------------------------------- @@ -129,7 +164,7 @@ LCD_RW = 0; LCD_E1 = 1; - wait_ms(5); // wait 40 us for most commands + wait_ms(2); // wait 40 us for most commands __nop(); LCD_E1 = 0; @@ -139,14 +174,14 @@ // ------------------------------------------------- LCD_DATA = value >> 0; LCD_E1 = 1; - wait_ms(5); // wait 40 us for most commands + wait_ms(2); // wait 40 us for most commands __nop(); LCD_E1 = 0; //printf("x1:%x\n\r", value); } - +/*****************************************************************************/ void LCD::writeByte2(int value) { // ------------------------------------------------- @@ -154,7 +189,7 @@ LCD_DATA = value >> 4; LCD_RW = 0; LCD_E2 = 1; - wait_ms(5); // wait 40 us for most commands + wait_ms(2); // wait 40 us for most commands __nop(); LCD_E2 = 0; @@ -164,15 +199,13 @@ // ------------------------------------------------- LCD_DATA = value >> 0; LCD_E2 = 1; - wait_ms(5); // wait 40 us for most commands + wait_ms(2); // wait 40 us for most commands __nop(); LCD_E2 = 0; //printf("x2:%x\n\r", value); } - - /************************************************************************/ void LCD::init(void) { @@ -214,36 +247,50 @@ } - +/*****************************************************************************/ void LCD::locate(int row, int column) { - _row = row; _column = column; +} - -} +/*****************************************************************************/ void LCD::cls(){ + clear(1,1); + wait_ms(2); + + //fprintf(stdout,"C1:\n\r"); } +/*****************************************************************************/ +void LCD::clear() +{ + locate(0,0);write(" "); + locate(1,0);write(" "); + locate(2,0);write(" "); + locate(3,0);write(" "); +} +/*****************************************************************************/ void LCD::clear(int e1, int e2 ){ + waitBusy(); writeCommand(LCD_clearDisplay,e1,e2); _row = 0; _column = 0; } - - +/*****************************************************************************/ int LCD::isBusy1(void) { - int input = 0; - int hi = 0; - int lo = 0; + volatile int input = 0; + volatile int hi = 0; + volatile int lo = 0; int result = 0; LCD_RW = 1; + __nop(); LCD_RS = 0; + LCD_DATA.input(); // switch port back to output LCD_E1 = 1; @@ -255,41 +302,47 @@ 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 + if ((0x8 & lo)) // wait until display is ready { result = 1; } result =0; LCD_RW = 0; LCD_DATA.output(); // switch port back to output + + //fprintf(stdout,"isBusy1? = 0x%02x 0x%02x \n\r", lo,hi ); + return result; } +/*****************************************************************************/ int LCD::isBusy2(void) { - int input = 0; + volatile int input = 0; + volatile int hi = 0; + volatile int lo = 0; int result = 0; LCD_RW = 1; + __nop(); LCD_RS = 0; + LCD_DATA.input(); // switch port back to output LCD_E2 = 1; __nop(); - input = LCD_DATA.read() >> 0; // read low bit + lo = LCD_DATA.read() >> 0; // read low bit LCD_E2 = 0; __nop(); LCD_E2 = 1; - input = LCD_DATA.read() >> 4; // read high bit + hi = 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 + if ((0x8 & lo)) // wait until display is ready { result = 1; } @@ -297,64 +350,29 @@ LCD_RW = 0; LCD_DATA.output(); // switch port back to output + //fprintf(stdout,"isBusy2? = 0x%02x 0x%02x \n\r", lo,hi ); 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; + result=isBusy1(); + } - //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; - } + result=isBusy2(); } - LCD_RW = 0; - LCD_DATA.output(); // switch port back to output return result; } +/*****************************************************************************/ void LCD::waitBusy() { unsigned char statusBit; @@ -362,11 +380,13 @@ //int i=0; while (statusBit) { + fprintf(stdout,"!"); statusBit = isBusy(1,1); - wait_us(5); + wait_us(1); } } +/*****************************************************************************/ void LCD::writeData(char value, int e1, int e2 ){ @@ -379,14 +399,14 @@ LCD_DATA = hi; LCD_E1 = 1; - wait_us(1000); // wait 40 us for most commands + wait_us(WAIT_TIME); // wait 40 us for most commands LCD_E1 = 0; - wait_us(1000); // wait 40 us for most commands + wait_us(WAIT_TIME); // wait 40 us for most commands LCD_DATA = lo; LCD_E1 = 1; - wait_us(1000); // wait 40 us for most commands + wait_us(WAIT_TIME); // wait 40 us for most commands LCD_E1 = 0; } @@ -394,35 +414,41 @@ { LCD_DATA = hi; LCD_E2= 1; - wait_us(1000); // wait 40 us for most commands + wait_us(WAIT_TIME); // wait 40 us for most commands LCD_E2= 0; - wait_us(1000); // wait 40 us for most commands + wait_us(WAIT_TIME); // wait 40 us for most commands LCD_DATA = lo; LCD_E2= 1; - wait_us(1000); // wait 40 us for most commands + wait_us(WAIT_TIME); // wait 40 us for most commands LCD_E2= 0; } } +/*****************************************************************************/ void LCD::writeCommand(char value, int e1, int e2){ + waitBusy(); LCD_RS = 0; writeData(value,e1,e2); } +/*****************************************************************************/ void LCD::writeChar(char value, int e1, int e2){ + //waitBusy(); 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]; @@ -433,13 +459,16 @@ va_end(argptr); } +/*****************************************************************************/ void LCD::write(const char* text) { //_row = 0; //_column = 0; int characterAmount =0; for(int i = 0; text[i] != '\0'; i++) { - if (!isprint(text[i])) { fprintf(stdout,"^%02x ", text[i]);} + if (!isprint(text[i])) { + fprintf(stdout,"^%02x : %s ", text[i],text); + } characterAmount++; } @@ -456,6 +485,7 @@ } } +/*****************************************************************************/ void LCD::writeLine(int line, const char* text ) { _row = line; @@ -477,11 +507,13 @@ } } +/*****************************************************************************/ 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); @@ -495,6 +527,7 @@ } } +/*****************************************************************************/ int LCD::address(int row, int column){ int a=0x80; @@ -518,6 +551,7 @@ return a; } +/*****************************************************************************/ void LCD::character(int row, int column, char c) { uint8_t a = address(row, column); @@ -535,7 +569,7 @@ if(_column < columns()) _column++; if (_column >= columns()){ - #if 0 + #if 1 if (_row == 0){ _column = 0; _row++; @@ -548,6 +582,7 @@ } } +/*****************************************************************************/ void LCD::setCursorMode( int mode ) { if (mode)