123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #include "max_LCD.h"
- #include <string.h>
- #define RS 0x04
- #define E 0x08
- #define SET_RS lcdPins |= RS
- #define CLR_RS lcdPins &= ~RS
- #define SET_E lcdPins |= E
- #define CLR_E lcdPins &= ~E
- #define SENDlcdPins() pUsb->gpioWr( lcdPins )
- #define LCD_sendcmd(a) { CLR_RS; \
- sendbyte(a); \
- }
- #define LCD_sendchar(a) { SET_RS; \
- sendbyte(a); \
- }
- static byte lcdPins;
- Max_LCD::Max_LCD(USB *pusb) : pUsb(pusb) {
- lcdPins = 0;
- }
- void Max_LCD::init() {
- _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
-
- begin(16, 1);
- }
- void Max_LCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
- if(lines > 1) {
- _displayfunction |= LCD_2LINE;
- }
- _numlines = lines;
- _currline = 0;
-
- if((dotsize != 0) && (lines == 1)) {
- _displayfunction |= LCD_5x10DOTS;
- }
-
-
-
- delayMicroseconds(50000);
- lcdPins = 0x30;
- SET_E;
- SENDlcdPins();
- CLR_E;
- SENDlcdPins();
- delayMicroseconds(10000);
-
- SET_E;
- SENDlcdPins();
- CLR_E;
- SENDlcdPins();
- delayMicroseconds(10000);
-
- SET_E;
- SENDlcdPins();
- CLR_E;
- SENDlcdPins();
- delayMicroseconds(10000);
-
- lcdPins = 0x20;
-
- SET_E;
- SENDlcdPins();
-
- CLR_E;
- SENDlcdPins();
- delayMicroseconds(10000);
-
- command(LCD_FUNCTIONSET | _displayfunction);
-
- _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
- display();
-
- clear();
-
- _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
-
- command(LCD_ENTRYMODESET | _displaymode);
- }
- void Max_LCD::clear() {
- command(LCD_CLEARDISPLAY);
- delayMicroseconds(2000);
- }
- void Max_LCD::home() {
- command(LCD_RETURNHOME);
- delayMicroseconds(2000);
- }
- void Max_LCD::setCursor(uint8_t col, uint8_t row) {
- int row_offsets[] = {0x00, 0x40, 0x14, 0x54};
- if(row > _numlines) {
- row = _numlines - 1;
- }
- command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
- }
- void Max_LCD::noDisplay() {
- _displaycontrol &= ~LCD_DISPLAYON;
- command(LCD_DISPLAYCONTROL | _displaycontrol);
- }
- void Max_LCD::display() {
- _displaycontrol |= LCD_DISPLAYON;
- command(LCD_DISPLAYCONTROL | _displaycontrol);
- }
- void Max_LCD::noCursor() {
- _displaycontrol &= ~LCD_CURSORON;
- command(LCD_DISPLAYCONTROL | _displaycontrol);
- }
- void Max_LCD::cursor() {
- _displaycontrol |= LCD_CURSORON;
- command(LCD_DISPLAYCONTROL | _displaycontrol);
- }
- void Max_LCD::noBlink() {
- _displaycontrol &= ~LCD_BLINKON;
- command(LCD_DISPLAYCONTROL | _displaycontrol);
- }
- void Max_LCD::blink() {
- _displaycontrol |= LCD_BLINKON;
- command(LCD_DISPLAYCONTROL | _displaycontrol);
- }
- void Max_LCD::scrollDisplayLeft(void) {
- command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
- }
- void Max_LCD::scrollDisplayRight(void) {
- command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
- }
- void Max_LCD::leftToRight(void) {
- _displaymode |= LCD_ENTRYLEFT;
- command(LCD_ENTRYMODESET | _displaymode);
- }
- void Max_LCD::rightToLeft(void) {
- _displaymode &= ~LCD_ENTRYLEFT;
- command(LCD_ENTRYMODESET | _displaymode);
- }
- void Max_LCD::autoscroll(void) {
- _displaymode |= LCD_ENTRYSHIFTINCREMENT;
- command(LCD_ENTRYMODESET | _displaymode);
- }
- void Max_LCD::noAutoscroll(void) {
- _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
- command(LCD_ENTRYMODESET | _displaymode);
- }
- void Max_LCD::createChar(uint8_t location, uint8_t charmap[]) {
- location &= 0x7;
- command(LCD_SETCGRAMADDR | (location << 3));
- for(int i = 0; i < 8; i++) {
- write(charmap[i]);
- }
- }
- inline void Max_LCD::command(uint8_t value) {
- LCD_sendcmd(value);
- delayMicroseconds(100);
- }
- #if defined(ARDUINO) && ARDUINO >=100
- inline size_t Max_LCD::write(uint8_t value) {
- LCD_sendchar(value);
- return 1;
- }
- #else
- inline void Max_LCD::write(uint8_t value) {
- LCD_sendchar(value);
- }
- #endif
- void Max_LCD::sendbyte(uint8_t val) {
- lcdPins &= 0x0f;
- lcdPins |= (val & 0xf0);
- SET_E;
- SENDlcdPins();
- delayMicroseconds(2);
- CLR_E;
- delayMicroseconds(2);
- SENDlcdPins();
- lcdPins &= 0x0f;
- lcdPins |= (val << 4) & 0xf0;
- SET_E;
- SENDlcdPins();
- CLR_E;
- SENDlcdPins();
- delayMicroseconds(100);
- }
|