Wednesday, May 9, 2018

Get the hex file from an Arduino compile

When you compile an Arduino C file a hex file is created but is often hard to find and also usually deleted .

This site explains how to find the relevant hex file.


The famous blink program but this tiome in assembler.

/*
 * Atmega2560asmtest.asm
 *
 *  Created: 6/1/2015 3:46:37 PM
 *   Author: DarkSector
 */ 



 LDI R16, LOW(RAMEND)
 OUT SPL, R16
 LDI R16, HIGH(RAMEND)
 OUT SPH, R16

;MAIN LABEL
MAIN:  
LDI R16, 0xFF  
OUT DDRB, R16 ;PORTB IS OUTPUT


BACK:  
COM R16 ;COMPLEMENT THE VALUE IN R16  
OUT PORTB, R16 ;PUSH THE VALUE IN R16 TO PORTB  
CALL DELAY  
RJMP BACK ;KEEP DOING THIS INFINITELY

DELAY:  
LDI R17, 100  
LOOP3: LDI R18, 255  
LOOP2: LDI R19, 255  
LOOP1: DEC R19  
BRNE LOOP1 ;KEEP DECREASING R19  
DEC R18  
BRNE LOOP2 ;FOR EVERY DECREASE OF R18 REDO THE PREVIOUS LOOP  
DEC R17  
BRNE LOOP3 ;FOR EVERY DECREASE OF R17 REPEAT PREVIOUS LOOP  
RET        ;RETURN TO PREVIOUS PC ADDRESS  
You can also use the inline assembler directly in Arduino IDE.


Setting up the stack

The code for the above program is:

.include "m328pdef.inc"
start: ;set up the stack
ldi r16, low(RAMEND)
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16

nop

call below
nop

finish: rjmp finish
below:
ldi r17,$99
ret

The relevant part of the def.inc file the shows what RAMEND is for this processor is:

; ***** CPU REGISTER DEFINITIONS *****************************************
.def XH = r27
.def XL = r26
.def YH = r29
.def YL = r28
.def ZH = r31
.def ZL = r30



; ***** DATA MEMORY DECLARATIONS *****************************************
.equ FLASHEND = 0x3fff ; Note: Word address
.equ IOEND = 0x00ff
.equ SRAM_START = 0x0100
.equ SRAM_SIZE = 2048
.equ RAMEND = 0x08ff
.equ XRAMEND = 0x0000
.equ E2END = 0x03ff
.equ EEPROMEND = 0x03ff
.equ EEADRBITS = 10
#pragma AVRPART MEMORY PROG_FLASH 32768
#pragma AVRPART MEMORY EEPROM 1024
#pragma AVRPART MEMORY INT_SRAM SIZE 2048
#pragma AVRPART MEMORY INT_SRAM START_ADDR 0x100



; ***** BOOTLOADER DECLARATIONS ******************************************
.equ NRWW_START_ADDR = 0x3800
.equ NRWW_STOP_ADDR = 0x3fff
.equ RWW_START_ADDR = 0x0
.equ RWW_STOP_ADDR = 0x37ff
.equ PAGESIZE = 64
.equ FIRSTBOOTSTART = 0x3f00

The DEC instruction





























Good site for all AVR opcodes is:

http://academy.cba.mit.edu/classes/embedded_programming/doc0856.pdf



Delay of about 20.5 milliseconds

The code below gives a delay of about 20 milliseconds.

;
; AssemblerApplication2.asm
;
; Created: 10/05/2018 10:05:11 AM
; Author : peterb
;


; Replace with your application code
start:
    clr r16
ldi r18,$ff
loop: dec r18
nop
nop
brne loop
dec r16
brne loop
 stop:   rjmp stop

Wednesday, May 2, 2018

AVR assembler delays

Using delays in AVR studio



Write a variation on the above program to produce any delay required for your project.

AVR Registers



Other resisters in this document as well as in the official PDF,

A outline of what's in the test. The test will be in our normal classroom on Tuesday 19 June at 8:00 There will be a picture of...