AVR Assembly on Mac OS X – Your First Program
In the past I wrote an article about writing assembly programs for AVR devices on the Mac but missed a couple of important points.
The major problem with my original article was that I was passing everything through the C compiler rather than just using avr-as as one should. The other problem was that I was using the libc headers to get the names of the specific ports. The better solution is to take the include files that Atmel supply with AVR Studio 4 on the PC and just make a couple of small changes to make them work using avr-as. The include file I use for the ATmega168 can be found here.
By using this approach there is basically no difference between the assembly that you would use in AVR Studio 4 and that you can use on your Mac. This makes the entire development process extremely easy.
So lets list the source code of our updated LEDon.S file from the original article:
.include "/home/dev/m168.h" rjmp init init: ser r16 out DDRB, r16 out DDRD, r16 clr r16 out PORTB, r16 out PORTD, r16 .global main main: sbi PORTB, 0 rjmp main
it should look pretty similar to the original.
The only real difference comes when you want to assemble it and produce a hex file which you then upload with AVRdude (as documented here). So you do that with these commands (assuming you have saved the above file as ledon.S and that the header file I provided is stored in /home/dev – if it is different change the include line to point to the correct path):
avr-as -mmcu=atmega168 -o ledon.out ledon.S avr-objcopy -O ihex ledon.out ledon.hex
this should result in a nice ledon.hex file.
Now that you can use the “raw” assembler you can just read the data sheets and the AVR instruction reference and use the port names as is rather than having to use the rather stupid C macros that AVR libc provides. Obviously this technique is completely cross platform and will work on any UNIX based machine, not just Macs.