From b1559460eeb0f4d5feb9d247db499a07f7daa169 Mon Sep 17 00:00:00 2001 From: "Christian L. V. Madsen" Date: Sat, 14 Jun 2025 19:47:03 +0200 Subject: [PATCH] some progress on 4 bit mode.. but doesnt work --- lcd_hd44780.c | 37 +++++++++++++++++++++++++++++++++---- lcd_hd44780.h | 2 ++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lcd_hd44780.c b/lcd_hd44780.c index a5290f2..63cca46 100644 --- a/lcd_hd44780.c +++ b/lcd_hd44780.c @@ -28,6 +28,32 @@ void lcd_hd44780_printf(lcd_hd44780_t *inst, char *string){ }*/ +static int WaitUntilReady(cm_lcd_hd44780_t *inst){ + + // If display is init, then use Polling Busy Flag! + if(inst->isInit){ + // RS = 0, RW = 1 + inst->gpio_data.rs_pin = 0; + inst->gpio_data.rw_pin = 1; + + // Read from the data bus + inst->setGetGpio_event(&(inst->gpio_data), LCD_HD44780_GET); + + // Loop until Busy Flag (D7) is clear + while (inst->gpio_data.data_bus & 0x80) { + inst->setGetGpio_event(&(inst->gpio_data), LCD_HD44780_GET); + } + + return 1; + + }else{ + inst->wait_event(1); + return 1; + } + + +} + static int lcd_write_8bit(cm_lcd_hd44780_t *inst, uint8_t dataOrCmd, hd44780_InstData_t mode){ // Set cmd on databus @@ -41,13 +67,15 @@ static int lcd_write_8bit(cm_lcd_hd44780_t *inst, uint8_t dataOrCmd, hd44780_Ins // E pin must be high when data is not ready yet. inst->gpio_data.e_pin = 1; - inst->setGetGpio_event(&(inst->gpio_data),LCD_HD44780_SET); // Then Clock data in with E pin. Data is clocked on Falling edge: inst->gpio_data.e_pin = 0; inst->setGetGpio_event(&(inst->gpio_data),LCD_HD44780_SET); + // Wait until display is ready: + WaitUntilReady(inst); + // Then set E pin high so its ready for next time: inst->gpio_data.e_pin = 1; inst->setGetGpio_event(&(inst->gpio_data),LCD_HD44780_SET); @@ -66,9 +94,9 @@ static void lcd_writeCmd_disp(cm_lcd_hd44780_t *inst, hd44780_cmd_t cmd, hd44780 }else{ // Write MSB first. - lcd_write_8bit(inst, (cmd >> 4) & 0xf, LCD_HD44780_INSTRUCTION); + lcd_write_8bit(inst, ((cmd >> 4) & 0x0F) << 4, LCD_HD44780_INSTRUCTION); // Write LSB last. - lcd_write_8bit(inst, cmd & 0xf, LCD_HD44780_INSTRUCTION); + lcd_write_8bit(inst, (cmd & 0x0F) << 4, LCD_HD44780_INSTRUCTION); } } @@ -94,7 +122,8 @@ void lcd_hd44780_initDisp(cm_lcd_hd44780_t *inst){ lcd_writeCmd_disp(inst, LCD_HD44780_ENTRY_MODE,LCD_HD44780_SET); - + // Init has been done, and we can now use Busy Flag polling instead of delay! + inst->isInit = 0x1; } diff --git a/lcd_hd44780.h b/lcd_hd44780.h index 02b83ae..f32f126 100644 --- a/lcd_hd44780.h +++ b/lcd_hd44780.h @@ -76,6 +76,8 @@ typedef struct{ wait_ms_Event_fpt wait_event; lcd_hd44780_bitmode bit_mode; hd44780_gpioset_t gpio_data; + // Flag for showing that init has been done.. After init im using polling Busy Flag instead of delay.. + uint8_t isInit; }cm_lcd_hd44780_t;