From 7470d22cc272d22ae75d9498e8d8f55d2e72b4a8 Mon Sep 17 00:00:00 2001 From: sweetlilmre Date: Thu, 7 Aug 2014 20:48:31 +0200 Subject: [PATCH] LCD configuration now in config.h for I2C expander bit mapping --- config.h | 16 +++++++++++--- lcd.c | 57 ++++++++++++++++++++++------------------------- lcd.h | 67 +++++++++++++++++++++++--------------------------------- 3 files changed, 66 insertions(+), 74 deletions(-) diff --git a/config.h b/config.h index 444a2c3..62c1fea 100644 --- a/config.h +++ b/config.h @@ -2,9 +2,19 @@ #define _CONFIG_H // LCD Definitions -#define LCD_I2C_ADDR 0x27 -#define LCD_NUM_LINES 2 -#define MAX_LCD_LINE_LEN 16 +// I2C config and expander data lines +#define LCD_I2C_ADDR 0x27 // I2C address for the LCD +#define LCD_BIT_RS 0 // Register select +#define LCD_BIT_RW 1 // Read / Write +#define LCD_BIT_EN 2 // Enable +#define LCD_BIT_BACKLIGHT 3 // Backlight +#define LCD_BIT_DATA0 4 // 4 bit data, bit 0 +#define LCD_BIT_DATA1 5 // 4 bit data, bit 1 +#define LCD_BIT_DATA2 6 // 4 bit data, bit 2 +#define LCD_BIT_DATA3 7 // 4 bit data, bit 3 +// dimension config +#define LCD_NUM_LINES 2 // number of display lines on the LCD +#define MAX_LCD_LINE_LEN 16 // max number of characters on a line // Timing constants #define SPINNER_RATE 250 // milliseconds, granularity 10ms diff --git a/lcd.c b/lcd.c index 19cd783..eac4d6c 100644 --- a/lcd.c +++ b/lcd.c @@ -9,6 +9,7 @@ #include #include +#include "config.h" #include "i2cmaster.h" #include "LCD.h" @@ -42,7 +43,7 @@ uint8_t _backlightval; void send(uint8_t value, uint8_t mode); -void write4bits(uint8_t value); +void write4bits(uint8_t value, uint8_t mode); void expanderWrite(uint8_t _data); void pulseEnable(uint8_t _data); @@ -81,7 +82,7 @@ void lcd_begin(uint8_t lcd_addr, uint8_t cols, uint8_t lines, uint8_t dotsize) { _delay_ms(50); // Now we pull both RS and R/W low to begin commands - expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 =1) + lcd_noBacklight(); // reset expander and turn backlight off (Bit 8 =1) _delay_ms(1000); //put the LCD into 4 bit mode @@ -89,19 +90,19 @@ void lcd_begin(uint8_t lcd_addr, uint8_t cols, uint8_t lines, uint8_t dotsize) { // figure 24, pg 46 // we start in 8bit mode, try to set 4 bit mode - write4bits(0x03 << 4); + write4bits(0x03, 0); _delay_us(4500); // wait min 4.1ms // second try - write4bits(0x03 << 4); + write4bits(0x03, 0); _delay_us(4500); // wait min 4.1ms // third go! - write4bits(0x03 << 4); + write4bits(0x03, 0); _delay_us(150); // finally, set to 4-bit interface - write4bits(0x02 << 4); + write4bits(0x02, 0); // set # lines, font size, etc. @@ -211,20 +212,20 @@ void lcd_createChar(uint8_t location, uint8_t charmap[]) { int i; location &= 0x7; // we only have 8 locations 0-7 command(LCD_SETCGRAMADDR | (location << 3)); - for (i=0; i<8; i++) { + for (i = 0; i < 8; i++) { lcd_write(charmap[i]); } } // Turn the (optional) backlight off/on void lcd_noBacklight(void) { - _backlightval=LCD_NOBACKLIGHT; - expanderWrite(0); + _backlightval = LCD_NOBACKLIGHT; + expanderWrite(_backlightval); } void lcd_backlight(void) { - _backlightval=LCD_BACKLIGHT; - expanderWrite(0); + _backlightval = LCD_BACKLIGHT; + expanderWrite(_backlightval); } void lcd_print(char* msg) { @@ -245,33 +246,27 @@ void lcd_print_P(const char *msg) { // write either command or data void send(uint8_t value, uint8_t mode) { - uint8_t highnib = value & 0xf0; - uint8_t lownib = (value<<4) & 0xf0; - write4bits((highnib) | mode); - write4bits((lownib) | mode); + uint8_t highnib = value >> 4; + uint8_t lownib = value & 0x0f; + write4bits(highnib, mode); + write4bits(lownib, mode); } -void write4bits(uint8_t value) { -/* +void write4bits(uint8_t value, uint8_t mode) { uint8_t res = 0; - uint8_t tmp = value >> 4; - if (tmp & 1) res |= _BV(LCD_BIT_DATA0); - tmp >>= 1; - if (tmp & 1) res |= _BV(LCD_BIT_DATA1); - tmp >>= 1; - if (tmp & 1) res |= _BV(LCD_BIT_DATA2); - tmp >>= 1; - if (tmp & 1) res |= _BV(LCD_BIT_DATA3); - res <<= 4; - value = (value & 0x0f) | res; -*/ - expanderWrite(value); - pulseEnable(value); + if (value & 0x1) res |= _BV(LCD_BIT_DATA0); + if (value & 0x2) res |= _BV(LCD_BIT_DATA1); + if (value & 0x4) res |= _BV(LCD_BIT_DATA2); + if (value & 0x8) res |= _BV(LCD_BIT_DATA3); + res |= mode | _backlightval; + + expanderWrite(res); + pulseEnable(res); } void expanderWrite(uint8_t _data){ i2c_start((_addr << 1) | I2C_WRITE); - i2c_write((_data) | _backlightval); + i2c_write(_data); i2c_stop(); } diff --git a/lcd.h b/lcd.h index 3707b25..523e1bc 100644 --- a/lcd.h +++ b/lcd.h @@ -42,49 +42,36 @@ #define LCD_5x10DOTS 0x04 #define LCD_5x8DOTS 0x00 -#define LCD_BIT_RS 0 -#define LCD_BIT_RW 1 -#define LCD_BIT_EN 2 -#define LCD_BIT_BACKLIGHT 3 -#define LCD_BIT_DATA0 4 -#define LCD_BIT_DATA1 5 -#define LCD_BIT_DATA2 6 -#define LCD_BIT_DATA3 7 - // flags for backlight control #define LCD_BACKLIGHT _BV(LCD_BIT_BACKLIGHT) #define LCD_NOBACKLIGHT 0x00 -//#define En 0b00000100 /* Enable bit */ -//#define Rw 0b00000010 /* Read/Write bit */ -//#define Rs 0b00000001 /* Register select bit */ - - void lcd_begin(uint8_t lcd_addr, uint8_t cols, uint8_t rows, uint8_t charsize); // = LCD_5x8DOTS - void lcd_clear(); - void lcd_home(); - void lcd_noDisplay(); - void lcd_display(); - void lcd_noBlink(); - void lcd_blink(); - void lcd_noCursor(); - void lcd_cursor(); - void lcd_scrollDisplayLeft(); - void lcd_scrollDisplayRight(); - void lcd_printLeft(); - void lcd_printRight(); - void lcd_leftToRight(); - void lcd_rightToLeft(); - void lcd_shiftIncrement(); - void lcd_shiftDecrement(); - void lcd_noBacklight(); - void lcd_backlight(); - void lcd_autoscroll(); - void lcd_noAutoscroll(); - void lcd_createChar(uint8_t, uint8_t[]); - void lcd_setCursor(uint8_t, uint8_t); - void lcd_command(uint8_t); - void lcd_print(char* msg); - void lcd_print_P(const char *msg); - void lcd_write(uint8_t value); +void lcd_begin(uint8_t lcd_addr, uint8_t cols, uint8_t rows, uint8_t charsize); // = LCD_5x8DOTS +void lcd_clear(); +void lcd_home(); +void lcd_noDisplay(); +void lcd_display(); +void lcd_noBlink(); +void lcd_blink(); +void lcd_noCursor(); +void lcd_cursor(); +void lcd_scrollDisplayLeft(); +void lcd_scrollDisplayRight(); +void lcd_printLeft(); +void lcd_printRight(); +void lcd_leftToRight(); +void lcd_rightToLeft(); +void lcd_shiftIncrement(); +void lcd_shiftDecrement(); +void lcd_noBacklight(); +void lcd_backlight(); +void lcd_autoscroll(); +void lcd_noAutoscroll(); +void lcd_createChar(uint8_t, uint8_t[]); +void lcd_setCursor(uint8_t, uint8_t); +void lcd_command(uint8_t); +void lcd_print(char* msg); +void lcd_print_P(const char *msg); +void lcd_write(uint8_t value); #endif