Path: Home => AVR-EN => Applications => Circular led => Asm source code   Diese Seite in Deutsch: Flag DE Logo
Circular-led

Assembler source code for the circular LED with ATmega324PA

The original assembler source code for the circular LED in asm format is here.


;
; ******************************
; * Circular-LED with 32 LEDs  *
; * ATmega324PA, Version 1     *
; * (C)2021 by Gerhard Schmidt *
; ******************************
;
.nolist
.include "m324padef.inc" ; Define device ATmega324PA
.list
;
; **********************************
;  D E B U G G I N G
; **********************************
;
; Switch certain LED(s) on
.equ dLed = 0 ; No debugging = 0
;
; **********************************
;        H A R D W A R E
; **********************************
;
; Device: ATmega324PA, Package: 40-pin-PDIP
;
;             __________
;          1 /          |40
; LED 32 o--|PB0     PA0|--o LED 1
; LED 31 o--|PB1     PA1|--o LED 2
; LED 30 o--|PB2     PA2|--o LED 3
; LED 29 o--|PB3     PA3|--o LED 4
; LED 28 o--|PB4     PA4|--o LED 5
; LED 27 o--|PB5     PA5|--o LED 6
; LED 26 o--|PB6     PA6|--o LED 7
; LED 25 o--|PB7     PA7|--o LED 8
;  RESET o--|RESET  AREF|--o
;     +V o--|VCC     GND|--o 0V
;     0V o--|GND    AVCC|--o
;        o--|XTAL2   PC7|--o LED 9
;        o--|XTAL1   PC6|--o LED 10
; LED 24 o--|PD0     PC5|--o LED 11
; LED 23 o--|PD1     PC4|--o LED 12
; LED 22 o--|PD2     PC3|--o LED 13
; LED 21 o--|PD3     PC2|--o LED 14
; LED 20 o--|PD4     PC1|--o LED 15
; LED 19 o--|PD5     PC0|--o LED 16
; LED 18 o--|PD6     PD7|--o LED 17
;         20|___________|21
;
; **********************************
;   F I X E D   C O N S T A N T S
; **********************************
;
.equ cLeft = 1<<7 ; Constant for left direction
.equ clock = 1000000
.equ cPresc = 64
.equ fTc1 = (clock+cPresc/2)/cPresc
.equ fRound = fTc1 * 32
.equ cSecondMult = ((clock+cPresc/2)/cPresc+16)/32
;
; **********************************
;       R E G I S T E R S
; **********************************
;
.def rRptA = R0 ; Repeat port value, port A
.def rRptC = R1 ; dto., port C
.def rRptD = R2 ; dto., port D
.def rRptB = R3 ; dto., port B
.def rOcrL = R4 ; OCR value, LSB
.def rOcrH = R5 ; dto., MSB
; free: R6 .. R15
.def rmp = R16 ; Define multi-purpose register
.def rRnd = R17 ; Round counter
.def rRnd0 = R18 ; Repetition value
.def rRpt = R19 ; Repetition counter
; free: R20 to R25
.def rLedA = R26 ; XL
.def rLedC = R27 ; XH
.def rLedD = R28 ; YL
.def rLedB = R29 ; YH
; used: R31:R30 = Z for LPM from Table
;
; **********************************
;           S R A M
; **********************************
;
.dseg
.org SRAM_START
; (No SRAM used)
;
; **********************************
;         C O D E
; **********************************
;
.cseg
.org 000000
;
; **********************************
; R E S E T  &  I N T - V E C T O R S
; **********************************
	rjmp Main ; Reset vector
	nop
	reti ; INT0
	nop
	reti ; INT1
	nop
	reti ; INT2
	nop
	reti ; PCI0
	nop
	reti ; PCI1
	nop
	reti ; PCI2
	nop
	reti ; PCI3
	nop
	reti ; WDT
	nop
	reti ; OC2A
	nop
	reti ; OC2B
	nop
	reti ; OVF2
	nop
	reti ; ICP1
	nop
	jmp Tc1Isr ; OC1A
	reti ; OC1B
	nop
	reti ; OVF1
	nop
	reti ; OC0A
	nop
	reti ; OC0B
	nop
	reti ; OVF0
	nop
	reti ; SPI
	nop
	reti ; URXC0
	nop
	reti ; UDRE0
	nop
	reti ; UTXC0
	nop
	reti ; ACI
	nop
	reti ; ADCC
	nop
	reti ; ERDY
	nop
	reti ; TWI
	nop
	reti ; SPMR
	nop
	reti ; URXC1
	nop
	reti ; UDRE1
	nop
	reti ; UTXC1
;
; **********************************
;  I N T - S E R V I C E   R O U T .
; **********************************
;
Tc1ISr:
  dec rRnd ; Decrease round counter
  brne Tc1IsrShift
  dec rRpt
  brne Tc1IsrRepeat
  lpm rRptA,Z+
  lpm rRptC,Z+
  lpm rRptD,Z+
  lpm rRptB,Z+
  lpm rOcrL,Z+
  lpm rOcrH,Z+
  sts OCR1AH,rOcrH
  sts OCR1AL,rOcrL
  lpm rRpt,Z+
  lpm rRnd0,Z+
  cpi ZL,Low(2*TableEnd)
  brne Tc1IsrRepeat
  cpi ZH,High(2*TableEnd)
  brne Tc1IsrRepeat
  ldi ZH,High(2*Table)
  ldi ZL,Low(2*Table)
Tc1IsrRepeat:
  mov rLedA,rRptA
  mov rLedC,rRptC
  mov rLedD,rRptD
  mov rLedB,rRptB
  mov rRnd,rRnd0
  andi rRnd,0x7F
Tc1IsrOut:
  out PORTA,rLedA
  out PORTC,rLedC
  out PORTD,rLedD
  out PORTB,rLedB
  reti
Tc1IsrShift:
  sbrc rRnd0,7
  rjmp Tc1IsrShiftLeft
  lsl rLedA
  ror rLedC
  ror rLedD
  ror rLedB
  brcc Tc1IsrShiftNc
  ori rLedA,1
Tc1IsrShiftNc:
  rjmp Tc1IsrOut
Tc1IsrShiftLeft:
  lsl rLedA
  rol rLedB
  rol rLedD
  ror rLedC
  brcc Tc1IsrShiftLeftNc
  ori rLedA,128
Tc1IsrShiftLeftNc:
  rjmp Tc1IsrOut
;
; **********************************
;  M A I N   P R O G R A M   I N I T
; **********************************
;
Main:
.ifdef SPH ; if SPH is defined
  ldi rmp,High(RAMEND)
  out SPH,rmp ; Init MSB stack pointer
  .endif
	ldi rmp,Low(RAMEND)
	out SPL,rmp ; Init LSB stack pointer
  ; Init the ports
  ldi rmp,0xFF
  out PORTA,rmp
  out PORTC,rmp
  out PORTD,rmp
  out PORTB,rmp
  out DDRA,rmp
  out DDRC,rmp
  out DDRD,rmp
  out DDRB,rmp
.if dLed != 0
    ldi rmp,Byte1(dLed)
    out PORTA,rmp
    ldi rmp,Byte2(dLed)
    out PORTC,rmp
    ldi rmp,Byte3(dLed)
    out PORTD,rmp
    ldi rmp,Byte4(dLed)
    out PORTB,rmp
  DebugLoop:
    rjmp DebugLoop
  .endif
  ldi ZH,High(2*Table)
  ldi ZL,Low(2*Table)
  ; Init the counters
  ldi rRnd,1
  ldi rRpt,1
  ; Init the timer
  ldi rmp,High(100)
  sts OCR1AH,rmp
  ldi rmp,Low(100)
  sts OCR1AL,rmp
  clr rmp
  sts TCCR1A,rmp
  ldi rmp,(1<<WGM12)|(1<<CS11)|(1<<CS10) ; CTC, Presc = 64
  sts TCCR1B,rmp
  ldi rmp,1<<OCIE1A
  sts TIMSK1,rmp
; Sleep mode
  ldi rmp,1<<SE ; Mode idle
  out SMCR,rmp
; Interrupt enable
	sei ; Enable interrupts
;
; **********************************
;    P R O G R A M   L O O P
; **********************************
;
Loop:
  sleep
	rjmp loop
;
Table:
  .dw 0xFFFE,0xFFFF, (10000*cSecondMult+500)/1000-1, 1+32*256 ; One LED, 1 time right, round time 10 seconds
  .dw 0xFFFE,0xFFFF, (2000*cSecondMult+500)/1000-1, 20+32*256 ; One LED, 20 times right, round time two seconds
  .dw 0xFFFF,0xFF7F, (2000*cSecondMult+500)/1000-1, 10+256*(32+cLeft) ; One LED, 10 times left, round time two seconds
  .dw 0xFFFE,0xFF7F, (1000*cSecondMult+500)/1000-1, 10+32*256 ; Two LEDs, 10 times right, round time one seconds
  .dw 0xFFFF,0xFFFF, (500*cSecondMult+500)/1000-1, 1+32*256 ; All off for half a second
  .dw 0xFFF0,0xFFFF, (100*cSecondMult+500)/1000-1, 100+(32+cLeft)*256 ; Four LEDs in a row, 100 times left, round time 0.1 seconds
  .dw 0xFFFF,0xFFFF, (2000*cSecondMult+500)/1000-1, 1+32*256 ; All off for two seconds
TableEnd:
;
; End of source code
;
; Copyright information
.db "(C)2021 by Gerhard Schmidt  " ; Source code readable
.db "C(2)20 1ybG reahdrS hcimtd  " ; Machine code format
;

To the top of this page

©2021 by http://www.avr-asm-tutorial.net