Skip to content

Commit

Permalink
LCD configuration now in config.h for I2C expander bit mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sweetlilmre committed Aug 7, 2014
1 parent a0d844c commit 7470d22
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 74 deletions.
16 changes: 13 additions & 3 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 26 additions & 31 deletions lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <util/delay.h>
#include <avr/pgmspace.h>

#include "config.h"
#include "i2cmaster.h"
#include "LCD.h"

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -81,27 +82,27 @@ 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
// this is according to the hitachi HD44780 datasheet
// 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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}

Expand Down
67 changes: 27 additions & 40 deletions lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7470d22

Please sign in to comment.