Arduino to MCP23016 and LCD Display
Basic diagram.

Arduino Interface MCP23016 GPIO Expander Driving LCD Display

by Lewis Loflin

The address of the MCP23016 is 0x20.

The data or command is written to port 0 (Gp0) of the MCP23016

Note: RS register select pin 4 on display is connected to MCP23016 pin 13 (Gp1.7). 0 for command, 1 for data R/W of display goes to ground. C1 is 39pf and R2 is 3.9K. The MCP23016 pin 12 (Gp1.6) is E pin 6 on display.

E = connected display pin 6 (E), hi to lo transition will clock info into display. Register for command or data is selected by RS. Data is output in port0, two bits of port1 on the MCP23060 (bits 6, 7) are to control E and RS. R/W goes to ground.


Hd44780 display commands:
0x0f = initiate display cursor on blinking
0x0c = initiate display cursor off
0x01 = clear display fills display with spaces (0x20).
0x02 = HOME returns to line one first character
0x38 = 2 lines X 16 char 8 bits mode. Defaults to 1 line mode.
0x10 = cursor left
0x14 = cursor right
0x18 = Shifts entire display left
0x1c = Shifts entire display right

Explanation of subroutines:

void BS() - backspace cursor on display. No return value.

void CLR() - clears display. No return value.

void type(char c) - outputs a character to display wherever cursor is at. Returns no value.

void writeCommand(char c) - will execute single command listed above. For example to clear the display to set the 2 line mode:

writeCommand(0x38); // two line mode
writeComaand(0x02); // clear display

More sample code:




 void typeln(char *array1, int i) - type one line of text to display.
 his can be a quoted text string or array of characters while i 
 selects line to be written to. The value of i is '1' for line one 
 and '2' for line 2. All text strings or arrays must have a "\n" to 
 terminate. Maximum 16 characters plus the '\n'. 
 
 Note the "\n" not needed in the 0017 compiler.
 
The line:
for (int j = 0; (array1[j] != '\n') && (j < 16); j++) type(array1[j]); 
Has been modified to:
for (int j = 0; (array1[j] != 0) && (j < 16); j++) type(array1[j]); 

The following examples go in the void loop() {  )  

 Example 1: 
 typeln("Hello word. \n", 1); 
 // displays "Hello world." on line one. 
 
 Example 2: 
 char textArray[] = "Hello world.\n";
 typeln(textArray, 2); // displays "Hello world." on line 2.
 
 Example 3 Convert int number to string 
 and output to display.
 int intNumber = 1023; 
 // the inter number range is from -32767 to 32767.
 char St1[10]; //char array or string
 itoa(intNumber, St1, 10); 
 // covert an integer number to text string.  
 typeln(St1, 1); // displays 1023 on line 1. 
 
 
 Example 4 using PString.h from 
 http://arduiniana.org/libraries/pstring/
 The ZIP file must be downloaded and 
 the unzipped folder (PSstring) must
 be placed in arduino-0017/hardware/libraries/
 Restart the compiler!
 
Be sure use PSring.h under Wire.h. 
 
 PString has all the functions of print 
 but instead of the serial port
 will print to a text buffer/array. 
 This can then be output with typeln().
 
 Ex. 4A: Will print PI  
  float PI = 3.141596;
  char buffer[16];
  PString(buffer, sizeof(buffer), pi);  
  // PI as text into buffer
  typeln(buffer, 2); // prints 3.14 on line 2
  
  Ex. 4B: 
  char buffer[16];
  PString(buffer, sizeof(buffer), "Hello World!");
  typeln(buffer, 2); // prints Hello World! on line 2
  
  Ex. 4C:
  char buffer[16];
  int SECS = 255;
  PString(buffer, sizeof(buffer), SECS);
  typeln(buffer, 2); // will print 255 on line 2
  
  
  Ex. 4D:
  char buffer[16];
  int SECS = 255;
  PString(buffer, sizeof(buffer), SECS, HEX);
  typeln(buffer, 2); 
  // will print FF (HEX for 255) on line 2
  
  Ex. 4E:
  char buffer[16];
  int SECS = 255;
  PString(buffer, sizeof(buffer), SECS, OCT);
  typeln(buffer, 2); // will print 377 (OCT for 255) on line 2
  
  Ex. 4F:
  char buffer[16];
  int SECS = 255;
  PString(buffer, sizeof(buffer), SECS, BIN);
  typeln(buffer, 2); 
  // will print 11111111 (binary for 255) on line 2
  
  Ex. 5:
  float Pi = 3.1415;
  char buffer[16];
  PString str(buffer, sizeof(buffer));
  str.print("Pi = ");
  str.print(Pi); 
  typeln(buffer, 2); 
  // will print Pi = 3.14 on display line 2
  
  Ex. 6:
  float Pi = 3.1415;
  char buffer[16];
  PString str(buffer, sizeof(buffer));
  str.print("Pi = ");
  str.print(Pi); 
  typeln(buffer, 1); 
  // will print "Pi = 3.14" on display line 1
  
  str.begin(); // re-use buffer
  str.print("Length is ");
  str.print(str.length());
  typeln(buffer, 2); will display "Length = 10" in line 2
  
  Ex. 7:
  char buffer[16];
  PString str(buffer, sizeof(buffer));
  str.print("Capacity is ");
  str.print(str.capacity());
  typeln(buffer, 2); // prints "Capacity is 16" in line 2.

 
ATMEGA168/Arduino connected to DS1307
Where to connect the MCP23016.

#include <Wire.h> 
// specify use of Wire.h library.
#include <PString.h> 
// specify use of PSring.h library.


#define Sw0  4
#define Sw1  5
#define ledPin  13


// names can't have number as first char.
const byte HOME = 0x02;
const byte CL = 0x10; 
// cursor left
const byte CR = 0x14; 
// cursor right
const byte SL = 0x18; 
// Shifts entire display left
const byte SR = 0x1c; 
// Shifts entire display right 


int j;

void setup()

{
  pinMode(Sw0, INPUT);  
// for this use a slide switch
  pinMode(Sw1, INPUT);  
// N.O. push button switch
  pinMode(ledPin, OUTPUT);
  digitalWrite(Sw0, HIGH); 
// pull-ups on
  digitalWrite(Sw1, HIGH);

  Wire.begin();
  Wire.beginTransmission(0x20);  
// set mcp23016 output
  Wire.send(0x06); 
  Wire.send(0x00);  
// DDR Port0 all output
  Wire.send(0x0F);  
// DDR 0-3 input 4-7 output
  Wire.endTransmission(); 
  
// setup port 1 D7 = E; D6 = RS 
  Wire.beginTransmission(0x20); 
  Wire.send(0x01); 
//pointer
  Wire.send(B10000000); 
// setup for command mode
  Wire.endTransmission(); 


  writeCommand(0x38); // 2 lines
  writeCommand(0x0F); // blinking cursor
  CLR(); // clear display
  writeCommand(HOME);

}

void loop()   {


  CLR(); // clear display

  typeln("Hello Lewis!\n", 1); 
// write to display line 1
  typeln("You got it! \n", 2); 
// write to display line 2
  toggle(ledPin);
  delay(500);
} // end loop


// Below we pass a pointer to array1[0].
//  If no '\n' then the limit is 16 char. (0 - 15)
void typeln(char *array1, int i)   {
  delayMicroseconds(1000);
  if (i == 1) writeCommand(0x80); // begin on 1st line
  if (i == 2) writeCommand(0x80 + 0x40); // begin on 2nd line 
  for (int j = 0; (array1[j] != '\n') && (j < 16); j++) type(array1[j]); 
}





// send command to Hd44780 display 

//  E High to Low transition write command or data to Hd44780 display

//  RS 0 for command, default 1 for data

//  setup port1 D7 = E; D6 = RS 
void writeCommand(byte x)   {

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x00); // begin here
  Wire.send(x);  // command code
  Wire.endTransmission();

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x01); // pointer
  Wire.send(B10000000);  // command mode E high
  Wire.endTransmission();

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x01); // pointer 
  Wire.send(B00000000);  // char mode E low
  Wire.endTransmission();

  delayMicroseconds(100);

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x01); //  pointer
  Wire.send(B10000000);  // E high back to command mode
  Wire.endTransmission();


} 

//  send char to Hd44780 display 
//  E High to Low transition write command or data to Hd44780 display
//  RS 0 for command, default 1 for data
//  setup port1 D7 = E; D6 = RS 

void type(byte x)   {

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x00); // begin here
  Wire.send(x);  // data
  Wire.endTransmission();

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x01); // pointer
  Wire.send(B11000000);  // char mode E high
  Wire.endTransmission();

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x01); // pointer 
  Wire.send(B01000000);  // char mode E low
  Wire.endTransmission();

  delayMicroseconds(100);

  Wire.beginTransmission(0x20);  //  mcp23016 
  Wire.send(0x01); //  pointer
  Wire.send(B10000000);  // E high back to command mode
  Wire.endTransmission();


} 


// clear display
void  CLR()   {
  writeCommand(0x01); // clear
}


// toggle the state on a pin
void toggle(int pinNum) 
{  
  int pinState = digitalRead(pinNum);
  pinState = !pinState;
  digitalWrite(pinNum, pinState); 
}