使用ATMEGA8A 自己制作ARDUINO全过程。
手上多余一大堆Mega8,最近刚好在熟悉ISP,想自己做一批来测试,重新写了Bootload,把代码贴出来给大家自己做,只用了512个字节,使用0x1c00开始地址作为Bootload地址,开机进入Bootload,10秒钟重启一次,如有有代码自动进入用户程序。
先上代码:
include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
//#include <avr/delay.h>//#define EEPROM 0
//FUCS FF D9 00 FF 0x1800
//FUCS FF DA 00 FF 0x1C00 OKOK//#define F_CPU 16000000/* We, Malmoitians, like slow interaction* therefore the slow baud rate ;-)*///#define BAUD_RATE 9600/* 6.000.000 is more or less 8 seconds at the* speed configured here*///#define MAX_TIME_COUNT 6000000
#define MAX_TIME_COUNT (F_CPU>>1)
//#define MAX_TIME_COUNT_MORATORY 1600000/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
#define HW_VER 0x02
#define SW_MAJOR 0x01
#define SW_MINOR 0x12// AVR-GCC compiler compatibility
// avr-gcc compiler v3.1.x and older doesn't support outb() and inb()
// if necessary, convert outb and inb to outp and inp
#ifndef outb#define outb(sfr,val) (_SFR_BYTE(sfr) = (val))
#endif
#ifndef inb#define inb(sfr) _SFR_BYTE(sfr)
#endif/* defines for future compatibility */
#ifndef cbi#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif/* Adjust to suit whatever pin your hardware uses to enter the bootloader */
#define eeprom_rb(addr) eeprom_read_byte ((uint8_t *)(addr))
#define eeprom_rw(addr) eeprom_read_word ((uint16_t *)(addr))
#define eeprom_wb(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))/* Onboard LED is connected to pin PB5 */
#define LED_DDR DDRB
#define LED_PORT PORTB
#define LED_PIN PIND
#define LED PIND5#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
#define SIG2 0x93
#define SIG3 0x07
#define PAGE_SIZE 0x20U //32 wordsvoid putch(char);
char getch(void);
void getNch(uint8_t);
void byte_response(uint8_t);
void nothing_response(void);
#if 1union address_union {uint16_t word;uint8_t byte[2];
} address;union length_union {uint16_t word;uint8_t byte[2];
} length;
#endif
//uint16_t length;
//uint16_t address;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 bootuart0=0,bootuart1=0;void (*app_start)(void) = 0x0000;void putch(char ch)
{/* m8 */while (!(inb(UCSRA) & _BV(UDRE)));outb(UDR,ch);
}char getch(void)
{/* m8 */uint32_t count = 0;while(!(inb(UCSRA) & _BV(RXC))) {/* HACKME:: here is a good place to count times*/count++;//putch('.');if (count > MAX_TIME_COUNT){putch('!');app_start();}}return (inb(UDR));
}void getNch(uint8_t count)
{uint8_t i;for(i=0;i<count;i++) {/* m8 *///while(!(inb(UCSRA) & _BV(RXC)));//inb(UDR);getch(); // need to handle time out}
}void byte_response(uint8_t val)
{if (getch() ==0x20) {putch(0x14);putch(val);putch(0x10);}
}void nothing_response(void)
{if (getch() == 0x20) {putch(0x14);putch(0x10);}
}int main(void)
{uint8_t ch,ch2;uint16_t w;//cbi(BL_DDR,BL);//sbi(BL_PORT,BL);asm volatile("nop\n\t");/* check if flash is programmed already, if not start bootloader anyway *///if(pgm_read_byte_near(0x0000) != 0xFF) {/* check if bootloader pin is set low */// if(bit_is_set(BL_PIN,BL)) app_start();//}/* initialize UART(s) depending on CPU defined *//* m8 */UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rateUBRRL = (((F_CPU/BAUD_RATE)/16)-1);UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & TxUCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1//UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);//UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;//UCSRA = 0x00;//UCSRC = 0x86;//UCSRB = _BV(TXEN)|_BV(RXEN);/* this was giving uisp problems, so I removed it; without it, the bootworks on with uisp and avrdude on the mac (at least). */putch('\0');//uint32_t l;//uint32_t time_count;//time_count=0;/* set LED pin as output */
// sbi(LED_DDR,LED);
// for (i = 0; i < 16; i++) {// outb(LED_PORT, inb(LED_PORT) ^ _BV(LED));
// _delay_loop_2(0);
// }//for (l=0; l<40000000; l++)//outb(LED_PORT, inb(LED_PORT) ^= _BV(LED));/* flash onboard LED three times to signal entering of bootloader *///for(i=0; i<3; ++i) {//for(l=0; l<40000000; ++l);// sbi(LED_PORT,LED);//for(l=0; l<40000000; ++l);////}/* see comment at previous call to putch() *///putch('\0'); // this line is needed for the synchronization of the programmer
//cbi(LED_PORT,LED); /* forever */for (;;) {//sbi(LED_PORT,LED); //if((inb(UCSRA) & _BV(RXC))){/* get character from UART */ch = getch();/* A bunch of if...else if... gives smaller code than switch...case ! *//* Hello is anyone home ? */ if((ch=='P')||(ch=='Q')||(ch=='R')||(ch=='0')) {nothing_response();if (ch=='Q')app_start();}/* 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);}}/* 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 versionelse if(ch2==0x81) byte_response(SW_MAJOR); // Software major versionelse 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.56else 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();}/* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */else if(ch=='V') {getNch(4);byte_response(00);}/* 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;// putch(length); // putch(length>>8);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() ==0x20) {// putch('W');
#ifdef EERPOMif (flags.eeprom) { //Write to EEPROM one byte at a timefor(w=0;w<length.word;w++) {eeprom_wb(address.word,buff[w]);address.word++;} // putch('E');} else
#else
if (flags.eeprom==0)
#endif { //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;//address = address << 1; //address * 2 -> byte location//if ((length.byte[0] & 0x01)) length++; //Even up an odd number of bytes// putch('F');#if 1cli(); //Disable interrupts, just to be sure
// sbi(PORTD,3);//sbi(LED_PORT,LED);while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete// sbi(PORTD,3);//cbi(LED_PORT,LED);asm volatile("clr r17 \n\t" //page_word_count"lds r30,address \n\t" //Address of FLASH location (in words)"lds r31,address+1 \n\t""lsl r30 \n\t" //address * 2 -> byte location"rol r31 \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""sbrs r24,0 \n\t" //Even up an odd number of bytes"rjmp length_loop \n\t""adiw r24,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" "rcall wait_spm \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" "rcall wait_spm \n\t"
// "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""no_page_erase: \n\t" "ld r0,Y+ \n\t" //Write 2 bytes into page buffer"ld r1,Y+ \n\t" "rcall wait_spm \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"rcall wait_spm \n\t"
// "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""ldi r16,0x05 \n\t" //Write page pointed to by Z"sts %0,r16 \n\t""spm \n\t""rcall wait_spm \n\t"
// "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" "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""wait_spm: \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_spm \n\t""ret \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: "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31");/* 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 */
#endif }//sbi(LED_PORT,LED);// sei();
// cbi(PORTD,3);putch(0x14);putch(0x10);} }/* 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();}/* Read memory block mode, length is big endian. */else if(ch=='t') {length.byte[1] = getch();length.byte[0] =getch();
#if EERPOM if (getch() == 'E') flags.eeprom = 1; //0x45 EEPROMelse
#elseif (getch() == 'F') #endif { //0x46 FLASHflags.eeprom = 0;address.word = address.word << 1; // address * 2 -> byte location}if (getch() == 0x20) { // Command terminatorputch(0x14);for (w=0;w <length.word;w++) { // Can handle odd and even lengths okay
#if EERPOMif (flags.eeprom) { // Byte access EEPROM readputch(eeprom_rb(address.word));address.word++;} else
#else
if (flags.eeprom==0)
#endif { //if (address.word<0x1c00){//if (!flags.rampz) putch(pgm_read_byte_near(address.word));}else{putch(0xff);}address.word++;}}putch(0x10);}}/* Get device signature bytes */else if(ch=='u') {if (getch() == ' ') {putch(0x14);putch(SIG1);putch(SIG2);putch(SIG3);putch(0x10);}}/* Read oscillator calibration byte */else if(ch=='v') {byte_response(0x00);}
// } else {// time_count++;
// if (time_count>=MAX_TIME_COUNT) {// c();
// }
// }//cbi(LED_PORT,LED);} /* end of forever loop */
}/* end of file ATmegaBOOT.c */
接下来是Makefile:
# Makefile for ATmegaBOOT
# E.Lins, 2004-10-14# program name should not be changed...
PROGRAM = ATmegaBOOTPRODUCT=atmega8# enter the parameters for the UISP isp tool
ISPPARAMS = -dprog=stk500 -dserial=$(SERIAL) -dspeed=19200#DIRAVR = F:\2022\AVR\miniuxAVR\arduino-lite\bin\win32\WinAVR-20090313
DIRAVR = ../../../bin/win32/WinAVR-20090313
DIRAVRBIN = $(DIRAVR)/binDIRAVRUTILS = $(DIRAVR)/utils/bin
DIRINC = $(DIRAVR)/include
DIRLIB = $(DIRAVR)/avr/libMCU_TARGET = atmega8
LDSECTION = --section-start=.text=0x1C00
#FUSE_L = 0xdf
#FUSE_H = 0xcaFUSE_L = 0xFF
FUSE_H = 0xDDISPFUSES = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --wr_fuse_l=$(FUSE_L) --wr_fuse_h=$(FUSE_H)
ISPFLASH = $(DIRAVRBIN)/uisp -dpart=ATmega8 $(ISPPARAMS) --erase --upload if=$(PROGRAM).hex -vOBJ = $(PROGRAM).o
OPTIMIZE = -OsDEFS = -DF_CPU=7372800 -DBAUD_RATE=19200
LIBS =CC = $(DIRAVRBIN)/avr-gcc# Override is only needed by avr-lib build system.override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -D$(PRODUCT) $(DEFS) -I$(DIRINC)
override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION)OBJCOPY = $(DIRAVRBIN)/avr-objcopy
OBJDUMP = $(DIRAVRBIN)/avr-objdump
SIZE = $(DIRAVRBIN)/avr-sizeall: $(PROGRAM).elf lst text asm sizeisp: $(PROGRAM).hex$(ISPFUSES)$(ISPFLASH)$(PROGRAM).elf: $(OBJ)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)clean:rm -rf *.srm -rf *.o *.elfrm -rf *.lst *.mapasm: $(PROGRAM).s%.s: %.c$(CC) -S $(CFLAGS) -g1 $^lst: $(PROGRAM).lst%.lst: %.elf$(OBJDUMP) -h -S $< > $@size: $(PROGRAM).hex$(SIZE) $^# Rules for building the .text rom imagestext: hex bin srechex: $(PROGRAM).hex
bin: $(PROGRAM).bin
srec: $(PROGRAM).srec%.hex: %.elf$(OBJCOPY) -j .text -j .data -O ihex $< $@%.srec: %.elf$(OBJCOPY) -j .text -j .data -O srec $< $@%.bin: %.elf$(OBJCOPY) -j .text -j .data -O binary $< $@
然后使用 make 编译出hex
用烧录器烧录或者ISP
配置位如图:
下载完代开Arduino
开发板选择 Arduino NG or older
处理器 Atmega8
编程器选择 STK500
这样使用 19200下载到开发板就可以了!
在Arduino\hardware\arduino\avr目录下有个broads.txt
由于我使用的是7.372800 19200BPS这里面需要改动
##############################################################
atmegang.name=Arduino NG or older
atmegang.upload.tool=avrdude
atmegang.upload.protocol=arduino
atmegang.upload.speed=19200
atmegang.bootloader.tool=avrdude
atmegang.bootloader.unlock_bits=0x3F
atmegang.bootloader.lock_bits=0x0F
atmegang.build.mcu=atmegang
atmegang.build.f_cpu=7372800L
atmegang.build.board=AVR_NG
atmegang.build.core=arduino
atmegang.build.variant=standard
Arduino NG or older w/ ATmega168
--------------------------------
atmegang.menu.cpu.atmega168=ATmega168
atmegang.menu.cpu.atmega168.upload.maximum_size=14336
atmegang.menu.cpu.atmega168.upload.maximum_data_size=1024
atmegang.menu.cpu.atmega168.bootloader.low_fuses=0xff
atmegang.menu.cpu.atmega168.bootloader.high_fuses=0xdd
atmegang.menu.cpu.atmega168.bootloader.extended_fuses=0xF8
atmegang.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_ng.hex
atmegang.menu.cpu.atmega168.build.mcu=atmega168
Arduino NG or older w/ ATmega8
------------------------------
atmegang.menu.cpu.atmega8=ATmega8
atmegang.menu.cpu.atmega8.upload.maximum_size=7168
atmegang.menu.cpu.atmega8.upload.maximum_data_size=1024
atmegang.menu.cpu.atmega8.bootloader.low_fuses=0xdf
atmegang.menu.cpu.atmega8.bootloader.high_fuses=0xca
atmegang.menu.cpu.atmega8.bootloader.extended_fuses=0xF8
atmegang.menu.cpu.atmega8.bootloader.file=atmega8/ATmegaBOOT-prod-firmware-2009-11-07.hex
atmegang.menu.cpu.atmega8.build.mcu=atmega8
##############################################################
这样就完成了。
使用ATMEGA8A 自己制作ARDUINO全过程。相关推荐
- 效率最高的Excel数据导入续---SSIS Package包制作图解全过程
目的:本文主要是详细讲解SSIS Package包的制作过程 本人买过的一张盗版windows 2003操作系统光盘上,上面自带有很详细的图文介绍,也就是傻瓜版的系统安装图解.因此,本文打算也是采用那 ...
- linux ramdisk 工具,Linux下用Busy Box制作Ramdisk全过程(转帖+部分完善) - 薛正华的专栏 - CSDN博客.pdf...
Linux下用BusyBox制作Ramdisk全过程(转帖部分完善)-薛正华的专栏-CSDN博客 Linux下用Busy Box制作Ramdisk全过程(转帖+部分完善) 收藏 /software_z ...
- psd文件生产html,PSD文件切图制作html全过程记录
PSD文件切图制作html全过程记录 最后效果图: 新建文件夹 开始时,在您的计算机中创建一个文件夹.我把它命名为zmool.再在文件夹中创建新文件夹images,放网站的所有图像.接下来打开代码编辑 ...
- 从一个空白芯片开始制作arduino uno最小系统板
摘要:只用一个空白的芯片是无法实现在arduino 编程环境中编程的,芯片需要烧录arduino专用的bootloader才能使用.本文介绍如何从购买一个空白芯片开始,制作arduino uno 的最 ...
- 制作Arduino库——摩斯码
制作Arduino库--摩斯码 前沿 1.文件结构 2.主文件代码 3.自定义Arduino库 3.1.编写.h头文件 3.2.编写.cpp源文件 3.3.编写keywords.txt文件 4.使自定 ...
- sw制作arduino模型
开始的时候想当然地认为arduino模型可以直接在一块做 但是真的有太多零件了,实在没办法一下子一起整,所以就分开整. 1.首先,把板做出来 2.制作端口 细节:端口不是直接长方体裁剪,这里还使用了倒 ...
- 一步步制作Arduino智能小车
Arduino nano的制作智能小车 1.引脚配置 首先是板子的图片 Nano 技术参数 控制器 ATmega328P Flash 32KB Sram 2KB EEPROM 1KB Sram ...
- 个人制作网站全过程分享
以下为自己制作网站过程,拿来和大家分享,也希望广大爱好网站制作的帅哥.美女们,一起交流讨论网站制作过程.共同进步.迈上更高的台阶. 这是我第一次写博客记录自己的成长过程,既兴奋又期待.这次是我个人第一 ...
- 使用超声波传感器采用图形化编程制作Arduino自动跟随小车
在智能小车项目中,我们通过控制直流电机的正反转.刹车和转速来控制小车的行动. 在这台小车中,对直流电机发出的控制指令来自于Arduino主控板,而Arduino主控板的外部输入则来自于超声波传感器.灰 ...
最新文章
- Qt安装后配置环境变量(Mac)
- 【实习招聘】字节跳动智能创作AutoML团队招聘
- UITableViewCell有关处理touch事件的问题
- Java:Java的输入类Scanner
- Tensorflow中的Protocol Buffer
- java响应鼠标滚轮事件_一文读懂鼠标滚轮事件(wheelEvent)
- Mybatis接口注解开发
- [Windows驱动开发](二)基础知识——数据结构
- sql中 in , not in , exists , not exists效率分析
- java类型之间的转换_JAVA基本数据类型及之间的转换
- 16.Mongodb之预写日志(Journaling)
- Spark:一个独立应用
- 自己动手写操作系统——开发环境搭建
- ZXing设置前置摄像头扫描
- 移动OA,为企业提供更高效的办公模式
- WinCC RT Professional网络通讯配置步骤
- 看不到工作组的其他计算机_就这一次,从现实世界的角度去理解计算机领域的知识(给新手)...
- 苹果平板怎么卸载软件_苹果手机怎么装第三方软件
- 断言(assert)方法
- 创业的捷径!打造黄金人脉!
热门文章
- 计算机快捷键任务管理器,任务管理器快捷键,教您win10怎么打开任务管理器
- 股票买到手想卖可以随时都能卖掉吗
- 无人机在计算机专业的应用,嵌入式计算机在无人机系统的应用
- 看完哪吒之魔童降世,突然想起以前考研时…
- 关于极光推送报错6003的一些问题
- JavaScriptJQuery_jQuery选择器
- 80老翁谈人生(151):老翁老眼昏花,读错了一个数量级
- 最全面的外包公司的解释
- 同行评审流程与撰写审稿意见要点
- htmldd隐藏,如何在隐藏“dd”后隐藏“dd”