找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 16373|回复: 4
收起左侧

用MEGA2560给MEGA328P刷bootloader经验分享!

[复制链接]
ID:113207 发表于 2016-4-11 01:10 | 显示全部楼层 |阅读模式
前天手贱把自己的UNO R3板子引导给写坏了,手上又没有USBASP来恢复。不过有一个MEGA2560的板子。在论坛里看到有人用UNO的板子给UNO的板子写bootloader的例子,在想能不能用mega2560来给UNO写呢?
说干就干,打开arduino IDE 软件。文件--示例--ArduinoISP 。将这个示例下载进MEGA2560的开发板,如下接线:

155942y3u2bz3iy929br9z.png
然后选  工具--编程器--arduino as isp  --板子选uno ,然后各种失败!包括各个板子型号全部选一遍都是失败。reset线上加10uf电解电容,也失败。
无奈之下,去看前面给MEGA2560下载的那个ArduinoISP的文件里写的到底是什么东西,发现MEGA2560的引脚应该不是跟UNO一样,而是有自己的MISO,MOSI,SCK引脚。如果直接烧网上所说的ArduinoISP。那么引脚就有问题。于是尝试自己修改引脚。具体步骤如下:

找到如下代码位置:
160615yk5jqxxq8v54qzpp.png
按照如下修改:
160656zlrsltv10sspsls1.png
意思大概就是使用自定义的引脚。为什么是51  50 52呢,是查MEGA2560的引脚手册知道的。
为了保险起见,我将如下代码也注释掉了
160835sb2pz04wamopjlus.png
然后下载进mega2560的板子里。重新按照如下接线:
161025z7takigti1aabg3e.png

也就是将UNO的11  接2560的51。12接50,13接52。RESET线不变任然接10号线。

重新选择  工具  ---编程器  --- -arduino as isp ---板子选
161315vukluk0thaklk1au.png
因为R3增强板使用的是CH340T直接做USB转串口的,所以UNO不能用)。

然后  工具--烧录引导程序--成功。
UNO的13号灯会一闪一闪。



最后把我修改好的ArduinoISP代码附上,直接复制过去使用也行。
  1.     // ArduinoISP
  2.     // Copyright (c) 2008-2011 Randall Bohn
  3.     // If you require a license, see
  4.     //     [url=http://www.opensource.org/licenses/bsd-license.php]http://www.opensource.org/licenses/bsd-license.php[/url]
  5.     //
  6.     // This sketch turns the Arduino into a AVRISP
  7.     // using the following arduino pins:
  8.     //
  9.     // Pin 10 is used to reset the target microcontroller.
  10.     //
  11.     // By default, the hardware SPI pins MISO, MOSI and SCK pins are used
  12.     // to communicate with the target. On all Arduinos, these pins can be found
  13.     // on the ICSP/SPI header:
  14.     //
  15.     //               MISO °. . 5V (!) Avoid this pin on Due, Zero...
  16.     //               SCK   . . MOSI
  17.     //                     . . GND
  18.     //
  19.     // On some Arduinos (Uno,...), pins MOSI, MISO and SCK are the same pins
  20.     // as digital pin 11, 12 and 13, respectively. That is why many tutorials
  21.     // instruct you to hook up the target to these pins. If you find this wiring
  22.     // more practical, have a define USE_OLD_STYLE_WIRING. This will work even
  23.     // even when not using an Uno. (On an Uno this is not needed).
  24.     //
  25.     // Alternatively you can use any other digital pin by configuring software ('BitBanged')
  26.     // SPI and having appropriate defines for PIN_MOSI, PIN_MISO and PIN_SCK.
  27.     //
  28.     // IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...)
  29.     // as the programmer, make sure to not expose any of the programmer's pins to 5V.
  30.     // A simple way to accomplish this is to power the complete system (programmer
  31.     // and target) at 3V3.
  32.     //
  33.     // Put an LED (with resistor) on the following pins:
  34.     // 9: Heartbeat   - shows the programmer is running
  35.     // 8: Error       - Lights up if something goes wrong (use red if that makes sense)
  36.     // 7: Programming - In communication with the slave
  37.     //
  38.      
  39.     #include "Arduino.h"
  40.     #undef SERIAL
  41.      
  42.      
  43.     #define PROG_FLICKER true
  44.      
  45.     // Configure SPI clock (in Hz).
  46.     // E.g. for an attiny @128 kHz: the datasheet states that both the high
  47.     // and low spi clock pulse must be > 2 cpu cycles, so take 3 cycles i.e.
  48.     // divide target f_cpu by 6:
  49.     //     #define SPI_CLOCK            (128000/6)
  50.     //
  51.     // A clock slow enough for an attiny85 @ 1MHz, is a reasonable default:
  52.     #define SPI_CLOCK                 (1000000/6)
  53.      
  54.      
  55.     // Select hardware or software SPI, depending on SPI clock.
  56.     // Currently only for AVR, for other archs (Due, Zero,...),
  57.     // hardware SPI is probably too fast anyway.
  58.      
  59.     #if defined(ARDUINO_ARCH_AVR)
  60.      
  61.     #if SPI_CLOCK > (F_CPU / 128)
  62.     #define USE_HARDWARE_SPI
  63.     #endif
  64.      
  65.     #endif
  66.      
  67.     // Configure which pins to use:
  68.      
  69.     // The standard pin configuration.
  70.     #ifndef ARDUINO_HOODLOADER2
  71.      
  72.     #define RESET     10 // Use pin 10 to reset the target rather than SS
  73.     #define LED_HB    9
  74.     #define LED_ERR   8
  75.     #define LED_PMODE 7
  76.      
  77.     // Uncomment following line to use the old Uno style wiring
  78.     // (using pin 11, 12 and 13 instead of the SPI header) on Leonardo, Due...
  79.      
  80.     #define USE_OLD_STYLE_WIRING   //此处别忘记取消注释
  81.      
  82.     #ifdef USE_OLD_STYLE_WIRING
  83.      
  84.     #define PIN_MOSI        51    //mega2560的MOSI
  85.     #define PIN_MISO        50   //mega2560的MISO
  86.     #define PIN_SCK                52   //mega2560的SCK
  87.      
  88.     #endif
  89.      
  90.     // HOODLOADER2 means running sketches on the atmega16u2
  91.     // serial converter chips on Uno or Mega boards.
  92.     // We must use pins that are broken out:
  93.     #else
  94.      
  95.     #define RESET             10
  96.     #define LED_HB            7
  97.     #define LED_ERR           6
  98.     #define LED_PMODE         5
  99.      
  100.     #endif
  101.      
  102.     //// By default, use hardware SPI pins:
  103.     //#ifndef PIN_MOSI
  104.     //#define PIN_MOSI         MOSI
  105.     //#endif
  106.     //
  107.     //#ifndef PIN_MISO
  108.     //#define PIN_MISO         MISO
  109.     //#endif
  110.     //
  111.     //#ifndef PIN_SCK
  112.     //#define PIN_SCK         SCK
  113.     //#endif
  114.     //
  115.     //// Force bitbanged SPI if not using the hardware SPI pins:
  116.     //#if (PIN_MISO != MISO) ||  (PIN_MOSI != MOSI) || (PIN_SCK != SCK)
  117.     //#undef USE_HARDWARE_SPI
  118.     //#endif
  119.      
  120.      
  121.     // Configure the serial port to use.
  122.     //
  123.     // Prefer the USB virtual serial port (aka. native USB port), if the Arduino has one:
  124.     //   - it does not autoreset (except for the magic baud rate of 1200).
  125.     //   - it is more reliable because of USB handshaking.
  126.     //
  127.     // Leonardo and similar have an USB virtual serial port: 'Serial'.
  128.     // Due and Zero have an USB virtual serial port: 'SerialUSB'.
  129.     //
  130.     // On the Due and Zero, 'Serial' can be used too, provided you disable autoreset.
  131.     // To use 'Serial': #define SERIAL Serial
  132.      
  133.     #ifdef SERIAL_PORT_USBVIRTUAL
  134.     #define SERIAL SERIAL_PORT_USBVIRTUAL
  135.     #else
  136.     #define SERIAL Serial
  137.     #endif
  138.      
  139.      
  140.     // Configure the baud rate:
  141.      
  142.     #define BAUDRATE        19200
  143.     // #define BAUDRATE        115200
  144.     // #define BAUDRATE        1000000
  145.      
  146.      
  147.     #define HWVER 2
  148.     #define SWMAJ 1
  149.     #define SWMIN 18
  150.      
  151.     // STK Definitions
  152.     #define STK_OK      0x10
  153.     #define STK_FAILED  0x11
  154.     #define STK_UNKNOWN 0x12
  155.     #define STK_INSYNC  0x14
  156.     #define STK_NOSYNC  0x15
  157.     #define CRC_EOP     0x20 //ok it is a space...
  158.      
  159.     void pulse(int pin, int times);
  160.      
  161.     #ifdef USE_HARDWARE_SPI
  162.     #include "SPI.h"
  163.     #else
  164.      
  165.     #define SPI_MODE0 0x00
  166.      
  167.     class SPISettings {
  168.     public:
  169.       // clock is in Hz
  170.       SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock){
  171.         (void) bitOrder;
  172.         (void) dataMode;
  173.       };
  174.      
  175.     private:
  176.       uint32_t clock;
  177.      
  178.     friend class BitBangedSPI;
  179.     };
  180.      
  181.     class BitBangedSPI {
  182.     public:
  183.       void begin() {
  184.         digitalWrite(PIN_SCK, LOW);
  185.         digitalWrite(PIN_MOSI, LOW);
  186.         pinMode(PIN_SCK, OUTPUT);
  187.         pinMode(PIN_MOSI, OUTPUT);
  188.         pinMode(PIN_MISO, INPUT);
  189.       }
  190.      
  191.       void beginTransaction(SPISettings settings) {
  192.         pulseWidth = (500000 + settings.clock - 1) / settings.clock;
  193.         if (pulseWidth == 0)
  194.           pulseWidth = 1;
  195.       }
  196.      
  197.       void end() {}
  198.      
  199.       uint8_t transfer (uint8_t b) {
  200.         for (unsigned int i = 0; i < 8; ++i) {
  201.           digitalWrite(PIN_MOSI, (b & 0x80) ? HIGH : LOW);
  202.           digitalWrite(PIN_SCK, HIGH);
  203.           delayMicroseconds(pulseWidth);
  204.           b = (b << 1) | digitalRead(PIN_MISO);
  205.           digitalWrite(PIN_SCK, LOW); // slow pulse
  206.           delayMicroseconds(pulseWidth);
  207.         }
  208.         return b;
  209.       }
  210.      
  211.     private:
  212.       unsigned long pulseWidth; // in microseconds
  213.     };
  214.      
  215.     static BitBangedSPI SPI;
  216.      
  217.     #endif
  218.      
  219.     void setup() {
  220.       SERIAL.begin(BAUDRATE);
  221.      
  222.       pinMode(LED_PMODE, OUTPUT);
  223.       pulse(LED_PMODE, 2);
  224.       pinMode(LED_ERR, OUTPUT);
  225.       pulse(LED_ERR, 2);
  226.       pinMode(LED_HB, OUTPUT);
  227.       pulse(LED_HB, 2);
  228.      
  229.     }
  230.      
  231.     int error = 0;
  232.     int pmode = 0;
  233.     // address for reading and writing, set by 'U' command
  234.     unsigned int here;
  235.     uint8_t buff[256]; // global block storage
  236.      
  237.     #define beget16(addr) (*addr * 256 + *(addr+1) )
  238.     typedef struct param {
  239.       uint8_t devicecode;
  240.       uint8_t revision;
  241.       uint8_t progtype;
  242.       uint8_t parmode;
  243.       uint8_t polling;
  244.       uint8_t selftimed;
  245.       uint8_t lockbytes;
  246.       uint8_t fusebytes;
  247.       uint8_t flashpoll;
  248.       uint16_t eeprompoll;
  249.       uint16_t pagesize;
  250.       uint16_t eepromsize;
  251.       uint32_t flashsize;
  252.     }
  253.     parameter;
  254.      
  255.     parameter param;
  256.      
  257.     // this provides a heartbeat on pin 9, so you can tell the software is running.
  258.     uint8_t hbval = 128;
  259.     int8_t hbdelta = 8;
  260.     void heartbeat() {
  261.       static unsigned long last_time = 0;
  262.       unsigned long now = millis();
  263.       if ((now - last_time) < 40)
  264.         return;
  265.       last_time = now;
  266.       if (hbval > 192) hbdelta = -hbdelta;
  267.       if (hbval < 32) hbdelta = -hbdelta;
  268.       hbval += hbdelta;
  269.       analogWrite(LED_HB, hbval);
  270.     }
  271.      
  272.     static bool rst_active_high;
  273.      
  274.     void reset_target(bool reset) {
  275.       digitalWrite(RESET, ((reset && rst_active_high) || (!reset && !rst_active_high)) ? HIGH : LOW);
  276.     }
  277.      
  278.     void loop(void) {
  279.       // is pmode active?
  280.       if (pmode) {
  281.         digitalWrite(LED_PMODE, HIGH);
  282.       } else {
  283.         digitalWrite(LED_PMODE, LOW);
  284.       }
  285.       // is there an error?
  286.       if (error) {
  287.         digitalWrite(LED_ERR, HIGH);
  288.       } else {
  289.         digitalWrite(LED_ERR, LOW);
  290.       }
  291.      
  292.       // light the heartbeat LED
  293.       heartbeat();
  294.       if (SERIAL.available()) {
  295.         avrisp();
  296.       }
  297.     }
  298.      
  299.     uint8_t getch() {
  300.       while (!SERIAL.available());
  301.       return SERIAL.read();
  302.     }
  303.     void fill(int n) {
  304.       for (int x = 0; x < n; x++) {
  305.         buff[x] = getch();
  306.       }
  307.     }
  308.      
  309.     #define PTIME 30
  310.     void pulse(int pin, int times) {
  311.       do {
  312.         digitalWrite(pin, HIGH);
  313.         delay(PTIME);
  314.         digitalWrite(pin, LOW);
  315.         delay(PTIME);
  316.       } while (times--);
  317.     }
  318.      
  319.     void prog_lamp(int state) {
  320.       if (PROG_FLICKER) {
  321.         digitalWrite(LED_PMODE, state);
  322.       }
  323.     }
  324.      
  325.     uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  326.       SPI.transfer(a);
  327.       SPI.transfer(b);
  328.       SPI.transfer(c);
  329.       return SPI.transfer(d);
  330.     }
  331.      
  332.     void empty_reply() {
  333.       if (CRC_EOP == getch()) {
  334.         SERIAL.print((char)STK_INSYNC);
  335.         SERIAL.print((char)STK_OK);
  336.       } else {
  337.         error++;
  338.         SERIAL.print((char)STK_NOSYNC);
  339.       }
  340.     }
  341.      
  342.     void breply(uint8_t b) {
  343.       if (CRC_EOP == getch()) {
  344.         SERIAL.print((char)STK_INSYNC);
  345.         SERIAL.print((char)b);
  346.         SERIAL.print((char)STK_OK);
  347.       } else {
  348.         error++;
  349.         SERIAL.print((char)STK_NOSYNC);
  350.       }
  351.     }
  352.      
  353.     void get_version(uint8_t c) {
  354.       switch (c) {
  355.         case 0x80:
  356.           breply(HWVER);
  357.           break;
  358.         case 0x81:
  359.           breply(SWMAJ);
  360.           break;
  361.         case 0x82:
  362.           breply(SWMIN);
  363.           break;
  364.         case 0x93:
  365.           breply('S'); // serial programmer
  366.           break;
  367.         default:
  368.           breply(0);
  369.       }
  370.     }
  371.      
  372.     void set_parameters() {
  373.       // call this after reading paramter packet into buff[]
  374.       param.devicecode = buff[0];
  375.       param.revision   = buff[1];
  376.       param.progtype   = buff[2];
  377.       param.parmode    = buff[3];
  378.       param.polling    = buff[4];
  379.       param.selftimed  = buff[5];
  380.       param.lockbytes  = buff[6];
  381.       param.fusebytes  = buff[7];
  382.       param.flashpoll  = buff[8];
  383.       // ignore buff[9] (= buff[8])
  384.       // following are 16 bits (big endian)
  385.       param.eeprompoll = beget16(&buff[10]);
  386.       param.pagesize   = beget16(&buff[12]);
  387.       param.eepromsize = beget16(&buff[14]);
  388.      
  389.       // 32 bits flashsize (big endian)
  390.       param.flashsize = buff[16] * 0x01000000
  391.                         + buff[17] * 0x00010000
  392.                         + buff[18] * 0x00000100
  393.                         + buff[19];
  394.      
  395.       // avr devices have active low reset, at89sx are active high
  396.       rst_active_high = (param.devicecode >= 0xe0);
  397.     }
  398.      
  399.     void start_pmode() {
  400.      
  401.       // Reset target before driving PIN_SCK or PIN_MOSI
  402.      
  403.       // SPI.begin() will configure SS as output,
  404.       // so SPI master mode is selected.
  405.       // We have defined RESET as pin 10,
  406.       // which for many arduino's is not the SS pin.
  407.       // So we have to configure RESET as output here,
  408.       // (reset_target() first sets the correct level)
  409.       reset_target(true);
  410.       pinMode(RESET, OUTPUT);
  411.       SPI.begin();
  412.       SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
  413.      
  414.       // See avr datasheets, chapter "SERIAL_PRG Programming Algorithm":
  415.      
  416.       // Pulse RESET after PIN_SCK is low:
  417.       digitalWrite(PIN_SCK, LOW);
  418.       delay(20); // discharge PIN_SCK, value arbitrally chosen
  419.       reset_target(false);
  420.       // Pulse must be minimum 2 target CPU clock cycles
  421.       // so 100 usec is ok for CPU speeds above 20KHz
  422.       delayMicroseconds(100);
  423.       reset_target(true);
  424.      
  425.       // Send the enable programming command:
  426.       delay(50); // datasheet: must be > 20 msec
  427.       spi_transaction(0xAC, 0x53, 0x00, 0x00);
  428.       pmode = 1;
  429.     }
  430.      
  431.     void end_pmode() {
  432.       SPI.end();
  433.       // We're about to take the target out of reset
  434.       // so configure SPI pins as input
  435.       pinMode(PIN_MOSI, INPUT);
  436.       pinMode(PIN_SCK, INPUT);
  437.       reset_target(false);
  438.       pinMode(RESET, INPUT);
  439.       pmode = 0;
  440.     }
  441.      
  442.     void universal() {
  443.       uint8_t ch;
  444.      
  445.       fill(4);
  446.       ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]);
  447.       breply(ch);
  448.     }
  449.      
  450.     void flash(uint8_t hilo, unsigned int addr, uint8_t data) {
  451.       spi_transaction(0x40 + 8 * hilo,
  452.                       addr >> 8 & 0xFF,
  453.                       addr & 0xFF,
  454.                       data);
  455.     }
  456.     void commit(unsigned int addr) {
  457.       if (PROG_FLICKER) {
  458.         prog_lamp(LOW);
  459.       }
  460.       spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
  461.       if (PROG_FLICKER) {
  462.         delay(PTIME);
  463.         prog_lamp(HIGH);
  464.       }
  465.     }
  466.      
  467.     unsigned int current_page() {
  468.       if (param.pagesize == 32) {
  469.         return here & 0xFFFFFFF0;
  470.       }
  471.       if (param.pagesize == 64) {
  472.         return here & 0xFFFFFFE0;
  473.       }
  474.       if (param.pagesize == 128) {
  475.         return here & 0xFFFFFFC0;
  476.       }
  477.       if (param.pagesize == 256) {
  478.         return here & 0xFFFFFF80;
  479.       }
  480.       return here;
  481.     }
  482.      
  483.      
  484.     void write_flash(int length) {
  485.       fill(length);
  486.       if (CRC_EOP == getch()) {
  487.         SERIAL.print((char) STK_INSYNC);
  488.         SERIAL.print((char) write_flash_pages(length));
  489.       } else {
  490.         error++;
  491.         SERIAL.print((char) STK_NOSYNC);
  492.       }
  493.     }
  494.      
  495.     uint8_t write_flash_pages(int length) {
  496.       int x = 0;
  497.       unsigned int page = current_page();
  498.       while (x < length) {
  499.         if (page != current_page()) {
  500.           commit(page);
  501.           page = current_page();
  502.         }
  503.         flash(LOW, here, buff[x++]);
  504.         flash(HIGH, here, buff[x++]);
  505.         here++;
  506.       }
  507.      
  508.       commit(page);
  509.      
  510.       return STK_OK;
  511.     }
  512.      
  513.     #define EECHUNK (32)
  514.     uint8_t write_eeprom(unsigned int length) {
  515.       // here is a word address, get the byte address
  516.       unsigned int start = here * 2;
  517.       unsigned int remaining = length;
  518.       if (length > param.eepromsize) {
  519.         error++;
  520.         return STK_FAILED;
  521.       }
  522.       while (remaining > EECHUNK) {
  523.         write_eeprom_chunk(start, EECHUNK);
  524.         start += EECHUNK;
  525.         remaining -= EECHUNK;
  526.       }
  527.       write_eeprom_chunk(start, remaining);
  528.       return STK_OK;
  529.     }
  530.     // write (length) bytes, (start) is a byte address
  531.     uint8_t write_eeprom_chunk(unsigned int start, unsigned int length) {
  532.       // this writes byte-by-byte,
  533.       // page writing may be faster (4 bytes at a time)
  534.       fill(length);
  535.       prog_lamp(LOW);
  536.       for (unsigned int x = 0; x < length; x++) {
  537.         unsigned int addr = start + x;
  538.         spi_transaction(0xC0, (addr >> 8) & 0xFF, addr & 0xFF, buff[x]);
  539.         delay(45);
  540.       }
  541.       prog_lamp(HIGH);
  542.       return STK_OK;
  543.     }
  544.      
  545.     void program_page() {
  546.       char result = (char) STK_FAILED;
  547.       unsigned int length = 256 * getch();
  548.       length += getch();
  549.       char memtype = getch();
  550.       // flash memory @here, (length) bytes
  551.       if (memtype == 'F') {
  552.         write_flash(length);
  553.         return;
  554.       }
  555.       if (memtype == 'E') {
  556.         result = (char)write_eeprom(length);
  557.         if (CRC_EOP == getch()) {
  558.           SERIAL.print((char) STK_INSYNC);
  559.           SERIAL.print(result);
  560.         } else {
  561.           error++;
  562.           SERIAL.print((char) STK_NOSYNC);
  563.         }
  564.         return;
  565.       }
  566.       SERIAL.print((char)STK_FAILED);
  567.       return;
  568.     }
  569.      
  570.     uint8_t flash_read(uint8_t hilo, unsigned int addr) {
  571.       return spi_transaction(0x20 + hilo * 8,
  572.                              (addr >> 8) & 0xFF,
  573.                              addr & 0xFF,
  574.                              0);
  575.     }
  576.      
  577.     char flash_read_page(int length) {
  578.       for (int x = 0; x < length; x += 2) {
  579.         uint8_t low = flash_read(LOW, here);
  580.         SERIAL.print((char) low);
  581.         uint8_t high = flash_read(HIGH, here);
  582.         SERIAL.print((char) high);
  583.         here++;
  584.       }
  585.       return STK_OK;
  586.     }
  587.      
  588.     char eeprom_read_page(int length) {
  589.       // here again we have a word address
  590.       int start = here * 2;
  591.       for (int x = 0; x < length; x++) {
  592.         int addr = start + x;
  593.         uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF);
  594.         SERIAL.print((char) ee);
  595.       }
  596.       return STK_OK;
  597.     }
  598.      
  599.     void read_page() {
  600.       char result = (char)STK_FAILED;
  601.       int length = 256 * getch();
  602.       length += getch();
  603.       char memtype = getch();
  604.       if (CRC_EOP != getch()) {
  605.         error++;
  606.         SERIAL.print((char) STK_NOSYNC);
  607.         return;
  608.       }
  609.       SERIAL.print((char) STK_INSYNC);
  610.       if (memtype == 'F') result = flash_read_page(length);
  611.       if (memtype == 'E') result = eeprom_read_page(length);
  612.       SERIAL.print(result);
  613.     }
  614.      
  615.     void read_signature() {
  616.       if (CRC_EOP != getch()) {
  617.         error++;
  618.         SERIAL.print((char) STK_NOSYNC);
  619.         return;
  620.       }
  621.       SERIAL.print((char) STK_INSYNC);
  622.       uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
  623.       SERIAL.print((char) high);
  624.       uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
  625.       SERIAL.print((char) middle);
  626.       uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
  627.       SERIAL.print((char) low);
  628.       SERIAL.print((char) STK_OK);
  629.     }
  630.     //////////////////////////////////////////
  631.     //////////////////////////////////////////
  632.      
  633.      
  634.     ////////////////////////////////////
  635.     ////////////////////////////////////
  636.     void avrisp() {
  637.       uint8_t ch = getch();
  638.       switch (ch) {
  639.         case '0': // signon
  640.           error = 0;
  641.           empty_reply();
  642.           break;
  643.         case '1':
  644.           if (getch() == CRC_EOP) {
  645.             SERIAL.print((char) STK_INSYNC);
  646.             SERIAL.print("AVR ISP");
  647.             SERIAL.print((char) STK_OK);
  648.           }
  649.           else {
  650.             error++;
  651.             SERIAL.print((char) STK_NOSYNC);
  652.           }
  653.           break;
  654.         case 'A':
  655.           get_version(getch());
  656.           break;
  657.         case 'B':
  658.           fill(20);
  659.           set_parameters();
  660.           empty_reply();
  661.           break;
  662.         case 'E': // extended parameters - ignore for now
  663.           fill(5);
  664.           empty_reply();
  665.           break;
  666.         case 'P':
  667.           if (!pmode)
  668.             start_pmode();
  669.           empty_reply();
  670.           break;
  671.         case 'U': // set address (word)
  672.           here = getch();
  673.           here += 256 * getch();
  674.           empty_reply();
  675.           break;
  676.      
  677.         case 0x60: //STK_PROG_FLASH
  678.           getch(); // low addr
  679.           getch(); // high addr
  680.           empty_reply();
  681.           break;
  682.         case 0x61: //STK_PROG_DATA
  683.           getch(); // data
  684.           empty_reply();
  685.           break;
  686.      
  687.         case 0x64: //STK_PROG_PAGE
  688.           program_page();
  689.           break;
  690.      
  691.         case 0x74: //STK_READ_PAGE 't'
  692.           read_page();
  693.           break;
  694.      
  695.         case 'V': //0x56
  696.           universal();
  697.           break;
  698.         case 'Q': //0x51
  699.           error = 0;
  700.           end_pmode();
  701.           empty_reply();
  702.           break;
  703.      
  704.         case 0x75: //STK_READ_SIGN 'u'
  705.           read_signature();
  706.           break;
  707.      
  708.         // expecting a command, not CRC_EOP
  709.         // this is how we can get back in sync
  710.         case CRC_EOP:
  711.           error++;
  712.           SERIAL.print((char) STK_NOSYNC);
  713.           break;
  714.      
  715.         // anything else we will return STK_UNKNOWN
  716.         default:
  717.           error++;
  718.           if (CRC_EOP == getch())
  719.             SERIAL.print((char)STK_UNKNOWN);
  720.           else
  721.             SERIAL.print((char)STK_NOSYNC);
  722.       }
  723.     }
复制代码



回复

使用道具 举报

ID:138262 发表于 2016-9-19 11:01 | 显示全部楼层
那如果是MEGA给MEGA烧呢?可不可以烧?如果可以的话需要改哪些呢?
最近一直在为烧录头疼求解惑!!谢谢!!!
回复

使用道具 举报

ID:310510 发表于 2018-4-18 22:47 | 显示全部楼层
真牛,好长啊
回复

使用道具 举报

ID:412414 发表于 2018-10-19 18:14 | 显示全部楼层
本帖最后由 camell 于 2018-10-19 18:30 编辑

有用, 支持
回复

使用道具 举报

ID:412414 发表于 2018-10-19 18:28 | 显示全部楼层
本帖最后由 camell 于 2018-10-21 11:57 编辑

在这总结一下楼主的过程
一, 用mega2560写上arduinoISP例程做成写入器
    原例程需要修改两处:
             1,  // #define USE_OLD_STYLE_WIRING  此处要取消注释
             2,原有的11,12,13口改掉
                #define PIN_MOSI   51  
                #define PIN_MISO   50
                #define PIN_SCK     52
             环境arduinoISP, 板子是mega2560
二, 断mega2560的电, 然后在mega2560的reset与GND之间插上一个10uf的电容(不能在一时就插)
三, 将mega3560与nano的六针连接,  名字对应
四, 环境arduino as isp, 板子nano(old bootloader)(因为CH340), 点击写bootloader
五, 好了
六, 测试
         

评分

参与人数 1黑币 +80 收起 理由
admin + 80 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表