From 047c4b5c31f495d7cc5c5c33f712bf7b80fa9f56 Mon Sep 17 00:00:00 2001 From: "Collin J. Doering" Date: Wed, 30 Sep 2015 04:46:49 -0400 Subject: [PATCH] Broken likely due to timing Signed-off-by: Collin J. Doering --- lcdLib.c | 54 +++++++++++++++++++++++++++---------------------- lcdLib.h | 58 +++++++++++++++++++++++++++++++++++++++++++++-------- lcdOutput.c | 2 +- 3 files changed, 81 insertions(+), 33 deletions(-) diff --git a/lcdLib.c b/lcdLib.c index f39eadf..6c4dd16 100644 --- a/lcdLib.c +++ b/lcdLib.c @@ -51,16 +51,13 @@ void clkLCD(void) { void loop_until_LCD_BF_clear(void) { LCD_CTRL_PORT = (LCD_CTRL_PORT & ~(1 << LCD_RS)) | (1 << LCD_RW); // RS=0, RW=1 LCD_DBUS_DDR &= ~(1 << LCD_BF); // Set LCD_BF as input - clkLCD(); STATUS_LED_PORT |= 1 << STATUS_LED; // DEBUG - while (bit_is_clear(LCD_DBUS_PIN, LCD_BF)) { + do { clkLCD(); - } - + } while (bit_is_clear(LCD_DBUS_PIN, LCD_BF)); /* loop_until_bit_is_clear(LCD_DBUS_PIN, LCD_BF); */ STATUS_LED_PORT &= ~(1 << STATUS_LED); // DEBUG - LCD_DBUS_DDR = 0xff; // Reset all LCD_DBUS_PORT pins as outputs } @@ -97,9 +94,14 @@ void writeStringToLCD(const char* str) { } } -void clearScreen(void) { - writeLCDInstr(CMD_CLEAR_SCREEN); - _delay_us(LCD_CLEAR_SCREEN_DELAY); +void clearDisplay(void) { + writeLCDInstr(CMD_CLEAR_DISPLAY); + _delay_us(LCD_CLEAR_DISPLAY_DELAY); +} + +void returnHome(void) { + writeLCDInstr(CMD_RETURN_HOME); + _delay_us(LCD_RETURN_HOME_DELAY); } char readCharFromLCD(void) { @@ -135,7 +137,7 @@ static inline void disableLCDOutput(void) { static inline void softwareLCDInitPulse(void) { enableLCDOutput(); LCD_CTRL_PORT &= ~((1 << LCD_RS) | (1 << LCD_RW)); // RS=RW=0 - LCD_DBUS_PORT = 0x30; + LCD_DBUS_PORT = CMD_INIT; clkLCD(); } @@ -143,36 +145,40 @@ static inline void softwareLCDInitPulse(void) { Do software initialization as specified by the datasheet */ void initLCD (void) { - _delay_ms(30); // Wait 15ms as per datasheet + enableLCDOutput(); + + // Wait minimum 15ms as per datasheet + _delay_ms(LCD_INIT_DELAY0); softwareLCDInitPulse(); - // Disable transmission and wait minimum 4.1ms as per datasheet - //disableLCDOutput(); - _delay_us(8200); + // Wait minimum 4.1ms as per datasheet + _delay_us(LCD_INIT_DELAY1); softwareLCDInitPulse(); - // Disable transmission and wait minimum 100us as per datasheet - //disableLCDOutput(); - _delay_us(200); + // Wait minimum 100us as per datasheet + _delay_us(LCD_INIT_DELAY2); softwareLCDInitPulse(); // Function set (2 lines with 5x7 dot character font) - writeLCDInstr_(0x38); // RS=RW=0, 0b00111000 + writeLCDInstr_(0x38); // RS=RW=0, 0b00111000, 0x38 - /* BF now has to be checked */ + /* BF now can be checked */ // Set functions of LCD - writeLCDInstr_(0x08); // Display off - _delay_ms(1); - writeLCDInstr_(0x01); // Clear display - _delay_ms(16); - //_delay_us(15200 - LCD_DELAY); // Extra delay for clear display + writeLCDInstr_(INSTR_DISPLAY); // Display off + _delay_us(LCD_GENERIC_INSTR_DELAY); + + writeLCDInstr_(CMD_CLEAR_DISPLAY); // Clear display + _delay_us(LCD_CLEAR_DISPLAY_DELAY); + writeLCDInstr_(0x06); // Increment mode, no shift - _delay_ms(1); + _delay_us(LCD_GENERIC_INSTR_DELAY); + writeLCDInstr_(0x0E); // Display on, cursor on, blink off + _delay_us(LCD_GENERIC_INSTR_DELAY); flashLED(5); // DEBUG } diff --git a/lcdLib.h b/lcdLib.h index b04140d..9dc983e 100644 --- a/lcdLib.h +++ b/lcdLib.h @@ -46,14 +46,54 @@ #define LCD_DBUS7 PB7 #define LCD_BF PB7 -// LCD delays (in microseconds) -#define LCD_DELAY 50 -#define LCD_CLEAR_SCREEN_DELAY 16000 -#define LCD_SEND_DELAY 40 // unused +// LCD in 4 bit mode (default is 8 bit mode) +//#define FOUR_BIT_MODE + +// LCD delays (in microseconds when unspecified) +#define LCD_DELAY 1 +#define LCD_INIT_DELAY0 30 // milliseconds +#define LCD_INIT_DELAY1 8200 +#define LCD_INIT_DELAY2 200 + +#define LCD_CLEAR_DISPLAY_DELAY 16000 +#define LCD_RETURN_HOME_DELAY 16000 +#define LCD_GENERIC_INSTR_DELAY 50 + +/* LCD Commands */ + +// Simple commands with no options +#define CMD_INIT 0x30 +#define CMD_CLEAR_DISPLAY 0x01 +#define CMD_RETURN_HOME 0x02 + +// Entry Set instruction and associated options +#define INSTR_ENTRY_SET 0x04 +#define INSTR_ENTRY_SET_ID 0x02 +#define INSTR_ENTRY_SET_S 0x01 + +// Display control instruction and associated options +#define INSTR_DISPLAY 0x08 +#define INSTR_DISPLAY_D 0x04 +#define INSTR_DISPLAY_C 0x02 +#define INSTR_DISPLAY_B 0x01 + +// Cursor or display shift instruction and associated options +#define INSTR_MOV_SHIFT 0x10 +#define INSTR_MOV_SHIFT_SC 0x08 +#define INSTR_MOV_SHIFT_RL 0x04 + +// Function set instruction and associated options +#define INSTR_FUNC_SET 0x20 +#define INSTR_FUNC_SET_DL 4 +#define INSTR_FUNC_SET_N 3 +#define INSTR_FUNC_SET_F 2 + +// Set CG RAM address instruction +#define INSTR_CGRAM_ADDR 0x60 + +// Set DD RAM address instruction +#define INSTR_DDRAM_ADDR 0x80 -// LCD Commands -#define CMD_CLEAR_SCREEN 0x01 -#define CMD_RETURN_HOME 0x02 //------------------------------------ @@ -80,7 +120,9 @@ void writeCharToLCD(char c); void writeStringToLCD(const char* str); -void clearScreen(void); +void clearDisplay(void); + +void returnHome(void); char readCharFromLCD(void); diff --git a/lcdOutput.c b/lcdOutput.c index eff87fc..66fb2a0 100644 --- a/lcdOutput.c +++ b/lcdOutput.c @@ -50,7 +50,7 @@ int main(void) { writeStringToLCD(data[i]); } _delay_ms(5000); - clearScreen(); + clearDisplay(); //flashLED(5); // DEBUG