DogFlu66
Member
I found it interesting to have the possibility of controlling the classic 1602 LCD (two lines with 16 characters each) using just two wires via the I2C bus. I noticed that there are kits available that include the 1602 LCD along with an adapter board featuring an integrated circuit called the PCF8574. This chip is an 8-bit I/O expander that can function as both inputs and outputs, and communication with it is carried out via the I2C protocol.
By connecting this chip to the microcontroller through the SDA and SCL lines, along with the two power supply wires, the LCD can be fully controlled. To achieve this, I have developed a library that manages the 1602 LCD using a 4-bit data bus and 2 control bits. I then integrated it with a library I previously wrote to control the PCF8574, resulting in a complete solution to control the 1602 LCD via I2C communication.
It is essential for the microcontroller to have an I2C module and to use the previously developed library for handling it. I am providing all the necessary files for anyone interested in experimenting with it.
Initializes the LCD in 4-bit mode, no cursor, 5x8 dots.
Sends the necessary startup commands and prepares the LCD for operation.
LCD_DefChar(CharIndex As Byte, CharMask_7 As Byte, CharMask_6 As Byte, CharMask_5 As Byte, CharMask_4 As Byte, CharMask_3 As Byte, CharMask_2 As Byte, CharMask_1 As Byte, CharMask_0 As Byte)
Defines a custom character in CGRAM.
Allows the user to create a custom character by specifying 8 bytes of pixel data.
LCD_PosXY(x As Byte, y As Byte)
Positions the cursor at a given (x, y) coordinate.
Sets the position on the LCD screen based on row and column values.
LCD_PutC(char As Byte)
Displays a single character on the LCD.
Sends an ASCII character to the current cursor position.
LCD_CursorBlink(sBit As Bit)
Enables or disables the blinking cursor.
Controls the visibility of the blinking cursor based on the input bit.
LCD_Cursor(sBit As Bit)
Turns the LCD cursor on or off.
Enables or disables the display of the cursor.
LCD_BackLight(sBit As Bit)
Controls the LCD backlight.
Turns the LCD backlight on or off based on the given bit value.
LCD_Out(line As Byte, pos As Byte, char[20] As String)
Prints a string on the LCD at a specified line and position.
Outputs up to 20 characters starting at the specified position.
LCD_Line1Home()
Moves the cursor to the beginning of line 1.
Resets the cursor to the home position on the first line.
Function LCD_Line2Home()
Moves the cursor to the beginning of line 2.
Resets the cursor to the home position on the second line.
LCD_Line1Pos(x As Byte)
Sets the cursor position on line 1 to the specified column.
Moves the cursor to a specific position on the first line.
LCD_Line2Pos(x As Byte)
Sets the cursor position on line 2 to the specified column.
Moves the cursor to a specific position on the second line.
LCD_Line1Clear()
Clears the first line of the LCD.
Removes all characters from line 1.
LCD_Line2Clear()
Clears the second line of the LCD.
Removes all characters from line 2.
LCD_Clear()
Clears the entire LCD display.
Erases all displayed characters and resets the cursor to the home position.
By connecting this chip to the microcontroller through the SDA and SCL lines, along with the two power supply wires, the LCD can be fully controlled. To achieve this, I have developed a library that manages the 1602 LCD using a 4-bit data bus and 2 control bits. I then integrated it with a library I previously wrote to control the PCF8574, resulting in a complete solution to control the 1602 LCD via I2C communication.
It is essential for the microcontroller to have an I2C module and to use the previously developed library for handling it. I am providing all the necessary files for anyone interested in experimenting with it.
Functions Overview (Lcd1602_I2CLib.bas):
LCD_Init()Initializes the LCD in 4-bit mode, no cursor, 5x8 dots.
Sends the necessary startup commands and prepares the LCD for operation.
LCD_DefChar(CharIndex As Byte, CharMask_7 As Byte, CharMask_6 As Byte, CharMask_5 As Byte, CharMask_4 As Byte, CharMask_3 As Byte, CharMask_2 As Byte, CharMask_1 As Byte, CharMask_0 As Byte)
Defines a custom character in CGRAM.
Allows the user to create a custom character by specifying 8 bytes of pixel data.
LCD_PosXY(x As Byte, y As Byte)
Positions the cursor at a given (x, y) coordinate.
Sets the position on the LCD screen based on row and column values.
LCD_PutC(char As Byte)
Displays a single character on the LCD.
Sends an ASCII character to the current cursor position.
LCD_CursorBlink(sBit As Bit)
Enables or disables the blinking cursor.
Controls the visibility of the blinking cursor based on the input bit.
LCD_Cursor(sBit As Bit)
Turns the LCD cursor on or off.
Enables or disables the display of the cursor.
LCD_BackLight(sBit As Bit)
Controls the LCD backlight.
Turns the LCD backlight on or off based on the given bit value.
LCD_Out(line As Byte, pos As Byte, char[20] As String)
Prints a string on the LCD at a specified line and position.
Outputs up to 20 characters starting at the specified position.
LCD_Line1Home()
Moves the cursor to the beginning of line 1.
Resets the cursor to the home position on the first line.
Function LCD_Line2Home()
Moves the cursor to the beginning of line 2.
Resets the cursor to the home position on the second line.
LCD_Line1Pos(x As Byte)
Sets the cursor position on line 1 to the specified column.
Moves the cursor to a specific position on the first line.
LCD_Line2Pos(x As Byte)
Sets the cursor position on line 2 to the specified column.
Moves the cursor to a specific position on the second line.
LCD_Line1Clear()
Clears the first line of the LCD.
Removes all characters from line 1.
LCD_Line2Clear()
Clears the second line of the LCD.
Removes all characters from line 2.
LCD_Clear()
Clears the entire LCD display.
Erases all displayed characters and resets the cursor to the home position.