GroupU - 05012018 1543

Fork of 352 by Elec351 MMB

Committer:
mslade
Date:
Tue Jan 09 14:53:07 2018 +0000
Revision:
1:84581acd1333
Final Version - Group U

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mslade 1:84581acd1333 1 #include "lcdClass.h"
mslade 1:84581acd1333 2 #include "mbed.h"
mslade 1:84581acd1333 3
mslade 1:84581acd1333 4 float W = 0.000040; float W1 = 0.00164;
mslade 1:84581acd1333 5 lcdClass::lcdClass(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7) : _rs(rs), _e(e), _d(d4, d5, d6, d7){
mslade 1:84581acd1333 6 _e = 1; _rs = 0; //E pin high, RS set to 0 for instruction register
mslade 1:84581acd1333 7 wait(0.015); //Wait for VCC to rise
mslade 1:84581acd1333 8 for (int i=0; i<3; i++) {writeByte(0x3);wait(W1);} //Send initialisation value, 0x3.
mslade 1:84581acd1333 9 writeByte(0x2); wait(W); //Set Bus Width (4-bit)
mslade 1:84581acd1333 10 writeCommand(0x28); //4-bit mode, 2-line, 5x7 font
mslade 1:84581acd1333 11 writeCommand(0x0C); //No Cursor or Blink
mslade 1:84581acd1333 12 writeCommand(0x6); //Automatic Increment, No Display Shift
mslade 1:84581acd1333 13 cls(); //Ensure screen is cleared
mslade 1:84581acd1333 14 }
mslade 1:84581acd1333 15
mslade 1:84581acd1333 16 void lcdClass::character(int column, int row, int c) {
mslade 1:84581acd1333 17 int a = 0x80 + (row * 0x40) + column;
mslade 1:84581acd1333 18 writeCommand(a); writeData(c);}
mslade 1:84581acd1333 19
mslade 1:84581acd1333 20 void lcdClass::cls() {writeCommand(0x01); wait(W1); locate(0, 0);} //Send Clear Display Command, Set location back to column 0, row 0
mslade 1:84581acd1333 21
mslade 1:84581acd1333 22 void lcdClass::locate(int column, int row) {_column = column; _row = row;} //Set protected column and row to input column and row
mslade 1:84581acd1333 23
mslade 1:84581acd1333 24 int lcdClass::_putc(int value) {
mslade 1:84581acd1333 25 if (value == '\n') {
mslade 1:84581acd1333 26 _column = 0; _row++; //If input character is \n, set column to 0, increment row.
mslade 1:84581acd1333 27 if (_row >= 2) {_row = 0;} //If row is 2, set row to 0
mslade 1:84581acd1333 28 }
mslade 1:84581acd1333 29 else {
mslade 1:84581acd1333 30 character(_column, _row, value);_column++; //Else write the input value to the
mslade 1:84581acd1333 31 if (_column >= 16) {
mslade 1:84581acd1333 32 _column = 0;_row++;
mslade 1:84581acd1333 33 if (_row >= 2) {_row = 0;}
mslade 1:84581acd1333 34 }
mslade 1:84581acd1333 35 }
mslade 1:84581acd1333 36 return value;
mslade 1:84581acd1333 37 }
mslade 1:84581acd1333 38 int lcdClass::_getc() {return -1;}
mslade 1:84581acd1333 39 void lcdClass::writeByte(int value) {
mslade 1:84581acd1333 40 _d = value >> 4; wait(W);
mslade 1:84581acd1333 41 _e = 0; wait(W);
mslade 1:84581acd1333 42 _e = 1;
mslade 1:84581acd1333 43 _d = value >> 0; wait(W);
mslade 1:84581acd1333 44 _e = 0; wait(W);
mslade 1:84581acd1333 45 _e = 1;
mslade 1:84581acd1333 46 }
mslade 1:84581acd1333 47
mslade 1:84581acd1333 48 void lcdClass::writeCommand(int command) {_rs = 0; writeByte(command);}
mslade 1:84581acd1333 49 void lcdClass::writeData(int data) { _rs = 1; writeByte(data);}