201309240805


AVR Assembler examples for AT90S2313 chip
-----------------------------------------

blinker.prj

.MICRO "AT90S2313"
.PROGRAM "my_idea.asm"
.TARGET "my_idea.hex"

.TRACE              ; Activate micro trace

; Following lines are optional; if not included
; exactly these values are taken by default
; ------------------------------------------------------------
.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

; Micro nodes: RESET, PB0-PB7, PD0-PD5, ACO, TIM1OVF
; Define here the hardware around the micro
; ------------------------------------------------------------

D1 VDD PB0   ; x: Panel LEDs 1 - 8
D2 VDD PB1   ; x: Panel LEDs 1 - 8
D3 VDD PB2   ; x: Panel LEDs 1 - 8
D4 VDD PB3   ; x: Panel LEDs 1 - 8
D5 VDD PB4   ; x: Panel LEDs 1 - 8
D6 VDD PB5   ; x: Panel LEDs 1 - 8
D7 VDD PB6   ; x: Panel LEDs 1 - 8
D8 VDD PB7   ; x: Panel LEDs 1 - 8


;blinker.asm

.device AT90S2313

.equ    SPL    = $3d   ; stack pointer location
.equ    RAMEND = $df   ; last on-chip SRAM location
.equ    DDRB   = 0x17
.equ    PORTB  = 0x18

.def    T       =r16  ; accumulator
.def    A       =r17  ; keyboard
.def    X       =r18  ; display

.def    D       =r0   ; delay counter 1
.def    DD      =r1   ; delay counter 2
.def    DDD     =r2   ; delay counter 3

RESET:
   rjmp START
   reti      ; Addr $01
   reti      ; Addr $02
   reti      ; Addr $03
   reti      ; Addr $04
   reti      ; Addr $05
   reti      ; Addr $06        Use 'rjmp myVector'
   reti      ; Addr $07        to define a interrupt vector
   reti      ; Addr $08
   reti      ; Addr $09
   reti      ; Addr $0A
   reti      ; Addr $0B        This is just an example
   reti      ; Addr $0C        Not all MCUs have the same
   reti      ; Addr $0D        number of interrupt vectors
   reti      ; Addr $0E
   reti      ; Addr $0F
   reti      ; Addr $10

START:
    ldi   A, low(RAMEND)
    out   SPL,  A     ; initialize stack
    ldi   A, $ff
    out   DDRB, A     ; Set PORTB to output
    clr   X

LOOP:
    rcall DELAY
    com   X
    rcall DISPL
    rjmp  LOOP

;[ delay ]---------------------------------

DELAY:
    ldi   T, $ff
    mov   D, T
    mov   DD, T
    ldi   T, $1      ; 5 is 1 sec on 3.69MHz
    mov   DDD, T

DLY00:
    dec   D
    brne  DLY00
    dec   DD
    brne  DLY00
    dec   DDD
    brne  DLY00
    ret

;[ displ ]---------------------------------

DISPL:
    out   PORTB, X    ; Update LEDS
    ret

========================================================================

; buttons.prj

.MICRO "AT90S2313"
.PROGRAM "buttons.asm"
.TARGET "buttons.hex"

.TRACE              ; Activate micro trace

; Following lines are optional; if not included
; exactly these values are taken by default
; ------------------------------------------------------------
.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

; Micro nodes: RESET, PB0-PB7, PD0-PD5, ACO, TIM1OVF
; Define here the hardware around the micro
; ------------------------------------------------------------

K1 VSS  PD0
R1 VDD  PD0  1K
K2 VSS  PD1
R2 VDD  PD1  1K
K3 VSS  PD2
R3 VDD  PD2  1K
K4 VSS  PD3
R4 VDD  PD3  1K
K5 VSS  PD4
R5 VDD  PD4  1K
K6 VSS  PD5
R6 VDD  PD5  1K
K7 VSS  PD6
R7 VDD  PD6  1K
D1 VDD  PB0  ; x: Panel LEDs 1 - 8
D2 VDD  PB1  ; x: Panel LEDs 1 - 8
D3 VDD  PB2  ; x: Panel LEDs 1 - 8
D4 VDD  PB3  ; x: Panel LEDs 1 - 8
D5 VDD  PB4  ; x: Panel LEDs 1 - 8
D6 VDD  PB5  ; x: Panel LEDs 1 - 8
D7 VDD  PB6  ; x: Panel LEDs 1 - 8
D8 VDD  PB7  ; x: Panel LEDs 1 - 8


; buttons.asm
.device AT90S2313

.equ PIND    = 0x10
.equ DDRD    = 0x11
.equ PORTD   = 0x12

.equ PINB    = 0x16
.equ DDRB    = 0x17
.equ PORTB   = 0x18

.equ SPL     = 0x3d  ; stack pointer
.equ RAMEND  = 0xdf  ; end of RAM

.def A       = R16
.def B       = R17

RESET:
   rjmp START
   reti      ; Addr $01
   reti      ; Addr $02
   reti      ; Addr $03
   reti      ; Addr $04
   reti      ; Addr $05
   reti      ; Addr $06        Use 'rjmp myVector'
   reti      ; Addr $07        to define a interrupt vector
   reti      ; Addr $08
   reti      ; Addr $09
   reti      ; Addr $0A
   reti      ; Addr $0B        This is just an example
   reti      ; Addr $0C        Not all MCUs have the same
   reti      ; Addr $0D        number of interrupt vectors
   reti      ; Addr $0E
   reti      ; Addr $0F
   reti      ; Addr $10

START:

;--------------------------------------
; Chip configuration
;
;    DDR bits: 0 - input, 1 - output
;--------------------------------------

              ldi   A,    low( RAMEND)
              out   SPL,  A

;                           76543210
              ldi   A,    0b11111111
              out   DDRB, A

;                           76543210
              ldi   A,    0b10000000
              out   DDRD, A

;--------------------------------------
; Task
;--------------------------------------

              ldi   A,    0
LOOP:         in    B,    PIND
              com   B
              out   PORTB,B
              rcall DELAY
              rjmp  LOOP

;[ delay ]---------------------------------

DELAY:
    ret

========================================================================

;comparator

.device AT90S2313

.equ    SPL    = $3d  ; stack pointer location
.equ    RAMEND = $df  ; last on-chip SRAM location

.equ    ACSR   = 0x08 ; analog comparator control and status
.equ    ACO    = 0x05 ; analog comparator output.
.equ    DDRB   = 0x17
.equ    PORTB  = 0x18

.def    A     = r16  ; accumulator

;[ main ]----------------------------------

RESET:
    ldi   A, low(RAMEND)     
    out   SPL,  A     ; initialize stack
    ldi   A, $04
    out   DDRB, A     ; Set PORTB to: PB1 (AIN1), PB0 (AIN0) in, PB2 out
    ldi   A, $00
    out   PORTB, A    ; Disable pull-up resistors
LOOP:
    sbis  ACSR, ACO
    rjmp  ZERO
ONE:
    sbi   PORTB, 2
    rjmp  LOOP
ZERO:
    cbi   PORTB, 2
    rjmp  LOOP

========================================================================

; Counter with buttons
;--------------------------------------
; Assembler declarations
;--------------------------------------

.device AT90S2313

.equ PIND    = 0x10
.equ DDRD    = 0x11
.equ PORTD   = 0x12

.equ PINB    = 0x16
.equ DDRB    = 0x17
.equ PORTB   = 0x18

.equ SPL     = 0x3d  ; stack pointer
.equ RAMEND  = 0xdf  ; end of RAM

.equ BSTEP   = 0
.equ BAUTO   = 1
.equ BRESET  = 2

.def A       = R16
.def B       = R17

.def COUNTER = R0

.def AUTO    = R1

;--------------------------------------
; Chip configuration
;
;    DDR bits: 0 - input, 1 - output
;--------------------------------------

              ldi   A,    low( RAMEND)
              out   SPL,  A

;                           76543210
              ldi   A,    0b11111111
              out   DDRB, A

;                           76543210
              ldi   A,    0b00000000
              out   DDRD, A

;--------------------------------------
; Task
;--------------------------------------

              clr   AUTO
              clr   COUNTER
              rcall DISPL
LOOP:         in    A,     PIND
              com   A
              andi  A,     0b00001111
        
              sbrc  A,     BSTEP
              rcall STEP
              sbrc  A,     BAUTO
              rcall AUTO
              sbrc  A,     BRESET
              rcall RESET
              
              sbrc  AUTO,  0
              rcall AUTOSTEP
              sbrc  AUTO,  1
              rcall FASTSTEP
        
              rjmp  LOOP

;--------------------------------------
        
STEP:         inc   COUNTER
              clr   AUTO
              rcall DISPL
              rcall KEYUP
              ret
        
;--------------------------------------
        
AUTOSTEP:     inc   COUNTER
              rcall DISPL
              rcall DELAY
              rcall DELAY
              rcall DELAY
              rcall DELAY
              rcall DELAY
              
              ret
        
;--------------------------------------
        
FASTSTEP:     inc   COUNTER
              rcall DISPL
              
              ret
        
;--------------------------------------

AUTO:         sbrc  AUTO,  0
              rjmp  AUTO_FAST
              clr   AUTO
              inc   AUTO
              rcall KEYUP
              ret
AUTO_FAST:    sbrc  AUTO,  1
              rjmp  AUTO_STOP
              clr   AUTO
              inc   AUTO
              inc   AUTO
              rcall KEYUP
              ret
              
AUTO_STOP:    clr   AUTO
              rcall KEYUP
              ret
              
;--------------------------------------

RESET:        clr   COUNTER
              clr   AUTO
              rcall DISPL
              rcall KEYUP
              ret
        
;--------------------------------------

KEYUP:        rcall DELAY
              in    A,     PIND
              com   A
              andi  A,     0b00000111
              sbrc  A,     BSTEP
              rjmp  KEYUP  
              sbrc  A,     BAUTO
              rjmp  KEYUP
              sbrc  A,     BRESET
              rjmp  KEYUP
              ret  
              
;--------------------------------------

DELAY:        clr   A
DELAY_1:      inc   A
              clr   B
DELAY_2:      inc   B
              sbrs  B,     7
              rjmp  DELAY_2
              sbrs  A,     6
              rjmp  DELAY_1

;--------------------------------------

DISPL:        out   PORTB,  COUNTER
              ret

========================================================================

; Interrupts
;--------------------------------------
; Assembler declarations
;--------------------------------------

.device AT90S2313

.equ PIND    = 0x10
.equ DDRD    = 0x11
.equ PORTD   = 0x12

.equ PINB    = 0x16
.equ DDRB    = 0x17
.equ PORTB   = 0x18

.equ SPL     = 0x3d  ; stack pointer
.equ RAMEND  = 0xdf  ; end of RAM

.equ SREG    = 0x3f
.equ GIMSK   = 0x3b
.equ INT0    = 6
.equ INT1    = 7

.equ IBIT    = 7

.def A       = R16
.def B       = R17
.def C       = R18
.def D       = R31

;--------------------------------------
; Interrupt table
;--------------------------------------

              rjmp  RESET
              rjmp  EXT_INT0
              rjmp  EXT_INT1
              
EXT_INT0:     ldi   D,     0x02
              com   D
              out   PORTB, D
              reti
                           
EXT_INT1:     ldi   D,     0x04
              com   D
              out   PORTB, D
              reti
              
;--------------------------------------
; Chip configuration
;
;    DDR bits: 0 - input, 1 - output
;--------------------------------------
RESET:
              ldi   A,    low( RAMEND)
              out   SPL,  A

;                           76543210
              ldi   A,    0b11111111
              out   DDRB, A

;                           76543210
              ldi   A,    0b00000000
              out   DDRD, A

;                           76543210
              ldi   A,    0b11000000              
              out   GIMSK, A
              
              clr   A
              sei

;--------------------------------------
; Task
;--------------------------------------

              ser   A
              out   PORTB, A
              sleep

========================================================================

; AT90S2313 Template
;--------------------------------------
; Assembler declarations
;--------------------------------------

.device AT90S2313

.equ PIND    = 0x10
.equ DDRD    = 0x11
.equ PORTD   = 0x12

.equ PINB    = 0x16
.equ DDRB    = 0x17
.equ PORTB   = 0x18

.equ SPL     = 0x3d  ; stack pointer
.equ RAMEND  = 0xdf  ; end of RAM

.def A       = R16

;--------------------------------------
; Chip configuration
;
;    DDR bits: 0 - input, 1 - output
;--------------------------------------

              ldi   A,    low( RAMEND)
              out   SPL,  A

;                           76543210
              ldi   A,    0b11111111
              out   DDRB, A

;                           76543210
              ldi   A,    0b00000000
              out   DDRD, A

;--------------------------------------
; Task
;--------------------------------------


========================================================================

; AT90S1200 template

;--------------------------------------
; Assembler declarations
;--------------------------------------

.device AT90S1200

.equ PIND    = 0x10
.equ DDRD    = 0x11
.equ PORTD   = 0x12

.equ PINB    = 0x16
.equ DDRB    = 0x17
.equ PORTB   = 0x18

.def A       = R16

;--------------------------------------
; Chip configuration
;
;    DDR bits: 0 - input, 1 - output
;--------------------------------------

;                           76543210
              ldi   A,    0b11111111
              out   DDRB, A

;                           76543210
              ldi   A,    0b00000000
              out   DDRD, A

;--------------------------------------
; Task
;--------------------------------------


========================================================================

Labor 01.

Staatiline mälu SRAM

Vajalikud materjalid

Windows 98/2000/XP/2k3
VMLAB 3.10
Atmel AVR Studio 3.56 (VMLAB kasutab programmaatori mooduli)

x86 tüüpi personaalarvuti
STK500 programmaator
AT90S8515 mikrokontroller
628128P SRAM


Projekti fail

.MICRO "AT90S8515_64K"
.PROGRAM "8515.asm"
.TARGET "8515.hex"

.TRACE              ; Activate micro trace

.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

Programm

.include "C:\VMLAB\include\8515def.inc"

.def  t = r16

reset:
   rjmp start
   reti      ; Addr $01
     ...
   reti      ; Addr $0F
   reti      ; Addr $10

start:
   ldi  t, LOW(RAMEND)
   out  SPL, t
   ldi  t, HIGH(RAMEND)
   out  SPH, t

forever:
   ldi  t, $AA
   sts  $f123, t
   ldi  t, $55
   sts  $f123, t
rjmp forever


Labor 02.

Katkestused

Vajalikud materjalid

Windows 98/2000/XP/2k3
VMLAB 3.10
Atmel AVR Studio 3.56 (VMLAB kasutab programmaatori mooduli)

x86 tüüpi personaalarvuti
STK500 programmaator
AT90S2313 mikrokontroller

Projekti Fail

.MICRO "AT90S2313"
.PROGRAM "int.asm"
.TARGET "int.hex"

.TRACE              ; Activate micro trace

.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

; Micro nodes: RESET, PB0-PB7, PD0-PD5, ACO, TIM1OVF
; Define here the hardware around the micro
; ------------------------------------------------------------

D1 VDD PB0  ; x: Panel LEDs 1 - 5
D2 VDD PB1  ; x: Panel LEDs 1 - 5
D3 VDD PB2  ; x: Panel LEDs 1 - 5
D4 VDD PB3  ; x: Panel LEDs 1 - 5
D5 VDD PB4  ; x: Panel LEDs 1 - 5

R1 PD2  VDD 1k
K1 PD2  VSS
R2 PD3  VDD 1k
K2 PD3  VSS


Programm

.include "C:\VMLAB\include\2313def.inc"

.def A       = R16
.def B       = R17
.def C       = R18
.def D       = R31

              rjmp  RESET
              rjmp  EXT_INT0
              rjmp  EXT_INT1

EXT_INT0:     ldi   D,     0x02
              com   D
              out   PORTB, D
              reti

EXT_INT1:     ldi   D,     0x04
              com   D
              out   PORTB, D
              reti

RESET:
              ldi   A,    low( RAMEND)
              out   SPL,  A

;                           76543210
              ldi   A,    0b11111111
              out   DDRB, A

;                           76543210
              ldi   A,    0b00000000
              out   DDRD, A

;                           76543210
              ldi   A,    0b11000000
              out   GIMSK, A

              clr   A
              sei

              ser   A
              out   PORTB, A
CYCLE:
              RJMP  CYCLE



Labor 03.

Taimer ja katkestused

Vajalikud materjalid

Windows 98/2000/XP/2k3
VMLAB 3.10
Atmel AVR Studio 3.56 (VMLAB kasutab programmaatori mooduli)

x86 tüüpi personaalarvuti
STK500 programmaator
AT90S2313 mikrokontroller

Projekti fail

.MICRO "AT90S2313"
.PROGRAM "timer.asm"
.TARGET "timer.hex"

.TRACE              ; Activate micro trace

.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

; Micro nodes: RESET, PB0-PB7, PD0-PD5, ACO, TIM1OVF
; Define here the hardware around the micro

D1 VDD NODE  ; x: Panel LEDs 1 - 5
R1 NODE PB0 1K
.PLOT v(PB0)

Programm

.include "C:\VMLAB\include\2313def.inc"

.equ  T0VAL  = $80
.def  temp   = r16

reset:
   rjmp start
   reti      ; Addr $01
   reti      ; Addr $02
   reti      ; Addr $03
   reti      ; Addr $04
   reti      ; Addr $05
   rjmp timer8 ;               
   reti      ; Addr $07
      ...
   reti      ; Addr $0F
   reti      ; Addr $10

timer8:
   LDI temp, T0VAL
   OUT TCNT0, temp
   out PORTB, R17
   com R17
   reti

start:
   LDI temp, low(RAMEND)     ; Initialize Stack
   OUT SPL, temp

   LDI temp, $01             ; PORTB pin 0 is output
   OUT DDRB, temp

   LDI temp, 0b101           ; Set prescaler to CK/1024
   OUT TCCR0, temp

   LDI temp, 1 << TOIE0      ; Set Timer 0 Overflow Enable
   OUT TIMSK, temp

   LDI temp, T0VAL           ; Set Timer Counter 0 
   OUT TCNT0, temp           ; initial value

   LDI R17, $0

   SEI                       ; Enable Interrupts
forever:
   rjmp forever



Labor 04.

Programmeerimine C keeles

Vajalikud materjalid

Windows 98/2000/XP/2k3
VMLAB 3.10
WinAVR C kompilaator
Atmel AVR Studio 3.56 (VMLAB kasutab programmaatori mooduli)

x86 tüüpi personaalarvuti
STK500 programmaator
AT90S2313 mikrokontroller

Projekti fail

.MICRO "AT90S2313"
.TOOLCHAIN "GCC"
.GCCPATH   "C:\WinAVR"
.GCCMAKE   AUTO
.TARGET    "my_idea.hex"
.SOURCE    "my_idea.c"

.TRACE              ; Activate micro trace

.POWER VDD=5 VSS=0  ; Power nodes
.CLOCK 1meg         ; Micro clock
.STORE 250m         ; Trace (micro+signals) storage time

D1 VDD PB0

Programm

#include <avr\io.h>              // Most basic include files
#include <avr\interrupt.h>       // Add the necessary ones
#include <avr\signal.h>          // here

SIGNAL(SIG_INTERRUPT0) {}        // Interrupt handler example

void my_function(void) {
   unsigned char xxxx = 0x55;
   DDRB = 0xff;
   while(1) {
     xxxx = ~xxxx;
     PORTB = xxxx;
   }
}

int main(void) {
    asm("nop");                  // Inline assembly example
    my_function();               // system behaviour
}


