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 support@ccsinfo.com

LCD HD44780 PCF8574T i2c Driver
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
i26c2



Joined: 01 Apr 2015
Posts: 19

View user's profile Send private message

Re: Working Driver!!!
PostPosted: Wed Apr 01, 2015 1:51 pm     Reply with quote

younder wrote:
Thanks for the tips Ttelmah!!!

Both Drivers are working now... but I've modified and recompiled a third one... I will share it here for anyone who needs in future...after all.. I owe a lot to this forum...

It can control any compatible LCD HITACHI from a bus I2C with
an PCF8574T I/O expander.

The Driver worked with 2x16, 2x20 and 4x20 lcd modules.

I renamed it for "i2c_Flex_LCD.h"

Driver code:
Code:

//-----------------------------------------------------------------------------
// Title:         i2c_Flex_LCD
// Description:   Driver for common LCD with 1/2/3 or 4 row modules using PCF8574T interface board with I2C protocol.
// Date:          Nov-2013
// Ver.Rev.:      1.0
// Author:        Hugo Silva (sergio-hugo@bol.com.br) #Based on the routines of 20X4_LCD_I2C_DRIVER.h from Pumrin S.
//-----------------------------------------------------------------------------
//
// lcd_init() Must be called before any other function.
//
// lcd_putc(c) Will display c on the next position of the LCD.
// 
//     \f Clear LCD dispay
//     \1 Set write position on LCD Line 1
//     \2 Set write position on LCD Line 2
//     \3 Set write position on LCD Line 3
//     \4 Set write position on LCD Line 4
//
//     lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)
//
//-----------------------------------------------------------------------------
// LCD pins D0-D3 are not used.
//-----------------------------------------------------------------------------
//
// Commment   : Control of a compatible LCD HITACHI from a bus I2C with
//              an EXPANDER of I/O with connection I2C. The tests of these
//              routines have been programmed using the IC PCF8574T of Phillips.
//              I used 4 bits mode programming. The 8 bits mode programming
//              is possible if you use 2 x PCF8574T.
//
// As defined in the following structure the pin connection is as follows:
//
//  PCF8574P     LCD
//  ========     ======
//     P0        RS
//     P1        RW
//     P2        Enable 
//     P3        Led Backlight
//     P4        D4
//     P5        D5
//     P6        D6
//     P7        D7
//
//  The SCL and SDA pins should be pull-up resistor as shown below:
//
//             +5v
//               |
//               <
//               > 4.7K       
//               <         
//To PIC         |          To i2c slave
//pin xx ------------------ SDA pin 
//(SDA)                     
//              +5v
//               |
//               <
//               > 4.7K       
//               <         
//To PIC         |          To i2c slave
//pin xx ------------------ SCL pin 
//(SCL)
//
//To PIC                    To i2c slave
//Vss pin ----------------- Vss or ground pin 
//                |
//              -----
//               ---  Ground
//                - 
// 
// THIS DOCUMENT IS PROVIDED TO THE USER "AS IS"
//-----------------------------------------------------------------------------

#define LCD_ADDR       0x4E //I2C slave address for LCD module

#define ON             1
#define OFF            0
#define RS             0b00000001  //P0 - PCF8574T Pin connected to RS
#define RW             0b00000010  //P1 - PCF8574T Pin connected to RW
#define EN             0b00000100  //P2 - PCF8574T Pin connected to EN
#define BACKLIGHT_LED  0b00001000  //P3 - PCF8574T Pin connected to BACKLIGHT LED

#define lcd_line_one   0x80   // LCD RAM address for line 1
#define lcd_line_two   0xC0   // LCD RAM address for line 2
#define lcd_line_three 0x94   // LCD RAM address for line 3
#define lcd_line_four  0xD4   // LCD RAM address for line 4
 
byte address;
int1 lcd_backlight=ON;

void i2c_send_nibble(unsigned char data)
   {   
        i2c_start();
        delay_us(20);
        i2c_write(LCD_ADDR); //the slave addresse
        delay_us(20);
        i2c_write(data);
        delay_us(20);
        i2c_stop();
        delay_us(20);
   }

void lcd_send_byte(unsigned char data)
   {
        if (lcd_backlight) data=data|EN|BACKLIGHT_LED; else data=data|EN; //set pin EN
        i2c_send_nibble(data);
        data=data-4;       //toggle EN back to 0
        i2c_send_nibble(data);
   }
   
void lcd_clear()
{
    lcd_send_byte(0x00);
    lcd_send_byte(0x10);
    delay_ms(2);
}

void lcd_init()
{
    delay_ms(200); //LCD power up delay
       
   //Request works on the command by set the RS = 0 R/W = 0 write
        lcd_send_byte(0x00);
        lcd_send_byte(0x10);
        lcd_send_byte(0x00);
        lcd_send_byte(0x00);
        lcd_send_byte(0x10);
           //First state in 8 bit mode
        lcd_send_byte(0x30);
        lcd_send_byte(0x30);
           //Then set to 4-bit mode
        lcd_send_byte(0x30);
        lcd_send_byte(0x20);
           //mode 4 bits, 2 lines, characters 5 x 7 (28 h)
        lcd_send_byte(0x20);
        lcd_send_byte(0x80);
           //no need cursor on (0Ch)
        lcd_send_byte(0x00);
        lcd_send_byte(0xC0);
           //the cursor moves to the left (06 h)
        lcd_send_byte(0x00);
        lcd_send_byte(0x60);
           //clears the display
        lcd_clear();
}

void lcd_gotoxy( byte x, byte y)
{     
static char data;
     
   switch(y)
   {
      case 1:  address= lcd_line_one;     break;
      case 2:  address= lcd_line_two;     break;
      case 3:  address= lcd_line_three;   break;
      case 4:  address= lcd_line_four;    break;
      default: address= lcd_line_one;     break; 
   }
 
   address+=x-1;
   data=address&0xF0;
   lcd_send_byte(data);
   data=address&0x0F;
   data=data<<4;
   lcd_send_byte(data);
}

//Display the character on LCD screen.
void LCD_PUTC(char in_data)
{
 char data;     
  switch(in_data)
   { 
     case '\f': lcd_clear()    ;  break;               
     case '\1': lcd_gotoxy(1,1);  break;
     case '\2': lcd_gotoxy(1,2);  break;
     case '\3': lcd_gotoxy(1,3);  break;
     case '\4': lcd_gotoxy(1,4);  break;

     default:
        data=in_data&0xF0;
        data=data|RS; //set RS pin to 1
        lcd_send_byte(data);
        data=in_data&0x0F;
        data=data<<4;
        data=data|RS; //set RS pin to 1
        lcd_send_byte(data);
     break;
   }
}


Main code:
Code:

#include <18f4550.h>
#device ICD = TRUE
#device ADC=10               
#use delay (clock=48000000)
#use i2c(Master,Fast=100000, sda=PIN_D6, scl=PIN_D7,force_sw)
#include <i2c_Flex_LCD.h>   
#fuses HSPLL,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,USBDIV,PLL5,CPUDIV1,DEBUG,ICSP1 // configura fuses

void main()
{
Char string="Test";
int16 test=1;

  lcd_init();
  lcd_backlight=ON;
 
        while (TRUE)
   {   
         test=test+1;
         
         lcd_clear();  //Clear Display
         printf(LCD_PUTC,"\1%s","LCD Line 1");  //Print on LCD line 1
         delay_ms(1000);
         
         printf(LCD_PUTC,"\2%s","LCD Line 2");  //Print on LCD line 2
         delay_ms(1000);
       
         printf(LCD_PUTC,"\f\1%s","LCD Line 1"); //Clear display, print again on Line 1
         delay_ms(1000);
         
         printf(LCD_PUTC,"\f\2%s","LCD Line 2"); //Clear display, print again on Line 2
         delay_ms(1000); 
         
         lcd_backlight=OFF;
         printf(LCD_PUTC,"\f\1%s\2%s","LCD BackLight","     OFF      "); //Clear display, print again on Line 1
         delay_ms(1000);
         
         lcd_backlight=ON;
         printf(LCD_PUTC,"\f\1%s\2%s","LCD BackLight","     ON      "); //Clear display, print again on Line 2
         delay_ms(1000);
   }   
}


Thanks guys for all support!
BR
Hugo



Hi, I am working with a PIC24f16ka101 20pin DIP micro controller and the DFRobot I2C / TWI LCD1602 Module. After removing a couple fuses that simply don't exist for my micro I was able to compile the code and load my PIC. The back light goes on for my display but nothing else works.

I then realized my slave address was 0x20 so I changed that, and I changed the I2C pins to the correct ones for my PIC but it still does not work. I am pulling up the clock and data pins to 3V instead of 5V but the display says it should work anywhere from 2.7-11V so it should not be an issue. I feel like something is missing or different in the code for the display and/or processor I am using.

Could someone help me out?
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 01, 2015 3:01 pm     Reply with quote

I 'googled' that lcd module, fist hit , was Wiki from dfrobot and ....Supply voltage: 5V !!!

I suspect that is your problem..
3 volt PIC, 5 volt LCD..
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 01, 2015 3:47 pm     Reply with quote

Quote:

DFRobot I2C / TWI LCD1602 Module

the display says it should work anywhere from 2.7-11V

Can you post a link to:

1. The product page for this lcd.

2. The data sheet that gives specs on the i2c interface. I don't want the
data sheet for the HD44780 controller. I want the one that says the lcd
module works from 2.7 to 11v, and gives specs on the i2c interface that
DFRobot added to the lcd.
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 01, 2015 4:39 pm     Reply with quote

i downloaded the schematic right from dfrobot.... and it clearly shows the LCD module/I2C chip being fed from VCC , which is 5 volts. There are 2 'logic level' translators on the I2C bus lines though, which would allow a 3V micro to communicate with the 5 volt LCD unit.

Now you 'might' be able to run a 3 volt LCD module IF the PCF chip will run at 3 volts....


Jay
i26c2



Joined: 01 Apr 2015
Posts: 19

View user's profile Send private message

PostPosted: Wed Apr 01, 2015 8:06 pm     Reply with quote

You are right, the 3-11V was for the LCD back-light and the LCD itself can run from 2.7 to 5.5V (From http://www.dfrobot.com/image/data/TOY0046/HD44780.pdf). Strangely they post the data sheet for the LCD module only and not for the i2C module, for that they just post a schematic. There is no indication on whether it can handle 3V. It is terrible that they don't have a data sheet for their own product.
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 12:38 am     Reply with quote

The order page for the module, says:

"3.2" LCD display; White character on Blue background; Supply voltage: 5V; I2C Address: 0x27".....

The core LCD controller can run from 2.7 to 5.5v, but the complete module is specified to use 5v.....

There are three different revisions of these units, and all say 5v somewhere.
i26c2



Joined: 01 Apr 2015
Posts: 19

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 10:50 am     Reply with quote

So I have added a level translator and I now have 5V on my display and my i2C is being pulled up on both sides of the level translator (resistors are part of the translator). I just connected and ran the code and used a logic analyzer and nothing appeared on the output pins of the PIC
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 11:36 am     Reply with quote

What level translator?. You do realise that you can't use a conventional 'level translator' IC. You need an I2C translator, like the PCA9306. Look at AN10441 from national (NXP).
i26c2



Joined: 01 Apr 2015
Posts: 19

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 11:58 am     Reply with quote

The level translator I am using was used by others for i2C before: https://www.sparkfun.com/products/12009. Even if that component is not working, the PIC should still output the data to the pin though.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 1:22 pm     Reply with quote

Run this program on your PIC and find out what i2c slave address it reports:
http://www.ccsinfo.com/forum/viewtopic.php?t=49713
i26c2



Joined: 01 Apr 2015
Posts: 19

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 2:32 pm     Reply with quote

I can't do anything with printf, I don't think I have enough pins left to use a serial connection. In any case, I think the issue might be the level translator and/or LCD. I removed the translator and pulled up to 3.3V through a 5.1k resistor and it appears on my logic analyzer just fine, as soon as I attach the level translator and LCD, it stops working, no data or clock, even with the additional pull up in place. Maybe I need to put additional pull ups on the other side of the translator as well. If that doesn't work I am out of ideas, lol.
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 4:46 pm     Reply with quote

You should post a schematic of your project for us. It's dificult to understand how you can't have ONE pin available for serial output to PC when you've got a 40 pin device and 2 pins are used for I2C!

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 02, 2015 4:54 pm     Reply with quote

Quote:
I can't do anything with printf, I don't think I have enough pins left to use a serial connection

All you need is one pin. Do you have the CCS IDE with their ICD-U40
or ICD-U64 debugger/programmer ? If so, it has a built-in software
UART output from the PIC that is displayed in the IDE. It's called the
monitor.
i26c2



Joined: 01 Apr 2015
Posts: 19

View user's profile Send private message

PostPosted: Fri Apr 03, 2015 10:52 am     Reply with quote

I am using the icd-u64 with 3 pins, clock, data, and mclr connected. My project is a sun following solar powered cell phone charger with internal battery. I want to implement data logging of the charging rate of the internal battery and display that rate on the LCD when a push button is pressed. Pretty much everything is implemented aside from the LCD and charging indicator, both of which use i2C thankfully, because the sensors and motors and programmer take almost all the pins on the device. 20 pins goes really fast in a large project.

Anyway, if I can get printf to work without using any additional pins please let me know. I have never used PIC before so I have no idea how to set that up.

I have determined that the issue has to be either with the level translator or the LCD, because without them plugged if I pull up to 3V I get i2c waveforms on the clock and data pins (checked using logic analyser). As soon as I plug in to the level translator (which is connected to the LCD) no more waveforms outputted from the PIC. As for address the documentation for the LCD i2C module clearly states that if all 3 jumpers are in place, which they are, the address is 0x20, which is what I am using in the code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 03, 2015 11:16 am     Reply with quote

Is the address really 0x20 ? The sample code for this lcd is Arduino.
Arduino uses the 7-bit format i2c address. But CCS uses 8-bit format.
That's why I wanted you to run the i2c bus scanner program. But since
you can't do that, try setting the i2c address to 0x40. That's in 8-bit
format. See if that works.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 2 of 6

 
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