#include "lcd.h" #include #include #include "defines.h" #define ENPIN 0 #define RWPIN 1 #define RSPIN 2 #define BLPIN 3 #define DATASHIFT 4 uint8_t _numlines; uint8_t _cols; uint8_t _En = 0x04; uint8_t _Rs = 0x01; uint8_t _Rw = 0x02; uint8_t _Bl = 0x08; uint8_t _BackliteOn = false; uint8_t _displaymode=0; uint8_t _displaycontrol=0; void i2csend(uint8_t data){ i2c_start(0x27<<1); i2c_write(data); i2c_stop(); } void pulseEnable (uint8_t data) { i2csend (data | _En); // En HIGH i2csend (data); // En LOW } void write4bits ( uint8_t value, uint8_t mode ) { uint8_t data = value << 4; // Is it a command or data // ----------------------- if ( mode == LCD_DATA ) data |= _Rs; if (_BackliteOn) data |= _Bl; pulseEnable ( data ); } void send(uint8_t value, uint8_t mode) { // No need to use the delay routines since the time taken to write takes // longer that what is needed both for toggling and enable pin an to execute // the command. if ( mode == FOUR_BITS ) { write4bits( (value & 0x0F), COMMAND ); } else { write4bits( (value >> 4), mode ); write4bits( (value & 0x0F), mode); } } void command(uint8_t value) { send(value, COMMAND); } void lcd_init(uint8_t cols, uint8_t lines) { i2c_init(); _cols=cols; _numlines=lines; _delay_ms(100); // this is according to the hitachi HD44780 datasheet // figure 24, pg 46 // we start in 8bit mode, try to set 4 bit mode // Special case of "Function Set" send(0x03, FOUR_BITS); _delay_us(4500); // wait min 4.1ms // second try send ( 0x03, FOUR_BITS ); _delay_us(150); // wait min 100us // third go! send( 0x03, FOUR_BITS ); _delay_us(150); // wait min of 100us // finally, set to 4-bit interface send ( 0x02, FOUR_BITS ); _delay_us(150); // wait min of 100us command(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE); _displaymode = LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } void lcd_clear() { command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero _delay_us(HOME_CLEAR_EXEC); // this command is time consuming } void lcd_home() { command(LCD_RETURNHOME); // set cursor position to zero _delay_us(HOME_CLEAR_EXEC); // This command is time consuming } void lcd_setCursor(uint8_t col, uint8_t row) { const uint8_t row_offsetsDef[] = { 0x00, 0x40, 0x14, 0x54 }; // For regular LCDs const uint8_t row_offsetsLarge[] = { 0x00, 0x40, 0x10, 0x50 }; // For 16x4 LCDs if ( row >= _numlines ) { row = _numlines-1; // rows start at 0 } // 16x4 LCDs have special memory map layout // ---------------------------------------- if ( _cols == 16 && _numlines == 4 ) { command(LCD_SETDDRAMADDR | (col + row_offsetsLarge[row])); } else { command(LCD_SETDDRAMADDR | (col + row_offsetsDef[row])); } } // Turn the display on/off void lcd_noDisplay() { _displaycontrol &= ~LCD_DISPLAYON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void lcd_display() { _displaycontrol |= LCD_DISPLAYON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turns the underline cursor on/off void lcd_noCursor() { _displaycontrol &= ~LCD_CURSORON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void lcd_cursor() { _displaycontrol |= LCD_CURSORON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turns on/off the blinking cursor void lcd_noBlink() { _displaycontrol &= ~LCD_BLINKON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void lcd_blink() { _displaycontrol |= LCD_BLINKON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // These commands scroll the display without changing the RAM void lcd_scrollDisplayLeft(void) { command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); } void lcd_scrollDisplayRight(void) { command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); } // This method moves the cursor one space to the right void lcd_moveCursorRight(void) { command(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVERIGHT); } // This method moves the cursor one space to the left void lcd_moveCursorLeft(void) { command(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVELEFT); } // This is for text that flows Left to Right void lcd_leftToRight(void) { _displaymode |= LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } // This is for text that flows Right to Left void lcd_rightToLeft(void) { _displaymode &= ~LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } // This will 'right justify' text from the cursor void lcd_autoscroll(void) { _displaymode |= LCD_ENTRYSHIFTINCREMENT; command(LCD_ENTRYMODESET | _displaymode); } // This will 'left justify' text from the cursor void lcd_noAutoscroll(void) { _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; command(LCD_ENTRYMODESET | _displaymode); } void lcd_createChar(uint8_t location, const char *charmap) { location &= 0x7; // we only have 8 memory locations 0-7 command(LCD_SETCGRAMADDR | (location << 3)); _delay_us(30); for (uint8_t i = 0; i < 8; i++) { lcd_write(pgm_read_byte_near(charmap++)); _delay_us(40); } } void lcd_write(uint8_t value) { send(value, LCD_DATA); } void lcd_backlight(uint8_t value){ _BackliteOn = value?1:0; }