Use Arduino as a AVR programmer to burn the AVR
Circuit
Connect the Arduino Duemilanove and the AVR chip and LEDs as follows:
Arduino..............AVR................LED
Pin 13..................SCK
Pin 12..................MISO
Pin 11..................MOSI
Pin 10..................Reset
+5V......................VCC
GND.....................GND
Pin 9.............................................Communication LED.....+ 330 ohm-->GND
Pin 8.............................................Error LED....................... + 330 ohm --> GND
Pin 7.............................................Heartbeat LED...............+ 330 ohm --> GND
Upload the follwoing sketch into the Arduino Duemilanove. I am using Arduino version 0011 IDE.
// this sketch turns the Arduino into a AVRISP
// using the following pins:
// 10: slave reset
// 11: MOSI
// 12: MISO
// 13: SCK
// Put an LED (with resistor) on the following pins:
// 9: Heartbeat - shows the programmer is running
// 8: Error - Lights up if something goes wrong (use red if that makes sense)
// 7: Programming - In communication with the slave
//
// February 2009 by Randall Bohn
// - Added support for writing to EEPROM (what took so long?)
// Windows users should consider WinAVR's avrdude instead of the
// avrdude included with Arduino software.
//
// January 2008 by Randall Bohn
// - Thanks to Amplificar for helping me with the STK500 protocol
// - The AVRISP/STK500 (mk I) protocol is used in the arduino bootloader
// - The SPI functions herein were developed for the AVR910_ARD programmer
// - More information at http://code.google.com/p/mega-isp
#define SCK 13
#define MISO 12
#define MOSI 11
#define RESET 10
#define LED_HB 9
#define LED_ERR 8
#define LED_PMODE 7
#define HWVER 2
#define SWMAJ 1
#define SWMIN 18
// STK Definitions
#define STK_OK 0x10
#define STK_FAILED 0x11
#define STK_UNKNOWN 0x12
#define STK_INSYNC 0x14
#define STK_NOSYNC 0x15
#define CRC_EOP 0x20 //ok it is a space...
void pulse(int pin, int times);
void setup() {
// 19200?
Serial.begin(19200);
pinMode(7, OUTPUT);
pulse(7, 2);
pinMode(8, OUTPUT);
pulse(8, 2);
pinMode(9, OUTPUT);
pulse(9, 2);
}
int error=0;
int pmode=0;
// address for reading and writing, set by 'U' command
int here;
uint8_t buff[256]; // global block storage
#define beget16(addr) (*addr * 256 + *(addr+1) )
typedef struct param {
uint8_t devicecode;
uint8_t revision;
uint8_t progtype;
uint8_t parmode;
uint8_t polling;
uint8_t selftimed;
uint8_t lockbytes;
uint8_t fusebytes;
int flashpoll;
int eeprompoll;
int pagesize;
int eepromsize;
int flashsize;
}
parameter;
parameter param;
// this provides a heartbeat on pin 9, so you can tell the software is running.
uint8_t hbval=128;
int8_t hbdelta=8;
void heartbeat() {
if (hbval > 192) hbdelta = -hbdelta;
if (hbval < hbdelta =" -hbdelta;" x =" 0;" spcr =" 0x53;" x="SPSR;" x="SPDR;" spdr="b;" reply =" SPDR;" n="spi_send(b);" error =" -1;" n="spi_send(c);" crc_eop ="=" crc_eop ="=" devicecode =" buff[0];" revision =" buff[1];" progtype =" buff[2];" parmode =" buff[3];" polling =" buff[4];" selftimed =" buff[5];" lockbytes =" buff[6];" fusebytes =" buff[7];" flashpoll =" buff[8];" eeprompoll =" beget16(&buff[10]);" pagesize =" beget16(&buff[12]);" eepromsize =" beget16(&buff[14]);" flashsize =" buff[16]" pmode =" 1;" pmode =" 0;" w =" 0;" ch =" spi_transaction(buff[0],">>8 & 0xFF,
addr & 0xFF,
data);
}
void commit(int addr) {
spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
}
//#define _current_page(x) (here & 0xFFFFE0)
int current_page(int addr) {
if (param.pagesize == 32) return here & 0xFFFFFFF0;
if (param.pagesize == 64) return here & 0xFFFFFFE0;
if (param.pagesize == 128) return here & 0xFFFFFFC0;
if (param.pagesize == 256) return here & 0xFFFFFF80;
return here;
}
uint8_t write_flash(int length) {
if (param.pagesize < page =" current_page(here);" x =" 0;" page =" current_page(here);" x =" 0;" result =" (char)" length =" 256"> 256) {
Serial.print((char) STK_FAILED);
return;
}
char memtype = getch();
for (int x = 0; x < crc_eop ="=" memtype ="=" result =" (char)write_flash(length);" memtype ="=" result =" (char)write_eeprom(length);">> 8) & 0xFF,
addr & 0xFF,
0);
}
char flash_read_page(int length) {
for (int x = 0; x < low =" flash_read(LOW," high =" flash_read(HIGH," x =" 0;" ee =" spi_transaction(0xA0," result =" (char)STK_FAILED;" length =" 256" memtype =" getch();" memtype ="=" result =" flash_read_page(length);" memtype ="=" result =" eeprom_read_page(length);" ch =" getch();" here =" getch()" low =" getch();" high =" getch();" data =" getch();" error="0;" crc_eop ="=">
You can start using AVRDUDE with this Arduino Duemilanove. First you need to know which port the Arduino is using. Mine is on port com5. You can run the following command line and read the device signature (0×1e910a).
> avrdude -P com5 -p t2313 -c avrisp -b 19200 -u -tEnter ’sig’ to view the device signature again:
avrdude> sig
Reading | ################################## | 100% 0.02s
Device signature = 0×1e910a
Congratuation ! You have constructed an AVR programmer !!!
You can then develop your program using CodeVisionAVR and debug/simulate the program using AVRStudio.
The program can then be transform into HEX file by CodeVisionAVR.
The HEX file can then be written into the flash of the AVR (e.g. ATTINY2313) using AVRDUDE.
> avrdude -P com5 -p t2313 -c avrisp -b 19200 -U flash:w:XXXX.hex
see tutorial in using avrdude : - http://www.ladyada.net/learn/avr/avrdude.html
Check also: - http://www.uchobby.com/index.php/2007/11/04/arduino-avr-in-system-programmer-isp/
沒有留言:
張貼留言