collected functions.. 8 bit version funcs can also be reused in 4 bit mode..

This commit is contained in:
2025-06-08 00:01:07 +02:00
parent cd15a9eddb
commit f55529eb52
2 changed files with 73 additions and 61 deletions

View File

@@ -1,7 +1,15 @@
#include "lcd_hd44780.h"
#include <stddef.h>
static int isStructOk(cm_lcd_hd44780_t *inst){
if(inst == NULL)return 0;
if(inst->setGetGpio_event == NULL) return 0;
if(inst->wait_event == NULL) return 0;
return 1;
}
void lcd_hd44780_writeToDisp(cm_lcd_hd44780_t *inst){
@@ -20,46 +28,69 @@ void lcd_hd44780_printf(lcd_hd44780_t *inst, char *string){
}*/
static void lcd_writeCmd_disp(cm_lcd_hd44780_t *inst, hd44780_cmd_t cmd, hd44780_setGet_t getSet){
static int lcd_write_8bit(cm_lcd_hd44780_t *inst, uint8_t dataOrCmd, hd44780_InstData_t mode){
inst->gpio_data.data_bus = cmd;
// Set cmd on databus
inst->gpio_data.data_bus = dataOrCmd;
inst->gpio_data.rw_pin = getSet;
// We want to set display
inst->gpio_data.rw_pin = LCD_HD44780_SET;
// We want to write an instruction.
inst->gpio_data.rs_pin = LCD_HD44780_INSTRUCTION;
inst->gpio_data.rs_pin = mode;
// E pin is sopposed to be zero when data is ready on port.
inst->gpio_data.e_pin = 0;
inst->setGetGpio_event(&(inst->gpio_data),getSet);
// Then Clock data in with E pin:
// E pin must be high when data is not ready yet.
inst->gpio_data.e_pin = 1;
inst->setGetGpio_event(&(inst->gpio_data),getSet);
inst->setGetGpio_event(&(inst->gpio_data),LCD_HD44780_SET);
// Then Clock data in with E pin:
// 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);
inst->setGetGpio_event(&(inst->gpio_data),getSet);
// 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);
return 0;
}
void lcd_hd44780_initDisp_8bit(cm_lcd_hd44780_t *inst){
static void lcd_writeCmd_disp(cm_lcd_hd44780_t *inst, hd44780_cmd_t cmd, hd44780_setGet_t getSet){
if(inst->bit_mode == LCD_HD44780_BITMODE_8BIT){
lcd_write_8bit(inst, cmd, LCD_HD44780_INSTRUCTION);
}else{
// Write MSB first.
lcd_write_8bit(inst, (cmd >> 4) & 0xf, LCD_HD44780_INSTRUCTION);
// Write LSB last.
lcd_write_8bit(inst, cmd & 0xf, LCD_HD44780_INSTRUCTION);
}
}
void lcd_hd44780_initDisp(cm_lcd_hd44780_t *inst){
// Make sure E pin is high when data isnt ready on BUS!
inst->gpio_data.e_pin = 1;
inst->setGetGpio_event(&(inst->gpio_data),LCD_HD44780_SET);
// Wait for more than 15ms after VCC rise..
// Delay(15)
inst->wait_event(15);
lcd_writeCmd_disp(inst, LCD_HD44780_FUNCTION_SET,LCD_HD44780_SET);
// Wait for 4.1ms..
// Delay(4)
inst->wait_event(4);
lcd_writeCmd_disp(inst, LCD_HD44780_DISP_ONOFF_CTL,LCD_HD44780_SET);
// delay 100us
inst->wait_event(1);
lcd_writeCmd_disp(inst, LCD_HD44780_ENTRY_MODE,LCD_HD44780_SET);
@@ -67,31 +98,18 @@ void lcd_hd44780_initDisp_8bit(cm_lcd_hd44780_t *inst){
}
void lcd_hd44780_initDisp_4bit(cm_lcd_hd44780_t *inst){
// Wait for more than 15ms after VCC rise..
// Delay(15)
lcd_writeCmd_disp(inst, LCD_HD44780_FUNCTION_SET >> 4,LCD_HD44780_SET);
// Delay(4)
lcd_writeCmd_disp(inst, LCD_HD44780_FUNCTION_SET >> 4,LCD_HD44780_SET);
// Delay(4)
lcd_writeCmd_disp(inst, LCD_HD44780_FUNCTION_SET >> 4,LCD_HD44780_SET);
}
void lcd_hd44780_regGpioEvt(cm_lcd_hd44780_t *inst, setGet_Gpio_Event_fpt getGpioEvt_fpt){
//if(inst == NULL)return;
if(inst == NULL)return;
if(getGpioEvt_fpt == NULL) return;
inst->setGetGpio_event = getGpioEvt_fpt;
}
void lcd_hd44780_regWaitEvt(cm_lcd_hd44780_t *inst, wait_ms_Event_fpt waitEvt_fpt){
//if(inst == NULL)return;
if(inst == NULL)return;
if(waitEvt_fpt == NULL) return;
inst->wait_event = waitEvt_fpt;
}
@@ -99,32 +117,10 @@ void lcd_hd44780_regWaitEvt(cm_lcd_hd44780_t *inst, wait_ms_Event_fpt waitEvt_fp
void lcd_hd44780_init(cm_lcd_hd44780_t *inst, lcd_hd44780_bitmode bit_mode){
//inst->gpio_data = 0x00;
if(!isStructOk(inst)) return;
inst->bit_mode = bit_mode;
/*
inst->gpio_data.data_bus = 0x0;
inst->gpio_data.e_pin = 0x0;
inst->gpio_data.rs_pin = 0x1;
inst->gpio_data.rw_pin = 0x0;
inst->setGetGpio_event(&(inst->gpio_data), 1);*/
//lcd_hd44780_initDisp(inst);
//lcd_writeCmd_disp(inst,LCD_HD44780_CLEAR_DISP,LCD_HD44780_SET);
//lcd_writeCmd_disp(inst,LCD_HD44780_DISP_ONOFF_CTL,LCD_HD44780_SET);
switch (inst->bit_mode) {
case LCD_BITMODE_8BIT:
lcd_hd44780_initDisp_8bit(inst);
break;
case LCD_BITMODE_4BIT:
break;
default:
break;
}
lcd_hd44780_initDisp(inst);
return;
}

View File

@@ -12,8 +12,8 @@
#define LCD_HD44780_READ 1
typedef enum {
LCD_BITMODE_8BIT,
LCD_BITMODE_4BIT,
LCD_HD44780_BITMODE_8BIT,
LCD_HD44780_BITMODE_4BIT,
}lcd_hd44780_bitmode;
@@ -40,9 +40,25 @@ typedef enum{
}hd44780_InstData_t;
typedef struct{
uint8_t DB0 : 1;
uint8_t DB1 : 1;
uint8_t DB2 : 1;
uint8_t DB3 : 1;
uint8_t DB4 : 1;
uint8_t DB5 : 1;
uint8_t DB6 : 1;
uint8_t DB7 : 1;
}hd44780_data_bits_t;
typedef struct {
union{
uint8_t data_bus : 8;
hd44780_data_bits_t data_bits;
};
hd44780_setGet_t rw_pin : 1;
uint8_t e_pin : 1;
hd44780_InstData_t rs_pin :1 ;