Friday, May 3, 2013

LCD Module (MR007-005.1)




My first add-on! This a LCD module with a 16x2 lines display. It is based on the controller SPLC780D which is totally compatible with the Hitachi HD44780 LCD controller chip. Hence all examples coming with the Arduino-IDE worked fine provided the change of RS pin from 12 to 7 and Enable pin from 11 to 6.


In order to try the module and its functionalities out, I conceived a small sketch for managing a menu with four options. The options that cannot be displayed can be visualized using button 3 (line up) and button 4 (line down). It remains to test for button 1 pressed to select the option from the menu, I leave this for the reader ;-)


Enjoy!




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***************************************************************************************************************
  AUTHOR: Enrico Formenti
  LICENCE: MIT Licence
  DATE: 5-5-2013.
***************************************************************************************************************/



  The circuit:
 * LCD RS pin to digital pin 7
 * LCD Enable pin to digital pin 6
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD button value to analog pin A0
*/



#include <LiquidCrystal.h>


// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


// initialize internal variables
unsigned int l;             // current line number displayed on the first row


// text content of the menus
char *txt[4]={"line 1", "line 2", "line 3", "line 4"};


// prints the lines of the menu
void printmenulines(unsigned int line) {
    lcd.clear();
    lcd.print(txt[line]);
    lcd.setCursor(0, 1);   
    lcd.print(txt[line+1]);     
}


void setup() {
 
  pinMode(A0, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // clear screen, cursor at 0,0
  lcd.clear();



  l = 0;
  printmenulines(0);

}


// the values below are found reading the analog pin A0
// when one of the five buttons is pressed
// no button = 1023
// B1 = 0
// B2 = 206
// B3 = 426
// B4 = 623
// B5 = 824


// the buttons are remapped to
// 5 = no button pressed
// 0 = button 1 (first button from left)
// 1 = button 2
// 2 = button 3
// 3 = button 4
// 4 = button 5


void loop() {


  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
 
  uint8_t button; // code of button pressed
  boolean buttonPressed; // true if a button has been pressed
 
  button=(uint8_t)map(analogRead(0),0,1023,0,5);
 
  switch(button) {
    case 3: // down arrow
      buttonPressed = true;
      if(l<2)
       ++l;     
       break;
     case 4: // up arrow
      buttonPressed = true;
      if(l>0)
        --l;
      break;
     default:
      buttonPressed = false;
      break;
  }
 
  if(buttonPressed)
      printmenulines(l);
 
  // delay before next buttons state read 
  delay(200);
}

No comments :

Post a Comment