Skip to content

Commit

Permalink
Examples: added lcd_driver_interface example implementations and temp…
Browse files Browse the repository at this point in the history
…late
  • Loading branch information
niwciu committed Jan 15, 2024
1 parent 670970c commit eb6931a
Show file tree
Hide file tree
Showing 4 changed files with 623 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* @Author: lukasz.niewelt
* @Date: 2023-12-04 20:13:23
* @Last Modified by: lukasz.niewelt
* @Last Modified time: 2023-12-08 10:49:44
*/


#include "lcd_hd44780_interface.h"
#include "lcd_hd44780_config.h"
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
// clang-format off
/*********************************Define hardware connection in your project*******************************/
#define LCD_DATA_PORT PORTD
#define LCD_DATA_PORT_DIR DDRD
#define LCD_DATA_INPUT PIND

#define LCD_SIG_PORT PORTB
#define LCD_SIG_PORT_DIR DDRB

#define LCD_BCKL_PORT PORTB
#define LCD_BCKL_PORT_DIR DDRB

#define LCD_PIN_D7 (1 << PIND7)
#define LCD_PIN_D6 (1 << PIND6)
#define LCD_PIN_D5 (1 << PIND5)
#define LCD_PIN_D4 (1 << PIND4)
#define LCD_PIN_RS (1 << PINB0)
#define LCD_PIN_RW (1 << PINC1)
#define LCD_PIN_E (1 << PINB1)
#define LCD_BCKL_PIN (1 << PINB2)
#if USE_RW_PIN == ON
#define LCD_PIN_RW (1 << PINBx)
#endif
/****************************END OFF define hardware connection in your application***********************/

#define LCD_D4_MASK 0x01
#define LCD_D5_MASK 0x02
#define LCD_D6_MASK 0x04
#define LCD_D7_MASK 0x08
// clang-format off

static void init_LCD_data_and_SIG_pins(void);
static void init_LCD_DATA_PINS_as_outputs(void);
static void init_LCD_DATA_PINS_as_inputs(void);
static void set_LCD_DATA_PINS_state(uint8_t data);
static uint8_t get_LCD_DATA_PINS_state(void);
static void LCD_set_SIG(enum lcd_sig LCD_SIG);
static void LCD_reset_SIG(enum lcd_sig LCD_SIG);
static void wraper_delay_us(uint32_t delay_us);
static void init_LCD_SIGNAL_PINS_as_outputs(void);

/************LCD_IO_driver_interface implementation START**************/
static const struct LCD_IO_driver_interface_struct LCD_IO_driver = {
init_LCD_data_and_SIG_pins,
init_LCD_DATA_PINS_as_outputs,
init_LCD_DATA_PINS_as_inputs,
set_LCD_DATA_PINS_state,
get_LCD_DATA_PINS_state,
LCD_set_SIG,
LCD_reset_SIG,
wraper_delay_us,
};
const struct LCD_IO_driver_interface_struct *LCD_IO_driver_interface_get(void)
{
return &LCD_IO_driver;
}

/*************LCD_IO_driver_interface implementation END***************/

static void init_LCD_data_and_SIG_pins(void)
{
//set BCKL PIN as output
LCD_BCKL_PORT_DIR |= LCD_BCKL_PIN;
//disable LCD backlight
LCD_BCKL_PORT &= ~ LCD_BCKL_PIN;
init_LCD_DATA_PINS_as_outputs();
init_LCD_SIGNAL_PINS_as_outputs();
}
static void init_LCD_DATA_PINS_as_outputs(void)
{
//set pins as output
LCD_DATA_PORT_DIR |= (LCD_PIN_D4 | LCD_PIN_D5 | LCD_PIN_D6 | LCD_PIN_D7);
}
static void init_LCD_DATA_PINS_as_inputs(void)
{

//set pins as inputs
LCD_DATA_PORT_DIR &= ~(LCD_PIN_D4 | LCD_PIN_D5 | LCD_PIN_D6 | LCD_PIN_D7);
// enable pull-up on input pins
LCD_DATA_PORT |= (LCD_PIN_D4 | LCD_PIN_D5 | LCD_PIN_D6 | LCD_PIN_D7);
}

static void set_LCD_DATA_PINS_state(uint8_t data)
{
if ((data & LCD_D4_MASK))
LCD_DATA_PORT |= LCD_PIN_D4;
else
LCD_DATA_PORT &= ~LCD_PIN_D4;

if ((data & LCD_D5_MASK))
LCD_DATA_PORT |= LCD_PIN_D5;
else
LCD_DATA_PORT &= ~LCD_PIN_D5;

if ((data & LCD_D6_MASK))
LCD_DATA_PORT |= LCD_PIN_D6;
else
LCD_DATA_PORT &= ~LCD_PIN_D6;

if ((data & LCD_D7_MASK))
LCD_DATA_PORT |= LCD_PIN_D7;
else
LCD_DATA_PORT &= ~LCD_PIN_D7;
}

static uint8_t get_LCD_DATA_PINS_state(void)
{
uint8_t data = 0;
if (LCD_DATA_INPUT & LCD_PIN_D4)
data = LCD_D4_MASK;
if (LCD_DATA_INPUT & LCD_PIN_D5)
data |= LCD_D5_MASK;
if (LCD_DATA_INPUT & LCD_PIN_D6)
data |= LCD_D6_MASK;
if (LCD_DATA_INPUT & LCD_PIN_D7)
data |= LCD_D7_MASK;
return data;
}

static void LCD_set_SIG(enum lcd_sig LCD_SIG)
{
switch (LCD_SIG)
{
case LCD_RS:
LCD_SIG_PORT |= LCD_PIN_RS;
break;
case LCD_E:
LCD_SIG_PORT |= LCD_PIN_E;
break;
#if USE_RW_PIN == ON
case LCD_RW:
LCD_SIG_PORT |= LCD_PIN_RW;
break;
#endif
case LCD_BCKL:
LCD_SIG_PORT |= LCD_BCKL_PIN;
break;
default:
break;
}
}

static void LCD_reset_SIG(enum lcd_sig LCD_SIG)
{
switch (LCD_SIG)
{
case LCD_RS:
LCD_SIG_PORT &= ~LCD_PIN_RS;
break;
case LCD_E:
LCD_SIG_PORT &= ~LCD_PIN_E;
break;
#if USE_RW_PIN == ON
case LCD_RW:
LCD_SIG_PORT &= ~LCD_PIN_RW;
break;
#endif
case LCD_BCKL:
LCD_SIG_PORT &= ~LCD_BCKL_PIN;
break;
default:
break;
}
}

static void wraper_delay_us(uint32_t delay_us)
{
_delay_us((double)(delay_us));
}

static void init_LCD_SIGNAL_PINS_as_outputs(void)
{
#if USE_RW_PIN == ON
LCD_SIG_PORT_DIR |= (LCD_PIN_RS | LCD_PIN_RW | LCD_PIN_E);

#else
LCD_SIG_PORT_DIR |= (LCD_PIN_RS | LCD_PIN_E);
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* @Author: lukasz.niewelt
* @Date: 2023-12-07 15:51:41
* @Last Modified by: lukasz.niewelt
* @Last Modified time: 2023-12-08 00:34:48
*/
#include <stdio.h>
#include "lcd_hd44780_interface.h"


// clang-format off
#define ON 1
#define OFF 0

/*********************************Define hardware connection in your project*******************************/
// ToDo define here name of lcd PINS and it's hardware represetation
// #define LCD_D4 xx
// #define LCD_D5 xx
// etc
/****************************END OFF define hardware connection in your application***********************/

#define LCD_D4_MASK 0x01
#define LCD_D5_MASK 0x02
#define LCD_D6_MASK 0x04
#define LCD_D7_MASK 0x08
// clang-format on

static void init_LCD_data_and_SIG_pins(void);
static void set_LCD_DATA_PINS_as_outputs(void);
static void set_LCD_DATA_PINS_as_inputs(void);
static void set_LCD_DATA_PINS_state(uint8_t data);
static uint8_t get_LCD_DATA_PINS_state(void);
static void LCD_set_SIG(enum lcd_sig LCD_SIG);
static void LCD_reset_SIG(enum lcd_sig LCD_SIG);
static void _delay_us(uint32_t delay_us);
static void init_LCD_SIGNAL_PINS_as_outputs(void);
static void init_LCD_BCKL_PIN_as_output(void);

/************LCD_IO_driver_interface implementation START**************/
static const struct LCD_IO_driver_interface_struct LCD_IO_driver = {
init_LCD_data_and_SIG_pins,
set_LCD_DATA_PINS_as_outputs,
set_LCD_DATA_PINS_as_inputs,
set_LCD_DATA_PINS_state,
get_LCD_DATA_PINS_state,
LCD_set_SIG,
LCD_reset_SIG,
_delay_us,
};
const struct LCD_IO_driver_interface_struct *LCD_IO_driver_interface_get(void)
{
return &LCD_IO_driver;
}

/*************LCD_IO_driver_interface implementation END***************/

static void init_LCD_data_and_SIG_pins(void)
{
// init all GPIOs

}

static void set_LCD_DATA_PINS_as_outputs(void)
{
// configure LCD_D4, LCD_D5, LCD_D6, LCD_D7 as output

}
static void set_LCD_DATA_PINS_as_inputs(void)
{
// configure LCD_D4, LCD_D5, LCD_D6, LCD_D7 as input
}

static void set_LCD_DATA_PINS_state(uint8_t data)
{
if ((data & LCD_D4_MASK))
//set LCD_D4 to HIGW
else
//Set LCD_D4 to LOW

if ((data & LCD_D5_MASK))
//set LCD_D5 to HIGH
else
//Set LCD_D5 to LOW

if ((data & LCD_D6_MASK))
// Set LCD_D6 to HIGH
else
LCD_D6_PORT->ODR &= ~LCD_D6_OUT_PIN;
//Set LCD_D6 to LOW

if ((data & LCD_D7_MASK))
//Set LCD_D7 to HIGH
else
//Set LCD_D7 to LOW
}

static uint8_t get_LCD_DATA_PINS_state(void)
{
uint8_t data = 0;
// if (LCD_D4 input port is set to HIGH)
data = LCD_D4_MASK;
// if (LCD_D5 input port is set to HIGH)
data |= LCD_D5_MASK;
// if (LCD_D6 input port is set to HIGH)
data |= LCD_D6_MASK;
// if (LCD_D7 input port is set to HIGH)
data |= LCD_D7_MASK;
return data;
}

static void LCD_set_SIG(enum lcd_sig LCD_SIG)
{
switch (LCD_SIG)
{
case LCD_RS:
// Set LCD_RS to High
break;
case LCD_E:
// Set LCD_E to High
break;
#if USE_RW_PIN == ON
case LCD_RW:
// Set LCD_RW to High
break;
#endif
case LCD_BCKL:
// Set LCD_BCKL to High
break;
default:
break;
}
}

static void LCD_reset_SIG(enum lcd_sig LCD_SIG)
{
switch (LCD_SIG)
{
case LCD_RS:
// Set LCD_RS to LOW
break;
case LCD_E:
// Set LCD_E to LOW
break;
#if USE_RW_PIN == 1
case LCD_RW:
// Set LCD_RW to LOW
break;
#endif
case LCD_BCKL:
// Set LCD_BCKL to LOW
break;
default:
break;
}
}

static void wraper_delay_us(uint32_t delay_us)
{
// ToDo update wraper
}

static void init_LCD_SIGNAL_PINS_as_outputs(void)
{
// Initialize LCD_E LCD_RS and optionl LCD_RW GPIOs as outputs
}

static void init_LCD_BCKL_PIN_as_output(void)
{
// Initialize LCD_BCKL GPIO as outputs
}
Loading

0 comments on commit eb6931a

Please sign in to comment.