sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
I2C LCD |
Posted: Sat Sep 05, 2015 4:18 am |
|
|
PCF8574.h
Code: | #define PCF8574_address 0x40
#define PCF8574_write_cmd PCF8574_address
#define PCF8574_read_cmd (PCF8574_address + 1)
#define SDA_pin pin_A5
#define SCL_pin pin_A4
#use I2C(Master, SDA = SDA_pin, SCL = SCL_pin)
unsigned char PCF8574_read();
void PCF8574_write(unsigned char data_byte); |
PCF8574.c
Code: | #include "PCF8574.h"
unsigned char PCF8574_read()
{
unsigned char port_byte = 0;
i2c_start();
i2c_write(PCF8574_read_cmd);
port_byte = i2c_read(0);
i2c_stop();
return port_byte;
}
void PCF8574_write(unsigned char data_byte)
{
i2c_start();
i2c_write(PCF8574_write_cmd);
i2c_write(data_byte);
i2c_stop();
} |
LCD.h
Code: | #include "PCF8574.c"
#define clear_display 0x01
#define goto_home 0x02
#define cursor_direction_inc (0x04 | 0x02)
#define cursor_direction_dec (0x04 | 0x00)
#define display_shift (0x04 | 0x01)
#define display_no_shift (0x04 | 0x00)
#define display_on (0x08 | 0x04)
#define display_off (0x08 | 0x02)
#define cursor_on (0x08 | 0x02)
#define cursor_off (0x08 | 0x00)
#define blink_on (0x08 | 0x01)
#define blink_off (0x08 | 0x00)
#define _8_pin_interface (0x20 | 0x10)
#define _4_pin_interface (0x20 | 0x00)
#define _2_row_display (0x20 | 0x08)
#define _1_row_display (0x20 | 0x00)
#define _5x10_dots (0x20 | 0x40)
#define _5x7_dots (0x20 | 0x00)
#define dly 0x01
unsigned char data_value = 0x00;
void LCD_init();
void LCD_command(unsigned char value);
void LCD_send_data(unsigned char value);
void LCD_4bit_send(unsigned char lcd_data);
void LCD_putstr(char *lcd_string);
void LCD_putchar(char char_data);
void LCD_clear_home();
void LCD_goto(unsigned char x_pos, unsigned char y_pos); |
LCD.c
Code: | #include "lcd.h"
void LCD_init()
{
unsigned char t = 0x1A;
data_value = 0x08;
PCF8574_write(data_value);
while(t > 0x00)
{
delay_ms(dly);
t--;
};
data_value = 0x30;
PCF8574_write(data_value);
data_value |= 0x08;
PCF8574_write(data_value);
delay_ms(dly);
data_value &= 0xF7;
PCF8574_write(data_value);
delay_ms(dly);
data_value = 0x30;
PCF8574_write(data_value);
data_value |= 0x08;
PCF8574_write(data_value);
delay_ms(dly);
data_value &= 0xF7;
PCF8574_write(data_value);
delay_ms(dly);
data_value = 0x30;
PCF8574_write(data_value);
data_value |= 0x08;
PCF8574_write(data_value);
delay_ms(dly);
data_value &= 0xF7;
PCF8574_write(data_value);
delay_ms(dly);
data_value = 0x20;
PCF8574_write(data_value);
data_value |= 0x08;
PCF8574_write(data_value);
delay_ms(dly);
data_value &= 0xF7;
PCF8574_write(data_value);
delay_ms(dly);
LCD_command(_4_pin_interface | _2_row_display | _5x7_dots);
LCD_command(display_on | cursor_off | blink_off);
LCD_command(clear_display);
LCD_command(cursor_direction_inc | display_no_shift);
}
void LCD_command(unsigned char value)
{
data_value &= 0xFB;
PCF8574_write(data_value);
LCD_4bit_send(value);
}
void LCD_send_data(unsigned char value)
{
data_value |= 0x04;
PCF8574_write(data_value);
LCD_4bit_send(value);
}
void LCD_4bit_send(unsigned char lcd_data)
{
unsigned char temp = 0x00;
temp = (lcd_data & 0xF0);
data_value &= 0x0F;
data_value |= temp;
PCF8574_write(data_value);
data_value |= 0x08;
PCF8574_write(data_value);
delay_ms(dly);
data_value &= 0xF7;
PCF8574_write(data_value);
delay_ms(dly);
temp = (lcd_data & 0x0F);
temp <<= 0x04;
data_value &= 0x0F;
data_value |= temp;
PCF8574_write(data_value);
data_value |= 0x08;
PCF8574_write(data_value);
delay_ms(dly);
data_value &= 0xF7;
PCF8574_write(data_value);
delay_ms(dly);
}
void LCD_putstr(char *lcd_string)
{
while (*lcd_string != '\0')
{
LCD_send_data(*lcd_string);
lcd_string++;
};
}
void LCD_putchar(char char_data)
{
LCD_send_data(char_data);
}
void LCD_clear_home()
{
LCD_command(clear_display);
LCD_command(goto_home);
}
void LCD_goto(unsigned char x_pos,unsigned char y_pos)
{
if(y_pos == 0)
{
LCD_command(0x80 | x_pos);
}
else
{
LCD_command(0x80 | 0x40 | x_pos);
}
} |
Example
Code: | #include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)
#include "lcd.c"
void setup();
void main()
{
unsigned char i = 0;
char txt1[] = {"MicroArena"};
char txt2[] = {"SShahryiar"};
char txt[6];
setup();
while(TRUE)
{
LCD_clear_home();
for(i = 0; i <= 9; i++)
{
LCD_goto((i + 3), 0);
LCD_putchar(txt1[i]);
delay_ms(99);
}
for(i = 0; i <= 9; i++)
{
LCD_goto((i + 3), 1);
LCD_putchar(txt2[i]);
delay_ms(99);
}
delay_ms(4000);
LCD_clear_home();
LCD_goto(3, 0);
LCD_putstr(txt1);
for(i = 0; i <= 100; i++)
{
LCD_goto(7, 1);
sprintf(txt,"%02u", i);
LCD_putstr(txt);
delay_ms(500);
}
};
}
void setup()
{
disable_interrupts(GLOBAL);
setup_comparator(NC_NC_NC_NC);
setup_ADC(ADC_off);
setup_ADC_ports(no_analogs);
setup_timer_0(T0_internal);
setup_timer_1(T1_disabled);
set_timer0(0);
set_timer1(0);
LCD_init();
delay_ms(100);
} |
More details:
http://www.libstock.com/projects/view/1530/i2c-lcd
https://www.youtube.com/watch?v=dBjV9LTCY1c _________________ https://www.facebook.com/MicroArena
SShahryiar |
|