痞酷網_PIGOO

 找回密碼
 立即註冊
!!! [系統偵測到廣告阻擋軟體] !!!

如果您覺得痞酷網對您有些許幫助,或者您認同痞酷網的理想,

那麼希望您將痞酷網設定為白名單.

並請在上論壇的時候,動動您的手指,用行動支持我們.

謝謝!
查看: 6299|回復: 8

沈大胖的MCU入門學習之路---04.04.阿當奴(ARDUINO)的標準函式庫

[複製鏈接]
發表於 2015-3-18 20:46:16 | 顯示全部樓層 |閱讀模式
前篇:
沈大胖的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語法將函式庫包入:

  1. /*
  2.   LiquidCrystal Library - Hello World

  3. Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  4. library works with all LCD displays that are compatible with the
  5. Hitachi HD44780 driver. There are many of them out there, and you
  6. can usually tell them by the 16-pin interface.

  7. This sketch prints "Hello World!" to the LCD
  8. and shows the time.

  9.   The circuit:(腳位定義)
  10. * LCD RS pin to digital pin 12
  11. * LCD Enable pin to digital pin 11
  12. * LCD D4 pin to digital pin 4
  13. * LCD D5 pin to digital pin 5
  14. * LCD D6 pin to digital pin 6
  15. * LCD D7 pin to digital pin 7
  16. * LCD R/W pin to ground
  17. * 10K resistor:
  18. * ends to +5V and ground
  19. * wiper to LCD VO pin (pin 3)

  20. Library originally added 18 Apr 2008
  21. by David A. Mellis
  22. library modified 5 Jul 2009
  23. by Limor Fried (http://www.ladyada.net)
  24. example added 9 Jul 2009
  25. by Tom Igoe
  26. modified 22 Nov 2010
  27. by Tom Igoe

  28. This example code is in the public domain.

  29. http://www.arduino.cc/en/Tutorial/LiquidCrystal
  30. */

  31. #include <Arduino.h>

  32. // include the library code:[color=Red](下面這行是最重要的,使用函式庫前一定要宣告函式庫的標頭檔位置並include進來)
  33. //                                 (由於是標準函式庫,所以檔案的相對路徑可以不宣告出來)[/color]
  34. #include <LiquidCrystal.h>

  35. // initialize the library with the numbers of the interface pins(宣告腳位)
  36. LiquidCrystal lcd(12, 11, 4, 5, 6, 7);

  37. void setup() {
  38.   // set up the LCD's number of columns and rows:(宣告LCD類似,這裡使用的是1602,也就是有二行,每行16個字元的LCD)
  39.   lcd.begin(16, 2);
  40.   // Print a message to the LCD.(下面這行就是大胖要在LCD第一行所顯示的資訊)
  41.   lcd.print("BBS.PIGOO.COM");
  42. }

  43. void loop() {
  44.   // set the cursor to column 0, line 1
  45.   // (note: line 1 is the second row, since counting begins with 0):
  46.   lcd.setCursor(0, 1);
  47.   // print the number of seconds since reset:
  48.   lcd.print("fatzeros");

  49.   lcd.setCursor(10, 1);(在第二行的第10個字元讀秒)
  50.   // print the number of seconds since reset:
  51.   lcd.print(millis()/1000);
  52. }
複製代碼


好了就進行編譯及燒錄,
假如有編譯錯誤的話,一般來說都是函式庫未使用include語法包入所致

然後大胖得到它了:

rps20150318_175356.jpg

以上就是ARDUINO標準函式庫的使用簡單介紹,
是不是很簡單,只要修改函式庫並編譯燒錄使用就行了,
等同於在ARDUINO上裝上其他周邊介面的驅動程式一般,
這就是ARDUINO能夠風行的原因之一啊!
謝謝收看!

評分

19

查看全部評分

發表於 2015-3-18 22:33:13 | 顯示全部樓層
這胖子比三太子還厲害{:14_1477:}
完全不懂,就請嘉芬來看您~

評分

1

查看全部評分

發表於 2015-3-19 02:13:39 | 顯示全部樓層
popcorn 發表於 2015-3-18 10:33 PM
這胖子比三太子還厲害
完全不懂,就請嘉芬來看您~

太子爺~輸人不輸陣~~快點錢扎下去~{:14_1477:}
拼了!!!{:14_1478:}

評分

1

查看全部評分

發表於 2015-3-19 10:26:44 | 顯示全部樓層
謝謝分享經驗
發表於 2015-3-19 11:05:15 | 顯示全部樓層
感謝大大分享!!
http://www.microchip.com.tw/Data_CD/
我是學習microchip的↑這個網站也有教學課程
大大有興趣可以逛逛!!

Best Regards

評分

1

查看全部評分

發表於 2015-3-19 14:19:52 | 顯示全部樓層
有內建函數真是好用又省工很多 {:14_1485:}
發表於 2015-3-19 16:46:04 | 顯示全部樓層
你那個板子好像是mini arduino pro,你是用什麼做編譯與燒錄,它的程序如何?
 樓主| 發表於 2015-3-19 17:53:57 | 顯示全部樓層
本文章最後由 fatzeros 於 2015-3-19 06:09 PM 編輯
scottwang 發表於 2015-3-19 04:46 PM
你那個板子好像是mini arduino pro,你是用什麼做編譯與燒錄,它的程序如何?


rps20150319_175136.jpg
rps20150319_175053.jpg

這次使用的是新買的arduino nano pro(主芯片是atmega328p),
算是山寨的版本,13RMB一支,
原來的mini usb端子已改為micro usb,
另外背面背著的芯片由ft232改為ch340g,
方便與直接使用usb與pc進行serial連線。

另外使用的燒錄器就是之前大胖拿出來換酷幣用的,
請看這裡:
USBASP_AVR及ARDUINO下載器兌換
http://bbs.pigoo.com/thread-55394-1-1.html
(出處: 痞酷網_PIGOO)
這個下載器是大胖自己手工焊的,有大胖一貫焊點圓胖的風格!

IDE是使用CODEBLOCK FOR ARDUINO,
這裡有介紹:
http://bbs.pigoo.com/forum.php?m ... 2575&pid=632997
IDE內建有下載程式

這次燒錄的程序原始碼就如同本文裡的那段。

評分

2

查看全部評分

發表於 2015-3-22 14:42:03 | 顯示全部樓層
fatzeros 發表於 2015-3-19 05:53 PM
這次使用的是新買的arduino nano pro(主芯片是atmega328p),
算是山寨的版本,13RMB一支,
原來的mini ...

F大:
      好利害啊!! 我已經幾個月沒有碰了!!  汗顏!!
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

關閉

站長小叮嚀上一條 /1 下一條

禁閉室|手機版|連繫我們|痞酷網電子技術論壇

GMT+8, 2024-5-21 01:17 AM , Processed in 0.074099 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.