Other Parts Discussed in Thread: PCF8574, C2000WARE
Tool/software:
I am trying to interface an I2C LCD with the TI launch pad. Several attempts were made using examples but nothing seems to be working. Please see my code and guide me. I am a beginner in this domain. I couldn't even do LCD back light off and on.
#include "driverlib.h"
#include "device.h"
#include "i2c.h"
#include "i2cLib_FIFO_polling.h"
// LCD and I2C definitions
#define SLAVE_ADDR 0x27 // Common address for PCF8574 I2C LCD backpack
#define LCD_BACKLIGHT 0x08 // Backlight control bit
#define ENABLE 0x04 // Enable bit
#define RS_DATA 0x01 // Register select bit for data
#define RS_COMMAND 0x00 // Register select bit for command
// Global variables
uint16_t AvailableI2C_slaves[20];
uint16_t status;
// Function prototypes
void I2C_GPIO_init(void);
void I2Cinit(void);
void I2C_sendByte(uint16_t data);
void lcd_backlightOn(void);
void lcd_backlightOff(void);
void delay_us(uint32_t us);
void main(void)
{
// Initialize device clock and peripherals
Device_init();
// Disable pin locks and enable internal pullups
Device_initGPIO();
// Initialize I2C pins
I2C_GPIO_init();
// Initialize PIE and clear PIE registers. Disable CPU interrupts
Interrupt_initModule();
// Initialize the PIE vector table
Interrupt_initVectorTable();
// Initialize I2C peripheral
I2Cinit();
// Optional: Scan I2C bus
uint16_t *pAvailableI2C_slaves = AvailableI2C_slaves;
status = I2CBusScan(I2CA_BASE, pAvailableI2C_slaves);
// Main loop: blink backlight ON and OFF every 1 second
while (1)
{
lcd_backlightOn();
DEVICE_DELAY_US(1000000); // 1 second delay
lcd_backlightOff();
DEVICE_DELAY_US(1000000); // 1 second delay
}
}
void I2C_GPIO_init(void)
{
GPIO_setDirectionMode(35U, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(35U, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(35U, GPIO_QUAL_ASYNC);
GPIO_setDirectionMode(37U, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(37U, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(37U, GPIO_QUAL_ASYNC);
GPIO_setPinConfig(GPIO_35_I2CA_SDA);
GPIO_setPinConfig(GPIO_37_I2CA_SCL);
}
void I2Cinit(void)
{
I2C_disableModule(I2CA_BASE);
I2C_initMaster(I2CA_BASE, DEVICE_SYSCLK_FREQ, 100000, I2C_DUTYCYCLE_50);
I2C_setConfig(I2CA_BASE, I2C_MASTER_SEND_MODE);
I2C_setSlaveAddress(I2CA_BASE, SLAVE_ADDR);
I2C_setOwnSlaveAddress(I2CA_BASE, 96);
I2C_disableLoopback(I2CA_BASE);
I2C_setBitCount(I2CA_BASE, I2C_BITCOUNT_8);
I2C_setDataCount(I2CA_BASE, 1);
I2C_setAddressMode(I2CA_BASE, I2C_ADDR_MODE_7BITS);
I2C_enableFIFO(I2CA_BASE);
I2C_clearInterruptStatus(I2CA_BASE, I2C_INT_ARB_LOST | I2C_INT_NO_ACK);
I2C_setFIFOInterruptLevel(I2CA_BASE, I2C_FIFO_TXEMPTY, I2C_FIFO_RX2);
I2C_enableInterrupt(I2CA_BASE, I2C_INT_ADDR_SLAVE | I2C_INT_ARB_LOST | I2C_INT_NO_ACK | I2C_INT_STOP_CONDITION);
I2C_setEmulationMode(I2CA_BASE, I2C_EMULATION_FREE_RUN);
I2C_enableModule(I2CA_BASE);
}
void I2C_sendByte(uint16_t data)
{
while (I2C_isBusBusy(I2CA_BASE)); // Wait for bus to be free
I2C_setDataCount(I2CA_BASE, 1); // Send one byte
I2C_putData(I2CA_BASE, data); // Put data in TX FIFO
I2C_setConfig(I2CA_BASE, I2C_MASTER_SEND_MODE);
I2C_sendStartCondition(I2CA_BASE); // START
while (I2C_getStatus(I2CA_BASE) & I2C_STS_BUS_BUSY); // Wait for transmission
if (I2C_getStatus(I2CA_BASE) & (I2C_STS_NO_ACK | I2C_STS_ARB_LOST))
{
I2C_clearStatus(I2CA_BASE, I2C_STS_NO_ACK | I2C_STS_ARB_LOST);
}
I2C_sendStopCondition(I2CA_BASE); // STOP
while (I2C_isBusBusy(I2CA_BASE)); // Wait for bus to clear
delay_us(50); // Small delay
}
void lcd_backlightOn(void)
{
// Send dummy data with backlight ON
I2C_sendByte(0x00 | LCD_BACKLIGHT | ENABLE);
delay_us(1);
I2C_sendByte(0x00 | LCD_BACKLIGHT); // EN low
}
void lcd_backlightOff(void)
{
// Send dummy data with backlight OFF
I2C_sendByte(0x00 | ENABLE); // EN high
delay_us(1);
I2C_sendByte(0x00); // EN low, backlight off
}
void delay_us(uint32_t us)
{
DEVICE_DELAY_US(us);
}
Please help

