把ATmega128开发板转为Arduino

暑假来临,闲来无事,听有同学在玩arduino,挺想尝试看看,但是不想买啊,正好手中有一套ATmege128的开发板。以前听过arduino是由avr封装而成,于是就想把这avr开发板改造下。GOOGLE了下,还是有办法的。

其实Arduino,就是在AVR单片机上烧录了一个bootloader,在通过arduino集成开发环境与单片机进行通讯,继而进行片上编程。

个人原创,转载请注明原文地址:http://blog.csdn.net/embbnux/article/details/9391781

AVR环境:

编译环境: ubuntu + avr-gcc

烧写工具: avrdude+usbasp

具体构建步骤查看上一篇博客。

avr环境多种多样,在linux下还可以用eclipse集成环境。在window下推荐用WINAVR,其实本质上和linux下一样是用avr-gcc,烧写工具用prgisp.当然有了avr jtag仿真器就更好了,不用usbasp.

下载arduino工具:

arduino.cc/en/Main/Software

我下载的linux 64位版本,下载后解压到主目录下。

开发板:

BK-AVR128开发板

 

一、制作属于atmega128的bootloader

代码主要是arduino工具自带的,编译时有点问题,我改了几处。

可以直接到我的资源里面下载编译好的hex文件直接烧写到单片机就可以了:

download.csdn.net/detail/canyue102/5809329

源代码boot _mega128.c:


/**********************************************************/
/* Serial Bootloader for Atmel megaAVR Controllers */
/* */
/* tested with ATmega8, ATmega128 and ATmega168 */
/* should work with other mega's, see code for details */
/* */
/* */
/* */
/* */
/* 20090308: integrated Mega changes into main bootloader */
/* source by D. Mellis */
/* 20080930: hacked for Arduino Mega (with the 1280 */
/* processor, backwards compatible) */
/* by D. Cuartielles */
/* 20070626: hacked for Arduino Diecimila (which auto- */
/* resets when a USB connection is made to it) */
/* by D. Mellis */
/* 20060802: hacked for Arduino by D. Cuartielles */
/* based on a previous hack by D. Mellis */
/* and D. Cuartielles */
/* */
/* Monitor and debug functions were added to the original */
/* code by Dr. Erik Lins, chip45.com. (See below) */
/* */
/* Thanks to Karl Pitrich for fixing a bootloader pin */
/* problem and more informative LED blinking! */
/* */
/* For the latest version see: */
/* http://www.chip45.com/ */
/* */
/* ------------------------------------------------------ */
/* */
/* based on stk500boot.c */
/* Copyright (c) 2003, Jason P. Kyle */
/* All rights reserved. */
/* see avr1.org for original file and information */
/* */
/* This program is free software; you can redistribute it */
/* and/or modify it under the terms of the GNU General */
/* Public License as published by the Free Software */
/* Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will */
/* be useful, but WITHOUT ANY WARRANTY; without even the */
/* implied warranty of MERCHANTABILITY or FITNESS FOR A */
/* PARTICULAR PURPOSE. See the GNU General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU General */
/* Public License along with this program; if not, write */
/* to the Free Software Foundation, Inc., */
/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* */
/* Licence can be viewed at */
/* http://www.fsf.org/licenses/gpl.txt */
/* */
/* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
/* m8515,m8535. ATmega161 has a very small boot block so */
/* isn't supported. */
/* */
/* Tested with m168 */
/**********************************************************/

/* $Id$ */
/* some includes */
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>

/* the current avr-libc eeprom functions do not support the ATmega168 */
/* own eeprom write/read functions are used instead */
#if !defined(__AVR_ATmega168__) || !defined(__AVR_ATmega328P__)
#include <avr/eeprom.h>
#endif

/* Use the F_CPU defined in Makefile */

/* 20060803: hacked by DojoCorp */
/* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */
/* set the waiting time for the bootloader */
/* get this from the Makefile instead */
#define MAX_TIME_COUNT (F_CPU>>1)

/* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
#define MAX_ERROR_COUNT 5

/* set the UART baud rate */
/* 20060803: hacked by DojoCorp */
//#define BAUD_RATE 115200
#ifndef BAUD_RATE
#define BAUD_RATE 19200
#endif
/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
/* never allow AVR Studio to do an update !!!! */
#define HW_VER 0x02
#define SW_MAJOR 0x01
#define SW_MINOR 0x10
/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
/* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
/* ATmega1280 has four UARTS, but for Arduino Mega, we will only use RXD0 to get code */
/* BL0... means UART0, BL1... means UART1 */
#ifdef __AVR_ATmega128__
#define BL_DDR DDRF
#define BL_PORT PORTF
#define BL_PIN PINF
#define BL0 PINF7
#define BL1 PINF6
#elif defined __AVR_ATmega1280__
/* we just don't do anything for the MEGA and enter bootloader on reset anyway*/
#else
/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
#define BL_DDR DDRD
#define BL_PORT PORTD
#define BL_PIN PIND
#define BL PIND6
#endif
/* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
/* if monitor functions are included, LED goes on after monitor was entered */
#if defined __AVR_ATmega128__ || defined __AVR_ATmega1280__
/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128, Arduino Mega) */
#define LED_DDR DDRB
#define LED_PORT PORTB
#define LED_PIN PINB
#define LED PINB7
#else
/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duomilanuove */
/* other boards like e.g. Crumb8, Crumb168 are using PB2 */
#define LED_DDR DDRB
#define LED_PORT PORTB
#define LED_PIN PINB
#define LED PINB5
#endif
/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
#define MONITOR 1
#endif
/* define various device id's */
/* manufacturer byte is always the same */
#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(

#if defined __AVR_ATmega1280__
#define SIG2 0x97
#define SIG3 0x03
#define PAGE_SIZE 0x80U //128 words

#elif defined __AVR_ATmega1281__
#define SIG2 0x97
#define SIG3 0x04
#define PAGE_SIZE 0x80U //128 words

#elif defined __AVR_ATmega128__
#define SIG2 0x97
#define SIG3 0x02
#define PAGE_SIZE 0x80U //128 words

#elif defined __AVR_ATmega64__
#define SIG2 0x96
#define SIG3 0x02
#define PAGE_SIZE 0x80U //128 words

#elif defined __AVR_ATmega32__
#define SIG2 0x95
#define SIG3 0x02
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega16__
#define SIG2 0x94
#define SIG3 0x03
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega8__
#define SIG2 0x93
#define SIG3 0x07
#define PAGE_SIZE 0x20U //32 words

#elif defined __AVR_ATmega88__
#define SIG2 0x93
#define SIG3 0x0a
#define PAGE_SIZE 0x20U //32 words

#elif defined __AVR_ATmega168__
#define SIG2 0x94
#define SIG3 0x06
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega328P__
#define SIG2 0x95
#define SIG3 0x0F
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega162__
#define SIG2 0x94
#define SIG3 0x04
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega163__
#define SIG2 0x94
#define SIG3 0x02
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega169__
#define SIG2 0x94
#define SIG3 0x05
#define PAGE_SIZE 0x40U //64 words

#elif defined __AVR_ATmega8515__
#define SIG2 0x93
#define SIG3 0x06
#define PAGE_SIZE 0x20U //32 words

#elif defined __AVR_ATmega8535__
#define SIG2 0x93
#define SIG3 0x08
#define PAGE_SIZE 0x20U //32 words
#endif
/* function prototypes */
void putch(char);
char getch(void);
void getNch(uint8_t);
void byte_response(uint8_t);
void nothing_response(void);
char gethex(void);
void puthex(char);
void flash_led(uint8_t);

/* some variables */
union address_union {
 uint16_t word;
 uint8_t byte[2];
} address;

union length_union {
 uint16_t word;
 uint8_t byte[2];
} length;

struct flags_struct {
 unsigned eeprom : 1;
 unsigned rampz : 1;
} flags;

uint8_t buff[256];
uint8_t address_high;

uint8_t pagesz=0x80;

uint8_t i;
uint8_t bootuart = 0;
uint8_t NUM_LED_FLASHES=3;
uint8_t error_count = 0;

void (*app_start)(void) = 0x0000;
/* main program starts here */
int main(void)
{
 uint8_t ch,ch2;
 uint16_t w;

#ifdef WATCHDOG_MODS
 ch = MCUSR;
 MCUSR = 0;

WDTCSR |= _BV(WDCE) | _BV(WDE);
 WDTCSR = 0;

// Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
 if (! (ch & _BV(EXTRF))) // if its a not an external reset...
 app_start(); // skip bootloader
#else
 asm volatile("nop\n\t");
#endif

/* set pin direction for bootloader pin and enable pullup */
 /* for ATmega128, two pins need to be initialized */
#ifdef __AVR_ATmega128__
 BL_DDR &= ~_BV(BL0);
 BL_DDR &= ~_BV(BL1);
 BL_PORT |= _BV(BL0);
 BL_PORT |= _BV(BL1);
#else
 /* We run the bootloader regardless of the state of this pin. Thus, don't
 put it in a different state than the other pins. --DAM, 070709
 This also applies to Arduino Mega -- DC, 080930
 BL_DDR &= ~_BV(BL);
 BL_PORT |= _BV(BL);
 */
#endif
#ifdef __AVR_ATmega128__
 /* check which UART should be used for booting */
 if(bit_is_clear(BL_PIN, BL0)) {
 bootuart = 1;
 }
 else if(bit_is_clear(BL_PIN, BL1)) {
 bootuart = 2;
 }
#endif

#if defined __AVR_ATmega1280__
 /* the mega1280 chip has four serial ports ... we could eventually use any of them, or not? */
 /* however, we don't wanna confuse people, to avoid making a mess, we will stick to RXD0, TXD0 */
 bootuart = 1;
#endif

/* check if flash is programmed already, if not start bootloader anyway */
 if(pgm_read_byte_near(0x0000) != 0xFF) {

#ifdef __AVR_ATmega128__
 /* no UART was selected, start application */
 if(!bootuart) {
 app_start();
 }
#else
 /* check if bootloader pin is set low */
 /* we don't start this part neither for the m8, nor m168 */
 //if(bit_is_set(BL_PIN, BL)) {
 // app_start();
 // }
#endif
 }

#ifdef __AVR_ATmega128__
 /* no bootuart was selected, default to uart 0 */
 if(!bootuart) {
 bootuart = 1;
 }
#endif
 /* initialize UART(s) depending on CPU defined */
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 if(bootuart == 1) {
 UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
 UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UCSR0B = _BV(TXEN0)|_BV(RXEN0);
 }
 if(bootuart == 2) {
 UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
 UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
 UCSR1A = 0x00;
 UCSR1C = 0x06;
 UCSR1B = _BV(TXEN1)|_BV(RXEN1);
 }
#elif defined __AVR_ATmega163__
 UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
 UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
 UCSRA = 0x00;
 UCSRB = _BV(TXEN)|_BV(RXEN);
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)

#ifdef DOUBLE_SPEED
 UCSR0A = (1<<U2X0); //Double speed mode USART0
 UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*8L)-1);
 UBRR0H = (F_CPU/(BAUD_RATE*8L)-1) >> 8;
#else
 UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
 UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
#endif

UCSR0B = (1<<RXEN0) | (1<<TXEN0);
 UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);

/* Enable internal pull-up resistor on pin D0 (RX), in order
 to supress line noise that prevents the bootloader from
 timing out (DAM: 20070509) */
 DDRD &= ~_BV(PIND0);
 PORTD |= _BV(PIND0);
#elif defined __AVR_ATmega8__
 /* m8 */
 UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
 UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
 UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
 UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
#else
 /* m16,m32,m169,m8515,m8535 */
 UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
 UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
 UCSRA = 0x00;
 UCSRC = 0x06;
 UCSRB = _BV(TXEN)|_BV(RXEN);
#endif

#if defined __AVR_ATmega1280__
 /* Enable internal pull-up resistor on pin D0 (RX), in order
 to supress line noise that prevents the bootloader from
 timing out (DAM: 20070509) */
 /* feature added to the Arduino Mega --DC: 080930 */
 DDRE &= ~_BV(PINE0);
 PORTE |= _BV(PINE0);
#endif
 /* set LED pin as output */
 LED_DDR |= _BV(LED);
 /* flash onboard LED to signal entering of bootloader */
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 // 4x for UART0, 5x for UART1
 flash_led(NUM_LED_FLASHES + bootuart);
#else
 flash_led(NUM_LED_FLASHES);
#endif

/* 20050803: by DojoCorp, this is one of the parts provoking the
 system to stop listening, cancelled from the original */
 //putch('\0');

/* forever loop */
 for (;;) {

/* get character from UART */
 ch = getch();

/* A bunch of if...else if... gives smaller code than switch...case ! */

/* Hello is anyone home ? */
 if(ch=='0') {
 nothing_response();
 }
 /* Request programmer ID */
 /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
 /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
 else if(ch=='1') {
 if (getch() == ' ') {
 putch(0x14);
 putch('A');
 putch('V');
 putch('R');
 putch(' ');
 putch('I');
 putch('S');
 putch('P');
 putch(0x10);
 } else {
 if (++error_count == MAX_ERROR_COUNT)
 app_start();
 }
 }
 /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
 else if(ch=='@') {
 ch2 = getch();
 if (ch2>0x85) getch();
 nothing_response();
 }
 /* AVR ISP/STK500 board requests */
 else if(ch=='A') {
 ch2 = getch();
 if(ch2==0x80) byte_response(HW_VER); // Hardware version
 else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
 else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
 else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
 else byte_response(0x00); // Covers various unnecessary responses we don't care about
 }
 /* Device Parameters DON'T CARE, DEVICE IS FIXED */
 else if(ch=='B') {
 getNch(20);
 nothing_response();
 }
 /* Parallel programming stuff DON'T CARE */
 else if(ch=='E') {
 getNch(5);
 nothing_response();
 }
 /* P: Enter programming mode */
 /* R: Erase device, don't care as we will erase one page at a time anyway. */
 else if(ch=='P' || ch=='R') {
 nothing_response();
 }
 /* Leave programming mode */
 else if(ch=='Q') {
 nothing_response();
#ifdef WATCHDOG_MODS
 // autoreset via watchdog (sneaky!)
 WDTCSR = _BV(WDE);
 while (1); // 16 ms
#endif
 }
 /* Set address, little endian. EEPROM in bytes, FLASH in words */
 /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
 /* This might explain why little endian was used here, big endian used everywhere else. */
 else if(ch=='U') {
 address.byte[0] = getch();
 address.byte[1] = getch();
 nothing_response();
 }
 /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
 else if(ch=='V') {
 if (getch() == 0x30) {
 getch();
 ch = getch();
 getch();
 if (ch == 0) {
 byte_response(SIG1);
 } else if (ch == 1) {
 byte_response(SIG2);
 } else {
 byte_response(SIG3);
 }
 } else {
 getNch(3);
 byte_response(0x00);
 }
 }
 /* Write memory, length is big endian and is in bytes */
 else if(ch=='d') {
 length.byte[1] = getch();
 length.byte[0] = getch();
 flags.eeprom = 0;
 if (getch() == 'E') flags.eeprom = 1;
 for (w=0;w<length.word;w++) {
 buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
 }
 if (getch() == ' ') {
 if (flags.eeprom) { //Write to EEPROM one byte at a time
 address.word <<= 1;
 for(w=0;w<length.word;w++) {
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
 while(EECR & (1<<EEPE));
 EEAR = (uint16_t)(void *)address.word;
 EEDR = buff[w];
 EECR |= (1<<EEMPE);
 EECR |= (1<<EEPE);
#else
 eeprom_write_byte((void *)address.word,buff[w]);
#endif
 address.word++;
 }
 }
 else { //Write to FLASH one page at a time
 if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
 else address_high = 0x00;
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__)
 RAMPZ = address_high;
#endif
 address.word = address.word << 1; //address * 2 -> byte location
 /* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
 if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
 cli(); //Disable interrupts, just to be sure
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__)
 while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
#else
 while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete
#endif
 asm volatile(
 "clr r17 \n\t" //page_word_count
 "lds r30,address \n\t" //Address of FLASH location (in bytes)
 "lds r31,address+1 \n\t"
 "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
 "ldi r29,hi8(buff) \n\t"
 "lds r24,length \n\t" //Length of data to be written (in bytes)
 "lds r25,length+1 \n\t"
 "length_loop: \n\t" //Main loop, repeat for number of words in block
 "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
 "brne no_page_erase \n\t"
 "wait_spm1: \n\t"
 "lds r16,%0 \n\t" //Wait for previous spm to complete
 "andi r16,1 \n\t"
 "cpi r16,1 \n\t"
 "breq wait_spm1 \n\t"
 "ldi r16,0x03 \n\t" //Erase page pointed to by Z
 "sts %0,r16 \n\t"
 "spm \n\t"
#ifdef __AVR_ATmega163__
 ".word 0xFFFF \n\t"
 "nop \n\t"
#endif
 "wait_spm2: \n\t"
 "lds r16,%0 \n\t" //Wait for previous spm to complete
 "andi r16,1 \n\t"
 "cpi r16,1 \n\t"
 "breq wait_spm2 \n\t"

"ldi r16,0x11 \n\t" //Re-enable RWW section
 "sts %0,r16 \n\t"
 "spm \n\t"
#ifdef __AVR_ATmega163__
 ".word 0xFFFF \n\t"
 "nop \n\t"
#endif
 "no_page_erase: \n\t"
 "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
 "ld r1,Y+ \n\t"

"wait_spm3: \n\t"
 "lds r16,%0 \n\t" //Wait for previous spm to complete
 "andi r16,1 \n\t"
 "cpi r16,1 \n\t"
 "breq wait_spm3 \n\t"
 "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
 "sts %0,r16 \n\t"
 "spm \n\t"

"inc r17 \n\t" //page_word_count++
 "cpi r17,%1 \n\t"
 "brlo same_page \n\t" //Still same page in FLASH
 "write_page: \n\t"
 "clr r17 \n\t" //New page, write current one first
 "wait_spm4: \n\t"
 "lds r16,%0 \n\t" //Wait for previous spm to complete
 "andi r16,1 \n\t"
 "cpi r16,1 \n\t"
 "breq wait_spm4 \n\t"
#ifdef __AVR_ATmega163__
 "andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
#endif
 "ldi r16,0x05 \n\t" //Write page pointed to by Z
 "sts %0,r16 \n\t"
 "spm \n\t"
#ifdef __AVR_ATmega163__
 ".word 0xFFFF \n\t"
 "nop \n\t"
 "ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
#endif
 "wait_spm5: \n\t"
 "lds r16,%0 \n\t" //Wait for previous spm to complete
 "andi r16,1 \n\t"
 "cpi r16,1 \n\t"
 "breq wait_spm5 \n\t"
 "ldi r16,0x11 \n\t" //Re-enable RWW section
 "sts %0,r16 \n\t"
 "spm \n\t"
#ifdef __AVR_ATmega163__
 ".word 0xFFFF \n\t"
 "nop \n\t"
#endif
 "same_page: \n\t"
 "adiw r30,2 \n\t" //Next word in FLASH
 "sbiw r24,2 \n\t" //length-2
 "breq final_write \n\t" //Finished
 "rjmp length_loop \n\t"
 "final_write: \n\t"
 "cpi r17,0 \n\t"
 "breq block_done \n\t"
 "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
 "rjmp write_page \n\t"
 "block_done: \n\t"
 "clr __zero_reg__ \n\t" //restore zero register
#if defined __AVR_ATmega168__ || __AVR_ATmega328P__ || __AVR_ATmega128__ || __AVR_ATmega1280__ || __AVR_ATmega1281__
 : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
#else
 : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
#endif
 );
 /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
 /* exit the bootloader without a power cycle anyhow */
 }
 putch(0x14);
 putch(0x10);
 } else {
 if (++error_count == MAX_ERROR_COUNT)
 app_start();
 }
 }
 /* Read memory block mode, length is big endian. */
 else if(ch=='t') {
 length.byte[1] = getch();
 length.byte[0] = getch();
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
 else flags.rampz = 0;
#endif
 address.word = address.word << 1; // address * 2 -> byte location
 if (getch() == 'E') flags.eeprom = 1;
 else flags.eeprom = 0;
 if (getch() == ' ') { // Command terminator
 putch(0x14);
 for (w=0;w < length.word;w++) { // Can handle odd and even lengths okay
 if (flags.eeprom) { // Byte access EEPROM read
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
 while(EECR & (1<<EEPE));
 EEAR = (uint16_t)(void *)address.word;
 EECR |= (1<<EERE);
 putch(EEDR);
#else
 putch(eeprom_read_byte((void *)address.word));
#endif
 address.word++;
 }
 else {

if (!flags.rampz) putch(pgm_read_byte_near(address.word));
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 else putch(pgm_read_byte_far(address.word + 0x10000));
 // Hmmmm, yuck FIXME when m256 arrvies
#endif
 address.word++;
 }
 }
 putch(0x10);
 }
 }
 /* Get device signature bytes */
 else if(ch=='u') {
 if (getch() == ' ') {
 putch(0x14);
 putch(SIG1);
 putch(SIG2);
 putch(SIG3);
 putch(0x10);
 } else {
 if (++error_count == MAX_ERROR_COUNT)
 app_start();
 }
 }
 /* Read oscillator calibration byte */
 else if(ch=='v') {
 byte_response(0x00);
 }
#if defined MONITOR

/* here come the extended monitor commands by Erik Lins */

/* check for three times exclamation mark pressed */
 else if(ch=='!') {
 ch = getch();
 if(ch=='!') {
 ch = getch();
 if(ch=='!') {
 PGM_P welcome = "";
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 uint16_t extaddr;
#endif
 uint8_t addrl, addrh;

#ifdef CRUMB128
 welcome = "ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
#elif defined PROBOMEGA128
 welcome = "ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
#elif defined SAVVY128
 welcome = "ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
#elif defined __AVR_ATmega1280__
 welcome = "ATmegaBOOT / Arduino Mega - (C) Arduino LLC - 090930\n\r";
#endif

/* turn on LED */
 LED_DDR |= _BV(LED);
 LED_PORT &= ~_BV(LED);

/* print a welcome message and command overview */
 for(i=0; welcome[i] != '\0'; ++i) {
 putch(welcome[i]);
 }

/* test for valid commands */
 for(;;) {
 putch('\n');
 putch('\r');
 putch(':');
 putch(' ');

ch = getch();
 putch(ch);

/* toggle LED */
 if(ch == 't') {
 if(bit_is_set(LED_PIN,LED)) {
 LED_PORT &= ~_BV(LED);
 putch('1');
 } else {
 LED_PORT |= _BV(LED);
 putch('0');
 }
 }

/* read byte from address */
 else if(ch == 'r') {
 ch = getch(); putch(ch);
 addrh = gethex();
 addrl = gethex();
 putch('=');
 ch = *(uint8_t *)((addrh << 8) + addrl);
 puthex(ch);
 }

/* write a byte to address */
 else if(ch == 'w') {
 ch = getch(); putch(ch);
 addrh = gethex();
 addrl = gethex();
 ch = getch(); putch(ch);
 ch = gethex();
 *(uint8_t *)((addrh << 8) + addrl) = ch;
 }

/* read from uart and echo back */
 else if(ch == 'u') {
 for(;;) {
 putch(getch());
 }
 }
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 /* external bus loop */
 else if(ch == 'b') {
 putch('b');
 putch('u');
 putch('s');
 MCUCR = 0x80;
 XMCRA = 0;
 XMCRB = 0;
 extaddr = 0x1100;
 for(;;) {
 ch = *(volatile uint8_t *)extaddr;
 if(++extaddr == 0) {
 extaddr = 0x1100;
 }
 }
 }
#endif

else if(ch == 'j') {
 app_start();
 }

} /* end of monitor functions */

}
 }
 }
 /* end of monitor */
#endif
 else if (++error_count == MAX_ERROR_COUNT) {
 app_start();
 }
 } /* end of forever loop */

}
char gethexnib(void) {
 char a;
 a = getch(); putch(a);
 if(a >= 'a') {
 return (a - 'a' + 0x0a);
 } else if(a >= '0') {
 return(a - '0');
 }
 return a;
}
char gethex(void) {
 return (gethexnib() << 4) + gethexnib();
}
void puthex(char ch) {
 char ah;

ah = ch >> 4;
 if(ah >= 0x0a) {
 ah = ah - 0x0a + 'a';
 } else {
 ah += '0';
 }

ch &= 0x0f;
 if(ch >= 0x0a) {
 ch = ch - 0x0a + 'a';
 } else {
 ch += '0';
 }

putch(ah);
 putch(ch);
}
void putch(char ch)
{
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 if(bootuart == 1) {
 while (!(UCSR0A & _BV(UDRE0)));
 UDR0 = ch;
 }
 else if (bootuart == 2) {
 while (!(UCSR1A & _BV(UDRE1)));
 UDR1 = ch;
 }
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
 while (!(UCSR0A & _BV(UDRE0)));
 UDR0 = ch;
#else
 /* m8,16,32,169,8515,8535,163 */
 while (!(UCSRA & _BV(UDRE)));
 UDR = ch;
#endif
}
char getch(void)
{
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 uint32_t count = 0;
 if(bootuart == 1) {
 while(!(UCSR0A & _BV(RXC0))) {
 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
 /* HACKME:: here is a good place to count times*/
 count++;
 if (count > MAX_TIME_COUNT)
 app_start();
 }

return UDR0;
 }
 else if(bootuart == 2) {
 while(!(UCSR1A & _BV(RXC1))) {
 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
 /* HACKME:: here is a good place to count times*/
 count++;
 if (count > MAX_TIME_COUNT)
 app_start();
 }

return UDR1;
 }
 return 0;
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
 uint32_t count = 0;
 while(!(UCSR0A & _BV(RXC0))){
 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
 /* HACKME:: here is a good place to count times*/
 count++;
 if (count > MAX_TIME_COUNT)
 app_start();
 }
 return UDR0;
#else
 /* m8,16,32,169,8515,8535,163 */
 uint32_t count = 0;
 while(!(UCSRA & _BV(RXC))){
 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
 /* HACKME:: here is a good place to count times*/
 count++;
 if (count > MAX_TIME_COUNT)
 app_start();
 }
 return UDR;
#endif
}
void getNch(uint8_t count)
{
 while(count--) {
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
 if(bootuart == 1) {
 while(!(UCSR0A & _BV(RXC0)));
 UDR0;
 }
 else if(bootuart == 2) {
 while(!(UCSR1A & _BV(RXC1)));
 UDR1;
 }
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
 getch();
#else
 /* m8,16,32,169,8515,8535,163 */
 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
 //while(!(UCSRA & _BV(RXC)));
 //UDR;
 getch(); // need to handle time out
#endif
 }
}
void byte_response(uint8_t val)
{
 if (getch() == ' ') {
 putch(0x14);
 putch(val);
 putch(0x10);
 } else {
 if (++error_count == MAX_ERROR_COUNT)
 app_start();
 }
}
void nothing_response(void)
{
 if (getch() == ' ') {
 putch(0x14);
 putch(0x10);
 } else {
 if (++error_count == MAX_ERROR_COUNT)
 app_start();
 }
}

void flash_led(uint8_t count)
{
 while (count--) {
 LED_PORT |= _BV(LED);
 _delay_ms(100);
 LED_PORT &= ~_BV(LED);
 _delay_ms(100);
 }
}
/* end of file */

编译:

avr-gcc -mmcu=atmega48 -Wall -Os -o boot_mega128.o boot_mega128.c

avr-objcopy -j .text -j .data -O boot_mega128.o boot_mega.hex

烧写:

用usbasp连接电脑和开发板

sudo avrdude -p m128 -c usbasp -P usbasp -Uefuse:w:0xff:m -Uhfuse:w:0xca:m -Ulfuse:w:0xff:m -Ulock:w:0x3F:m

sudo avrdude -p m128 -c usbasp -e -U flash:w:boot_mega128.hex -Ulock:w:0x0F:m

 二、Arduino开发环境设置

参考:

arduinoexplained.blogspot.com/2012/03/custom-board-programming-on-arduino-10.html

解压刚才下载的arduino

进入该目录

修改/hardware/arduino/avr/boards.txt

在最后添加:


###############################################################
atmega128A.name=Custom ATmega 128 Breakout Board using AVRISP
atmega128A.upload.using=avrispmkii
atmega128A.upload.maximum_size=126976
atmega128A.bootloader.low_fuses=0xFF
atmega128A.bootloader.high_fuses=0xCA
atmega128A.bootloader.extended_fuses=0xFF
atmega128A.bootloader.path=atmega
atmega128A.bootloader.file=boot_mega128.hex
atmega128A.bootloader.unlock_bits=0x3F
atmega128A.bootloader.lock_bits=0x0F
atmega128A.build.mcu=atmega128
atmega128A.build.f_cpu=8000000L
atmega128A.build.core=arduino
atmega128A.build.variant=standard
##############################################################

三、设置arduino引脚定义

在网上没有找到atmega128的arduino pin mapping,于是自己写了一个

在/hardware/arduino/avr/variant文件夹下新建一个文件夹mega128

在mega128里面新建一个pins_arduino.h


/*
 pins_arduino.h - Pin definition functions for Arduino ATmega128
 Part of Arduino - http://blog.csdn.net/canyue102/article/details/9451771

Copyright (c) 2013 Dongyu_canyue102

This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

*/

#ifndef Pins_Arduino_h
#define Pins_Arduino_h

#include <avr/pgmspace.h>

#define NUM_DIGITAL_PINS 53
#define NUM_ANALOG_INPUTS 8
#define analogInputToDigitalPin(p) ((p < 8) ? (p) + 45 : -1)
#define digitalPinHasPWM(p) (((p) >= 12 && (p) <= 15) || ((p) >= 35 && (p)<= 37))

static const uint8_t SS = 8;
static const uint8_t MOSI = 10;
static const uint8_t MISO = 11;
static const uint8_t SCK = 9;

static const uint8_t SDA = 25;
static const uint8_t SCL = 24;
static const uint8_t LED_BUILTIN = 12;

static const uint8_t A0 = 40;
static const uint8_t A1 = 41;
static const uint8_t A2 = 42;
static const uint8_t A3 = 43;
static const uint8_t A4 = 44;
static const uint8_t A5 = 45;
static const uint8_t A6 = 46;
static const uint8_t A7 = 47;

// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
// Only pins available for RECEIVE (TRANSMIT can be on any pin):
// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me)
// Pins: 0-53

#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 53) ? (&PCICR) : ((uint8_t *)0))

#define digitalPinToPCICRbit(p) ( (((p) >= 0) && ((p) <= 7)) || (((p) >= 8) && ((p) <=47 )) ? 0 : \
 ( (((p) >= 48) && ((p) <= 52)) ? 2 : \
 0 ) )

#define digitalPinToPCMSK(p) ( (((p) >= 0) && ((p) <= 7)) || (((p) >= 8) && ((p) <= 47)) ? (&PCMSK0) : \
 ( (((p) >= 48) && ((p) <= 52)) ? (&PCMSK2) : \
 ((uint8_t *)0) ) )

#define digitalPinToPCMSKbit(p) ( (((p) >= 0) && ((p) <= 7)) ? ((p) - 6) : \
 ( ((p) == 8) ? 3 : \
 ( ((p) == 9) ? 2 : \
 ( ((p) == 10) ? 1 : \
 ( ((p) == 11) ? 0 : \
 ( (((p) >= 12) && ((p) <= 52)) ? ((p) - 12) : \
 0 ) ) ) ) ) )

#ifdef ARDUINO_MAIN

const uint16_t PROGMEM port_to_mode_PGM[] = {
 NOT_A_PORT,
 (uint16_t) &DDRA,
 (uint16_t) &DDRB,
 (uint16_t) &DDRC,
 (uint16_t) &DDRD,
 (uint16_t) &DDRE,
 (uint16_t) &DDRF,
 (uint16_t) &DDRG,

};

const uint16_t PROGMEM port_to_output_PGM[] = {
 NOT_A_PORT,
 (uint16_t) &PORTA,
 (uint16_t) &PORTB,
 (uint16_t) &PORTC,
 (uint16_t) &PORTD,
 (uint16_t) &PORTE,
 (uint16_t) &PORTF,
 (uint16_t) &PORTG,

};

const uint16_t PROGMEM port_to_input_PGM[] = {
 NOT_A_PIN,
 (uint16_t) &PINA,
 (uint16_t) &PINB,
 (uint16_t) &PINC,
 (uint16_t) &PIND,
 (uint16_t) &PINE,
 (uint16_t) &PINF,
 (uint16_t) &PING,

};

const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
 // PORTLIST
 // -------------------------------------------
 PA , // PA 0 ** 0 ** D22
 PA , // PA 1 ** 1 ** D23
 PA , // PA 2 ** 2 ** D24
 PA , // PA 3 ** 3 ** D25
 PA , // PA 4 ** 4 ** D26
 PA , // PA 5 ** 5 ** D27
 PA , // PA 6 ** 6 ** D28
 PA , // PA 7 ** 7 ** D29
 PB , // PB 0 ** 8 ** SPI_MISO
 PB , // PB 1 ** 9 ** SPI_MOSI
 PB , // PB 2 ** 10 ** SPI_SCK
 PB , // PB 3 ** 11 ** SPI_SS
 PB , // PB 4 ** 12 ** PWM10
 PB , // PB 5 ** 13 ** PWM11
 PB , // PB 6 ** 14 ** PWM12
 PB , // PB 7 ** 15 ** PWM13
 PC , // PC 0 ** 16 ** D30
 PC , // PC 1 ** 17 ** D31
 PC , // PC 2 ** 18 ** D32
 PC , // PC 3 ** 19 ** D33
 PC , // PC 4 ** 20 ** D34
 PC , // PC 5 ** 21 ** D35
 PC , // PC 6 ** 22 ** D36
 PC , // PC 7 ** 23 ** D37
 PD , // PD 0 ** 24 ** USART1_TX
 PD , // PD 1 ** 25 ** USART1_RX
 PD , // PD 2 ** 26 ** I2C_SDA
 PD , // PD 3 ** 27 ** I2C_SCL
 PD , // PD 4 ** 28 ** D38
 PD , // PD 5 ** 29 ** I2C_SDA
 PD , // PD 6 ** 30 ** I2C_SCL
 PD , // PD 7 ** 31 ** D38
 PE , // PE 0 ** 32 ** USART0_RX
 PE , // PE 1 ** 33 ** USART0_TX
 PE , // PE 2 ** 34 ** PWM2
 PE , // PE 3 ** 35 ** PWM3
 PE , // PE 4 ** 36 ** PWM5
 PE , // PE 5 ** 37 ** PWM2
 PE , // PE 6 ** 38 ** PWM3
 PE , // PE 7 ** 39 ** PWM5
 PF , // PF 0 ** 40 ** A0
 PF , // PF 1 ** 41 ** A1
 PF , // PF 2 ** 42 ** A2
 PF , // PF 3 ** 43 ** A3
 PF , // PF 4 ** 44 ** A4
 PF , // PF 5 ** 45 ** A5
 PF , // PF 6 ** 46 ** A6
 PF , // PF 7 ** 47 ** A7
 PG , // PG 0 ** 48 ** D39
 PG , // PG 1 ** 49 ** D40
 PG , // PG 2 ** 50 ** D41
 PG , // PG 3 ** 51 ** D41
 PG , // PG 4 ** 52 ** D41
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
 // PIN IN PORT
 // -------------------------------------------
 _BV( 0 ) , // PA 0 ** 22 ** D22
 _BV( 1 ) , // PA 1 ** 23 ** D23
 _BV( 2 ) , // PA 2 ** 24 ** D24
 _BV( 3 ) , // PA 3 ** 25 ** D25
 _BV( 4 ) , // PA 4 ** 26 ** D26
 _BV( 5 ) , // PA 5 ** 27 ** D27
 _BV( 6 ) , // PA 6 ** 28 ** D28
 _BV( 7 ) , // PA 7 ** 29 ** D29
 _BV( 0 ) , // PB 3 ** 50 ** SPI_MISO
 _BV( 1 ) , // PB 2 ** 51 ** SPI_MOSI
 _BV( 2 ) , // PB 1 ** 52 ** SPI_SCK
 _BV( 3 ) , // PB 0 ** 53 ** SPI_SS
 _BV( 4 ) , // PB 4 ** 10 ** PWM10
 _BV( 5 ) , // PB 5 ** 11 ** PWM11
 _BV( 6 ) , // PB 6 ** 12 ** PWM12
 _BV( 7 ) , // PB 7 ** 13 ** PWM13
 _BV( 0 ) , // PC 7 ** 30 ** D30
 _BV( 1 ) , // PC 6 ** 31 ** D31
 _BV( 2 ) , // PC 5 ** 32 ** D32
 _BV( 3 ) , // PC 4 ** 33 ** D33
 _BV( 4 ) , // PC 3 ** 34 ** D34
 _BV( 5 ) , // PC 2 ** 35 ** D35
 _BV( 6 ) , // PC 1 ** 36 ** D36
 _BV( 7 ) , // PC 0 ** 37 ** D37
 _BV( 0 ) , // PD 3 ** 18 ** USART1_TX
 _BV( 1 ) , // PD 2 ** 19 ** USART1_RX
 _BV( 2 ) , // PD 1 ** 20 ** I2C_SDA
 _BV( 3 ) , // PD 0 ** 21 ** I2C_SCL
 _BV( 4 ) , // PD 3 ** 18 ** USART1_TX
 _BV( 5 ) , // PD 2 ** 19 ** USART1_RX
 _BV( 6 ) , // PD 1 ** 20 ** I2C_SDA
 _BV( 7 ) , // PD 0 ** 21 ** I2C_SCL
 _BV( 0 ) , // PE 0 ** 0 ** USART0_RX
 _BV( 1 ) , // PE 1 ** 1 ** USART0_TX
 _BV( 2 ) , // PE 4 ** 2 ** PWM2
 _BV( 3 ) , // PE 5 ** 3 ** PWM3
 _BV( 4 ) , // PE 0 ** 0 ** USART0_RX
 _BV( 5 ) , // PE 1 ** 1 ** USART0_TX
 _BV( 6 ) , // PE 4 ** 2 ** PWM2
 _BV( 7 ) , // PE 5 ** 3 ** PWM3
 _BV( 0 ) , // PF 0 ** 54 ** A0
 _BV( 1 ) , // PF 1 ** 55 ** A1
 _BV( 2 ) , // PF 2 ** 56 ** A2
 _BV( 3 ) , // PF 3 ** 57 ** A3
 _BV( 4 ) , // PF 4 ** 58 ** A4
 _BV( 5 ) , // PF 5 ** 59 ** A5
 _BV( 6 ) , // PF 6 ** 60 ** A6
 _BV( 7 ) , // PF 7 ** 61 ** A7
 _BV( 0 ) , // PG 0 ** 62 ** A8
 _BV( 1 ) , // PG 1 ** 63 ** A9
 _BV( 2 ) , // PG 2 ** 64 ** A10
 _BV( 3 ) , // PG 3 ** 65 ** A11
 _BV( 4 ) , // PG 4 ** 66 ** A12

};

const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
 // TIMERS
 // -------------------------------------------
 NOT_ON_TIMER , // PA 0 ** 22 ** D22
 NOT_ON_TIMER , // PA 1 ** 23 ** D23
 NOT_ON_TIMER , // PA 2 ** 24 ** D24
 NOT_ON_TIMER , // PA 3 ** 25 ** D25
 NOT_ON_TIMER , // PA 4 ** 26 ** D26
 NOT_ON_TIMER , // PA 5 ** 27 ** D27
 NOT_ON_TIMER , // PA 6 ** 28 ** D28
 NOT_ON_TIMER , // PA 7 ** 29 ** D29
 NOT_ON_TIMER , // PB 0 ** 13 ** PWM13
 NOT_ON_TIMER , // PB 1 ** 50 ** SPI_MISO
 NOT_ON_TIMER , // PB 2 ** 51 *7* SPI_MOSI
 NOT_ON_TIMER , // PB 3 ** 52 ** SPI_SCK
 TIMER0A , // PB 4 ** 53 ** SPI_SS
 TIMER1A , // PB 5 ** 10 ** PWM10
 TIMER1B , // PB 6 ** 11 ** PWM11
 TIMER2A , // PB 7 ** 12 ** PWM12
 NOT_ON_TIMER , // PC 0 ** 30 ** D30
 NOT_ON_TIMER , // PC 1 ** 31 ** D31
 NOT_ON_TIMER , // PC 2 ** 32 ** D32
 NOT_ON_TIMER , // PC 3 ** 33 ** D33
 NOT_ON_TIMER , // PC 4 ** 34 ** D34
 NOT_ON_TIMER , // PC 5 ** 35 ** D35
 NOT_ON_TIMER , // PC 6 ** 36 ** D36
 NOT_ON_TIMER , // PC 7 ** 37 ** D37
 NOT_ON_TIMER , // PD 0 ** 18 ** USART1_TX
 NOT_ON_TIMER , // PD 1 ** 19 ** USART1_RX
 NOT_ON_TIMER , // PD 2 ** 20 ** I2C_SDA
 NOT_ON_TIMER , // PD 3 ** 21 ** I2C_SCL
 NOT_ON_TIMER , // PD 4 ** 19 ** USART1_RX
 NOT_ON_TIMER , // PD 5 ** 20 ** I2C_SDA
 NOT_ON_TIMER , // PD 6 ** 21 ** I2C_SCL
 NOT_ON_TIMER , // PD 7 ** 21 ** I2C_SCL
 NOT_ON_TIMER , // PE 0 ** 0 ** USART0_RX
 NOT_ON_TIMER , // PE 1 ** 1 ** USART0_TX
 NOT_ON_TIMER , // PE 2 ** 2 ** PWM2
 TIMER3A , // PE 3 ** 3 ** PWM3
 TIMER3B , // PE 4 ** 0 ** USART0_RX
 TIMER3C , // PE 5 ** 1 ** USART0_TX
 NOT_ON_TIMER , // PE 6 ** 2 ** PWM2
 NOT_ON_TIMER , // PE 7 ** 3 ** PWM3
 NOT_ON_TIMER , // PG 0 ** 39 ** D39
 NOT_ON_TIMER , // PG 1 ** 40 ** D40
 NOT_ON_TIMER , // PG 2 ** 41 ** D41
 NOT_ON_TIMER , // PG 3 ** 4 ** PWM4
 NOT_ON_TIMER , // PG 4 ** 41 ** D41
};

#endif

#endif

再将boards.txt的

atmega128A.build.variant=standard
改为
atmega128A.build.variant=mega128

arduino io 主芯片 IO 口 外部资源 1 外部资源 2 外部资源 3 外部资源 4
0 PA0/A/D0 流水灯(D1) 数码管(位 1) LCD1602(D0) LCD12864(D0)
1 PA1 流水灯(D2) 数码管(位 2) LCD1602(D1) LCD12864(D1)
2 PA2 流水灯(D3) 数码管(位 3) LCD1602(D2) LCD12864(D2)
3 PA3 流水灯(D4) 数码管(位 4) LCD1602(D3) LCD12864(D3)
4 PA4 流水灯(D5) 数码管(位 5) LCD1602(D4) LCD12864(D4)
5 PA5 流水灯(D6) 数码管(位 6) LCD1602(D5) LCD12864(D5)
6 PA6 流水灯(D7) 数码管(位 7) LCD1602(D6) LCD12864(D6)
7 PA7 流水灯(D8) 数码管(位 8) LCD1602(D7) LCD12864(D7)
8 PB0/SS 步进电机
9 PB1/SCK 步进电机
10 PB2/MOSI 步进电机
11 PB3/MISO 步进电机
12 PB4/OC0 直流电机 PWM(D9)
13 PB5/OC1A LCD1602(RS) LCD12864(RS)
14 PB6/OC1B LCD1602(RW) LCD12864
15 PB7/OC1C LCD1602(EN) (RW)
16 PC0/A8 数码管(段 A) LCD12864(EN)
17 PC1 数码管(段 B)
18 PC2 数码管(段 C)
19 PC3 数码管(段 D)
20 PC4 数码管(段 E)
21 PC5 数码管(段 F)
22 PC6 数码管(段 G)
23 PC7 数码管(段 dp)
24 PD0/SCL/INT0 矩阵键盘(行 1) 按键 K17 AT24C02(SCL)
25 PD1/SDA/INT1 矩阵键盘(行 2) 按键 K18 AT24C02(SDA)
26 PD2/RXD1/INT2 矩阵键盘(行 3) 按键 K110 PSII(DAT) 红外线接收
27 PD3/TXD1/INT3 矩阵键盘(行 4) 按键 K20 PSII(CLK)
28 PD4/ICP1 矩阵键盘(列 1)
29 PD5/XCK1 矩阵键盘(列 2)
30 PD6/T1 矩阵键盘(列 3)
31 PD7/T2 矩阵键盘(列 4)
32 PE0/RXD0/PDI 串口(RXD)
33 PE1/TXD0/PDO 串口(TXD)
34 PE2/XCK0/AIN0 DS1302(SCLK)
35 PE3/OC3A/AIN1 DS1302(I/0)
36 PE4/OC3B/INT4 DS1302(RST)
37 PE5/OC3C/INT5 空闲
38 PE6/T3/INT6 DS18B20
39 PE7/ICP3/INT7 蜂鸣器
40 PF0/ADC0 ADC 输入
41 PF1/ADC1 数码管段选锁存
42 PF2 数码管位选锁存
43 PF3 流水灯锁存
44 PF4 JTAG(TCK)
45 PF5 JTAG(TMS)
46 PF6 JTAG(TDO)
47 PF7 JTAG(TDI)
48 PG0/WR 空闲
49 PG1/RD 空闲
50 PG2/ALE 空闲
51 PG3/TOSC2 空闲
52 PG4/TOSC2 空闲

四、 最后启动arduino开发环境:
sudo ./arduino
测试程序:

int ledPin =12;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);

}

点击upload则在开发板上开始一个一闪一闪的led。第12pin是PB4开发板上正好对应一个LED。
好吧,可是开始arduino的开发之旅了。要学好arduino还是要花很多精力和时间的。

《把ATmega128开发板转为Arduino》上有5条评论

  1. 通过Arduino as ISP烧写的时候出现错误Error while burning bootloader: missing ‘bootloader.tool’ configuration parameter
    求问大侠是怎么回事

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload the CAPTCHA.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据