|
前篇:
沈大胖的MCU入門學習之路---04.03.阿當奴語言
http://bbs.pigoo.com/thread-52643-1-1.html
中斷了好一陣子,大胖最近終於又學習了阿當奴最讓人稱讚的函式庫(Libraries)操作方式,
首先,可以到原廠網頁上去看看目前阿當奴有那些標準的函式庫:
http://arduino.cc/en/Reference/Libraries
可以看到標準函式庫有:
Standard Libraries
EEPROM - reading and writing to "permanent" storage
Ethernet - for connecting to the internet using the Arduino Ethernet Shield
Firmata - for communicating with applications on the computer using a standard serial protocol.
GSM - for connecting to a GSM/GRPS network with the GSM shield.
LiquidCrystal - for controlling liquid crystal displays (LCDs)
SD - for reading and writing SD cards
Servo - for controlling servo motors
SPI - for communicating with devices using the Serial Peripheral Interface (SPI) Bus
SoftwareSerial - for serial communication on any digital pins. Version 1.0 and later of Arduino incorporate Mikal Hart's NewSoftSerial library as SoftwareSerial.
Stepper - for controlling stepper motors
TFT - for drawing text , images, and shapes on the Arduino TFT screen
WiFi - for connecting to the internet using the Arduino WiFi shield
Wire - Two Wire Interface (TWI/I2C) for sending and receiving data over a net of devices or sensors.
以上包含了EEEPROM(外置儲存)、LiquidCrystal(液晶顯示)、SD(SD卡)、Servo(伺服電機)、SPI(SPI通訊)、Wire(I2C通訊)等等,
實作上常用的驅動都已經是標準函式庫了,也就是說大胖可以直接使用或是修改這些函式庫,
簡單的來說,函式庫(Libraries)就比較類似個人電腦上其他周邊硬體介面的驅動程式。
接下來,大胖要實作一個比較簡單且常用的函式庫,LiquidCrystal函式庫(由Hitachi HD44780或相容芯片的驅動程式),
也就是常看到的LCD1602或是LCD2004等液晶螢幕。
一樣的相關類似的教學網路上也有一大堆:
葉難:http://yehnan.blogspot.tw/2012/0 ... itachi-hd44780.html
cooper Maa:http://coopermaa2nd.blogspot.tw/ ... 2x16-lcd-world.html
市面上的1602LCD大概都是由HD44780或相容的芯片所驅動的,
那麼大胖要在這個LCD顯示大胖想要的資訊要怎麼作呢?
傳統的作法:
①查看LCD的DATASHEET
②分析DATASHEEET上的腳位資訊
③定義ARDUINO及LCD的腳位連接
④查看訊號收送的時序圖並分析訊號時序
⑤編寫ARDUINO程式以符合LCD的訊號時序
⑥編譯燒錄並測試
⑦重覆①至⑥直到成功
使用函式庫的作法:
①查看LCD的DATASHEET
②分析DATASHEEET上的腳位資訊
③定義ARDUINO及LCD的腳位連接
④修改或直接使用函式庫
⑤編譯燒錄並測試
⑥重覆①至⑤直到成功
乍看下來傳統和使用函式庫的作法好像差不多,
但是在傳統作法的第④和第⑤步上就差很多了,
一個要自己研究,由無到有的寫出程式來,
使用函式庫的作法只要套用進去就行了!開發上的時間會差很多啊!
接下來大胖來看看這個函式庫的說明:
http://arduino.cc/en/Reference/LiquidCrystal
This library allows an Arduino board to control LiquidCrystal displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-based LCDs. The library works with in either 4- or 8-bit mode (i.e. using 4 or 8 data lines in addition to the rs, enable, and, optionally, the rw control lines).
上面說明了這個函式庫可讓使用者在ARDUINO上驅動基於Hitachi HD44780(或相容芯片)所製作的文字型LCD,
並且可以使用4BIT或是8BIT模式驅動。
Function:(此函式度所內建的函式)
LiquidCrystal()
begin()
clear()
home()
setCursor()
write()
print()
cursor()
noCursor()
blink()
noBlink()
display()
noDisplay()
scrollDisplayLeft()
scrollDisplayRight()
autoscroll()
noAutoscroll()
leftToRight()
rightToLeft()
createChar()
Examples:(此函式庫所包含的範例)
Hello World
Blink
Cursor
Display
Text Direction
Autoscroll
Serial input
SetCursor
Scroll
那麼大胖要怎麼使用這個標準函式庫,
直接看第一個「Hello World」這個範例:
http://arduino.cc/en/Tutorial/LiquidCrystal
並接照著原廠網頁上的說明將訊號線接好:
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
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
由於是標準的ARDUINO函式庫,都已內建在IDE內了,
大胖要作的是將程式碼放入IDE中,並使用include語法將函式庫包入:
- /*
- LiquidCrystal Library - Hello World
- Demonstrates the use a 16x2 LCD display. The LiquidCrystal
- library works with all LCD displays that are compatible with the
- Hitachi HD44780 driver. There are many of them out there, and you
- can usually tell them by the 16-pin interface.
- This sketch prints "Hello World!" to the LCD
- and shows the time.
- The circuit:(腳位定義)
- * LCD RS pin to digital pin 12
- * LCD Enable pin to digital pin 11
- * 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 R/W pin to ground
- * 10K resistor:
- * ends to +5V and ground
- * wiper to LCD VO pin (pin 3)
- Library originally added 18 Apr 2008
- by David A. Mellis
- library modified 5 Jul 2009
- by Limor Fried (http://www.ladyada.net)
- example added 9 Jul 2009
- by Tom Igoe
- modified 22 Nov 2010
- by Tom Igoe
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/LiquidCrystal
- */
- #include <Arduino.h>
- // include the library code:[color=Red](下面這行是最重要的,使用函式庫前一定要宣告函式庫的標頭檔位置並include進來)
- // (由於是標準函式庫,所以檔案的相對路徑可以不宣告出來)[/color]
- #include <LiquidCrystal.h>
- // initialize the library with the numbers of the interface pins(宣告腳位)
- LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
- void setup() {
- // set up the LCD's number of columns and rows:(宣告LCD類似,這裡使用的是1602,也就是有二行,每行16個字元的LCD)
- lcd.begin(16, 2);
- // Print a message to the LCD.(下面這行就是大胖要在LCD第一行所顯示的資訊)
- lcd.print("BBS.PIGOO.COM");
- }
- void loop() {
- // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- lcd.setCursor(0, 1);
- // print the number of seconds since reset:
- lcd.print("fatzeros");
- lcd.setCursor(10, 1);(在第二行的第10個字元讀秒)
- // print the number of seconds since reset:
- lcd.print(millis()/1000);
- }
複製代碼
好了就進行編譯及燒錄,
假如有編譯錯誤的話,一般來說都是函式庫未使用include語法包入所致
然後大胖得到它了:
以上就是ARDUINO標準函式庫的使用簡單介紹,
是不是很簡單,只要修改函式庫並編譯燒錄使用就行了,
等同於在ARDUINO上裝上其他周邊介面的驅動程式一般,
這就是ARDUINO能夠風行的原因之一啊!
謝謝收看! |
評分
-
19
查看全部評分
-
|