Arduino LCD Keypad Shield LCD1602

回覆文章
yehlu
Site Admin
文章: 3244
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Arduino LCD Keypad Shield LCD1602

文章 yehlu »

http://goods.ruten.com.tw/item/show?21107305237618

LCD Keypad Shield輸入輸出擴展板使用2行16個字符液晶,具有對比度調節和背光燈,使用1個模擬口便完成5個按鍵的輸入,1個複位按鍵,未使用的IO口都擴展出來備用,充分利用IO口。

佔用數字端口:

PIN4 - DB4
DB5 PIN5 -
PIN6,DB6
,DB7 PIN7
引腳8 - RS
- E PIN9
PIN10 -背光控制
模擬0按鍵端口



模塊的調試:

將LCD Keypad Shield插接到Arduino控制器上,接著編譯下面的一個測試程序再下載到Arduino中,初次使用LCD Keypad Shield,先觀察LCD有沒有顯示字符,如果沒有顯示字符那可能是對比度不正確,可以使用一字起子調節RP1(順時針旋轉),調到出現清晰地字符即可。

與Arduino配合使用:

代碼: 選擇全部

/*
The circuit:
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7
 * LCD BL pin to digital pin 10
 * KEY pin to analogl pin 0
 */

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

char msgs[5][16] = {"Right Key OK ",
                    "Up Key OK    ",               
                    "Down Key OK  ",
                    "Left Key OK  ",
                    "Select Key OK" };

int adc_key_val[5] ={50, 200, 400, 600, 800 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

void setup()
{
  lcd.clear(); 
  lcd.begin(16, 2);
  lcd.setCursor(0,0); 
  lcd.print("ADC key testing"); 
}

void loop()
{
  adc_key_in = analogRead(0);    // read the value from the sensor 
  key = get_key(adc_key_in);  // convert into key press
 
  if (key != oldkey)   // if keypress is detected
   {
    delay(50);  // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor 
    key = get_key(adc_key_in);    // convert into key press
    if (key != oldkey)    
    {   
      lcd.setCursor(0, 1);
      oldkey = key;
      if (key >=0){
           lcd.print(msgs[key]);              
      }
    }
}
 delay(100);
}

// Convert ADC value to key number
int get_key(unsigned int input)
{
    int k;
   
    for (k = 0; k < NUM_KEYS; k++)
    {
      if (input < adc_key_val[k])
 {
            return k;
        }
   }
   
    if (k >= NUM_KEYS)k = -1;  // No valid key pressed
    return k;
}
回覆文章

回到「Arduino」