Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update on your sketch #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 29 additions & 161 deletions Tutorial 42 - LCD16x2 I2C/lcd16x2_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* lcd16x2_i2c.h
*
* Created on: Mar 28, 2020
* modified on : April 4 2022 by Zorrovicky
* Author: Mohamed Yaqoob
*/

Expand Down Expand Up @@ -50,7 +51,7 @@ void lcd16x2_i2c_cursorShow(bool state);
void lcd16x2_i2c_clear(void);

/**
* @brief Display ON/OFF, to hide all characters, but not cl/*
* @brief Display ON/OFF, to hide all characters, but not clear
* lcd16x2_i2c.c
*
* Created on: Mar 28, 2020
Expand Down Expand Up @@ -96,216 +97,83 @@ void lcd16x2_i2c_clear(void);

/* Library variables */
static I2C_HandleTypeDef* lcd16x2_i2cHandle;
static uint8_t LCD_I2C_SLAVE_ADDRESS=0;
//static uint8_t LCD_I2C_SLAVE_ADDRESS=0;
#define LCD_I2C_SLAVE_ADDRESS_0 0x4E
#define LCD_I2C_SLAVE_ADDRESS_1 0x7E
#define LCD_I2C_SLAVE_ADDRESS_1 0x27

/* Private functions */
static void lcd16x2_i2c_sendCommand(uint8_t command)
{
const uint8_t command_0_3 = (0xF0 & (command<<4));
const uint8_t command_4_7 = (0xF0 & command);
uint8_t i2cData[4] =
{
command_4_7 | LCD_EN | LCD_BK_LIGHT,
command_4_7 | LCD_BK_LIGHT,
command_0_3 | LCD_EN | LCD_BK_LIGHT,
command_0_3 | LCD_BK_LIGHT,
};
HAL_I2C_Master_Transmit(lcd16x2_i2cHandle, LCD_I2C_SLAVE_ADDRESS, i2cData, 4, 200);
}

static void lcd16x2_i2c_sendData(uint8_t data)
{
const uint8_t data_0_3 = (0xF0 & (data<<4));
const uint8_t data_4_7 = (0xF0 & data);
uint8_t i2cData[4] =
{
data_4_7 | LCD_EN | LCD_BK_LIGHT | LCD_RS,
data_4_7 | LCD_BK_LIGHT | LCD_RS,
data_0_3 | LCD_EN | LCD_BK_LIGHT | LCD_RS,
data_0_3 | LCD_BK_LIGHT | LCD_RS,
};
HAL_I2C_Master_Transmit(lcd16x2_i2cHandle, LCD_I2C_SLAVE_ADDRESS, i2cData, 4, 200);
}
static void lcd16x2_i2c_sendCommand(uint8_t command);


static void lcd16x2_i2c_sendData(uint8_t data);



/**
* @brief Initialise LCD16x2
* @param[in] *pI2cHandle - pointer to HAL I2C handle
*/
bool lcd16x2_i2c_init(I2C_HandleTypeDef *pI2cHandle)
{
HAL_Delay(50);
lcd16x2_i2cHandle = pI2cHandle;
if(HAL_I2C_IsDeviceReady(lcd16x2_i2cHandle, LCD_I2C_SLAVE_ADDRESS_0, 5, 500) != HAL_OK)
{
if(HAL_I2C_IsDeviceReady(lcd16x2_i2cHandle, LCD_I2C_SLAVE_ADDRESS_1, 5, 500) != HAL_OK)
{
return false;
}
else
{
LCD_I2C_SLAVE_ADDRESS = LCD_I2C_SLAVE_ADDRESS_1;
}
}
else
{
LCD_I2C_SLAVE_ADDRESS = LCD_I2C_SLAVE_ADDRESS_0;
}
//Initialise LCD for 4-bit operation
//1. Wait at least 15ms
HAL_Delay(45);
//2. Attentions sequence
lcd16x2_i2c_sendCommand(0x30);
HAL_Delay(5);
lcd16x2_i2c_sendCommand(0x30);
HAL_Delay(1);
lcd16x2_i2c_sendCommand(0x30);
HAL_Delay(8);
lcd16x2_i2c_sendCommand(0x20);
HAL_Delay(8);

lcd16x2_i2c_sendCommand(LCD_FUNCTIONSET | LCD_FUNCTION_N);
HAL_Delay(1);
lcd16x2_i2c_sendCommand(LCD_DISPLAYCONTROL);
HAL_Delay(1);
lcd16x2_i2c_sendCommand(LCD_CLEARDISPLAY);
HAL_Delay(3);
lcd16x2_i2c_sendCommand(0x04 | LCD_ENTRY_ID);
HAL_Delay(1);
lcd16x2_i2c_sendCommand(LCD_DISPLAYCONTROL | LCD_DISPLAY_D);
HAL_Delay(3);

return true;
}
bool lcd16x2_i2c_init(I2C_HandleTypeDef *pI2cHandle);


/**
* @brief Set cursor position
* @param[in] row - 0 or 1 for line1 or line2
* @param[in] col - 0 - 15 (16 columns LCD)
*/
void lcd16x2_i2c_setCursor(uint8_t row, uint8_t col)
{
uint8_t maskData;
maskData = (col)&0x0F;
if(row==0)
{
maskData |= (0x80);
lcd16x2_i2c_sendCommand(maskData);
}
else
{
maskData |= (0xc0);
lcd16x2_i2c_sendCommand(maskData);
}
}
void lcd16x2_i2c_setCursor(uint8_t row, uint8_t col);

/**
* @brief Move to beginning of 1st line
*/
void lcd16x2_i2c_1stLine(void)
{
lcd16x2_i2c_setCursor(0,0);
}
void lcd16x2_i2c_1stLine(void);

/**
* @brief Move to beginning of 2nd line
*/
void lcd16x2_i2c_2ndLine(void)
{
lcd16x2_i2c_setCursor(1,0);
}
void lcd16x2_i2c_2ndLine(void);

/**
* @brief Select LCD Number of lines mode
*/
void lcd16x2_i2c_TwoLines(void)
{
lcd16x2_i2c_sendCommand(LCD_FUNCTIONSET | LCD_FUNCTION_N);
}
void lcd16x2_i2c_OneLine(void)
{
lcd16x2_i2c_sendCommand(LCD_FUNCTIONSET);
}
void lcd16x2_i2c_TwoLines(void);

void lcd16x2_i2c_OneLine(void);

/**
* @brief Cursor ON/OFF
*/
void lcd16x2_i2c_cursorShow(bool state)
{
if(state)
{
lcd16x2_i2c_sendCommand(LCD_DISPLAYCONTROL | LCD_DISPLAY_B | LCD_DISPLAY_C | LCD_DISPLAY_D);
}
else
{
lcd16x2_i2c_sendCommand(LCD_DISPLAYCONTROL | LCD_DISPLAY_D);
}
}
void lcd16x2_i2c_cursorShow(bool state);

/**
* @brief Display clear
*/
void lcd16x2_i2c_clear(void)
{
lcd16x2_i2c_sendCommand(LCD_CLEARDISPLAY);
HAL_Delay(3);
}
void lcd16x2_i2c_clear(void);


/**
* @brief Display ON/OFF, to hide all characters, but not clear
*/
void lcd16x2_i2c_display(bool state)
{
if(state)
{
lcd16x2_i2c_sendCommand(LCD_DISPLAYCONTROL | LCD_DISPLAY_B | LCD_DISPLAY_C | LCD_DISPLAY_D);
}
else
{
lcd16x2_i2c_sendCommand(LCD_DISPLAYCONTROL | LCD_DISPLAY_B | LCD_DISPLAY_C);
}
}
void lcd16x2_i2c_display(bool state);


/**
* @brief Shift content to right
*/
void lcd16x2_i2c_shiftRight(uint8_t offset)
{
for(uint8_t i=0; i<offset;i++)
{
lcd16x2_i2c_sendCommand(0x1c);
}
}
void lcd16x2_i2c_shiftRight(uint8_t offset);

/**
* @brief Shift content to left
*/
void lcd16x2_i2c_shiftLeft(uint8_t offset)
{
for(uint8_t i=0; i<offset;i++)
{
lcd16x2_i2c_sendCommand(0x18);
}
}
void lcd16x2_i2c_shiftLeft(uint8_t offset);


/**
* @brief Print to display
*/
void lcd16x2_i2c_printf(const char* str, ...)
{
char stringArray[20];
va_list args;
va_start(args, str);
vsprintf(stringArray, str, args);
va_end(args);
for(uint8_t i=0; i<strlen(stringArray) && i<16; i++)
{
lcd16x2_i2c_sendData((uint8_t)stringArray[i]);
}
}
ear
*/
void lcd16x2_i2c_printf(const char* str, ...);


void lcd16x2_i2c_display(bool state);

/**
Expand Down