Sunday, March 13, 2011

Arduino and a 16x1 LCD (Displaytech 161A)

Got myself an Arduino last year, unfortunately I didn't have time to get into it that much yet, but I started playing with my LCD display today. You would imagine that the LiquidCrystal library would take care of the 16x1 display but no, it needs a bit on tweaking as you have to address it like a 8x2 display.
Below is my very simple serial monitor testing code. It will just take whatever comes in from the serial line and write it to the display and wrap to the beginning every 16 character.

/* Testing code for using a 16x1 LCD display with Arduino LiquidCrystal library.

   LCD used is a Displaytech 161A 16x1 display.

  See this page for an explanation how it works (161A is a "Type-1"):
  http://web.alfredstate.edu/weimandn/lcd/lcd_addressing/lcd_addressing_index.html
*/

#include 

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int c=0;
int r=0;

void setup(){
  lcd.begin(8, 2);
  Serial.begin(9600);
  lcd.clear();
  lcd.cursor();
}

void loop()
{
  int i=0;
  if (Serial.available()) {
    while (Serial.available() > 0) {
      i=Serial.read();
      if (i==-1)
        break; 
      lcd.write(i);
      c++;
      if (c==8) {
       r=(r==1 ? 0 : 1);
       c=0;
       lcd.setCursor(c,r);
      }
      Serial.print(c);
      Serial.print("\t");
      Serial.print(r);
      Serial.println("
      ");
    }
  }
}

No comments: