搜索
bottom↓
回复: 5

play AVR 8-Bit ATmega series in ubuntu

[复制链接]

出0入0汤圆

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

 楼主| 发表于 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 'disassemble  Compile 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 -offset  0x00 -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

出0入0汤圆

 楼主| 发表于 2014-3-4 18:06:06 | 显示全部楼层
因为建立环境太复杂,我半途而废了,现在重返windows开发avr。不要嘲笑我~

出0入0汤圆

 楼主| 发表于 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!

出0入0汤圆

 楼主| 发表于 2014-3-4 18:10:45 | 显示全部楼层
选择的是集成GCC工具链的AVR Studio6。选择的原因是官方出品,正版免费,有帮忙系统。

出0入0汤圆

 楼主| 发表于 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 as  ldi 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)
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 00:27

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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