element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
BeagleBoard
  • Products
  • Dev Tools
  • Single-Board Computers
  • BeagleBoard
  • More
  • Cancel
BeagleBoard
Forum Beaglebone black wanted to use i2c with c on LCD2004 pcf8574t
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join BeagleBoard to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 4 replies
  • Answers 1 answer
  • Subscribers 101 subscribers
  • Views 2028 views
  • Users 0 members are here
  • lcd i2c interface
  • bbb
  • BeagleBone
Related

Beaglebone black wanted to use i2c with c on LCD2004 pcf8574t

olty
olty over 5 years ago

I m new in c programing language and embedded .

  i've tried to take some exemples from internet and make them run for me trying to display something.The first i come across this :

that use iobb.h . i've tried to understand this library over 1week now and its driving me crazy

 

  1. #define I2CBUS 2
  2. #define LCD_ADDR 0x27  // the address is good checked 1000 times
  3. int handle; 
  4. int
  5. main(void) 
  6. { 
  7.   unsigned char buf[6]={0xc9, 0xe0, 0xf0, 0xf8, 0x00, 0xff};  //this line i dont understand
  8.   configure_i2c_pins(19, 20); 
  9.   handle=i2c_open(I2CBUS, LCD_ADDR); 
  10.   i2c_write(handle, buf, 6); 
  11.   i2c_close(handle); 
  12. return(0); 
  13. }

and nothing happened it compile no error.Then i tried something else :

 

/* -------------------------------------- *

* I2C control program for a AQM0802A LCD *   //but modified

* This is the LCD display test program.  *

*                                        *

* 2015-12-21 support@frank4dd.com        *

*                                        *

* Usage: lcdtest <line1-str> <line2-str  *

* -------------------------------------- */

#include <stdint.h>

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

#include <linux/i2c-dev.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/ioctl.h>

#include <fcntl.h>

 

 

#define ASCIITABLESIZE 96

#define DISPLAYSIZE 20

#define DISPLAYLINES 4

 

 

char * charlookup(char syschar) {

  char * lcdcode;

  char chartable[ASCIITABLESIZE][2];

  int i;

 

 

   chartable[0][0] = ' ';   chartable[0][1] = 0x20;

   chartable[1][0] = '!';   chartable[1][1] = 0x21;

   chartable[2][0] = '"';   chartable[2][1] = 0x22;

   chartable[3][0] = '#';   chartable[3][1] = 0x23;

   chartable[4][0] = '$';   chartable[4][1] = 0x24;

   chartable[5][0] = '%';   chartable[5][1] = 0x25;

   chartable[6][0] = '&';   chartable[6][1] = 0x26;

   chartable[7][0] = '\'';  chartable[7][1] = 0x27;

   chartable[8][0] = '(';   chartable[8][1] = 0x28;

   chartable[9][0] = ')';   chartable[9][1] = 0x29;

  chartable[10][0] = '*';  chartable[10][1] = 0x2A;

  chartable[11][0] = '+';  chartable[11][1] = 0x2B;

  chartable[12][0] = ',';  chartable[12][1] = 0x2C;

  chartable[13][0] = '-';  chartable[13][1] = 0x2D;

  chartable[14][0] = '.';  chartable[14][1] = 0x2E;

  chartable[15][0] = '/';  chartable[15][1] = 0x2F;

  chartable[16][0] = '0';  chartable[16][1] = 0x30;

  chartable[17][0] = '1';  chartable[17][1] = 0x31;

  chartable[18][0] = '2';  chartable[18][1] = 0x32;

  chartable[19][0] = '3';  chartable[19][1] = 0x33;

  chartable[20][0] = '4';  chartable[20][1] = 0x34;

  chartable[21][0] = '5';  chartable[21][1] = 0x35;

  chartable[22][0] = '6';  chartable[22][1] = 0x36;

  chartable[23][0] = '7';  chartable[23][1] = 0x37;

  chartable[24][0] = '8';  chartable[24][1] = 0x38;

  chartable[25][0] = '9';  chartable[25][1] = 0x39;

  chartable[26][0] = ':';  chartable[26][1] = 0x3A;

  chartable[27][0] = ';';  chartable[27][1] = 0x3B;

  chartable[28][0] = '<';  chartable[28][1] = 0x3C;

  chartable[29][0] = '=';  chartable[29][1] = 0x3D;

  chartable[30][0] = '>';  chartable[30][1] = 0x3E;

  chartable[31][0] = '?';  chartable[31][1] = 0x3F;

  chartable[32][0] = '@';  chartable[32][1] = 0x40;

  chartable[33][0] = 'A';  chartable[33][1] = 0x41;

  chartable[34][0] = 'B';  chartable[34][1] = 0x42;

  chartable[35][0] = 'C';  chartable[35][1] = 0x43;

  chartable[36][0] = 'D';  chartable[36][1] = 0x44;

  chartable[37][0] = 'E';  chartable[37][1] = 0x45;

  chartable[38][0] = 'F';  chartable[38][1] = 0x46;

  chartable[39][0] = 'G';  chartable[39][1] = 0x47;

  chartable[40][0] = 'H';  chartable[40][1] = 0x48;

  chartable[41][0] = 'I';  chartable[41][1] = 0x49;

  chartable[42][0] = 'J';  chartable[42][1] = 0x4A;

  chartable[43][0] = 'K';  chartable[43][1] = 0x4B;

  chartable[44][0] = 'L';  chartable[44][1] = 0x4C;

  chartable[45][0] = 'M';  chartable[45][1] = 0x4D;

  chartable[46][0] = 'N';  chartable[46][1] = 0x4E;

  chartable[47][0] = 'O';  chartable[47][1] = 0x4F;

  chartable[48][0] = 'P';  chartable[48][1] = 0x50;

  chartable[49][0] = 'Q';  chartable[49][1] = 0x51;

  chartable[50][0] = 'R';  chartable[50][1] = 0x52;

  chartable[51][0] = 'S';  chartable[51][1] = 0x53;

  chartable[52][0] = 'T';  chartable[52][1] = 0x54;

  chartable[53][0] = 'U';  chartable[53][1] = 0x55;

  chartable[54][0] = 'V';  chartable[54][1] = 0x56;

  chartable[55][0] = 'W';  chartable[55][1] = 0x57;

  chartable[56][0] = 'X';  chartable[56][1] = 0x58;

  chartable[57][0] = 'Y';  chartable[57][1] = 0x59;

  chartable[58][0] = 'Z';  chartable[58][1] = 0x5A;

  chartable[59][0] = '[';  chartable[59][1] = 0x5B;

  chartable[60][0] = ' ';  chartable[60][1] = 0x5C; // The Yen currency symbol

  chartable[61][0] = ']';  chartable[61][1] = 0x5D;

  chartable[62][0] = '^';  chartable[62][1] = 0x5E;

  chartable[63][0] = '_';  chartable[63][1] = 0x5F;

  chartable[64][0] = '\\'; chartable[64][1] = 0x60;

  chartable[65][0] = 'a';  chartable[65][1] = 0x61;

  chartable[66][0] = 'b';  chartable[66][1] = 0x62;

  chartable[67][0] = 'c';  chartable[67][1] = 0x63;

  chartable[68][0] = 'd';  chartable[68][1] = 0x64;

  chartable[69][0] = 'e';  chartable[69][1] = 0x65;

  chartable[70][0] = 'f';  chartable[70][1] = 0x66;

  chartable[71][0] = 'g';  chartable[71][1] = 0x67;

  chartable[72][0] = 'h';  chartable[72][1] = 0x68;

  chartable[73][0] = 'i';  chartable[73][1] = 0x69;

  chartable[74][0] = 'j';  chartable[74][1] = 0x6A;

  chartable[75][0] = 'k';  chartable[75][1] = 0x6B;

  chartable[76][0] = 'l';  chartable[76][1] = 0x6C;

  chartable[77][0] = 'm';  chartable[77][1] = 0x6D;

  chartable[78][0] = 'n';  chartable[78][1] = 0x6E;

  chartable[79][0] = 'o';  chartable[79][1] = 0x6F;

  chartable[80][0] = 'p';  chartable[80][1] = 0x70;

  chartable[81][0] = 'q';  chartable[81][1] = 0x71;

  chartable[82][0] = 'r';  chartable[82][1] = 0x72;

  chartable[83][0] = 's';  chartable[83][1] = 0x73;

  chartable[84][0] = 't';  chartable[84][1] = 0x74;

  chartable[85][0] = 'u';  chartable[85][1] = 0x75;

  chartable[86][0] = 'v';  chartable[86][1] = 0x76;

  chartable[87][0] = 'w';  chartable[87][1] = 0x77;

  chartable[88][0] = 'x';  chartable[88][1] = 0x78;

  chartable[89][0] = 'y';  chartable[89][1] = 0x79;

  chartable[90][0] = 'z';  chartable[90][1] = 0x7A;

  chartable[91][0] = '{';  chartable[91][1] = 0x7B;

  chartable[92][0] = '|';  chartable[92][1] = 0x7C;

  chartable[93][0] = '}';  chartable[93][1] = 0x7D;

  chartable[94][0] = ' ';  chartable[94][1] = 0x7E; // arrow pointing right

  chartable[95][0] = ' ';  chartable[95][1] = 0x7F; // arrow pointing left

 

 

  lcdcode = NULL;

  for(i=0;i<ASCIITABLESIZE;i++) {

    if(syschar==chartable[i][0]) break;

  }

  lcdcode = &chartable[i][1];

  return lcdcode;

}

 

 

// I2C Linux device handle

int i2cFile;

 

 

// open the Linux device

void i2cOpen() {

   i2cFile = open("/dev/i2c-2", O_RDWR);

   if (i2cFile < 0) {

      perror("i2cOpen");

      exit(1);

   }

}

 

 

// close the Linux device

void i2cClose() { close(i2cFile); }

 

 

// set the I2C slave address for all subsequent I2C device transfers

void i2cSetAddress(int address) {

   if (ioctl(i2cFile, I2C_SLAVE, address) < 0) {

      perror("i2cSetAddress");

      exit(1);

   }

}

 

 

void lcd_config() {

   char initseq[] = { 0x00, 0x38, 0x39, 0x14, 0x70, 0x56, 0x6c };

   char dispon[] = { 0x00, 0x38, 0x0c, 0x01 };

   char increm[] = { 0x00, 0x38, 0x06 };

 

 

   write(i2cFile, initseq, sizeof(initseq));

   sleep (0.3);

   write(i2cFile, dispon, sizeof(dispon));

   sleep (0.3);

   write(i2cFile, increm, sizeof(increm));

   sleep (0.3);

}

 

 

void lcd_clear() {

   char bytes[] = { 0x00, 0x01 };

   write(i2cFile, bytes, sizeof(bytes));

}

 

 

void lcd_off() {

   char bytes[] = { 0x00, 0x08 };

   write(i2cFile, bytes, sizeof(bytes));

}

 

 

void lcdputchar(char displaychar) {

  char * lcdchar;

  char bytes[2];

  lcdchar = charlookup(displaychar);

  if(lcdchar==NULL) return;

  bytes[0] = 0x40;

  bytes[1] = *lcdchar;

   write(i2cFile, bytes, sizeof(bytes));

}

 

 

void lcdwritestr(int line, char s[]) {

  int i;

  char bytes[2];

  bytes[0] = 0x00;

  if (line == 1)  bytes[1] = 0x80;          // Set display address for line 1

  if (line == 2)  bytes[1] = 0xc0;          // Set display address for line 2\

  if (line == 3)  bytes[1]=0x94

  if (line == 4)  bytes[1]=0xD4

   write(i2cFile, bytes, sizeof(bytes));

  for(i=0;i<DISPLAYSIZE && s[i];i++)         // Send only max chars to display

    lcdputchar(s[i]);                        // when the given string is longer

}

 

 

int main(int argc, char** argv) {

   if (argc != 3) return -1;

   // open Linux I2C device

   i2cOpen();

 

 

  

   i2cSetAddress(0x27);

 

 

   // initialize lcd

   lcd_config();

   sleep (0.3);

 

 

   printf("arg 1: %s\n",  argv[1]);

   printf("arg 2: %s\n",  argv[2]);

   lcdwritestr( atoi(argv[1]), argv[2]);

 

 

   // close Linux I2C device

   i2cClose();

 

 

   return 0;

}

 

all good and fine no error when i give arguments nothing happens.and so long and so forth. I hope you understand my struggle as a new programer wannabe.

PS: If you have some materials or free books for me to read it will be awesome.

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 5 years ago +2 suggested
    Hi Olty, Regarding your comment of reading material, the most concise book is The C Programming Language , it is a well-known reference. However, some may find it too concise and hard to follow. Personally…
  • aswinvenu
    aswinvenu over 5 years ago +1 verified
    Adding to what Shabaz has mentioned. Embedded C programming slightly different from the general application C programming (Its the same "C" But the way you approach writing code is different). So I would…
  • olty
    olty over 5 years ago in reply to shabaz +1
    I have knowledge of c programming but the transition from c to embedded its hard for me. Thank you for the response it's very helpful
  • shabaz
    0 shabaz over 5 years ago

    Hi Olty,

     

    Regarding your comment of reading material, the most concise book is The C Programming Language, it is a well-known reference. However, some may find it too concise and hard to follow. Personally I like it. Otherwise, I used this book to learn and liked it - it is lower cost, slightly easier to follow (but more verbose) you don't need to buy the latest edition, you can buy any older edition and it is just as useful: Problem Solving and Program Design in C. I'm sure others will have equally good or better suggestions. You can also learn from web based resources, but I have no personal experience of learning C from there, and personally I found those two books are very good.

     

    Trying to use the display really is running before walking, so it's worth taking the couple of steps back, and putting the display to one side for now and reading up on C programming, and trying simpler programming exercises.

    C only has about 30 keywords, and it will take just a few days reading/experimenting to be productive. Then, you'll be in a position to recognise what the code you've found is doing at a high level, and how to partially troubleshoot.

     

    When you do get to the display, you may also have to spend some time getting familiar with the datasheet of the device you're using, unless you're lucky and any code you find for it just works.

     

    Also, an oscilloscope can help with troubleshooting I2C. There's $100 Picoscope devices that will allow you to see if I2C communications is occurring, and it will come in useful in future if you need to do more interfacing, so it's a good investment.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • aswinvenu
    0 aswinvenu over 5 years ago

    Adding to what Shabaz has mentioned. Embedded C programming slightly different from the general application C programming (Its the same "C" But the way you approach writing code is different). So I would recommend you to go through basics of micro-controller and micro-processor architectures first. Once you know the architecture It will make more sense when you write some code. There are plenty of books available in the marker on embedded systems and programming.

    I find these two YouTube channels really helpful.

    Check it out:

    https://www.youtube.com/channel/UCMGXFEew8I6gzjg3tWen4Gw

    https://www.youtube.com/user/DerekMolloyDCU

     

    Reading on common embedded bus protocols like I2C, SPI, UART will also help you to write embedded code.

     

    Regards,

    Aswin

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • olty
    0 olty over 5 years ago in reply to shabaz

    I have knowledge of c programming but the transition from c to embedded its hard for me. Thank you for the response it's very helpful

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • olty
    0 olty over 5 years ago in reply to aswinvenu

    I know the channels i will watch all . Thank you

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube