Wednesday, May 16, 2018

This is a screen shot of an AVR assembler program that turns PORTB's least significant bit on and off very quickly.




;
; AssemblerApplication4.asm
;
; Created: 17/05/2018 10:15:30 AM
; Author : peterb
;

; Set up stack pointer and turn PB0 on and off. No delay.
start:
 out PORTB,r16
 LDI R16, LOW(RAMEND)
 OUT SPL, R16
 LDI R16, HIGH(RAMEND)
 OUT SPH, R16
 ;better make PB0 an output. Write a 1 into PB0 of DDRB.
 clr r16
 inc r16
 out DDRB,r16
repeat:
 clr r18
 inc r18
 rcall sendtob
 clr r18
 rcall sendtob
 rjmp repeat
 ; the subroutine is below. Always ends with ret.
 sendtob:
  out PORTB, r18
  ret

   

Wednesday, May 9, 2018

Set bit and clear bit

We can set and clear a bit in any internal register.


IO Ports

All AVR devices, like our Mega328, have ports. Usually they are a group of 8 pins that send or receive binary to or from the outside world.

There are three registers associated with each port.

  1. The output register PORTX
  2. The input register PINX
  3. The data direction register DDRX.
For instance port B is made up of 8 pins PB0, PB1, PB2 ....PB7. It has three associated registers:
  1. PORTB
  2. PINB
  3. DDRB
These registers live in the I/O register space just above the 32 general purpose registers.




Some good port pages:
Useful but about GCC.
Some good stuff here.

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

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...