Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I2C 4x20 display with OTG and Micro
19-04-2012, 11:32 PM,
#1
I2C 4x20 display with OTG and Micro
I am still trying to convince myself that I should persevere with Pinguinos vs Arduinos.

I was able to fairly easily get a 4x20 I2C/SPI/RS232/TTL Newhaven display working on an Arduino 2560 using I2C.

Not having much luck with doing so on my Pinguino.

I am using a Win XP computer with x.2 installed and I am able to load and run basic programs on my OTG.

I have a couple Micros on the way and think that will be my preferred Pinguino.

Can anyone point me in the right direction to get this display saying "Hello world"

Thanks
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
20-04-2012, 06:58 AM,
#2
RE: I2C 4x20 display with OTG and Micro
Try this code ...
http://forum.pinguino.cc/showthread.php?tid=3444

The X.2 I2C code has problems.
Reply
20-04-2012, 02:32 PM, (This post was last modified: 21-04-2012, 02:37 AM by KiloOne.)
#3
RE: I2C 4x20 display with OTG and Micro
Thanks RL but I do not have knowledge at a low enough level yet to modify your LIS3310 code to drive my display.

This is how I produced Hello World with an Aurduino 2560:
Code:
#include <Wire.h>
#include <LCDi2cNHD.h>
LCDi2cNHD lcd = LCDi2cNHD(4,20,0x50>>1,0);

void setup() {
  lcd.init();
}

void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Hello World");
  delay(1000);
  lcd.clear();
  delay(500);
}

For this to compile, LCDi2cNHD.h and LCDi2cNHD.cpp had to be in a folder called LCDi2cNHD that was in the Arduino's libraries directory.

Here is LCDi2cNHD.h:
Code:
#ifndef LCDi2cNHD_h
#define LCDi2cNHD_h


#define _LCDEXPANDED                // If defined turn on advanced functions

#include <inttypes.h>

#include "Print.h"


class LCDi2cNHD : public Print {
    
public:
    
LCDi2cNHD(uint8_t num_lines, uint8_t num_col, int i2c_address, uint8_t display);
    
    void command(uint8_t value);
    
    void init();
    
    void setDelay(int,int);
    
    virtual void write(uint8_t);
    
    void clear();
    
    void home();
    
    void on();
    
    void off();
    
    void cursor_on();
    
    void cursor_off();
    
    void blink_on();
    
    void blink_off();
    
    void setCursor(uint8_t Line, uint8_t Col );
    
    
    // [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
    // []
    // []    Extended Functions
    // []
    // [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
    
    
#ifdef _LCDEXPANDED        

    
    uint8_t status();
    
    void load_custom_character(uint8_t char_num, uint8_t *rows);
    
     void setBacklight(uint8_t new_val);
    
     void setContrast(uint8_t new_val);
    
        
#endif
    
private:
    
    
};

#endif
And LCDi2cNHD.cpp:
Code:
#include <Wire.h>

#include <inttypes.h>

#include "arduino.h"  //all things wiring / arduino
  
#include "LCDi2cNHD.h"
  

#define _LCDEXPANDED                // If defined turn on advanced functions

// Global Vars

uint8_t g_num_lines = 4;
uint8_t g_num_col = 20;
int g_i2caddress = 0x50 >> 1; // NHD documents address in 8-bit but arduino uses 7
uint8_t g_display = 0;
int g_cmdDelay = 1;
int g_charDelay = 0;



// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    LCDi2c Class
// []
// []    num_lines = 1-4
// []   num_col = 1-80
// []   i2c_address = 7 bit address of the device
// []   display = Not used at this time
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
//--------------------------------------------------------



LCDi2cNHD::LCDi2cNHD (uint8_t num_lines,uint8_t num_col,int i2c_address,uint8_t display){
    
    g_num_lines = num_lines;
    g_num_col = num_col;
    g_i2caddress = i2c_address;
    g_display = display;    
}

// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    initiatize lcd after a short pause
// []
// []    Init the i2c buss
// []   Put the display in some kind of known mode
// []   Put the cursor at 0,0
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::init () {
    
    Wire.begin();
    on();
    clear();
    blink_off();
    cursor_off();
    home();
    
}

// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Over ride the default delays used to send commands to the display
// []
// []    The default values are set by the library
// []   this allows the programer to take into account code delays
// []   and speed things up.
// []  
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::setDelay (int cmdDelay,int charDelay) {
    
    g_cmdDelay = cmdDelay;
    g_charDelay = charDelay;
    
}




// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []   Send a command to the display.
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::command(uint8_t value) {

  Wire.beginTransmission(g_i2caddress);
  Wire.write(0xFE);
  Wire.write(value);
  Wire.endTransmission();
  delay(g_cmdDelay);
}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []   Send a command to the display.
// []
// []    This is also used by the print, and println
// []    
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]



size_t LCDi2cNHD::write(uint8_t value) {

  Wire.beginTransmission(g_i2caddress);
  Wire.write(value);
  Wire.endTransmission();
  delay(g_charDelay);

}



// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Clear the display, and put cursor at 0,0
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::clear(){
    
      command(0x51);

}

// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Home to custor to 0,0
// []
// []    Do not disturb data on the displayClear the display
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::home(){

    setCursor(0,0);                    // The command to home the cursor does not work on the version 1.3 of the display
}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Turn on the display
// []
// []    Depending on the display, might just turn backlighting on
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

void LCDi2cNHD::on(){

      command(0x41);

}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Turn off the display
// []
// []    Depending on the display, might just turn backlighting off
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

void LCDi2cNHD::off(){

      command(0x42);

        
}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Turn on the underline cursor
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

void LCDi2cNHD::cursor_on(){


      command(0x47);

        
}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Turn off the underline cursor
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

void LCDi2cNHD::cursor_off(){

      command(0x48);
}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Turn on the blinking block cursor
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::blink_on(){

      command(0x4B);

}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Turn off the blinking block cursor
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

void LCDi2cNHD::blink_off(){

      command(0x4C);

}


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Position the cursor to position line,column
// []
// []    line is 0 - Max Display lines
// []    column 0 - Max Display Width
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::setCursor(uint8_t line_num, uint8_t x){

      uint8_t base = 0x00;
      if (line_num == 1)
          base = 0x40;
      if (line_num == 2)
          base = 0x14;
      if (line_num == 3)
          base = 0x54;
      Wire.beginTransmission(g_i2caddress);
      Wire.write(0xFE);
      Wire.write(0x45);
      Wire.write(base + x);
      Wire.endTransmission();
      delay(g_cmdDelay*2);

}

#ifdef _LCDEXPANDED


// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Return the status of the display
// []
// []    Does nothing on this display
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]    

uint8_t LCDi2cNHD::status(){
    
    return 0;
}





// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// []    Load data for a custom character
// []
// []    Char = custom character number 0-7
// []    Row is array of chars containing bytes 0-7
// []
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


void LCDi2cNHD::load_custom_character(uint8_t char_num, uint8_t *rows)
{


    Wire.beginTransmission(g_i2caddress);
    Wire.write(0xFE);
    Wire.write(0x54);
    Wire.write(char_num);
    for (uint8_t i = 0 ; i < 8 ; i++)
        Wire.write(rows[i]);
    Wire.endTransmission();
    delay(g_cmdDelay);
}




void LCDi2cNHD::setBacklight(uint8_t new_val)
{
    
    Wire.beginTransmission(g_i2caddress);
    Wire.write(0xFE);
    Wire.write(0x53);
    Wire.write(new_val);
    Wire.endTransmission();
    delay(g_cmdDelay);

}


void LCDi2cNHD::setContrast(uint8_t new_val)
{
    
    Wire.beginTransmission(g_i2caddress);
    Wire.write(0xFE);
    Wire.write(0x52);
    Wire.write(new_val);
    Wire.endTransmission();
    delay(g_cmdDelay);
}


#endif

So, after trying to make this work for a few hours and from my very 'BASIC' stage of C knowledge, can anyone suggest my next step in Pinguino world?

Thanks again,
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
20-04-2012, 04:17 PM,
#4
RE: I2C 4x20 display with OTG and Micro
Did you write the C++ code you just posted?

I'm guessing you didn't and that's where the problem is.

John
Reply
20-04-2012, 05:39 PM, (This post was last modified: 21-04-2012, 02:53 AM by KiloOne.)
#5
RE: I2C 4x20 display with OTG and Micro
The LCDi2cNHD code came from Newhaven (Edit - OOPS NOT Newhaven from Arduino world somewhere).

I can do very little C programming at this point but have been programming in (ugh) Basic for 40 years..

Not sure how to modify LCDi2cNHD code to work.

I believe problems lie in the include files etc and it quickly goes beyond my current C knowledge.

I just posted the code to show what worked in Aurduino world.
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
20-04-2012, 06:43 PM,
#6
RE: I2C 4x20 display with OTG and Micro
The code is C++ which a confusingly named language not especially like C.

You'd need to change it to appropriate C.

John
Reply
20-04-2012, 09:17 PM,
#7
RE: I2C 4x20 display with OTG and Micro
A quick google review of converting C++ to C tells me I have dead-ended here.

Looks like there is no easy way to do the conversion and to do it manually requires a pretty in-depth knowledge of both languages. As you can see, since I didn't even know there was a difference, I will not be able to convert it anytime soon.

I thought that Pinguino was almost 100% compatible with Arduino, why does Arduino run it and Pinguino not?

Thanks,
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
21-04-2012, 05:46 AM,
#8
RE: I2C 4x20 display with OTG and Micro
Translate the wire lib for arduino to pinguino is day a day more needed
Reply
21-04-2012, 02:29 PM,
#9
RE: I2C 4x20 display with OTG and Micro
Hi KiloOne,
I updated i2c library in x.3 rev. 350 (thanks to RLMonitor).
You can take a look at examples/10.Libraries/i2c/LCDi2c.pde which is an example how you can drive a 2x16 LCD with a PCF8574 i2c IO expander. There is also an example how you can blink a led through this expander : PCF8574.pde
You can also go deeper and have a look at p32/include/pinguino/libraries/lcdi2c.c (some comments are still in french).
Those 2 examples are using I2C1 port on the UEXT connector.
Default speed is 100KHz and the library only support Master mode at this moment.
If you use I2C.init() it's will be then just the same as I2C1.init().
If you want to change speed or port then use :
I2C.init(I2C1, I2C_MASTER_MODE, I2C_100KHZ)
or
I2C.init(I2C2, I2C_MASTER_MODE, I2C_400KHZ)
or
I2C.init(I2C2, I2C_MASTER_MODE, I2C_1MHZ)
But you will have to delete i2c.pdl32 before (this is still something I have to do)

If you need some help, just ask Wink.


(21-04-2012, 05:46 AM)PaX Wrote: Translate the wire lib for arduino to pinguino is day a day more needed
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
21-04-2012, 06:19 PM, (This post was last modified: 22-04-2012, 12:03 AM by KiloOne.)
#10
RE: I2C 4x20 display with OTG and Micro
OK, so I am trying to convert the LCHi2cNHD.h and .cpp files from using wire.h to using i2c.c as modified by RLmonitor.

I am starting by trying to send a simple display command that will display the firmware version number on it using an OTG.

I have the display receiving 5v and I have the SDA line on OTG's A4 and the SCL line on OTG's A5. The backlight is lit. The display is not faulty as it works on the Arduino.

I am using this code:
Code:
#include "i2c.c"
int g_i2caddress = 0x50 >> 1; // NHD documents address in 8-bit but arduino uses 7
int g_cmdDelay = 10;
#define I2C_TIMEOUT    4000    

void setup() {
  I2C_init(I2C1, I2C_MASTER_MODE, I2C_100KHZ);
  pinMode(13,OUTPUT);
}

void loop()  {
  digitalWrite(13,HIGH);
  command(0x70);            //0x70 tells display to Display firmware version number
  delay(2500);
  digitalWrite(13,LOW);
  delay(500);
}

void command(uint8_t value) {
//dk   Wire.beginTransmission(g_i2caddress);
//dk   Wire.write(0xFE);
//dk   Wire.write(value);
//dk   Wire.endTransmission();
      I2C_start(I2C1);                        //dk
    I2C_writechar(I2C1,g_i2caddress);        //dk
    I2C_writechar(I2C1,0xFE);                //dk
    I2C_writechar(I2C1,value);                //dk
    I2C_stop(I2C1);                            //dk
      delay(g_cmdDelay);
}


My green LED blinks as commanded but the display remains blank and unresponsive.

Can anyone help me out here as to why it is not doing what I expect?

Thanks,
Dale

(21-04-2012, 02:29 PM)regis Wrote: Hi KiloOne,
I updated i2c library in x.3 rev. 350 (thanks to RLMonitor).
You can take a look at examples/10.Libraries/i2c/LCDi2c.pde which is an example how you can drive a 2x16 LCD with a PCF8574 i2c IO expander. There is also an example how you can blink a led through this expander : PCF8574.pde
You can also go deeper and have a look at p32/include/pinguino/libraries/lcdi2c.c (some comments are still in french).
Those 2 examples are using I2C1 port on the UEXT connector.
Default speed is 100KHz and the library only support Master mode at this moment.
If you use I2C.init() it's will be then just the same as I2C1.init().
If you want to change speed or port then use :
I2C.init(I2C1, I2C_MASTER_MODE, I2C_100KHZ)
or
I2C.init(I2C2, I2C_MASTER_MODE, I2C_400KHZ)
or
I2C.init(I2C2, I2C_MASTER_MODE, I2C_1MHZ)
But you will have to delete i2c.pdl32 before (this is still something I have to do)

If you need some help, just ask Wink.


Sorry I posted before I saw this (didn't refresh), I will see if your comments lead me to the answer, Thanks.

Deleting i2c.pdl32 in the p32\lib folder had no effect.

With Rev 350 of i2c.c my program halts with green light ON and blank display.
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)