Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ code for LCD display of A-Z, a-z, and 0-9 characters
- const int RS = 13;
- const int E = 12;
- const int DB4 = 11;
- const int DB5 = 10;
- const int DB6 = 9;
- const int DB7 = 8;
- void initlcd(); // Function prototypes
- void writeAlpha(char ch);
- void sent(int rs, int db4, int db5, int db6, int db7);
- void setup() {
- pinMode(RS, OUTPUT);
- pinMode(E, OUTPUT);
- pinMode(DB4, OUTPUT);
- pinMode(DB5, OUTPUT);
- pinMode(DB6, OUTPUT);
- pinMode(DB7, OUTPUT);
- // Initialize the LCD
- initlcd();
- }
- void loop() {
- // Test by printing a sample string with uppercase, lowercase, and numeric characters
- const char *message = "Hello World 123 ABC xyz";
- for (int i = 0; message[i] != '\0'; i++) {
- writeAlpha(message[i]);
- }
- delay(2000); // Delay before repeating the message
- }
- void initlcd() {
- // LCD initialization sequence
- sent(0, 0, 1, 0, 0); // Function set
- sent(0, 0, 0, 0, 0); sent(0, 1, 0, 0, 0); // 4-bit mode
- sent(0, 0, 0, 0, 0); sent(0, 0, 1, 0, 0); // Display on
- sent(0, 0, 0, 0, 0); sent(0, 0, 0, 1, 1); // Clear display
- }
- void writeAlpha(char ch) {
- switch (ch) {
- // Uppercase letters
- case 'A': sent(1, 0, 0, 1, 0); sent(1, 1, 0, 0, 0); break;
- case 'B': sent(1, 0, 0, 1, 0); sent(1, 0, 1, 0, 0); break;
- case 'C': sent(1, 0, 0, 1, 0); sent(1, 1, 1, 0, 0); break;
- case 'D': sent(1, 0, 0, 1, 1); sent(1, 0, 0, 0, 0); break;
- case 'E': sent(1, 0, 0, 1, 1); sent(1, 0, 1, 0, 0); break;
- case 'F': sent(1, 0, 0, 1, 1); sent(1, 1, 1, 0, 0); break;
- case 'G': sent(1, 0, 1, 0, 0); sent(1, 0, 0, 0, 0); break;
- case 'H': sent(1, 0, 1, 0, 0); sent(1, 0, 1, 0, 0); break;
- case 'I': sent(1, 0, 1, 0, 0); sent(1, 1, 1, 0, 0); break;
- case 'J': sent(1, 0, 1, 0, 1); sent(1, 0, 0, 0, 0); break;
- case 'K': sent(1, 0, 1, 0, 1); sent(1, 0, 1, 0, 0); break;
- case 'L': sent(1, 0, 1, 0, 1); sent(1, 1, 1, 0, 0); break;
- case 'M': sent(1, 0, 1, 1, 0); sent(1, 0, 0, 0, 0); break;
- case 'N': sent(1, 0, 1, 1, 0); sent(1, 0, 1, 0, 0); break;
- case 'O': sent(1, 0, 1, 1, 0); sent(1, 1, 1, 0, 0); break;
- case 'P': sent(1, 0, 1, 1, 1); sent(1, 0, 0, 0, 0); break;
- case 'Q': sent(1, 0, 1, 1, 1); sent(1, 0, 1, 0, 0); break;
- case 'R': sent(1, 0, 1, 1, 1); sent(1, 1, 1, 0, 0); break;
- case 'S': sent(1, 1, 0, 0, 0); sent(1, 0, 0, 0, 0); break;
- case 'T': sent(1, 1, 0, 0, 0); sent(1, 0, 1, 0, 0); break;
- case 'U': sent(1, 1, 0, 0, 1); sent(1, 0, 0, 0, 0); break;
- case 'V': sent(1, 1, 0, 0, 1); sent(1, 0, 1, 0, 0); break;
- case 'W': sent(1, 1, 0, 1, 0); sent(1, 0, 0, 0, 0); break;
- case 'X': sent(1, 1, 0, 1, 0); sent(1, 0, 1, 0, 0); break;
- case 'Y': sent(1, 1, 0, 1, 1); sent(1, 0, 0, 0, 0); break;
- case 'Z': sent(1, 1, 0, 1, 1); sent(1, 0, 1, 0, 0); break;
- // Lowercase letters
- case 'a': sent(1, 0, 1, 1, 0); sent(1, 1, 0, 0, 1); break;
- case 'b': sent(1, 0, 1, 1, 0); sent(1, 0, 1, 0, 0); break;
- case 'c': sent(1, 0, 1, 1, 0); sent(1, 1, 1, 0, 0); break;
- case 'd': sent(1, 0, 1, 1, 1); sent(1, 0, 0, 0, 0); break;
- case 'e': sent(1, 0, 1, 1, 1); sent(1, 0, 1, 0, 0); break;
- case 'f': sent(1, 0, 1, 1, 1); sent(1, 1, 1, 0, 0); break;
- case 'g': sent(1, 1, 0, 0, 0); sent(1, 0, 0, 0, 1); break;
- case 'h': sent(1, 1, 0, 0, 0); sent(1, 0, 1, 0, 0); break;
- case 'i': sent(1, 1, 0, 0, 0); sent(1, 1, 1, 0, 0); break;
- case 'j': sent(1, 1, 0, 0, 1); sent(1, 0, 0, 0, 1); break;
- case 'k': sent(1, 1, 0, 0, 1); sent(1, 0, 1, 0, 0); break;
- case 'l': sent(1, 1, 0, 0, 1); sent(1, 1, 1, 0, 0); break;
- case 'm': sent(1, 1, 0, 1, 0); sent(1, 0, 0, 0, 1); break;
- case 'n': sent(1, 1, 0, 1, 0); sent(1, 0, 1, 0, 0); break;
- case 'o': sent(1, 1, 0, 1, 0); sent(1, 1, 1, 0, 0); break;
- case 'p': sent(1, 1, 0, 1, 1); sent(1, 0, 0, 0, 1); break;
- case 'q': sent(1, 1, 0, 1, 1); sent(1, 0, 1, 0, 0); break;
- case 'r': sent(1, 1, 0, 1, 1); sent(1, 1, 1, 0, 0); break;
- case 's': sent(1, 1, 1, 0, 0); sent(1, 0, 0, 0, 1); break;
- case 't': sent(1, 1, 1, 0, 0); sent(1, 0, 1, 0, 0); break;
- case 'u': sent(1, 1, 1, 0, 1); sent(1, 0, 0, 0, 1); break;
- case 'v': sent(1, 1, 1, 0, 1); sent(1, 0, 1, 0, 0); break;
- case 'w': sent(1, 1, 1, 1, 0); sent(1, 0, 0, 0, 1); break;
- case 'x': sent(1, 1, 1, 1, 0); sent(1, 0, 1, 0, 0); break;
- case 'y': sent(1, 1, 1, 1, 1); sent(1, 0, 0, 0, 1); break;
- case 'z': sent(1, 1, 1, 1, 1); sent(1, 0, 1, 0, 0); break;
- // Numbers
- case '0': sent(1, 0, 1, 1, 1); sent(1, 0, 0, 0, 0); break;
- case '1': sent(1, 0, 1, 1, 1); sent(1, 0, 0, 0, 1); break;
- case '2': sent(1, 0, 1, 1, 1); sent(1, 0, 0, 1, 0); break;
- case '3': sent(1, 0, 1, 1, 1); sent(1, 0, 0, 1, 1); break;
- case '4': sent(1, 0, 1, 1, 1); sent(1, 0, 1, 0, 0); break;
- case '5': sent(1, 0, 1, 1, 1); sent(1, 0, 1, 0, 1); break;
- case '6': sent(1, 0, 1, 1, 1); sent(1, 0, 1, 1, 0); break;
- case '7': sent(1, 0, 1, 1, 1); sent(1, 0, 1, 1, 1); break;
- case '8': sent(1, 0, 1, 1, 1); sent(1, 1, 0, 0, 0); break;
- case '9': sent(1, 0, 1, 1, 1); sent(1, 1, 0, 0, 1); break;
- // Space
- case ' ': sent(1, 0, 1, 0, 0); sent(1, 0, 0, 0, 0); break;
- // Default case to handle unknown characters
- default: sent(1, 0, 1, 0, 0); sent(1, 0, 0, 0, 0); break; // Space
- }
- }
- void sent(int rs, int db4, int db5, int db6, int db7) {
- // Set RS pin (1 for data, 0 for command)
- digitalWrite(RS, rs);
- // Set data pins to send 4-bit data
- digitalWrite(DB4, db4);
- digitalWrite(DB5, db5);
- digitalWrite(DB6, db6);
- digitalWrite(DB7, db7);
- // Pulse the enable pin
- digitalWrite(E, LOW);
- delayMicroseconds(1);
- digitalWrite(E, HIGH);
- delayMicroseconds(1);
- digitalWrite(E, LOW);
- delay(10); // Small delay to let LCD process
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement