在arduino的板子上,作為核心的avr單片機往往都會燒錄一個bootloader,這個叫做bootloader的東東其實是arduino研發團隊針對arduino板子開發的一小段代碼,借助於這段代碼,我們可以在不用外部燒錄工具的情況下來把我們自己的代碼下載到AVR單片機中。為了使一些朋友更容易理解,不妨打個比方,bootloader類似於我們電腦中的windows操作系統,而我們的代碼則類似於運行於windows上的各種程序。
一般而言,arduino板的賣家都會把每塊板的bootloader都燒好后再出售,這樣買家直接收到板后就能夠把自己在arduinoIDE中編寫的程序借助PC的USB口來下載到arduino單片機內。當然,下載bootloader是需要借助於外部下載器的,可支持的下載器不少,基中比較具有性價比的是usbtinyisp,在淘寶上很多店家都有出售,比如易捷機器人電路的價格就只有38元。對於一般用戶,因為在下載自己代碼的時候偶爾會出現破壞bootloader的情況,就像PC的windows系統突然之間崩潰了一樣。這時候,就會需要用外部下載器來恢復這個bootloader,就相當於PC重裝系統。
在arduinoIDE的菜單中有一項是Burnbootloader,專門是用來燒bootloader用的,在連接好下載器和arduino板的ISP接口后,選擇"Burnbootloader",以目前主流的uno板為例,這時程序會按以下步驟自動操作:
(1)確認采用stk500的通訊協議。bootloader.atmega328P-<BOARD>.programmer(default value: stk500) is the protocal used by thebootloader.
(2)允許對相應內存地址空間操作。bootloader.atmega328P-<BOARD>.unlock_bits(default value: 0x3F) is the value to write to theATmega328 lock byte to unlock the bootloadersection.
(3)寫熔絲位的擴展位。bootloader.atmega328P-<BOARD>.extended_fuses(default value: 0x05) is the value to write tothe extended byte of the ATmega168fuses.
(4)寫熔絲位的高位。bootloader.atmega328P-<BOARD>.high_fuses(default value: 0xde) is the value to write to the high byte of theATmega328 fuses.
(5)寫熔絲位的低位。bootloader.atmega328P-<BOARD>.low_fuses(default value: 0xff) is the value to write to the low byte of theATmega328 fuses.
(6)給出bootloader文件的路徑。bootloader.atmega328P-<BOARD>.path(default value: optiboot) is the path (relative to the Arduinoapplication directory) containing the precompiled bootloader.
(7)給出bootloader文件名。bootloader.atmega328P-<BOARD>.file(default value: optiboot_atmega328.hex) is the name ofthe file containing the precompiled bootloader code (inbootloader.path).
(8)鎖定,禁止再修改相應內存空間。bootloader.atmega328P-<BOARD>.lock_bits(default value: 0x0F) is the value to write to theATmega328 lock byte to lock the bootloader section (soit doesn't get accidently overwritten when you upload asketch).
通常,arduino板在下載好了bootloader但還沒有upload任何用戶代碼的時候,會定義一個led(pin13)一閃一閃的,以作指示。