0% found this document useful (0 votes)
36 views

I 2 C

This document contains C code for initializing and sending data to an LCD display over I2C. It includes functions for sending commands and data to the LCD, initializing the LCD by setting it to 4-bit interface mode and turning the display on, sending a string to the LCD by passing a character array, clearing the LCD display, and positioning the cursor on the LCD by row and column. The code defines the I2C peripheral and slave address used to communicate with the LCD display.

Uploaded by

Trieu Huynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

I 2 C

This document contains C code for initializing and sending data to an LCD display over I2C. It includes functions for sending commands and data to the LCD, initializing the LCD by setting it to 4-bit interface mode and turning the display on, sending a string to the LCD by passing a character array, clearing the LCD display, and positioning the cursor on the LCD by row and column. The code defines the I2C peripheral and slave address used to communicate with the LCD display.

Uploaded by

Trieu Huynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/**

Edit by modify: Ngoc Hang


**/

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/systick.h"
#include "i2c-lcd.h"

#if defined I2C0


#define I2C SYSCTL_PERIPH_I2C0
#endif

#if defined I2C1


#define I2C SYSCTL_PERIPH_I2C1
#endif

#if defined I2C2


#define I2C SYSCTL_PERIPH_I2C2
#endif

#if defined I2C3


#define I2C SYSCTL_PERIPH_I2C3
#endif

//extern I2C_HandleTypeDef hi2c1; // change your handler here accordingly

#define SLAVE_ADDRESS_LCD 0x27 // change this according to ur setup

void I2C0_Send(uint16_t device_address, uint8_t device_data)


{
// Xac dinh dia chi Slave de Master tuong tac du lieu
// false: transmit Master --> Slave
// true: receive Master <-- Slave
I2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);

// Dua du lieu vao


I2CMasterDataPut(I2C0_BASE, device_data);

// Truyen du lieu
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

// Cho cho den khi hoan tat


while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ));
}

void lcd_send_cmd (char cmd)


{
char data_u, data_l;
uint8_t data_t[4];
data_u = (cmd&0xf0);
data_l = ((cmd<<4)&0xf0);
data_t[0] = data_u|0x0C; //en=1, rs=0
data_t[1] = data_u|0x08; //en=0, rs=0
data_t[2] = data_l|0x0C; //en=1, rs=0
data_t[3] = data_l|0x08; //en=0, rs=0
I2C0_Send(SLAVE_ADDRESS_LCD, (uint8_t *) data_t );
}

void lcd_send_data (char data)


{
char data_u, data_l;
uint8_t data_t[4];
data_u = (data&0xf0);
data_l = ((data<<4)&0xf0);
data_t[0] = data_u|0x0D; //en=1, rs=0
data_t[1] = data_u|0x09; //en=0, rs=0
data_t[2] = data_l|0x0D; //en=1, rs=0
data_t[3] = data_l|0x09; //en=0, rs=0
I2C0_Send(SLAVE_ADDRESS_LCD, (uint8_t *) data_t );
}

void Delay_lcd(uint16_t nCount)


{
/* Decrement nCount value */
while (nCount --)
{
SysCtlDelay(SysCtlClockGet()/6000);
}
}

void lcd_init (void)


{
lcd_send_cmd (0x33); /* set 4-bits interface */
lcd_send_cmd (0x32);
Delay_lcd(50);
lcd_send_cmd (0x28); /* start to set LCD function */
Delay_lcd(50);
lcd_send_cmd (0x01); /* clear display */
Delay_lcd(50);
lcd_send_cmd (0x06); /* set entry mode */
Delay_lcd(50);
lcd_send_cmd (0x0f); /* set display to on */
Delay_lcd(50);
lcd_send_cmd (0x02); /* move cursor to home and set data address to 0 */
Delay_lcd(50);
lcd_send_cmd (0x80);
}
void lcd_send_string (char *str)
{
while (*str) lcd_send_data (*str++);
}

void lcd_clear_display (void)


{
lcd_send_cmd (0x01); //clear display
}

void lcd_goto_XY (int row, int col)


{
uint8_t pos_Addr;
if(row == 1)
{
pos_Addr = 0x80 + row - 1 + col;
}
else
{
pos_Addr = 0x80 | (0x40 + col);
}
lcd_send_cmd(pos_Addr);
}

You might also like