366
/* File: main.c * Author: Marwen Maghrebi * Description: Advanced LCD Interface for PIC16F877A Using PCF8574 * This code demonstrates an enhanced interface for LCD (Liquid Crystal Display) with a PIC microcontroller, specifically the PIC16F877A, *utilizing the PCF8574 I2C I/O expander. It initializes the LCD, clears the screen, and prints messages. Additionally, it provides functions *to control the cursor position, send commands or data to the LCD, and manage the backlight. */ // Include necessary header files #include <xc.h> // Define constants #define _XTAL_FREQ 16000000 #define I2C_BaudRate 100000 #define SCL_D TRISC3 #define SDA_D TRISC4 #define LCD_BACKLIGHT 0x08 #define LCD_NOBACKLIGHT 0x00 #define LCD_FIRST_ROW 0x80 #define LCD_SECOND_ROW 0xC0 #define LCD_THIRD_ROW 0x94 #define LCD_FOURTH_ROW 0xD4 #define LCD_CLEAR 0x01 #define LCD_RETURN_HOME 0x02 #define LCD_ENTRY_MODE_SET 0x04 #define LCD_CURSOR_OFF 0x0C #define LCD_UNDERLINE_ON 0x0E #define LCD_BLINK_CURSOR_ON 0x0F #define LCD_MOVE_CURSOR_LEFT 0x10 #define LCD_MOVE_CURSOR_RIGHT 0x14 #define LCD_TURN_ON 0x0C #define LCD_TURN_OFF 0x08 #define LCD_SHIFT_LEFT 0x18 #define LCD_SHIFT_RIGHT 0x1E #define LCD_TYPE 2 // 0 -> 5x7 | 1 -> 5x10 | 2 -> 2 lines // Function prototypes void main(void); void I2C_Master_Init(void); void I2C_Master_Wait(void); void I2C_Master_Start(void); void I2C_Master_RepeatedStart(void); void I2C_Master_Stop(void); void I2C_ACK(void); void I2C_NACK(void); unsigned char I2C_Master_Write(unsigned char data); unsigned char I2C_Read_Byte(void);