dela2000 发表于 2013-8-28 10:07:24

play AVR 8-Bit ATmega series in ubuntu

这段时间在linux下学习C语言,用的是VIM编辑器,将《C Primer Plus》的例子都手敲了一遍,越发感觉在linux下的乐趣,linux下有很多好的文档和指导手册。所以这贴将持续更新。也请大家多多指教。
首先因为对AVR不太熟悉,我下载了datasheet(之前有51的少许编程经验)在看,计划40个小时。(主要是有个大致框架熟悉了解,至少不明白时查询更容易点)。计划9/1~9/15完成。

dela2000 发表于 2013-11-12 15:23:19

网上有篇文章,上面有作者写makefile,我想用在ATmega8上,请问该如何修改?谢谢:
baud=19200
src=project
avrType=attiny2313
avrFreq=4000000 # 4MHz for accurate baudrate timing
programmerDev=/dev/ttyUSB003
programmerType=arduino

cflags=-g -DF_CPU=$(avrFreq) -Wall -Os -Werror -Wextra

memoryTypes=calibration eeprom efuse flash fuse hfuse lfuse lock signature application apptable boot prodsig usersig

.PHONY: backup clean disassemble dumpelf edit eeprom elf flash fuses help hex makefile object program

help:
    @echo 'backup       Read all known memory types from controller and write it into a file. Available memory types: $(memoryTypes)'
    @echo 'clean      Delete automatically created files.'
    @echo 'disassembleCompile source code, then disassemble object file to mnemonics.'
    @echo 'dumpelf      Dump the contents of the .elf file. Useful for information purposes only.'
    @echo 'edit   Edit the .cpp source file.'
    @echo 'eeprom       Extract EEPROM data from .elf file and program the device with it.'
    @echo 'elf      Create $(src).elf'
    @echo 'flash      Program $(src).hex to controller flash memory.'
    @echo 'fuses      Extract FUSES data from .elf file and program the device with it.'
    @echo 'help   Show this text.'
    @echo 'hex      Create all hex files for flash, eeprom and fuses.'
    @echo 'object       Create $(src).o'
    @echo 'program      Do all programming to controller.'

edit:
    vi $(src).cpp

makefile:
    vi Makefile

#all: object elf hex

clean:
    rm $(src).elf $(src).eeprom.hex $(src).fuses.hex $(src).lfuse.hex $(src).hfuse.hex $(src).efuse.hex $(src).flash.hex $(src).o
    date

object:
    avr-gcc $(cflags) -mmcu=$(avrType) -Wa,-ahlmns=$(src).lst -c -o $(src).o $(src).cpp

elf: object
    avr-gcc $(cflags) -mmcu=$(avrType) -o $(src).elf $(src).o
    chmod a-x $(src).elf 2>&1

hex:    elf
    avr-objcopy -j .text -j .data -O ihex $(src).elf $(src).flash.hex
    avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(src).elf $(src).eeprom.hex
    avr-objcopy -j .fuse -O ihex $(src).elf $(src).fuses.hex --change-section-lma .fuse=0
    srec_cat $(src).fuses.hex -Intel -crop 0x00 0x01 -offset0x00 -O $(src).lfuse.hex -Intel
    srec_cat $(src).fuses.hex -Intel -crop 0x01 0x02 -offset -0x01 -O $(src).hfuse.hex -Intel
    srec_cat $(src).fuses.hex -Intel -crop 0x02 0x03 -offset -0x02 -O $(src).efuse.hex -Intel

disassemble: elf
    avr-objdump -s -j .fuse $(src).elf
    avr-objdump -C -d $(src).elf 2>&1

eeprom: hex
    #avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$(baud) -v -U eeprom:w:$(src).eeprom.hex
    date

fuses: hex
    avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$(baud) -v -U lfuse:w:$(src).lfuse.hex
    #avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$(baud) -v -U hfuse:w:$(src).hfuse.hex
    #avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$(baud) -v -U efuse:w:$(src).efuse.hex
    date

dumpelf: elf
    avr-objdump -s -h $(src).elf

program: flash eeprom fuses

flash: hex
    avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$(baud) -v -U flash:w:$(src).flash.hex
    date

backup:
    @for memory in $(memoryTypes); do \
      avrdude -p $(avrType) -c$(programmerType) -P$(programmerDev) -b$(baud) -v -U $$memory:r:./$(avrType).$$memory.hex:i; \
    done

dela2000 发表于 2014-3-4 18:06:06

因为建立环境太复杂,我半途而废了,现在重返windows开发avr。不要嘲笑我~{:shy:}

dela2000 发表于 2014-3-4 18:08:54

学习avr,如何开始入门呢,有人很纠结在选择C语言还是汇编语言,从一篇外文觉得说的蛮清楚的,给需要的朋友们:
many people ask this question, this article will answer this question:

The question we are often asked is should I start with C or assembly language to program my AVR microcontroller? Well, there are pros and cons to both approaches.

C has many advantages, including much faster code writing and increased portability. It is also much easier to understand and modify the code later. By using library files, code can be reused easily or pre-written functions can just be added by including a library.

So, why not just use C then? Well, the main downside is the much larger code produced by C compilers. C code can often be many times bigger than the equivalent code written in assembler, especially with free C compilers like WinAVR. Commercial C compilers such as IAR are much more efficient and you are paying lots of money for optimising C code back to a size closer to the assembler equivalent. AVR microcontrollers have a much better architecture for C compilers than other microcontroller families like PIC as they were designed with C in mind (multiple accumulators, lots of SRAM and 16-bit address (pointer) registers) but C code will always be bigger.

AVR microcontrollers with larger memory are not that much more expensive now, so why not just write in C and get a larger memory? No problem, unless your project is price critical. The main problem with larger code is speed. If you want your AVR microcontroller to operate in real time, for example to read an UART, process the data and output it on SPI bus while also monitoring switches or other functions, you can easily run out of time to do all the processes needed.

In this situation, using assembly language for time critical sections makes sense, and most C compilers will let you include assembly language files or assembled code. How can you do this if you don’t know assembly language?

More importantly, C compilers hide the workings of the microcontroller, as they do all the memory management, control interrupts and peripherals so you never get an understanding about the structure of the AVR microcontroller and how it operates. It is comparable to those tools that let you design a website in 5 minutes. Yes, you can, but you will never understand how the process works and they produce huge amounts of code.

In our opinion, you should start with assembly language as this lets you understand the microcontroller. Once you have written programs in assembler, you can then move on to C programming with much more idea of what is actually going on.
读完这篇文章我决定从汇编入手AVR!

dela2000 发表于 2014-3-4 18:10:45

选择的是集成GCC工具链的AVR Studio6。选择的原因是官方出品,正版免费,有帮忙系统。

dela2000 发表于 2014-3-4 18:11:48

本帖最后由 dela2000 于 2014-3-4 18:12 编辑

第一个汇编程序:
.cseg                                ;select current segment as code
.org 0                                ;begin assembling at address 0

.def leds                = r16        ;current LED state
.def switches        = r17        ;switch values just read
.def temp                = r18        ;used as a temporary register

;.equ PORTB   = 0x18        ;Port B's output register
;.equ DDRB        = 0x17        ;Port B's Data Direction Register
;.equ PIND        = 0x10        ;Port D's input register
;.equ DDRD        = 0x11        ;Port D's Data Direction Register

          ldi        temp, 0xFF        ;configure PORTB as output
          out        DDRB, temp
          clr        temp                        ;configure PORTD as input, the same asldi temp,0x00
          out        DDRD, temp

          ldi        leds, 0x00        ;Initialize LED's all on
          out        PORTB, leds        ;Display initial LED's

          ;wait for switch to be pressed
          ;while (no button is depressed);
          waitpress:
                        in                switches, PIND
                        cpi                switches, 0xFF                ;0xFF means none pressed
                        breq        waitpress
          ;one or more switches are depressed (0's)
                        com          switches                ;flip all bits, now 1's indicate pressed
                        eor   leds, switches         ;toggle associated bits in led status
                        out          PORTB, leds              ;(Re)display LED'S

          ;wait for all switches to be released
          ;while (at least one button is depressed);
          waitrelease:
                        in                switches, PIND
                        cpi                switches, 0xFF ;0xFF means none pressed
                        brne        waitrelease           ;注意这个brne和breq区别

                        rjmp        waitpress           ;repeat (forever)
页: [1]
查看完整版本: play AVR 8-Bit ATmega series in ubuntu