admin 管理员组

文章数量: 1086940

用arduino mega2560通过isp给 arduino uno烧录程序

用arduino mega2560通过isp给 arduino uno烧录程序

一直想通过最基本的方式来烧录arduino程序,没有bootloader,不使用arduino IDE,基于avr-libc库来写程序,再用avrdude来烧录。

  • 由于手上有两块arduino板,一块uno,一块mega2560。就想着用mega来做isp工具,来给uno烧录程序,就有了以下过程

  • mega做成isp烧录工具

    1. 在arduino IDE里写入isp程序:

    2. 首先选择工具档: Tools > Board > mega2560

    3. 再选择 File > Example > ArduinoISP

    4. 编译上传


  • 连接meag和uno

    1. 参考这篇文章

      Arduino as ISP and Arduino Bootloaders | Arduino Documentation

    2. 具体是这张图:

    3. 描述如下:

      The Arduino MEGA above is programming an Arduino UNO connecting D51-D11, D50-D12, D52-D13, GND-GND, 5V-5V and D10 to RESET. This type of board needs a 10µF electrolytic capacitor connected to RESET and GND with the positive (long leg) connected to RESET. The capacitor has to be placed after the programmer board has been loaded with the ISP sketch.

      The 10µF electrolytic capacitor connected to RESET and GND of the programming board is needed only for the boards that have an interface between the microcontroller and the computer's USB, like Mega, UNO, Mini, Nano. Boards like Leonardo, Esplora and Micro, with the USB directly managed by the microcontroller, don't need the capacitor.

      注意:reset引脚上要接一 个电容


  • 通过arduino IDE确认烧录命令

    1. 打开一个想烧录的例子程序,比如 Blink
    2. 选择目标板(uno):Tools > Board > Arduino Uno
    3. 选择工具板 (mega2560):Tools > Port > 选择mega2560 (来作为"Arduino as ISP")
    4. 选择 Tools > Programmer > Arduino as ISP
    5. 注意,选择 Sketch > Upload Using Programmer ;而不是点工具栏的 编译。
    6. 第5步之后,就会通过isp方式,擦除uno的flash,并烧录blink的程序。可以在arduino IDE后台的log里找到刚刚执行的烧录的命令:
      • 我的系统是arch linux,我的用户目录为 /home/suah,截取的log的第一行就是烧录的命令,后续我们自己修改目标文件,就可以随时烧录自己的程序了

        /home/suah/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude -C/home/suah/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf -v -patmega328p -cstk500v1 -P/dev/ttyACM0 -b19200 -Uflash:w:/tmp/arduino_build_456577/Blink.ino.hex:iavrdude: Version 6.3-20190619Copyright (c) 2000-2005 Brian Dean, </>Copyright (c) 2007-2014 Joerg WunschSystem wide configuration file is "/home/suah/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"User configuration file is "/home/suah/.avrduderc"User configuration file does not exist or is not a regular file, skippingUsing Port                    : /dev/ttyACM0Using Programmer              : stk500v1Overriding Baud Rate          : 19200AVR Part                      : ATmega328PChip Erase delay              : 9000 usPAGEL                         : PD7BS2                           : PC2RESET disposition             : dedicatedRETRY pulse                   : SCKserial program mode           : yesparallel program mode         : yesTimeout                       : 200StabDelay                     : 100CmdexeDelay                   : 25SyncLoops                     : 32ByteDelay                     : 0PollIndex                     : 3PollValue                     : 0x53Memory Detail                 :Block Poll               Page                       PolledMemory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xffflash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xfflfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00Programmer Type : STK500Description     : Atmel STK500 Version 1.x firmwareHardware Version: 2Firmware Version: 1.18Topcard         : UnknownVtarget         : 0.0 VVaref           : 0.0 VOscillator      : OffSCK period      : 0.1 usavrdude: AVR device initialized and ready to accept instructionsReading | ################################################## | 100% 0.02savrdude: Device signature = 0x1e950f (probably m328p)
        avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performedTo disable this feature, specify the -D option.
        avrdude: erasing chip
        avrdude: reading input file "/tmp/arduino_build_456577/Blink.ino.hex"
        avrdude: writing flash (922 bytes):Writing | ################################################## | 100% 1.11savrdude: 922 bytes of flash written
        avrdude: verifying flash memory against /tmp/arduino_build_456577/Blink.ino.hex:
        avrdude: load data flash data from input file /tmp/arduino_build_456577/Blink.ino.hex:
        avrdude: input file /tmp/arduino_build_456577/Blink.ino.hex contains 922 bytes
        avrdude: reading on-chip flash data:Reading | ################################################## | 100% 0.62savrdude: verifying ...
        avrdude: 922 bytes of flash verifiedavrdude done.  Thank you.
        

  • 写个程序,手动编译,烧录
    • 程序 main.c

      #include <avr/io.h>
      #include <util/delay.h>void Led_Config();
      void Led_On();
      void Led_Off();void main()
      {Led_Config();while(1) {Led_On();_delay_ms(500);Led_Off();_delay_ms(500);}
      }void Led_Config()
      {DDRB |= (1 << 5);
      }void Led_On()
      {PORTB |= (1 << 5);
      }void Led_Off()
      {PORTB &= ~(1 << 5);
      }
      
    • Makefile(注意修改 烧录时的变量 AVRDUDE,使其与上一大步确认过的烧录命令一样)

      # parameters (change this stuff accordingly)
      # project name
      PRJ = main
      # avr mcu
      MCU = atmega328p
      # mcu clock frequency
      CLK = 16000000
      # avr programmer (and port if necessary)
      # e.g. PRG = usbtiny -or- PRG = arduino -P /dev/tty.usbmodem411
      PRG =# fuse values for avr: low, high, and extended
      # these values are from an Arduino Uno (ATMega328P)
      # see </> for other MCUs and options
      LFU = 0xFF
      HFU = 0xDE
      EFU = 0x05
      # program source files (not including external libraries)
      SRC = $(PRJ).c
      # where to look for external libraries (consisting of .c/.cpp files and .h files)
      # e.g. EXT = ../../EyeToSee ../../YouSART
      EXT =#################################################################################################
      # \\/ stuff nobody needs to worry about until such time that worrying about it is appropriate \\/ #
      ################################################################################################## include path
      INCLUDE := $(foreach dir, $(EXT), -I$(dir))
      # c flags
      CFLAGS    = -Wall -Os -DF_CPU=$(CLK) -mmcu=$(MCU) $(INCLUDE)
      # any aditional flags for c++
      CPPFLAGS =# executables
      AVRDUDE = avrdude -C ~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf  $(PRG) -v -p $(MCU) -cstk500v1 -P/dev/ttyACM0 -b19200
      OBJCOPY = avr-objcopy
      OBJDUMP = avr-objdump
      SIZE    = avr-size --format=avr --mcu=$(MCU)
      CC      = avr-gcc# generate list of objects
      CFILES    = $(filter %.c, $(SRC))
      EXTC     := $(foreach dir, $(EXT), $(wildcard $(dir)/*.c))
      CPPFILES  = $(filter %.cpp, $(SRC))
      EXTCPP   := $(foreach dir, $(EXT), $(wildcard $(dir)/*.cpp))
      OBJ       = $(CFILES:.c=.o) $(EXTC:.c=.o) $(CPPFILES:.cpp=.o) $(EXTCPP:.cpp=.o)# user targets
      # compile all files
      all: $(PRJ).hex# test programmer connectivity
      test:$(AVRDUDE) -v# flash program to mcu
      flash: all$(AVRDUDE) -U flash:w:$(PRJ).hex:i# write fuses to mcu
      fuse:$(AVRDUDE) -U lfuse:w:$(LFU):m -U hfuse:w:$(HFU):m -U efuse:w:$(EFU):m# generate disassembly files for debugging
      disasm: $(PRJ).elf$(OBJDUMP) -d $(PRJ).elf# remove compiled files
      clean:rm -f *.hex *.elf *.o$(foreach dir, $(EXT), rm -f $(dir)/*.o;)# other targets
      # objects from c files
      .c.o:$(CC) $(CFLAGS) -c $< -o $@# objects from c++ files
      .cpp.o:$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@# elf file
      $(PRJ).elf: $(OBJ)$(CC) $(CFLAGS) -o $(PRJ).elf $(OBJ)# hex file
      $(PRJ).hex: $(PRJ).elfrm -f $(PRJ).hex$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex$(SIZE) $(PRJ).elf
      
    • 把makefile和main.c放在同一目录下,先 执行make ,再make flash 直接烧录,如下log:(注意,提前安装好所需的工具链,如 :make,avr-gcc等 )

      > make
      avr-gcc -Wall -Os -DF_CPU=16000000 -mmcu=atmega328p  -c main.c -o main.o
      main.c:8:6: warning: return type of 'main' is not 'int' [-Wmain]8 | void main()|      ^~~~
      main.c: In function 'Led_Off':
      main.c:31:15: warning: array subscript 0 is outside array bounds of 'volatile uint8_t[0]' {aka 'volatile unsigned char[]'} [-Warray-bounds]31 |         PORTB &= ~(1 << 5);|               ^~
      avr-gcc -Wall -Os -DF_CPU=16000000 -mmcu=atmega328p  -o main.elf main.o
      rm -f main.hex
      avr-objcopy -j .text -j .data -O ihex main.elf main.hex
      avr-size --format=avr --mcu=atmega328p main.elf
      AVR Memory Usage
      ----------------
      Device: atmega328pProgram:     194 bytes (0.6% Full)
      (.text + .data + .bootloader)Data:          0 bytes (0.0% Full)
      (.data + .bss + .noinit)> make flash
      avrdude -C ~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf   -v -p atmega328p -cstk500v1 -P/dev/ttyACM0 -b19200 -U flash:w:main.hex:iavrdude: Version 7.0Copyright (c) Brian Dean, </>Copyright (c) Joerg WunschSystem wide configuration file is "/home/suah/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"User configuration file is "/home/suah/.avrduderc"User configuration file does not exist or is not a regular file, skippingUsing Port                    : /dev/ttyACM0Using Programmer              : stk500v1Overriding Baud Rate          : 19200AVR Part                      : ATmega328PChip Erase delay              : 9000 usPAGEL                         : PD7BS2                           : PC2RESET disposition             : dedicatedRETRY pulse                   : SCKSerial program mode           : yesParallel program mode         : yesTimeout                       : 200StabDelay                     : 100CmdexeDelay                   : 25SyncLoops                     : 32PollIndex                     : 3PollValue                     : 0x53Memory Detail                 :Block Poll               Page                       PolledMemory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------eeprom                 65    20     4    0 no       1024    4      0  3600  3600 0xff 0xffflash                  65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xfflfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00hfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00efuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00lock                    0     0     0    0 no          1    1      0  4500  4500 0x00 0x00calibration             0     0     0    0 no          1    1      0     0     0 0x00 0x00signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00Programmer Type : STK500Description     : Atmel STK500 Version 1.x firmwareHardware Version: 2Firmware Version: 1.18Topcard         : UnknownVtarget         : 0.0 VVaref           : 0.0 VOscillator      : OffSCK period      : 0.1 usavrdude: AVR device initialized and ready to accept instructionsReading | ################################################## | 100% 0.02savrdude: Device signature = 0x1e950f (probably m328p)
      avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performedTo disable this feature, specify the -D option.
      avrdude: erasing chip
      avrdude: reading input file "main.hex"
      avrdude: writing flash (194 bytes):Writing | ################################################## | 100% 0.28savrdude: 194 bytes of flash written
      avrdude: verifying flash memory against main.hex:Reading | ################################################## | 100% 0.16savrdude: 194 bytes of flash verifiedavrdude done.  Thank you.
      

本文标签: 用arduino mega2560通过isp给 arduino uno烧录程序