/* * CLCD.cpp - Arduino library for CharacterLCD - C++ adaptation * by Homin Lee (Suapapa) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. You should have received * a copy of the license along with this program. * If not, see . */ #include "CLCD.h" // initiatize lcd after a short pause: needed by the LCDs controller #define PRE_FUCTIONSET 0x30 // function set: DL 8bit, 2-line, 5x8 dot #ifdef CLCD_INCLUDE_8BIT_INTERFACE #define CMD_FUNCTIONSET_8 0x38 #endif #define CMD_FUNCTIONSET_4 0x28 // display control: turn display on, cursor on, no blinking #define CMD_DISPLAYCNTL 0x0E // entry mode set: increment automatically, display shift, right shift #define CMD_ENTRYMODESET 0x06 #define CMD_CLEAR 0x01 #define CMD_INDEX 0x80 CLCD::CLCD(uint8_t RS, uint8_t RW, uint8_t EN, uint8_t *DBUS, uint8_t busLen) { _pinRS = RS; _pinRW = RW; _pinEN = EN; _busData = DBUS; #ifdef CLCD_INCLUDE_8BIT_INTERFACE _busLenth = busLen; #endif } void CLCD::init(void) { uint8_t i = 0; #ifdef CLCD_INCLUDE_8BIT_INTERFACE for (i=0; i < _busLenth; i++) #else for (i=0; i < 4; i++) #endif { pinMode(_busData[i],OUTPUT); } pinMode(_pinRS,OUTPUT); pinMode(_pinRW,OUTPUT); pinMode(_pinEN,OUTPUT); digitalWrite(_pinRS, LOW); digitalWrite(_pinRW, LOW); digitalWrite(_pinEN, LOW); delay(20); #ifdef CLCD_INCLUDE_8BIT_INTERFACE if (_busLenth == 8) { _putByte(PRE_FUCTIONSET); delay(5); _putByte(PRE_FUCTIONSET); delay(1); _putByte(PRE_FUCTIONSET); delay(1); command(CMD_FUNCTIONSET_8); delay(1); } else if (_busLenth == 4) #endif { _putNibble(PRE_FUCTIONSET>>4); delay(5); _putNibble(PRE_FUCTIONSET>>4); delay(1); _putNibble(PRE_FUCTIONSET>>4); delay(1); command(CMD_FUNCTIONSET_4); delay(1); } //command(0x20); delay(2); command(CMD_DISPLAYCNTL); delay(1); command(CMD_ENTRYMODESET); delay(1); clear(); } void CLCD::clear(void) { command(CMD_CLEAR); delay(2); } void CLCD::command(uint8_t cmd) { digitalWrite(_pinRS, LOW); delayMicroseconds(1); _putByte(cmd); } void CLCD::putChar(char ch) { digitalWrite(_pinRS, HIGH); delayMicroseconds(1); _putByte(ch); } void CLCD::putStr(char str[]) { uint8_t i = 0; uint8_t currChar = str[i]; while (currChar) { if (currChar == '\n') setCursor(1, 0); //command(0xC0); else putChar(currChar); currChar = str[++i]; } } /* itoa: convert n to characters in s */ uint8_t CLCD::_itoa(int n) { int i, sign; if ((sign = n) < 0) n = -n; i = 0; do { _itoaBuf[i++] = n % 10 + '0'; if (i > CLCD_ITOA_BUF_LEN) { _itoaBuf[i] = '\0'; return CLCD_ITOA_BUF_LEN; } } while ((n /= 10) > 0); if (sign < 0) _itoaBuf[i++] = '-'; _itoaBuf[i] = '\0'; return i; } void CLCD::putDec(int num) { uint8_t i = _itoa(num); while(i) { putChar(_itoaBuf[--i]); } } //0-79, index for one line display, 8 bit mode //0-39 and 64-103 for lines one and two of two line display, void CLCD::setCursor(uint8_t line, uint8_t index) { uint8_t cmd = CMD_INDEX+index; if (line == 1) cmd += 64; command(cmd); delay(1); } void CLCD::_putNibble(uint8_t data) { int i; for (i=0; i < 4; i++) { if (data & 0x01) digitalWrite(_busData[i], HIGH); else digitalWrite(_busData[i], LOW); data >>= 1; } digitalWrite(_pinEN,HIGH); delayMicroseconds(3); digitalWrite(_pinEN,LOW); delayMicroseconds(3); } void CLCD::_putByte(uint8_t data) { #ifdef CLCD_INCLUDE_8BIT_INTERFACE int i; if (_busLenth == 8) { // poll all the pins for (i=0; i < 8; i++) { if (data & 0x01) digitalWrite(_busData[i], HIGH); else digitalWrite(_busData[i], LOW); data >>= 1; } digitalWrite(_pinEN,HIGH); delayMicroseconds(1); digitalWrite(_pinEN,LOW); delayMicroseconds(1); } else if (_busLenth == 4) #endif { _putNibble(data>>4); _putNibble(data); } }