Pic16f877a LCD Mplab Code (8-bit mode)

 Pic16f877a LCD Code




#include <xc.h>


#define _XTAL_FREQ 4000000 // Define crystal frequency


// LCD connections

#define RS RB0

#define EN RB1

#define DATA_PORT PORTD


void lcd_command(unsigned char cmd) {

    RS = 0;            // Select command register

    DATA_PORT = cmd;    // Send command to data port

    EN = 1;            // Enable pulse

    __delay_ms(1);

    EN = 0;

    __delay_ms(3);      // Command execution time

}


void lcd_data(unsigned char data) {

    RS = 1;            // Select data register

    DATA_PORT = data;   // Send data to data port

    EN = 1;            // Enable pulse

    __delay_ms(1);

    EN = 0;

    __delay_ms(3);      // Data write time

}


void lcd_init() {

    TRISD = 0x00;       // Set PORTD as output

    TRISB = 0x00;       // Set PORTB as output

    

    lcd_command(0x38);  // 8-bit mode, 2 lines, 5x8 dots

    lcd_command(0x0C);  // Display ON, Cursor OFF

    lcd_command(0x01);  // Clear display

    __delay_ms(2);

    lcd_command(0x06);  // Entry mode, auto-increment cursor

    lcd_command(0x80);  // Set cursor to home position

}


void lcd_print(const char *str) {

    while (*str) {

        lcd_data(*str++);

    }

}


void main() {

    lcd_init();                     // Initialize the LCD

    lcd_print("Hello, World!");     // Print message on LCD

    

    while (1) {

        // Infinite loop

    }

}


Comments

Popular posts from this blog

PIC16F877A - UART Registers

Introduction to SPI Communication on the PIC16F877A

Introduction to UART Communication