CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

I2C LCD

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

I2C LCD
PostPosted: Sat Sep 05, 2015 4:18 am     Reply with quote

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
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 2:28 am     Reply with quote

hello! I am a student and the goal of my project is to interface date and hours from RTC DS1307 to the Lcd 16x2. I use PIC12F675 with an expander PCF8574. I ve used your codes to display something to the LCD and it works very well. Could you help me to do this? thank you very much! I really need it! please, help me![/img]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group