Author Topic: == Arduino I2C Source Code for Keypad & IO functions ==  (Read 1846 times)

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
== Arduino I2C Source Code for Keypad & IO functions ==
« on: August 17, 2014, 01:07:35 AM »
We will soon be adding Arduino example code showing how to read a matrix keypad, and write to outputs OUT1 and OUT2. The LCD2S has 2 general purpose open collector outputs, OUT1 and OUT2. These outputs can supply up to 1000mA, and have protection circuitry for driving relays.
  • When using only OUT1, a 4x4 (16 button) or smaller keypad can be used.
  • When using OUT1 and OUT2, a 4x3 (12 buttons) or smaller keypad can be used.

Using a 4x4 Keypad and OUT1
When using a 4x4 keypad, the OUT1 output is NOT available. The keypad is connected to the pins marked R1 to R4 (rows) and C1 to C4 (columns) on the X1 1x10 pin header. The LCD2S has built in button debouncing, which can be configured for different values. The default should work fine though.


Click for larger view

Using a 4x3 Keypad, OUT1 and OUT2
When using OUT1 and OUT2 outputs, the largest keypad that can be used is a 4x3, 12 button keypad. The keypad is connected to the pins marked R1 to R4 (rows) and C1 to C3 (columns) on the X1 1x10 pin header. The C4 pin can not be used (function is shared with OUT1). The "OUT1" jumper on the X1 (1x10 pin) header also has to be inserted.


Click for larger view

« Last Edit: August 17, 2014, 01:52:35 AM by modtro2 »

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: == Arduino I2C Source Code for Keypad & IO functions ==
« Reply #1 on: March 07, 2016, 05:01:18 PM »
Following code can be used to write to LCD2S display, and read keys via Arduino.

Code: [Select]
#include <Wire.h>


// LCD2S Commands /////////////////////////////////////////////////////////////
#define CMD_WRITE_STRING    0x80    // Write parsed String command
#define CMD_CONFIG_DEVICE   0x95    // Configure device using 4 bytes
#define CMD_GET_STATUS      0xD0    // Read Status command
#define CMD_GET_KEY         0xD1    // Get Key command

// Constant Variables /////////////////////////////////////////////////////////
const int ledPin =  13;                     //The number of the LED pin
const unsigned char LCD2S_I2C_ADR = 0x28;   //I2C address of the LCD

// Variables //////////////////////////////////////////////////////////////////
int ledState = LOW;             // ledState used to set the LED

unsigned long tmrFlashLed = 0;  // Timer for flashing system LED
unsigned long tmrCheckKey = 0;  // Check if a key on keyboard was pressee

void setup()
{
  Wire.begin();         //Join bus as master
  Serial.begin(9600);   // start serial for output

  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);

  //Initialize LCD2S
  delay(300);           //Give LCD2S time to initialize itself
  initializeLcd2s();

  //Write "Hello World" message
  Wire.beginTransmission(LCD2S_I2C_ADR);  //Start condition at the given I2C address or Mark the beginning of a data packet, change 0x28 to your device's I2C address
  Wire.write(CMD_WRITE_STRING);           //Display Commands
  Wire.write(0x0c);                       //Clear display and go to first line
  Wire.write("Hello world!");             //Write message
  Wire.endTransmission(false);            //End condition or Mark the end of data packet and send to LCD and hold bus connection
}

void loop() {

  unsigned long currentMillis = millis();

  // Check if key was pressed every 50ms ///////////////////
  if(currentMillis - tmrCheckKey >= 50) {
    unsigned char status, key;
    tmrCheckKey = currentMillis;

    //Read "status" register of LCD2S. Bit 7(MSB) indicates if Keypad buffer contains any data
    Wire.beginTransmission(LCD2S_I2C_ADR);
    Wire.write(CMD_GET_STATUS);             //"Get Status Register" command
    Wire.endTransmission(false);            //Send "repeated start"
    Wire.requestFrom(LCD2S_I2C_ADR, 1);     //Request 1 bytes from slave device = Status byte
    while (Wire.available()) {
      status = Wire.read();                 //Receive status byte
    }

    //Has the LCD got keys to be read
    if (status & 0x80) {
      char buf[10];
      Serial.print("\r\nButton Pressed");
     
      //Get key from LCD
      Wire.beginTransmission(LCD2S_I2C_ADR);
      Wire.write(CMD_GET_KEY);                //"Get Key" command
      Wire.endTransmission(false);            //Send "repeated start"
      Wire.requestFrom(LCD2S_I2C_ADR, 1);     //Request 1 bytes from slave device = Status byte
      while (Wire.available()) {
        key = Wire.read();                    //Receive status byte
      }

      //Write key code to second line of LCD
      Wire.beginTransmission(LCD2S_I2C_ADR);  //Start condition at the given I2C address or Mark the beginning of a data packet, change 0x28 to your device's I2C address
      Wire.write(CMD_WRITE_STRING);           //Display Commands
      Wire.write(0x0d);                       //Go to beginning of first line
      Wire.write(0x0a);                       //Go to beginning of next line = second line
      Wire.write("Key = ");                   //Write message
      sprintf((char*)buf, "%c", key);
      Wire.write(buf);                        //Write key code
      Wire.endTransmission(false);            //End condition or Mark the end of data packet and send to LCD and hold bus connection
    }
  }

 
  // Flash system LED ///////////////////////////////////////
  if(currentMillis - tmrFlashLed >= 500) {
    tmrFlashLed = currentMillis;

    //Serial.print("blink");    // print the character
   
    //Toggle LED
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }
}


/**
 * Initialization code to initialize I2C 1 and LCD2S display connected to it.
 * Configure LCD using "Configure Device" command (0x95).
 * See LCD2S documentation for details on "Configure Device" command:
 * http://modtronix.com/prod/lcd2s/lcd2sr2_v140.pdf
 */
void initializeLcd2s(void) {
    //Configure LCD
    Wire.beginTransmission(LCD2S_I2C_ADR);  //Start condition at the given I2C address or Mark the beginning of a data packet, change 0x28 to your device's I2C address

    //Byte 1 = 0x95
    //Configure LCD using "Configure Device" command (0x95).
    Wire.write(CMD_CONFIG_DEVICE);          //Configure Device command

    //Byte 2 (Display) of command
    //xx1x xxxx - Interrupt pin is Open Collector output type
    //xxx1 1xxx - Backlight On, Display On
    //xxxx x001 - Cursor off, block cursor off, Cursor moves forward
    //0011 1001 = 0x39
    Wire.write(0x39);

    //Byte 3 (Contrast and Brightness) of command
    //01xx xxxx - Set Backlight to 100 (range is 0 to 255)
    //xx11 0111 - Set contrast to 220 (0x37 x 4)
    //0111 0111 = 0x77
    Wire.write(0x77);
   
    //Byte 4 (keypad and IO) of command
    //0x0f Configures device for 4x4 keypad (if present), OUT1 disabled, GPIO1 to 3 disabled
    Wire.write(0x0f);
   
    //Byte 5 (Keypad and Buzzer) of command
    //01xx xxxx - Keypad Buzzer off
    //xx10 xxxx - Keypad repeat rate = 320ms
    //xxxx 10xx - Keypad Repeat Delay = 1 second
    //xxxx xx10 - Keypad Debounce Time = 64ms
    //0110 1010 = 0x6A
    Wire.write(0x6a);

    //End condition or Mark the end of data packet and send to LCD and hold bus connection
    Wire.endTransmission(false);
}