From cc2ebfe37bd39b51d712497ca1e810e7ff7d02e8 Mon Sep 17 00:00:00 2001 From: Stefan Rupp Date: Wed, 19 Mar 2014 03:49:57 +0100 Subject: [PATCH 1/2] implement timeout in LCD code --- lcd.c | 68 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/lcd.c b/lcd.c index ae1b59f..e1341ce 100644 --- a/lcd.c +++ b/lcd.c @@ -50,6 +50,10 @@ // !!!!!!! +#define T_TIMEOUT (10) + +static uint8_t lcd_initialized; + static inline uint8_t lcd_read_byte(uint8_t RS) { @@ -79,14 +83,19 @@ static inline uint8_t lcd_read_byte(uint8_t RS) } -static inline void lcd_wait_while_busy(void) +static inline int8_t lcd_wait_while_busy(void) { uint8_t byte; + uint32_t t_enter = timer_get(); + do { + if (timer_get() > t_enter + T_TIMEOUT) { + return 0; + } byte = lcd_read_byte(0); } while (byte & 0x80); - return; + return 1; } @@ -105,46 +114,59 @@ static inline void send_nibble(uint8_t nibble) * \param byte The byte to send to the LCD * \param RS specifies whether byte sent is data (RS==1) or command (RS==0) */ -static inline void lcd_send_byte(uint8_t byte, uint8_t RS) +static inline int8_t lcd_send_byte(uint8_t byte, uint8_t RS) { - lcd_wait_while_busy(); - if (RS) - LCD_DATA_MODE(); - else - LCD_CMD_MODE(); + if (lcd_wait_while_busy() ) { + if (RS) + LCD_DATA_MODE(); + else + LCD_CMD_MODE(); - send_nibble(byte >> 4); - _NOP(); - send_nibble(0x0f & byte); + send_nibble(byte >> 4); + _NOP(); + send_nibble(0x0f & byte); + return 1; + } + return 0; } int lcd_get(void) { - lcd_wait_while_busy(); - return lcd_read_byte(1); - + if ( lcd_wait_while_busy() ) { + return lcd_read_byte(1); + } + return -1; } int lcd_put(char c) { - lcd_send_byte(c, 1); - return 0; + if (!lcd_initialized) { + return 0; + } + return lcd_send_byte(c, 1); } void lcd_puts(char *s) { + if (!lcd_initialized) { + return; + } + while (*s) { //lcd_put(*s++); - lcd_send_byte(*s++, 1); + if (!lcd_send_byte(*s++, 1)) { + return; + } } + return; } void lcd_cursor(uint8_t blink) { if (blink) { // enable display with cursor + blinking - lcd_send_byte(0x0f, 0); + lcd_send_byte(0x0f, 0); } else { // enable display, no cursor, no blinking @@ -160,6 +182,8 @@ void lcd_init(void) * display we be set to 4-bit data mode */ + lcd_initialized = 0; + // wait until LCD gets ready to accept instructions. timer_wait(50); @@ -189,7 +213,11 @@ void lcd_init(void) // set to 5 x 8 dots per character, // 16 characters per line, 2 lines - lcd_send_byte(0x28, 0); + int success = lcd_send_byte(0x28, 0); + + if ( !success ) { + return; + } // enable display, no cursor, no blinking //lcd_send_byte(0x0c, 0); @@ -204,6 +232,8 @@ void lcd_init(void) // clear dislay lcd_clear(); + + lcd_initialized = 1; return; } From 50fd1f1004f4a9f12ac1d41551a52ab7cd19aaf9 Mon Sep 17 00:00:00 2001 From: Stefan Rupp Date: Wed, 19 Mar 2014 03:51:25 +0100 Subject: [PATCH 2/2] lcd_locate is useless, if not initialized --- lcd.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lcd.c b/lcd.c index e1341ce..ce65e92 100644 --- a/lcd.c +++ b/lcd.c @@ -245,6 +245,10 @@ void lcd_clear(void) void lcd_locate(uint8_t row, uint8_t col) { + if (!lcd_initialized) { + return; + } + if (row) col += 0x40; lcd_send_byte(0x80 + col, 0);