I wrote a tutorial few days ago explaining I2C and PIC16 and today I am going to show you an application of it. So today I am going to interface LCD 16×2 via I2C with PIC16.
LCD 16×2 have around 16 pins which needs to be connected to microcontroller in order to make it work. But by using an I2C extender, we only needs to connect 2 pins (SDA and SCL) to the microcontroller. This saves other pins for other functions and also reduce the wiring.
The I2C extender I am going to use is PCF8574 with the I2C address 0x4E and it is already attached to the LCD I have. I am using PIC16F877A and the concept will be pretty much same for other PIC microcontrollers.
First of all let’s see the connections between the PCF8574 and the LCD.
As shown in the figure, connect the first pin of the PCF8574 to the VSS of the LCD, 2nd pin to the VCC and so on. The input pins of the PCF8574 i.e. SCL and SDA should be connected with the SCL and SDA of the microcontroller.
Check out the connections in the next TAB.
Some Insight into the CODE
As I mentioned I am going to use the same I2C functions as discussed in the previous tutorial with additional few. Check out that tutorial HERE. These functions are as follows:-
- I2C_wait (); wait for the bits in the status register to clear before starting another transaction
- I2C_start (); Starts the I2C
- I2C_stop (); Stops the I2C
- I2C_write(); writes the data or command to the slave
- I2C_init (uint32_t clock); initialise I2C with the given clock speed
Other than these, I am going to write some new functions for the LCD purpose
lcd_send_cmd sends the command to the LCD
lcd_send_data sends the data to the LCD
lcd_send_string sends the string of data
lcd_init initialise the Display
Now we just need to initialise I2C and Display and than start sending the data
You can buy me a
coffee sensor 🙂
download the CODE below