2014年11月17日 星期一

使用 Arduino UNO / Duemilnova 燒錄 Arduino bootloader

網頁最後修改時間:2018/02/07

在網路上蒐一下 Arduino bootloader,可以找到下面這一段描述:
What is a Bootloader?
Atmel AVRs are great little ICs, but they can be a bit tricky to program. You need a special programmer and some fancy .hex files, and its not very beginner friendly. The Arduino has largely done away with these issues. They’ve put a .hex file on their AVR chips that allows you to program the board over the serial port, meaning all you need to program your Arduino is a USB cable.
The bootloader is basically a .hex file that runs when you turn on the board. It is very similar to the BIOS that runs on your PC. It does two things. First, it looks around to see if the computer is trying to program it. If it is, it grabs the program from the computer and uploads it into the ICs memory (in a specific location so as not to overwrite the bootloader). That is why when you try to upload code, the Arduino IDE resets the chip. This basically turns the IC off and back on again so the bootloader can start running again. If the computer isn’t trying to upload code, it tells the chip to run the code that’s already stored in memory. Once it locates and runs your program, the Arduino continuously loops through the program and does so as long as the board has power.
Why Install a Bootloader
If you are building your own Arduino, or need to replace the IC, you will need to install the bootloader. You may also have a bad bootloader (although this is very rare) and need to reinstall the bootloader. There are also cases where you’ve put your board in a weird setting and reinstalling the bootloader and getting it back to factory settings is the easiest way to fix it. We’ve seen boards where people have turned off the serial port meaning that there is no way to upload code to the board, while there may be other ways to fix this, reinstalling the bootloader is probably the quickest and easiest. Like I said, having a bad bootloader is actually very very rare. If you have a new board that isn’t accepting code 99.9% of the time its not the bootloader, but for the 1% of the time it is, this guide will help you fix that problem.
source:Install an Arduino Bootloader 

由這篇網頁中的說明可以知道,Arduino bootloader 類似 PC 的 BIOS,主要做兩件事:負責確認電腦是否要對晶片編程。如果是,就將程式載入到 IC 記憶體中,所以這就是為什麼 Arduino IDE 在 Upload ( 上傳 ) sketch ( .ino) 時,要先重置晶片 ( reset )。如果不是,就轉而請晶片執行已存在記憶體中的程式。

學習怎麼燒錄 Arduino bootloader,可以避免 Arduino 板子上面的晶片壞掉時沒有備用的控制晶片可以用;另外也學習怎麼使用 ISP 的方式燒錄 ATMEL AVRs 晶片。

套件中的 ATMEGA328P-PU 晶片預設出貨是不預先燒錄 Arduino bootloader,所以要先在麵包板上建構基本的啟動電路,接著需要 Arduino 板子或是 ISP 燒錄器作為燒錄 bootloader 之用。Arduino bootloader 燒錄完成之後,就可以利用串列埠 ( serial port ) 上傳 sketch 來確認 Arduino bootloader 的燒錄成功了沒?這就是大概的 Arduino bootloader 燒錄與確認的步驟,如下:
  • 麵包板電路建置
  • 燒錄 Arduino Bootloader
  • 上傳第一個 Sketch 程式

麵包板電路設置:

若接線不是太大問題,可以直接從雲端硬碟下載接線的電路圖 ( eagleCAD ) 或麵包板佈線圖 ( Fritzing )。
燒錄 Arduino bootloader 基本電路零件
照個上面零件的編號,準備一片半尺寸麵包板 (或全尺寸麵包板 ) 和一隻剝線鉗,將單芯線 ( 1 ) 照下面照片中安裝的長度剝好線並插到位置上
佈線 - 01
 插上 ( 2 ) ATMEGS328P-PU ( 注意晶片凹朝位置朝左,第一支接腳在麵包板 9 的位置 )
佈線 - 02
 插上 ( 3 ) 按鈕到麵包板 6、8 位置 ( 注意按鈕的方向,接腳凸出的方向剛好朝麵包板上下數字 6 和 8 的位置 )
佈線 - 03
插上電阻 ( 4 ) 一腳到正 ( + ),另一腳到麵包板 9。
佈線 - 04
  連接一條黑線在麵包板 8 和 9
佈線 - 05
 再連接一條藍線在麵包板負 ( - ) 和 6
佈線 - 06
處理振盪器的部分。在麵包板 17 和 18 分別接上頭上有黑點的電容 ( 5 ) 到負 ( - )。這電容不分正負極性。
佈線 - 07
佈線 - 08
最後插上 16 MHz 振盪器 ( 6 ) 在麵包板 17 和 18
佈線 - 09
下方照片中間上方,靠近接腳 15 和17 的正負極處插上電容 ( 7 )。這電容不分正負極性。
佈線 - 10
設置狀態 LED 燈,一般都是設置為 D13 ( 也就是 ATMEGA328P-PU, PB5, Pin 19 )。

插上 LED 的限流電阻 ( 8 ) ( 就是電阻上面的色帶有兩條是相同顏色的 ),一邊在負 ( - )一邊在麵包板 23
佈線 - 11
 LED ( 9 ) 跨接在麵包板 18 和 23 處。

這樣就完成燒錄 Arduino bootloader 的電路,這個電路也是 ATMEGA328P-PU 可獨立運作的最小電路。
佈線 - 12
所完成的電路只要燒錄上 bootloader,晶片就如下圖左上角變成 Arduino 板子的控制晶片,不但可獨立運行,也可以做為 Arduino 板子上備用的控制晶片。
Arduino UNO 與 ATMEGS328P-PU pinout,來源:pighixx.com

"ATMEGA328P-PU Arduino UNO/Duemilnova bootloader 燒錄入門學習套件" 可至賣場購買。

燒錄 Arduino Bootloder:
燒錄 Arduino bootloader 可以使用現有的 Arduino 板子,只要上傳 ISP 的 sketch 到板子上去,就可以把 Arduino 板子當作 ISP 燒錄器用。另一種方法就是使用實際的 ISP 燒錄器,例如 USBASP、USBTiny、Atmel AVR MKII Programmer ... 等都可以。

這些 ISP 燒錄器都會使用 AVR ISR pinout 的接腳定義:6-pin 和 10-pin 兩種,接腳名稱如下所示。只要將燒錄器上面的這些接腳直接接到上面接好的麵包板晶片相對應的接腳上,設定好燒錄的參數之後就可以開始燒錄了。

AVR ISP pinouts – top view, 來源:sparkfun.com

Bootloader 燒錄接線的部分,不包括電源就只需要四條線!下面我會使用 Arduino Duemilnova 和 Arduino UNO 兩塊板子燒錄 Arduino UNO 的 bootloader 來做示範,至於若要選擇使用其他 ISP 燒錄器的話,也是相同的方式,步驟都是一樣,只差在 ISP 燒錄器與目標板 bootloader 的選擇而已。

 Arduino Duemilnova
/ UNO  
 麵包板   ATMEGA328P-PU  
+5V+
GND-
D109Pin1 ( RESET )
D1120Pin17 ( MOSI )
D1219Pin18 ( MISO )
D1318Pin19 ( SCK )

下面說明的 Arduino IDE 的版本是 arduino 1.5.8,版本不同或許會有一些差異,請再自行確認!

Arduino Duemilnova 作為 ISP 燒錄器,燒錄 Arduino UNO Bootloader 到 ATMEGA328P-PU:
使用 Arduino Duemilinova 燒錄 Arduino UNO bootloader 到麵包板上晶片

照著照片接好線之後,將 Arduino 連接上電腦再打開 Arduino IDE,確認連線的 COM 埠位置 ( 假設 Arduino Duemilnov 在作業系統上是使用 COM4 做通訊 )
  • 選擇作為ISP 的 Arduino 板子
    • "Tools / Board / Arduino Duemilnova or Diecimila"
    • "Tools / Processor / ATmega328"
    • "Tools / Port / COM4"
  • 選擇等一下要上傳的 Sketch 程式 "File / Examples / Arduino as ISP"
  • 上傳 "Arduino as ISP"

  • Arduino Duemilnova as ISP 燒錄 Arduino UNO bootloader
    • 選擇目標 bootloader 的板子 "Tools / Board / Arduino UNO" 和 COM 埠 "Tools / Port / COM4"
    • 選擇燒錄器 "Tools / Programmer / Arduino as ISP"
    • 選擇開始燒錄 "Tools / Burn Bootloader"



Arduino UNO 作為 ISP 燒錄器,燒錄 Arduino UNO Bootloader 到 ATMEGA328P-PU:
使用 Arduino UNO 燒錄 Arduino UNO bootloader 到麵包板上晶片

照著照片接好線之後,將 Arduino 連接上電腦再打開 Arduino IDE,確認連線的 COM 埠位置 ( 假設 Arduino Duemilnov 在作業系統上是使用 COM7 做通訊 )
  • 選擇作為ISP 的 Arduino 板子
    • "Tools / Board / Arduino UNO"
    • "Tools / Port / COM7"
  • 選擇等一下要上傳的 Sketch 程式 "File / Examples / Arduino as ISP"
  • 上傳 "Arduino as ISP"

  • Arduino UNO as ISP 燒錄 Arduino UNO bootloader
    • 選擇目標 bootloader 的板子 "Tools / Board / Arduino UNO" 和 COM 埠 "Tools / Port / COM7"
    • 選擇燒錄器 "Tools / Programmer / Arduino as ISP"
    • 選擇開始燒錄 "Tools / Burn Bootloader"


麵包板與公對公跳線可至賣場購買:
/*-/--*-*/*/*/*/***//-*-*-**-*/*-*-/*/*/*-*-/-////--/**/**--**/--///--//**----**//--**//**----***//*-**//*
燒錄 bootloader 另一個方法就是使用 "Arduino AVR ISP Bootloader 燒錄擴充板"
Arduino AVR ISP Bootloader 燒錄擴充板"
/*-/--*-*/*/*/*/***//-*-*-**-*/*-*-/*/*/*-*-/-////--/**/**--**/--///--//**----**//--**//**----***//*-**//*

上傳 Arduino Sketch:

一但 Bootloader 燒錄成功之後,就可以直接 Serial Port 來傳輸程式。

可利用 Arduino 板子上面的 UART ( D0:RX;D1:TX),或是 FTDI TTL-232R-3V3 USB - TTL Level Serial Converter Cable ( USB 轉 TTL 線 ) 來上傳 Sketch。


使用 USB 轉 TTL ( 沒有 DTR 接腳 ) 上傳 Arduino Sketch:

一般使用 USB 轉 TTL 線,若要能夠自動上傳,則額外需要 DTR 接腳接在 Arduino REST 接腳上。但並非每一種 USB 轉 TTL 線都會拉出 DTR 接腳,因此在上傳時則會因為沒有連接 DTR 到 RESET 接腳,在上傳 sketch 時無法自動重置晶片,因此可以利用手動重置晶片的方式完成上傳,這是以前在舊版的 Arduino 板子使用的方法。詳細的操作方法,請看下面提供的影片。



USB 轉 TTL 線可至賣場購買:

使用 Arduino 板子上傳 Sketch 的接線方式:

除了上面的方法之外,可以直接使用 Arduino 板子來上傳 Sketch 到麵包板上面的 ATMEGA328P-PU 晶片,因為在 Arduino 板子上就已經內建 Serial Port 通訊晶片,而且上傳 Sketch 不再需要手動重置晶片的動作了。

首先,先將 Arduino 板子上面的晶片拔掉,再照著下面表格接線 ( 或是參考下方個段落的照片 )

 Arduino Duemilnova
/ UNO 
 麵包板   ATMEGA328P-PU  
+5V+
GND-
RESET9Pin 1 ( RESET )
D0 ( RX )10Pin2 (TXD )
D1 ( TX )11

要確認 bootloader 是否上傳成功最簡單的方法就是由 Arduino IDE 上傳一個 Sketch。下面選用 Sketch  "File / Examples / 02.Digital / BlinkWithoutDelay"來上傳,如果成功就會看到 LED 一閃一滅。


使用 Arduino Duemilnova上傳 Arduino Sketch:
使用 Arduino Duemilnova 上傳 Sketch 到外置 ATMEGS328P-PU 晶片

Arduino IDE 的操作流程,請看下方影片



使用 Arduino UNO上傳 Arduino Sketch:

使用 Arduino UNO上傳 Sketch 到外置 ATMEGS328P-PU 晶片

Arduino IDE 的操作流程,請看下方影片



測試結果:

上傳成功之後,應該晶片就會自動重置,這時就會看到麵包板上的 LDE 一閃一滅。

Sketch upload 測試 - 01
Sketch upload 測試 - 02

結論:

學習使用現有的 Arduino 板子自己燒錄備用或是獨立可運作的 ATMEGA328P-PU 晶片,使用 Arduino IDE 做程式編輯將會非常方便。若對於 ATMEL AVR 晶片熟悉的話,另一種 Tiny 系列的晶片現在也可以使用 Arduino IDE 做開發,所不同的是:不需要預先燒錄 bootloader,而是可以直接使用 Arduino as ISP 或是 ISP 燒錄器上傳 Sketch,對於使用的 Sketch 程式容量不大的情況之下非常好用,程式設計師所要做的就是轉換使用的核心程式即可,幾乎是無痛轉換!

希望大家都能從這網頁中學到東西!!!


<<網頁相關資料下載>>

2 則留言:

  1. 您好
    我想請問reset鍵為什麼是這樣接啊?
    這樣不就變成沒有按下去就有電流通過嗎?
    謝謝

    回覆刪除
    回覆
    1. RESET 鍵是低態動作,沒用到的時候可以浮接或是串接電阻到 VCC。
      按下按鈕的時候,就會啟動晶片 RESET的 功能。

      刪除

留言屬名為"Unknown"或"不明"的用戶,大多這樣的留言都會直接被刪除掉,不會得到任何回覆!

發問問題,請描述清楚你(妳)的問題,別人回答前不會想去 "猜" 問題是什麼?

不知道怎麼發問,請看 [公告] 部落格提問須知 - 如何問問題 !