I have had a problem with this type LCD, as it is a 5V and not 3.3.
I can read the LCD text at 2 line display; at 4 line display however need a 
torch to be shined on the LCD to see that the text appeared

to make sure it is detected, execute
i2cdetect -y -r 0 (assuming connected to pin 19+20 from memory)

my LCD had address 0x20; so commends followed are as such

i2cset -y 1 0x20 0x00 0x00 -> set all pins to output
i2cset -y 1 0x20 0x09 0xff ->should see LCD backlight go on
i2cset -y 1 0x20 0x09 0x00 ->should see LCD backlight go off

However the code, which I have got it work is; eclipse cross compiled

#include <iostream>
#include <iomanip>
#include <bitset>
#include <climits>

#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

using namespace std;

#define  output "out"
#define  input  "in"

//i2c
#define  usei2cLCD  1
#define  data  0
#define  text  1
#define  raw  1
int i;
int r;
int fd;
unsigned char i2ccommand[2];
useconds_t delay = 2000;

char *dev = "/dev/i2c-1";
int addr = 0x20;

//i2c LCD
#define LCD_LINE_1 0x80
#define LCD_LINE_2 0xC0
#define LCD_LINE_3 0x94
#define LCD_LINE_4 0xD4
#define LCD_CLEAR 0x01

#define backlight 0x02
#define DB7 0x04
#define DB6 0x08
#define DB5 0x10
#define DB4 0x20
#define db7 DB7
#define db6 DB6
#define db5 DB5
#define db4 DB4
#define E 0x40
#define RS 0x80



//ADC coms
#define  ADCstring "BB-ADC"
char command[] = "echo BB-ADC > /sys/devices/bone_capemgr.\"*\"/slots";
#define  useADC  1
#define  ADC "AIN"

unsigned char j,k = 0;

fstream workfile,workfile1;
string line,line1;

int analog[7]={0,};  //ADC actual values
int analogavg = 500;  //ADC actual values

void init();
void adcmeasure();
int i2csend4bit(int location, int value,int textdisplay);

int i2csend(int location, int value,int instruction);
int i2csend(int location, int value,int instruction,unsigned char direct);

struct HexCharStruct
{
  unsigned char c;
  HexCharStruct(unsigned char _c) : c(_c) { }
};

inline std::ostream& operator<<(std::ostream& o, const HexCharStruct& hs)
{
  return (o << std::hex << (int)hs.c);
}

inline HexCharStruct hex(unsigned char _c)
{
  return HexCharStruct(_c);
}

int main ()
{
    init();

    cout << "Hello World!" << endl;
    while(1)
    {
        adcmeasure();
    }
    return 0;
}




void init()
{
    if(usei2cLCD)
    {
        sleep(1);
        i2csend(0x00,0x00,data,raw); //turn on direction to all outputs on 
MCP23008
        i2csend(0x09,0x00,data,raw); //turn off all outputs on MCP23008

        //4 bit mode select
        i2csend(0x09,(backlight+DB5+DB4),data); //4 bit mode execute ->0011
        i2csend(0x09,(backlight+DB5+DB4),data); //4 bit mode execute ->0011
        i2csend(0x09,(backlight+DB5+DB4),data); //4 bit mode execute ->0011
        i2csend(0x09,(backlight+DB5),data); //4 bit mode execute ->0010
        //4 bit mode + 2 line LCD
        i2csend(0x09,(backlight+DB5),data); //4 bit mode execute ->0010
        i2csend(0x09,(backlight),data); //4 bit mode execute ->0100 ?? 2 
line mode 
//        i2csend(0x09,(backlight+DB7),data); //4 bit mode execute ->0100 
?? 4 line mode
        //curser+display shift
        i2csend(0x09,(backlight),data); //4 bit mode execute ->0000
        i2csend(0x09,(backlight+DB7+DB6),data); //4 bit mode execute ->1100

        i2csend(0x09,(backlight),data); //4 bit mode execute ->0000
        i2csend(0x09,(backlight+DB6+DB5),data); //4 bit mode execute ->0110

        //clear screen and return to zero
        i2csend(0x09,(backlight),data); //4 bit mode execute ->0000
        i2csend(0x09,(backlight+DB4),data); //4 bit mode execute ->0001

        sleep(1);


//        i2csend4bit(0x09,LCD_LINE_1,data);
//        i2csend4bit(0x09,'t',text);


    }



    if(useADC)
    {
        system("find /sys/devices/ -name bone_capemgr\.\* > /tmp/adctmp");
        workfile.open ("/tmp/adctmp");
            if ( ! workfile.is_open() )
            {
                cout <<" Failed to open adcstatus at init" << endl;
            }
            else
            {
                while (getline(workfile,line))
                {
                    line.append("/slots");

                    workfile1.open(line.c_str());
                    if ( ! workfile1.is_open() )
                    {
                        cout <<" Failed to open workfile1" << endl;
                    }
                    else
                    {
                        workfile1 << ADCstring;
                    }
                workfile1.close();
            }
        }
    workfile.close();
    }//ADC unable

}

void adcmeasure()
{

    if(useADC)
    {
        system("find /sys -name \*in_voltage\?_raw\* > /tmp/adctmp");

        workfile.open ("/tmp/adctmp");
        if ( ! workfile.is_open() )
        {
            cout <<" Failed to open adcstatus" << endl;
        }
        else
        {
        i=0;
            while (getline(workfile,line))
            {
                workfile1.open(line.c_str(), std::ifstream::in);
                if ( ! workfile1.is_open() )
                {
                    cout <<" Failed to open workfile1" << endl;
                }
                else
                {
                    getline (workfile1,line1);
                    stringstream(line1) >> analog[i];
                }
                i++;
                workfile1.close();
            }
        }
        cout <<"analog0 " << analog[0]  << endl <<" analog1  (GND)" << 
analog[1] << endl <<" analog2 " << analog[2]  << endl<<" analog3 " << 
analog[3]  << endl<<" analog4 (1.8V)" << analog[4] << endl <<" analog5 
(SENSOR) " << analog[5]  << endl<<" analog6 " << analog[6]  << endl<<" 
analog7 " << analog[7] << endl;
        workfile.close();

        analogavg = ((analogavg+analog[5])/2);
        cout <<"Average ADC result is " << analogavg <<" ADC results 
updated" << endl;

        i2csend4bit(0x09,LCD_LINE_1,data);
        i2csend4bit(0x09,'t',text);
        i2csend4bit(0x09,'h',text);
        i2csend4bit(0x09,'e',text);
        i2csend4bit(0x09,' ',text);
        i2csend4bit(0x09,'s',text);
        i2csend4bit(0x09,'e',text);
        i2csend4bit(0x09,'n',text);
        i2csend4bit(0x09,'s',text);
        i2csend4bit(0x09,'o',text);
        i2csend4bit(0x09,'r',text);
        i2csend4bit(0x09,' ',text);
        i2csend4bit(0x09,'i',text);
        i2csend4bit(0x09,'s',text);
        i2csend4bit(0x09,':',text);
        i2csend4bit(0x09,' ',text);
        i2csend4bit(0x09,(analog[5]/1000)%10+48,text);
        i2csend4bit(0x09,(analog[5]/100)%10+48,text);
        i2csend4bit(0x09,(analog[5]/10)%10+48,text);
        i2csend4bit(0x09,(analog[5]/1)%10+48,text);


    }
}

int i2csend(int location, int value,int instruction)
{
    if(instruction)
        value |= RS; //set RS to 1
    else
        value &= ~RS; //set RS to 0

    fd = open(dev, O_RDWR );
    if(fd < 0)
    {
            perror("Opening i2c device node\n");
            return 1;
    }
    usleep(30);

    r = ioctl(fd, I2C_SLAVE, addr);
    if(r < 0)
    {
            perror("Selecting i2c device\n");
            return 1;
    }
    usleep(30);

    //E->0, E->1, E->0
    i2ccommand[0] = location;
    i2ccommand[1] = value & ~E; //set E to zero

//    cout << (bitset<8>)i2ccommand[1] << endl;
    r = write(fd, &i2ccommand, 2);
    usleep(500);

    i2ccommand[0] = location;
    i2ccommand[1] = value | E; //set E to 1
//    cout << (bitset<8>)i2ccommand[1] << endl;
    r = write(fd, &i2ccommand, 2);
    usleep(600);

    i2ccommand[0] = location;
    i2ccommand[1] = value & ~E; //set E to zero
//    cout << (bitset<8>)i2ccommand[1] << endl << endl;
    r = write(fd, &i2ccommand, 2);
    usleep(6000);

    close(fd);
    return r;
}

int i2csend(int location, int value,int instruction,unsigned char direct)
{

    fd = open(dev, O_RDWR );
    if(fd < 0)
    {
            perror("Opening i2c device node\n");
            return 1;
    }
    usleep(30);

    r = ioctl(fd, I2C_SLAVE, addr);
    if(r < 0)
    {
            perror("Selecting i2c device\n");
            return 1;
    }
    usleep(300);

    i2ccommand[0] = location;
    i2ccommand[1] = value;
//    cout << (bitset<8>)i2ccommand[1] << endl << endl;
    r = write(fd, &i2ccommand, 2);
    usleep(6000);

    close(fd);
    return r;
}

int i2csend4bit(int location, int value,int textdisplay)
{
//1st nibble + execute
    i2ccommand[0] = location;

    i2ccommand[1] = 0;

    if((value >> 7) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB7 ; //DB7 high
    if((value >> 6) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB6 ; //DB6 high
    if((value >> 5) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB5 ; //DB5 high
    if((value >> 4) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB4 ; //DB4 high
    if(textdisplay)
        i2ccommand[1] = i2ccommand[1] + RS ;  //If not instruction, set to 
text mode

    i2ccommand[1] = i2ccommand[1] + backlight ; //backlight on

//   cout << (bitset<8>)i2ccommand[1] << endl;

    i2csend(i2ccommand[0],i2ccommand[1],textdisplay);

    //2nd nibble+ execute

    i2ccommand[1] = 0;
    if((value >> 3) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB7 ; //DB7 high
    if((value >> 2) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB6 ; //DB6 high
    if((value >> 1) & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB5 ; //DB5 high
    if(value & 0x01)
        i2ccommand[1] = i2ccommand[1] + DB4 ; //DB4 high
    if(textdisplay)
        i2ccommand[1] = i2ccommand[1] + RS ;  //If not instruction, set to 
text mode

    i2ccommand[1] = i2ccommand[1] + backlight ; //backlight on

//    cout << (bitset<8>)i2ccommand[1] << endl;
    i2csend(i2ccommand[0],i2ccommand[1],textdisplay);

//    cout<<"value is "<< value << " i2ccommand[1]  " << i2ccommand[1]+0 << 
" back " << backlight + E + DB4<< endl;

}




On Wednesday, 11 December 2013 12:09:13 UTC+11, Jim Hodgers wrote:
>
> I bought one of these
>
> http://www.ebay.com/itm/New-Arduino-IIC-I2C-TWI-Serial-LCD-2004-20-4-Module-Shield-Display-Blue-White-/320949546464?pt=LH_DefaultDomain_0&hash=item4aba156de0
>
> a 20 char 4 line LCD
>

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to