Programming AVR Assembly language

From media_wiki
Jump to: navigation, search

Learning inline assembly language on the Arduino AVR 8-bit platform is a daunting task

The ATmel AVR Assembly Language 8-bit Instruction Set
http://www.atmel.com/images/atmel-0856-avr-instruction-set-manual.pdf

  • Connect USBtiny to ATmega328P Target Board.
  • Test the connection to programmer: avrdude -p m328p -c usbtiny -v
  • Connect LED with pull down resistor to Pin D13 on ATmega328P Target bd. (Not needed if using Target Bd built in Yellow LED)
  • Open Arduino IDE and change the following settings: Under the Tools tab
      Board: ="Arduino/Genunio Uno"
      Programmer: = "USBtiny or USBasp"
  • Copy-Paste/Load ASM-blink.ino Arduino file. (Do not Flash until instructed )
// ASM-blink.ino MicroRusty 2018
// Blink Program using assembly language in the Ardion IDE 
// AVR RISC processors offers to embed assembly language code into C programs
void setup() {
 asm("sbi 0x04, 5");                 //sbi= Set bit in I/O Data Direction Register DDRB 0x4 with the value 5
 }
void loop() {
 asm("start:");
 asm("sbi 0x05, 5");                 // sbi= Set bit in I/O Register-PortB bit 0x5 with the value 5 (00010000)
 asm("ldi r16,40 \n\t rcall delay"); // Load imediate Reqister r16 with vaulue 40 - RCALL Relative Subroutine Call
 asm("cbi 0x05, 5");                 // cbi= Clear bit in I/O Register-PortB bit 0x5 with the value 5 (00000000)
 asm("ldi r16,40 \n\t rcall delay"); // Load imediate Reqister r16 with vaulue 40 - RCALL Relative Subroutine Call
 asm("rjmp start");                  // Relative Jump back to start 
asm("delay:"); // Delay subroutine called by RCALL asm("dec r8"); // Decrease r8 by 1 starting at 255 asm("brne delay"); // keep going until 0 asm("dec r9"); // Decrease r9 by 1 starting at 255 asm("brne delay"); // keep going until 0 asm("dec r16"); // Decrease r16 by 1 starting at 40 asm("brne delay"); // Keep going until 0 asm("ret"); // Return from subroutine after Three nested delay loops -ugh }
  • Generate the binary file by selecting under the Sketch Tab: "Export complied Binary"
  • Flash the chip by Selecting Upload Using Programmer under Sketch Tab in Arduino IDE.

Sketch uses 474 bytes (1%) of program storage space. Maximum is 32256 bytes. Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes

  • Notice the speed of the blinking LED at 1MHz. Try the different clock speeds
1 MHz 
avrdude -p m328p -c usbtiny -v -U lfuse:w:0x62:m -U hfuse:w:0xD9:m -U efuse:w:0xFF:m
8 MHz 
avrdude -p m328p -c usbtiny -v -U lfuse:w:0xE2:m -U hfuse:w:0xD6:m -U efuse:w:0xFF:m
16 MHz 
avrdude -p m328p -c usbtiny -v -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFF:m 


AVRASM2 is the ASM compiler and was once a stand alone program that has now since been incorporated into ATMEL Studio
I will not cover AVRASM2 in this workshop. AVRASM2 will be covered in part of ATMEL Studio Workshop that will be sometime in the furture.


AVRDUDE Class