Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280
Revision 23:a6bb5298346c, committed 2018-01-10
- Comitter:
- mwthewsey
- Date:
- Wed Jan 10 02:36:50 2018 +0000
- Parent:
- 22:617bf92b481f
- Child:
- 24:7bf408dc491a
- Commit message:
- Added Custom LCD Driver
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DriverLCD.cpp Wed Jan 10 02:36:50 2018 +0000
@@ -0,0 +1,94 @@
+#include "DriverLCD.h"
+#include "mbed.h"
+
+DriverLCD::DriverLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7) : _rs(rs),_e(e), _d(d4, d5, d6, d7)
+{
+
+ _e = 1; //Enable high, rs low to enter command mode
+ _rs = 0;
+
+ wait(0.015); //Wait 15ms for LCD to powerup
+
+ for (int i=0; i<3; i++) { //Send command 3 times
+ LCD_DATA(0x3,INIT);
+ wait(0.002); //Wait 2ms, so wait for it
+ }
+ LCD_DATA(0x2,INIT); //4-bit mode
+ wait(0.000040f); //Wait 40us
+
+ LCD_DATA(0x28,CMD); //Function set. 2 lines, 5x8 dots, 1/16 dusy factor
+ LCD_DATA(0x0C,CMD); //Turn Display on
+ LCD_DATA(0x6,CMD); //Entry mode setting. Standard increment
+ cls(); //Clear display
+}
+
+void DriverLCD::cls()
+{
+ LCD_DATA(0x01,CMD); //Clear display command.
+ wait(0.002); //Wait 2ms
+ locate(0, 0); //Resets cursor to 0,0
+}
+
+void DriverLCD::locate(int column, int row)
+{
+ _column = column; //Set new column and row data to internal variables
+ _row = row;
+}
+
+void DriverLCD::LCD_DATA(int data,int command)
+{
+ if(command == 0) { //CMD = 0, enter command mode
+ _rs = 0;
+ } else if (command == 1) { //STR = 1
+ _rs = 1;
+ }
+
+ _d = data >> 4; //Send most significant nibble first
+ wait(0.00004); //Wait 40us
+ _e = 0; //Clock enable line to put in data
+ wait(0.00004);
+ _e = 1;
+
+ _d = data >> 0; //Send least significant nibble seccond
+ wait(0.00004);
+ _e = 0;
+ wait(0.00004);
+ _e = 1;
+}
+
+void DriverLCD::character(int column, int row, int data)
+{
+ int addr = 0x80 + (row * 0x40) + column; //Calculate address of segment
+ LCD_DATA(addr,CMD); //Move cursor to new address
+ LCD_DATA(data,STR); //Write data
+}
+
+
+int DriverLCD::_putc(int data)
+{
+ if (data == '\n') { //If new line is found, move down a row
+ _column = 0; //Reset to first column
+ _row++; //inc row
+ if (_row >= 2) {//Only two rows on this LCD, move up a row instead.
+ _row = 0;
+ }
+ } else {
+ character(_column, _row, data); //Write char
+ _column++; //Then increment column and row to next location
+ if (_column >= 16) {
+ _column = 0;
+ _row++;
+ if (_row >= 2) {
+ _row = 0;
+ }
+ }
+ }
+ return data;
+}
+
+int DriverLCD::_getc()
+{
+ return -1;
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DriverLCD.h Wed Jan 10 02:36:50 2018 +0000
@@ -0,0 +1,51 @@
+#ifndef __DriverLCD__
+#define __DriverLCD__
+
+#include "mbed.h"
+
+/*
+* This module handles the low level control of a 16x2 LCD.
+* It contains a class structure, and allows for text to be written to the LCD.
+* Text can be written by using printf, achived by including virtual Stream.
+*/
+
+//Datasheet sparkfun.com/datasheets/LCD/HD44780.pdf
+
+#define CMD 0
+#define STR 1
+#define INIT 2
+
+class DriverLCD : public Stream
+{
+public:
+
+ DriverLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7);
+ //Constructor
+
+ void locate(int column, int row);
+ //Moves cursor to specific position
+
+ void cls();
+ //Clear LCD
+
+protected:
+
+ // Stream implementation functions
+ virtual int _putc(int data);
+ virtual int _getc();
+
+ void character(int column, int row, int data);
+ //Writes a char to the display at set coordinates
+
+ void LCD_DATA(int data,int command);
+ //Configure LCD state
+
+
+ DigitalOut _rs, _e; //Write and command pins
+ BusOut _d; //Data out
+
+ int _column; //Internal position store
+ int _row;
+};
+
+#endif
\ No newline at end of file
--- a/LCD.cpp Wed Jan 10 00:32:08 2018 +0000
+++ b/LCD.cpp Wed Jan 10 02:36:50 2018 +0000
@@ -1,11 +1,11 @@
#include "mbed.h"
#include "LCD.h"
-#include "TextLCD.h"
+#include "DriverLCD.h"
#include "rtos.h"
using namespace std;
//Initalise TextLCD and button1 before constructor.
-ENVDISPLAY::ENVDISPLAY(PinName rsPin, PinName rePin, PinName d4Pin, PinName d5Pin, PinName d6Pin, PinName d7Pin, PinName Button1Pin, PinName Button2Pin) : TextLCD(rsPin, rePin, d4Pin, d5Pin, d6Pin, d7Pin), Button1(Button1Pin), Button2(Button2Pin)
+ENVDISPLAY::ENVDISPLAY(PinName rsPin, PinName rePin, PinName d4Pin, PinName d5Pin, PinName d6Pin, PinName d7Pin, PinName Button1Pin, PinName Button2Pin) : DriverLCD(rsPin, rePin, d4Pin, d5Pin, d6Pin, d7Pin), Button1(Button1Pin), Button2(Button2Pin)
{
//constructor. Initalise variables
_latestTemp = 0.0;
@@ -74,7 +74,7 @@
strcpy(_editingInTime, "Day"); //Say what we will be adjusting
_EditTime.tm_mday = TimeEditor(_EditTime.tm_mday, 31,1); //Enter time set loop then update value in structure
strcpy(_editingInTime, "MM"); //Say what we will be adjusting
- _EditTime.tm_mday = TimeEditor(_EditTime.tm_mon, 12,1); //Enter time set loop then update value in structure
+ _EditTime.tm_mon = TimeEditor(_EditTime.tm_mon, 12,1); //Enter time set loop then update value in structure
strcpy(_editingInTime, "Year"); //Say what we will be adjusting
_EditTime.tm_year = TimeEditor(_EditTime.tm_year, 2020,1996); //Enter time set loop then update value in structure
strcpy(_editingInTime, "Hour"); //Say what we will be adjusting
--- a/LCD.h Wed Jan 10 00:32:08 2018 +0000
+++ b/LCD.h Wed Jan 10 02:36:50 2018 +0000
@@ -2,7 +2,7 @@
#define __LCD__
#include "mbed.h"
-#include "TextLCD.h"
+#include "DriverLCD.h"
#include "rtos.h"
#include "TimeInterface.h"
#include "Logging.h"
@@ -23,7 +23,7 @@
#define SCREENDELAY_MS 2707 //Delay for screen update
-class ENVDISPLAY : TextLCD //Class inherits TextLCD
+class ENVDISPLAY : DriverLCD //Class inherits TextLCD
{
private:
char _message[32]; //32 characters on display
--- a/TextLCD.lib Wed Jan 10 00:32:08 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://mbed.org/users/simon/code/TextLCD/#308d188a2d3a