顯示具有 Arduino pro mini 標籤的文章。 顯示所有文章
顯示具有 Arduino pro mini 標籤的文章。 顯示所有文章

2025年6月9日 星期一

Arduino Nano program flash the hex, bin file

 這篇說明

1. 如何用 avrdude 燒錄 Arduino

2. 如何從 Arduino IDE 取得 hex , bin file


AVRDUDE is a utility to program AVR microcontrollers
https://github.com/avrdudes/avrdude/releases



✅ 前提條件(請確認)

  • 目標板子:Arduino Nano(ATmega328P)

  • 使用哪種方式上傳:

    1. 使用 USB 直接接 Nano(內建 CH340 或 FTDI)

    2. 使用 外部 ISP 燒錄器(如 USBasp、Arduino as ISP)


🔌 方式一:直接用 USB 線連接 Arduino Nano

Nano 自帶 bootloader,可以直接用 USB 上傳。

📌 指令範例(CH340 或 FTDI 晶片):

bash
avrdude -c arduino -p m328p -P COM3 -b 57600 -U flash:w:your_program.hex:i

📌 參數說明:

參數說明
-c arduino使用 Arduino bootloader protocol
-p m328pNano 的晶片 ATmega328P
-P COM3替換成你實際的 COM port
-b 57600舊版 bootloader(通常 Nano 這個 baudrate 就對)
-U flash:w:xxx.hex:i燒錄 hex 檔進 flash

💡 若你的 Nano 是新版(或 DIY 自燒 bootloader),也可能要改為 -b 115200


🧰 方式二:使用外部 ISP(如 USBasp 或 Arduino as ISP)

這會跳過 bootloader,直接燒錄程式進 flash。

【1】USBasp 方式:

bash
avrdude -c usbasp -p m328p -U flash:w:your_program.hex:i

【2】Arduino as ISP(使用 Arduino UNO 當 ISP):

bash
avrdude -c arduino -p m328p -P COM3 -b 19200 -U flash:w:your_program.hex:i

✅ 請確保 Arduino UNO 上有燒錄「ArduinoISP」範例程式


💡 從 Arduino IDE 取得 HEX 重點回顧

  • 點選:檔案 > 導出已編譯的二進位檔

  • 在專案資料夾會出現 .ino.hex 和 .ino.with_bootloader.hex

    • 一般使用 .ino.hex(純程式)

    • .with_bootloader.hex 是要燒錄整個 bootloader 的


2022年8月3日 星期三

74HC595移位暫存器,同時控制 16 個輸出

74HC595 是一顆八位元的移位暫存器,同時可以控制八個輸出,我們可以把多顆移位暫存器串接 (Daisy chain) 在一起以擴充腳位,例如: 如果串接兩顆 74HC595 移位暫存器,便可以同時控制 16 個輸出。

74HC595 總共有 16 支接腳,底下這 16 支接腳的圖表說明:


image_thumb4
腳位編號名稱說明
1-7, 15Q0 ~ Q7輸出腳位
8GND接地
7Q7’序列輸出 (Serial Out)
10MRMaster Reset, 清除所有資料, 低電位有效 (Active low)
11SH_CPSHift register clock pin (Clock Pin)
12ST_CPSTorage register clock pin (Latch Pin)
13OEOutput Enable, 允許輸出,低電位有效 (Active low)
14DS序列資料輸入 (Serial data input)
16Vcc供應電壓



步驟一:接上第一顆 74HC595 以及 8 顆 LED
  • 接上 8 顆 LED,將每顆 LED 短腳 (陰極) 直接接到 GND,然後將每顆 LED 的長腳 (陽極) 個別接至 74HC595 的輸出腳位 D0 ~ D7 並串接一顆 220 ohm 電阻
  • 74HC595 接線其一:
    • Vcc (pin 16) 與 MR (pin 10) 接 5V
    • GND (pin 8) 與 OE (pin 13) 接地
  • 74HC595 接線其二:
    • DS (pin 14) 接 Arduino pin 11 (下圖藍線)
    • ST_CP (pin 12, latch pin) 接 Arduino pin 8 (下圖綠線)
    • SH_CP (pin 11, clock pin) 接 Arduino pin 12 (下圖黃線)
  • 假如發現 LED 有震動閃爍的現象,可以在 ST_CP (pin 12, latch pin) 上接一顆 0.1uF 電容以去除閃爍現象
image_thumb21
步驟二:加上第二顆 74HC595,第二顆移位暫存器一樣要接線連到電源與接地
  • Vcc (pin 16) 與 MR (pin 10) 接 5V
  • GND (pin 8) 與 OE (pin 13) 接地
image
步驟三:把兩顆 74HC595 連接起來
只要把第一顆 74HC595 的 SH_CP (Clock Pin) 和 ST_CP (Latch Pin) 兩支腳位接到第二顆 74HC595 上 (下圖中的綠線以及黃線),接著把第一顆 74HC595 的 Q7’ (序列輸出腳) 接到第二顆 74HC595 的 DS (序列資料輸入) 就可以了 (下圖中的藍線)。
image
步驟四:加上第二組 LED
image
電路圖
image
(圖片來源: arduino.cc)
程式碼
底下這支程式 (Shiftout_With_Two_74HC595.pde) 會讓 16 顆 LED 由左往右一顆一顆點亮,實際效果請見範例影片,:
01// Lab12 使用兩顆 74HC595 和三支腳位控制 16 顆 LED
02
03// 接 74HC595 的 ST_CP (pin 12,latch pin)
04int latchPin = 8;
05// 接 74HC595 的 SH_CP (pin 11, clock pin)
06int clockPin = 12;
07// 接 74HC595 的 DS (pin 14)
08int dataPin = 11;
09
10void setup() {
11  // 將 latchPin, clockPin, dataPin 設置為輸出
12  pinMode(latchPin, OUTPUT);
13  pinMode(clockPin, OUTPUT);
14  pinMode(dataPin, OUTPUT);
15}
16
17void loop() {     
18  for (int led = 0; led < 16; led++) {
19    int numberToDisplay  = 1 << led;
20    byte high_Byte = highByte(numberToDisplay);
21    byte low_Byte = lowByte(numberToDisplay);
22     
23    // 送資料前要先把 latchPin 拉成低電位
24    digitalWrite(latchPin, LOW);
25     
26    // 先送高位元組 (Hight Byte), 給離 Arduino 較遠的那顆 74HC595
27    shiftOut(dataPin, clockPin, MSBFIRST, high_Byte); 
28    // 再送低位元組 (Low Byte), 給離 Arduino 較近的那顆 74HC595
29    shiftOut(dataPin, clockPin, MSBFIRST, low_Byte); 
30
31    // 送完資料後要把 latchPin 拉回成高電位
32    digitalWrite(latchPin, HIGH);
33    
34    delay(40);
35  }
36}


因為 shiftOut() 函式一次只能送一個位元組,所以必須將 numberDisplay 拆成兩個位元組分兩次傳送:
1// 先送高位元組 (Hight Byte), 給離 Arduino 較遠的那顆 74HC595
2shiftOut(dataPin, clockPin, MSBFIRST, high_Byte); 
3// 再送低位元組 (Low Byte), 給離 Arduino 較近的那顆 74HC595
4shiftOut(dataPin, clockPin, MSBFIRST, low_Byte); 

再次提醒:在送資料前,記得要先把 latchPin 拉成低電位,緊接著使用 shiftOut() 函式送出資料,送完資料後還要把 latchPin 拉回高電位。


shiftOut()


shiftOut()

Description

Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available.
Note: if you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW).
This is a software implementation; see also the SPI library, which provides a hardware implementation that is faster but works only on specific pins.

Syntax

shiftOut(dataPin, clockPin, bitOrder, value)