ผู้เขียน หัวข้อ: งานครั้งที่ 37 เขียนโปรแกรมรับค่าจากคีย์แพดแสดงผลผ่าน UART  (อ่าน 6201 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 706
    • ดูรายละเอียด
    • อีเมล์
เขียนโปรแกรมรับค่าจากคีย์แพดแสดงผลผ่าน UART
ฟังก์ชั่นอยู่ในกลุ่ม Serial http://arduino.cc/en/Reference/Serial
-Serial.begin() http://arduino.cc/en/Serial/Begin
-if (Serial) http://arduino.cc/en/Serial/IfSerial
-Serial.available() http://arduino.cc/en/Serial/Available
-Serial.read() http://arduino.cc/en/Serial/Read
-Serial.print() http://arduino.cc/en/Serial/Print
-Serial.println() http://arduino.cc/en/Serial/Println
-Serial.flush() http://arduino.cc/en/Serial/Flush

ฟังก์ชั่นที่ใช้งาน
-pinMode() http://arduino.cc/en/Reference/PinMode
-digitalWrite() http://arduino.cc/en/Reference/DigitalWrite
-delay() http://arduino.cc/en/reference/delay

ไลบารี่เพิ่มเติม
-Keypad.h
http://playground.arduino.cc/Code/Keypad#Download

การเพิ่มไลบารี่ลงในโปรแกรม Arduino IDE จากไฟล์ Zip
1. ดาวน์โหลดไฟล์ Zip ดังรูป


2. ทำการเพิ่มไลบรารี่ลงในโปรแกรม Arduino IDE โดยการเพิ่มจากไฟล์ zip แล้วทำการหาไฟล์ zip ที่ได้จากการดาวน์โหลดในข้อ 1


ฟังก์ชั่นในไลบารี่ Keypad.h http://playground.arduino.cc/Code/Keypad
-begin(
-waitForKey()
-getKey()
-getState()
-keyStateChanged()
-setHoldTime()
-setDebounceTime()
-addEventListener()
*หมายเหตุ จากการทดลองไลบรารี่ keypad เมื่อใช้ขา D13 สำหรับผู้ที่ใช้งานบอร์ด Arduino รุ่นต่าง ๆ เนื่องจากขา D13 มีการต่อ LED อยู่บนบอร์ดจึงทำให้ไม่สามารถใช้งานได้  ดังนั้นเวลาใช้งานขา D13 จึงห้ามใช้

โจทย์การทดลอง
-เขียนโปรแกรมรับค่าจากคีย์แพดแสดงผลผ่าน UART
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้บอร์ด Arduino


วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้ไอซี ATmega328 (ที่มี Boot Loader Arduino)


บริเวณใช้งานบอร์ดทดลอง


โมดูลสำเร็จรูปเพิ่มเติม
Keypad
http://www.arduinoall.com (คลิก)

ตำแหน่งขาต่อของคีย์แพด


ตัวอย่างโปรแกรมรับค่าจากคีย์แพด
โค๊ด: [Select]
#include <Keypad.h>
const byte ROWS = 4;       //four rows
const byte COLS = 3;       //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {12,11,10,9};    //connect to the row pinouts of the keypad
byte colPins[COLS] = {8,7,6};         //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
     Serial.begin(9600);
}
void loop()
{
     char key = keypad.getKey();
     if (key)
     {
           Serial.println(key);
     }
}
« แก้ไขครั้งสุดท้าย: สิงหาคม 08, 2017, 10:27:49 AM โดย admin »