Hello Everyone,
I am trying to display some text data on 16x2 alphanumeric lcd display connected through gpio pins of beagle bone black board but the lcd initially displayed the text message in the code for 12 times of execution and later at final time of execution, lcd is simply giving backlight but not text data after thorough debugging of gpio pin mux configurations and pin states. Here is the code for 16x2 lcd:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define LCD_RS â66â // Pin P8_7
#define LCD_EN â67â // Pin P8_8
#define LCD_D4 â69â // Pin P8_9
#define LCD_D5 â68â // Pin P8_10
#define LCD_D6 â45â // Pin P8_11
#define LCD_D7 â44â // Pin P8_12
#define LCD_BACKLIGHT â47â // Pin P8_15
// Function Prototypes
void gpio_export(const char *pin);
void gpio_unexport(const char *pin);
void gpio_direction(const char *pin, const char *dir);
void gpio_write(const char *pin, int value);
void lcd_init();
void lcd_clear();
void lcd_send_command(int command);
void lcd_send_data(int data);
void lcd_message(const char *message);
void lcd_set_backlight(int state);
int main() {
// Initialize GPIO and LCD
lcd_init();
// Display a message on the LCD
lcd_message("Hello, BBB!\n16x2 LCD Demo");
sleep(10);
// Clear the display and show another message
lcd_clear();
lcd_message("LCD Test Done!");
sleep(10);
// Turn off the backlight
lcd_set_backlight(0);
//Unexport pins to start fresh
gpio_unexport(LCD_RS);
gpio_unexport(LCD_EN);
gpio_unexport(LCD_D4);
gpio_unexport(LCD_D5);
gpio_unexport(LCD_D6);
gpio_unexport(LCD_D7);
gpio_unexport(LCD_BACKLIGHT);
return 0;
}
// Function to export GPIO pin
void gpio_export(const char *pin) {
int fd = open(â/sys/class/gpio/exportâ, O_WRONLY);
if (fd == -1) {
perror(âError exporting GPIOâ);
return;
}
write(fd, pin, strlen(pin));
close(fd);
usleep(100000);
}
// Function to set GPIO direction
void gpio_direction(const char *pin, const char *dir) {
char path[35];
sprintf(path, â/sys/class/gpio/gpio%s/directionâ, pin);
int fd = open(path, O_WRONLY);
if (fd == -1) {
perror(âError setting GPIO directionâ);
return;
}
write(fd, dir, strlen(dir));
close(fd);
usleep(100000);
}
// Function to write GPIO value
void gpio_write(const char *pin, int value) {
char path[35];
sprintf(path, â/sys/class/gpio/gpio%s/valueâ, pin);
int fd = open(path, O_WRONLY);
if (fd == -1) {
perror(âError writing GPIO valueâ);
return;
}
if (value)
{
if(write(fd, â1â, 1)==-1)
perror(âError writing 1 to GPIOâ);
}
else
{
if(write(fd, â0â, 1)==-1)
perror(âError writing 0 to GPIOâ);
}
close(fd);
}
// Initialize the LCD
void lcd_init()
{
// Export pins and set directions
gpio_export(LCD_RS); gpio_direction(LCD_RS, âoutâ);
gpio_export(LCD_EN); gpio_direction(LCD_EN, âoutâ);
gpio_export(LCD_D4); gpio_direction(LCD_D4, âoutâ);
gpio_export(LCD_D5); gpio_direction(LCD_D5, âoutâ);
gpio_export(LCD_D6); gpio_direction(LCD_D6, âoutâ);
gpio_export(LCD_D7); gpio_direction(LCD_D7, âoutâ);
gpio_export(LCD_BACKLIGHT); gpio_direction(LCD_BACKLIGHT, âoutâ);
// Initialize LCD
gpio_write(LCD_BACKLIGHT, 1); // Turn on backlight
usleep(15000); // Wait for power up
lcd_send_command(0x03); // Initialization sequence
lcd_send_command(0x02); // 4-bit mode
lcd_send_command(0x28); // 2 lines, 5x8 font
lcd_send_command(0x0C); // Display on, cursor off
lcd_clear();
}
// Clear LCD
void lcd_clear() {
lcd_send_command(0x01);
usleep(2000);
}
// Send command to LCD
void lcd_send_command(int command) {
gpio_write(LCD_RS, 0);
// Send upper nibble
gpio_write(LCD_D4, (command >> 4) & 0x01);
gpio_write(LCD_D5, (command >> 4) & 0x02);
gpio_write(LCD_D6, (command >> 4) & 0x04);
gpio_write(LCD_D7, (command >> 4) & 0x08);
gpio_write(LCD_EN, 1);
usleep(1);
gpio_write(LCD_EN, 0);
usleep(1);
// Send lower nibble
gpio_write(LCD_D4, command & 0x01);
gpio_write(LCD_D5, command & 0x02);
gpio_write(LCD_D6, command & 0x04);
gpio_write(LCD_D7, command & 0x08);
gpio_write(LCD_EN, 1);
usleep(1);
gpio_write(LCD_EN, 0);
usleep(40);
}
// Send data to LCD
void lcd_send_data(int data) {
gpio_write(LCD_RS, 1);
// Send upper nibble
gpio_write(LCD_D4, (data >> 4) & 0x01);
gpio_write(LCD_D5, (data >> 4) & 0x02);
gpio_write(LCD_D6, (data >> 4) & 0x04);
gpio_write(LCD_D7, (data >> 4) & 0x08);
gpio_write(LCD_EN, 1);
usleep(1);
gpio_write(LCD_EN, 0);
usleep(1);
// Send lower nibble
gpio_write(LCD_D4, data & 0x01);
gpio_write(LCD_D5, data & 0x02);
gpio_write(LCD_D6, data & 0x04);
gpio_write(LCD_D7, data & 0x08);
gpio_write(LCD_EN, 1);
usleep(1);
gpio_write(LCD_EN, 0);
usleep(40);
}
// Display message on LCD
void lcd_message(const char *message) {
for (int i = 0; i < strlen(message); i++) {
if (message[i] == â\nâ) {
lcd_send_command(0xC0); // Move to second line
} else {
lcd_send_data(message[i]);
}
}
}
// Set backlight on or off
void lcd_set_backlight(int state) {
gpio_write(LCD_BACKLIGHT, state);
}
void gpio_unexport(const char *pin)
{
int fd = open(â/sys/class/gpio/unexportâ,O_WRONLY);
if (fd == -1)
{
perror(âError unexporting GPIOâ);
return;
}
write(fd, pin, strlen(pin));
close(fd);
}
I request everyone to please help me to resolve the code issue and support me to get the output.
Thanks and regards,
Imran Pathan.