การแสดงผล 7-segment 8 digit Max7219 ด้วยไลบรารี่ LedControl
วงจรที่ใช้ทดลอง
กรณีใช้บอร์ดรุ่น NodeMCU
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/sch1.png)
กรณีใช้บอร์ดรุ่น WeMos D1 mini
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/sch2.png)
ข้อดีของไลบรารี่ LedControl
1. เลือกใช้ขาเชื่อมต่อใดก็ได้
2. สามารถสร้างรูปแบบการติดของเซกเมนต์ได้จากฟังกชั่นที่มีให้มา
ข้อเสียของไลบรารี่ LedControl
1. ไม่สามารถสั่งแสดงผลที่เป็นตัวเลขหรือค่าจากตัวแปรที่มีตัวเลขจำนวนหลายหลักได้ (ต้องเขียนฟังก์ชั่นแยกหลักเอาเอง)
2. ไม่สามารสั่งแสดงข้อความที่เป็นประโยคได้ (ต้องเขียนฟังก์ชั่นแยกเอง)
ไลบารี่เพิ่มเติม
-LedControl.h https://github.com/wayoda/LedControl/releases (https://github.com/wayoda/LedControl/releases)
การเพิ่มไลบารี่ลงในโปรแกรม Arduino IDE จากไฟล์ Zip
1. ดาวน์โหลดไฟล์ Zip ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/02.png)
2. ทำการเพิ่มไลบรารี่ลงในโปรแกรม Arduino IDE โดยการเพิ่มจากไฟล์ zip แล้วทำการหาไฟล์ zip ที่ได้จากการดาวน์โหลดในข้อ 1
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/03.png)
ฟังก์ชั่นในไลบารี่ LedControl.h
-LedControl lc=LedControl(dataPin,clkPin,csPin,numDevices);
-lc.shutdown(addr,false); // Enable display
-lc.setIntensity(addr,value); // Set brightness level (0 is min, 15 is max)
-lc.clearDisplay(addr); // Clear display register
-lc.setDigit(addr, digit, value, point_decimal) //display number 0-9
-lc.setChar(addr, digit, character, false) //display character etc. A,b,c,d,E,F,H,L,P,-,_
-lc.setRow(addr, digit,value) //display part of segment
รายละเอียดเพิ่มเติมของไลบารี่ http://playground.arduino.cc/Main/LedControl (http://playground.arduino.cc/Main/LedControl)
ตัวอย่างคำสั่งการแสดงผล
การแสดงผลจากไลบรารี่ LedControl มีฟังก์ชั่นในในส่วนของการแสดงผลอยู่ 3 ฟังก์ชั่นจากรูปเป็นตัวอย่างการแสดงผลในสองฟังก์ชั่นคือฟังก์ชั่นของการแสดงตัวเลขและฟังก์ชั่นที่สามารถการกำหนดการติดของแต่ละเซกเมนต์ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/04.png)
ตัวอย่างโปรแกรม
/*
PINOUT:
MODULE.....NodeMCU/WeMos D1 mini
VCC.............+5V or +3.3V
GND.............GND
DIN.............GPIO12(D6)
CS (LOAD).......GPIO13(D7)
CLK.............GPIO15(D8)
*/
#include "LedControl.h"
LedControl lc=LedControl(D6,D8,D7,1); // D6-->DIN, D8-->Clk, D7-->LOAD, no.of devices is 1
void setup()
{
lc.shutdown(0,false); // Enable display
lc.setIntensity(0,10); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register
}
void loop()
{
lc.setDigit(0,7,2,false);
lc.setDigit(0,6,3,true);
lc.setDigit(0,5,4,false);
lc.setRow(0,4,B00110111);
lc.setRow(0,3,B01001111);
lc.setRow(0,2,B00001110);
lc.setRow(0,1,B00001110);
lc.setRow(0,0,B01111110);
delay(1000);
}
การแก้ไขข้อผิดพลาดหลังการติดตังไลบรารี่
หลังจากการติดตังไลบรารี่ LedControl เสร็จสิ้นไลบรารี่นี้เมื่อใช้กับ Arduino จะไม่มีปัญหาใด แต่ถ้าหากนำมาใช้กับ esp8266 จะไม่สามารถคอมไพล์ได้โดยผลจะเป็นดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/06.png)
ขั้นตอนการแก้ไขดังนี้
1. ไปที่โฟลเดอร์ที่เก็บไลบรารี่ LedControl ที่
C:\Users\....\Documents\Arduino\libraries\LedControl
แล้วเข้าโฟลเดอร์ย่อย scr
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/07.png)
2. เปิดไฟล์ LedControl.h โดยใช้โปรแกรมแก้ไขใด ๆ ก็ได้ (ตัวอย่างนี้ใช้ SubLime Text)
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/08.png)
3. ทำการปิดการรวมไฟล์ #include <avr/pgmspace.h> ดังรูป (ปิดแล้วทำการบันทึก)
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/09.png)
4. ผลการคอมไพล์จากการดำเนินการจะสามารถคอมไพล์ผ่านได้
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/13.png)
ผลการรัน
(http://www.praphas.com/PhotoForum/iot/Lab-13-LedControl/05.png)