Pfad: Home => AVR-Übersicht => Anwendungen => Eieruhr-Timer m8 => asm-quellcode   This page in english: Flag EN
Eieruhr-Timer mit m8 Anwendungen von
AVR-Einchip-Prozessoren AT90S, ATtiny, ATmega und ATxmega
Quellcode Eieruhr-Timer mit ATmega8
Logo

Quellcode zum Eieruhr-Timer mit einem ATmega8

Zum Original-Quellcode geht es hier, dies ist nur die HTML-Fassung des Quellcodes.

;
; ************************************
; * Red/green egg timer with ATmega8 *
; * Version 1 as of July 2021        *
; * (C)2021 by Gerhard Schmidt       *
; ************************************
;
.nolist
.include "m8def.inc" ; Define device ATmega8
.list
;
; **********************************
;        H A R D W A R E
; **********************************
;
; Device: ATmega8, Package: 28-pin-PDIP
;
;           __________
;        1 /          |28
; 10k+ o--|RESET   PC5|--o NC
; L1ar o--|PD0     PC4|--o NC
; L1ag o--|PD1     PC3|--o NC
; L2ar o--|PD2     PC2|--o NC
; L2ag o--|PD3     PC1|--o NC
; L3ar o--|PD4     PC0|--o Speaker
;  +Vo o--|VCC     GND|--o 0 V
;  0 V o--|GND    AREF|--o NC
; L8ar o--|PB6    AVCC|--o NC
; L8ag o--|PB7     PB5|--o L7ag
; L3ag o--|PD5     PB4|--o L7ar
; L4ar o--|PD6     PB3|--o L6ag
; L4ag o--|PD7     PB2|--o L6ar
; L5ar o--|PB0     PB1|--o L5ag
;       14|___________|15
;
; **********************************
;  P O R T S   A N D   P I N S
; **********************************
;
.equ pOut0 = PortD
.equ pOut1 = PortB
.equ pDir0 = DDRD
.equ pDir1 = DDRB
.equ pSpkO = PortC
.equ pSpkD = DDRC
.equ bSpkO = PortC0
.equ bSpkD = DDC0
;
; **********************************
;   A D J U S T A B L E   C O N S T
; **********************************
;
.equ cTc2Cmp = 60 ; TC2 compare value
.equ cPwmRpt = 14 ; PWM repeats
;
; **********************************
;  F I X  &  D E R I V.  C O N S T
; **********************************
;
;
; **********************************
;       R E G I S T E R S
; **********************************
;
; free: R0 to R12
.def rPwmCmp = R13 ; PWM compare value
.def rPwmCnt = R14 ; PWM phase counter
.def rmp = R16 ; Define multipurpose register
.def rmpH = R17 ; Multi purpose High byte
.def rFlag = R18 ; Flag register
  .equ bQuiet = 0 ; 1 = Speaker quiet
.def rPwmRpt = R19 ; PWM repeat counter
.def rSpkO = R20 ; Speaker output
; free: R21 to R23
.def rSpkCntL = R24 ; Speaker counter, 16 bits, LSB
.def rSpkCntH = R25 ; dto., MSB
; used: X as downcounter PWM cycle duration
; used: Y as SRAM pointer for LEDs
; used: Z as pointer to melody table
;
; **********************************
;           S R A M
; **********************************
;
.dseg
.org SRAM_START
sLeds:
  .byte 32
sLedsEnd:
;
; **********************************
;         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
	reti ; INT0
	reti ; INT1
	rjmp Oc2Isr ; OC2
	reti ; OVF2
	reti ; ICP1
	rjmp Oc1aIsr ; OC1A
	reti ; OC1B
	reti ; OVF1
	reti ; OVF0
	reti ; SPI
	reti ; URXC
	reti ; UDRE
	reti ; UTXC
	reti ; ADCC
	reti ; ERDY
	reti ; ACI
	reti ; TWI
	reti ; SPMR
;
; **********************************
;  I N T - S E R V I C E   R O U T .
; **********************************
;
; TC2 compare match interrupt service routine
Oc2Isr: ; 7 clock cycles
  cp rPwmCnt,rPwmCmp ; +1=8
  brne Oc2IsrInc ; +1/2=9/10
 ; Compare match, set reversed
  ldd rmp,Y+2 ; +2=11
  out pOut0,rmp ; +1=12
  ldd rmp,Y+3 ; +2=14
  out pOut1,rmp ; +1=15
Oc2IsrInc: ; 10/15 clock cycles
  inc rPwmCnt ; +1 = 11/16
  brne Oc2IsrReti ; +1/2=12/17/13/18
  sbiw XL,1 ; Repeat counter, +2=14/19
  brne Oc2IsrRst ; +1/2=15/20/16/21
  ldi XH,High(cPwmRpt) ; +1=16/21
  ldi XL,Low(cPwmRpt) ; +1=17/22
  inc rPwmCmp ; +1=18/23
  brne Oc2IsrRst ; +1/2=19/24/20/25
  adiw YL,4 ; +2=21/26
  cpi YL,Low(sLedsEnd) ; +1=22/27
  breq Oc2IsrOff ; +1/2=23/28/24/29
  ; Start the next melody
  ldi rSpkCntL,1 ; 24/29
  clr rSpkCntH ; 25/30
  ldi rmp,0 ; +1=26/31
  out OCR1AH,rmp ; +1=27/32
  ldi rmp,10 ; +1=28/33
  out OCR1AL,rmp ; +1=29/34
  ldi rmp,(1<<WGM12)|(1<<CS10) ; +1=30/35
  out TCCR1B,rmp ; +1=31/36
  ldi rmp,(1<<OCIE1A)|(1<<OCIE2) ; +1=32/37
  out TIMSK,rmp ; +1=33/38
  ldi rSpkO,1 ; +1=34/39
  ; PWM phase restart, set start value
Oc2IsrRst: ; 16/21/20/25/34/39
  ld rmp,Y ; +2=18/23/22/27/36/41
  out pOut0,rmp ; +1=19/24/23/28/37/42
  ldd rmp,Y+1 ; +2=21/26/25/30/39/44
  out pOut1,rmp ; +1=22/27/26/31/40/45
Oc2IsrReti: ; 13/18/22/27/26/31/40/45
  reti ; +4=17/22/26/31/30/35/44/49
  ; End of operation
Oc2IsrOff: ; No further time-crtical ints any more
  ldi rmp,(1<<OCIE1A)
  out TIMSK,rmp
  clr rmp
  out pOut0,rmp
  out pOut1,rmp
  out pDir0,rmp
  out pDir1,rmp
  out TCCR2,rmp
  ldi rSpkCntL,1
  clr rSpkCntH
  ldi rmp,0
  out OCR1AH,rmp
  ldi rmp,10
  out OCR1AL,rmp
  ldi rmp,(1<<WGM12)|(1<<CS10)
  out TCCR1B,rmp
  ldi rSpkO,1
  reti
; Max = 49 clock cycles
;   is less than 75 cycles to next OCR2 int
;
; Speaker output interrupt service routine
Oc1aIsr: ; 7 clock cycles
  ldi rmp,0x01 ; Toggle the output, +1=8
  eor rSpkO,rmp ; Exclusive OR, +1=9
  sbis pSpkO,bSpkO ; If output = 1 skip next, +1/2=10/11
  sbrs rFlag,bQuiet ; Speaker quiet? ; +1/2=11/12
  out pSpkO,rSpkO ; Reverse speaker pin, +1=12
  brne Oc1aIsrReti ; Output pin is not zero, +1/2=13/14
  sbiw rSpkCntL,1 ; Count down, +2=15
  brne Oc1aIsrReti ; Not zero, +1/2=16/17
  lpm rmp,Z+ ; Read next tone height, LSB, +3=19
  lpm rmpH,Z+ ; dto., MSB, +3=22
  tst rmp ; +1=23
  breq Oc1aIsrStop ; +1/2=24/25
Oc1aIsrNoEnd: ; 24 clock cycles
  lpm rSpkCntL,Z+ ; +3=27
  lpm rSpkCntH,Z+ ; +3=30
  cbr rFlag,1<<bQuiet ; +1=31
  cpi rmp,Low(cPs) ; +1=32
  brne Oc1aIsrSetCmp ; +1/2=33/34
  sbr rFlag,1<<bQuiet ; +1=34
Oc1aIsrSetCmp: ; 34 clock cycles
  out OCR1AH,rmpH ; To the timer compare A in TC1, MSB, +1=35
  out OCR1AL,rmp ; dto., LSB, +1=36
  reti ; +4=40
Oc1aIsrStop: ; 25 clock cycles
  in rmp,TIMSK ; +1=26
  andi rmp,1<<OCIE2 ; +1=27
  out TIMSK,rmp ; +1=28
  ldi rmp,1<<WGM12 ; +1=29
  out TCCR1B,rmp ; +1=30
  sbr rFlag,1<<bQuiet ; +1=31
Oc1aIsrReti: ; 14/17/31
  reti ; +4=18/21/35
; Max 40 = less than 75 cycles
;
; **********************************
;  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 start values for LED-PWM
  ldi ZH,High(sLeds)
  ldi ZL,Low(sLeds)
  ldi XL,1
  clr XH
  clr YH
  clr YL
  st Z+,XL
  st Z+,XH
  lsl XL
  rol XH
  st Z+,XL
  st Z+,XH
  lsl XL
  rol XH
  ldi YL,1
FillSram:
  mov rmp,XL
  or rmp,YL
  st Z+,rmp
  mov rmp,XH
  or rmp,YH
  st Z+,rmp
  lsl XL
  rol XH
  mov rmp,XL
  or rmp,YL
  st Z+,rmp
  mov rmp,XH
  or rmp,YH
  st Z+,rmp
  lsl XL
  rol XH
  lsl YL
  rol YH
  lsl YL
  rol YH
  cpi ZL,Low(sLedsEnd)
  brne FillSram
  ; Enable led ports as outputs
  ldi rmp,0xFF
  out pDir0,rmp
  out pDir1,rmp
  ; Set and enable speaker output
  sbi pSpkD,bSpkD ; Pin as output
  sbr rFlag,1<<bQuiet
  ldi ZH,High(2*Melodies)
  ldi ZL,Low(2*Melodies)
  ldi rSpkO,1
  clr rmp
  out OCR1AH,rmp
  ldi rmp,30
  out OCR1AL,rmp
  ldi rSpkCntL,1
  clr rSpkCntH
  ldi rmp,(1<<WGM12)|(1<<CS10) ; TC1 as CTC
  out TCCR1B,rmp
  ; Set PWM start and reverse conditions
  ldi YH,High(sLeds)
  ldi YL,Low(sLeds)
  ; Set PWM properties
  ldi XH,High(cPwmRpt)
  ldi XL,Low(cPwmRpt)
  clr rPwmCnt
  clr rPwmCmp
  ldi rmp,cTc2Cmp
  out OCR2,rmp
  ldi rmp,(1<<WGM21)|(1<<CS20) ; CTC, Prescaler=1
  out TCCR2,rmp
  ldi rmp,(1<<OCIE2)|(1<<OCIE1A) ; Enable interrupts
  out TIMSK,rmp
  ; Start with L1 red
  ld rmp,Y
  out pOut0,rmp
  ldd rmp,Y+1
  out pOut1,rmp
  ; Sleep mode
  ldi rmp,1<<SE
  out MCUCR,rmp
;
	sei ; Enable interrupts
;
; **********************************
;    P R O G R A M   L O O P
; **********************************
;
Loop:
  sleep
	rjmp loop
;
; Gamut CTC table
Gamut:
  .equ cPs = 7812 ; f = 64,000 Hz
  .equ cA0 = 18181 ; f = 27,500 Hz
  .equ cH0 = 16197 ; f = 30,868 Hz
  .equ cC0 = 15288 ; f = 32,703 Hz
  .equ cD0 = 13620 ; f = 36,708 Hz
  .equ cE0 = 12134 ; f = 41,203 Hz
  .equ cF0 = 11453 ; f = 43,654 Hz
  .equ cG0 = 10203 ; f = 48,999 Hz
  .equ cA1 = 9090 ; f = 55,000 Hz
  .equ cH1 = 8098 ; f = 61,735 Hz
  .equ cC1 = 7644 ; f = 65,406 Hz
  .equ cD1 = 6809 ; f = 73,416 Hz
  .equ cE1 = 6066 ; f = 82,407 Hz
  .equ cF1 = 5726 ; f = 87,307 Hz
  .equ cG1 = 5101 ; f = 97,999 Hz
  .equ cA2 = 4544 ; f = 110,000 Hz
  .equ cH2 = 4049 ; f = 123,471 Hz
  .equ cC2 = 3821 ; f = 130,813 Hz
  .equ cD2 = 3404 ; f = 146,833 Hz
  .equ cE2 = 3033 ; f = 164,814 Hz
  .equ cF2 = 2862 ; f = 174,614 Hz
  .equ cG2 = 2550 ; f = 195,998 Hz
  .equ cA3 = 2272 ; f = 220,000 Hz
  .equ cH3 = 2024 ; f = 246,942 Hz
  .equ cC3 = 1910 ; f = 261,626 Hz
  .equ cD3 = 1702 ; f = 293,665 Hz
  .equ cE3 = 1516 ; f = 329,628 Hz
  .equ cF3 = 1431 ; f = 349,228 Hz
  .equ cG3 = 1275 ; f = 391,996 Hz
  .equ cA4 = 1135 ; f = 440,000 Hz
  .equ cH4 = 1011 ; f = 493,883 Hz
  .equ cC4 = 955 ; f = 523,251 Hz
  .equ cD4 = 850 ; f = 587,330 Hz
  .equ cE4 = 757 ; f = 659,255 Hz
  .equ cF4 = 715 ; f = 698,456 Hz
  .equ cG4 = 637 ; f = 783,991 Hz
  .equ cA5 = 567 ; f = 880,000 Hz
  .equ cH5 = 505 ; f = 987,766 Hz
  .equ cC5 = 477 ; f = 1046,502 Hz
  .equ cD5 = 425 ; f = 1174,660 Hz
  .equ cE5 = 378 ; f = 1318,510 Hz
  .equ cF5 = 357 ; f = 1396,912 Hz
  .equ cG5 = 318 ; f = 1567,982 Hz
  .equ cA6 = 283 ; f = 1760,000 Hz
  .equ cH6 = 252 ; f = 1975,532 Hz
  .equ cC6 = 238 ; f = 2093,004 Hz
  .equ cD6 = 212 ; f = 2349,320 Hz
  .equ cE6 = 189 ; f = 2637,020 Hz
  .equ cF6 = 178 ; f = 2793,824 Hz
  .equ cG6 = 158 ; f = 3135,964 Hz
  .equ cA7 = 141 ; f = 3520,000 Hz
  .equ cH7 = 126 ; f = 3951,064 Hz
  .equ cC7 = 118 ; f = 4186,008 Hz
  .equ cD7 = 105 ; f = 4698,640 Hz
  .equ cE7 = 94 ; f = 5274,040 Hz
  .equ cF7 = 88 ; f = 5587,648 Hz
  .equ cG7 = 79 ; f = 6271,928 Hz
  .equ cA8 = 70 ; f = 7040,000 Hz
  .equ cH8 = 62 ; f = 7902,128 Hz
  .equ cC8 = 59 ; f = 8372,016 Hz
  .equ cD8 = 52 ; f = 9397,280 Hz
  .equ cE8 = 46 ; f = 10548,080 Hz
  .equ cF8 = 44 ; f = 11175,296 Hz
  .equ cG8 = 39 ; f = 12543,856 Hz
  .equ cA9 = 35 ; f = 14080,000 Hz
  .equ cC9 = 31 ; f = 15804,256 Hz
  .equ cD9 = 29 ; f = 16744,032 Hz
  .equ cE9 = 26 ; f = 18794,560 Hz
  .equ cEnd = 0 ; f = 0,000 Hz
;
; Gamut duration table
Durations:
  .equ nPs = 128
  .equ nA0 = 55
  .equ nH0 = 55
  .equ nC0 = 62
  .equ nD0 = 65
  .equ nE0 = 73
  .equ nF0 = 82
  .equ nG0 = 87
  .equ nA1 = 98
  .equ nH1 = 110
  .equ nC1 = 123
  .equ nD1 = 131
  .equ nE1 = 147
  .equ nF1 = 165
  .equ nG1 = 175
  .equ nA2 = 196
  .equ nH2 = 220
  .equ nC2 = 247
  .equ nD2 = 262
  .equ nE2 = 294
  .equ nF2 = 330
  .equ nG2 = 349
  .equ nA3 = 392
  .equ nH3 = 440
  .equ nC3 = 494
  .equ nD3 = 523
  .equ nE3 = 587
  .equ nF3 = 659
  .equ nG3 = 698
  .equ nA4 = 784
  .equ nH4 = 880
  .equ nC4 = 988
  .equ nD4 = 1047
  .equ nE4 = 1175
  .equ nF4 = 1319
  .equ nG4 = 1397
  .equ nA5 = 1568
  .equ nH5 = 1760
  .equ nC5 = 1976
  .equ nD5 = 2093
  .equ nE5 = 2349
  .equ nF5 = 2637
  .equ nG5 = 2794
  .equ nA6 = 3136
  .equ nH6 = 3520
  .equ nC6 = 3951
  .equ nD6 = 4186
  .equ nE6 = 4699
  .equ nF6 = 5274
  .equ nG6 = 5588
  .equ nA7 = 6272
  .equ nH7 = 7040
  .equ nC7 = 7902
  .equ nD7 = 8372
  .equ nE7 = 9397
  .equ nF7 = 10548
  .equ nG7 = 11175
  .equ nA8 = 12544
  .equ nH8 = 14080
  .equ nC8 = 15804
  .equ nD8 = 16744
  .equ nE8 = 18795
  .equ nF8 = 21096
  .equ nG8 = 22351
  .equ nA9 = 25088
  .equ nC9 = 28160
  .equ nD9 = 31609
  .equ nE9 = 37589
;
; Melodies
Melodies:
  Melody0:
  ;   Völ-                      ker
  .dw cH3,nH3*3/16, cPs,nPs/16, cA3,nA3/16, cPs,nPs/16
  ;   hört                   die                      Sig-                   na-                    le
  .dw cG2,nG2/2, cPs,nPs/16, cD2,nG2*3/8, cPs,nPs/16, cH1,nH1/8, cPs,nPs/16, cE2,nE2/2, cPs,nPs/16, cC2,nC2/4,cPs,nPs/8
  ; Auf                         zum                     letz-                  ten                      Ge-                    fecht!
  .dw cA3,nA3*3/16, cPs,nPs/16, cG2,nG2/16, cPs,nPs/16, cF2,nF2/2, cPs,nPs/16, cE2,nE2*3/8, cPs,nPs/16, cD2,nD2/8, cPs,nPs/16, cD2,nD2/2,cPs,nPs/8
  ;   Die                    In-                      ter-                   na-                    tio-       na-                    le
  .dw cD2,nD2/4, cPs,nPs/16, cH3,nH3*3/8, cPs,nPs/16, cH3,nH3/8, cPs,nPs/16, cA3,nH3/4, cPs,nPs/16, cD2,nD2/4, cPs,nPs/16, cG2,nG2/2, cF2,nF2*3/8,cPs,nPs/8
  ;   er-                       kämpft                 das                    Men-                   schen-                 recht.
  .dw cF2,nF2*3/16, cPs,nPs/16, cE2,nE2/8, cPs,nPs/16, cD2,nD2/8, cPs,nPs/16, cE2,nE2/4, cPs,nPs/16, cA3,nA3/4, cPs,nPs/16, cA3,nA3/2
  .dw cEnd
  Melody1:
  .dw cA3,nA3/16,cPs,nPs/16,cA3,nA3/16,cEnd
  Melody2:
  .dw cA4,nA4/16,cPs,nPs/16,cA4,nA4/16,cPs,nPs/16,cA4,nA4/16,cEnd
  Melody3:
  .dw cA5,nA5/16,cPs,nPs/16,cA5,nA5/16,cPs,nPs/16,cA5,nA5/16,cPs,nPs/16,cA5,nA5/16,cEnd
  Melody4:
  .dw cA6,nA6/16,cPs,nPs/16,cA6,nA6/16,cPs,nPs/16,cA6,nA6/16,cPs,nPs/16,cA6,nA6/16,cPs,nPs/16,cA6,nA6/16,cEnd
  Melody5:
  .dw cA7,nA7/16,cPs,nPs/16,cA7,nA7/16,cPs,nPs/16,cA7,nA7/16,cPs,nPs/16,cA7,nA7/16,cPs,nPs/16,cA7,nA7/16,cPs,nPs/16,cA7,nA7/16,cEnd
  Melody6:
  .dw cA7,nA7/16,cPs,nPs/16,cA6,nA6/16,cPs,nPs/16,cA5,nA5/16,cPs,nPs/16,cA4,nA4/16,cPs,nPs/16,cA3,nA3/16,cPs,nPs/16,cA2,nA2/16,cPs,nPs/16,cA1,nA1/16,cEnd
  Melody7:
  .dw cA6,nA6/16,cPs,nPs/16,cA5,nA5/16,cPs,nPs/16,cA4,nA4/16,cPs,nPs/16,cA3,nA3/16,cPs,nPs/16,cA2,nA2/16,cPs,nPs/16,cA1,nA1/16,cPs,nPs/16,cA0,nA0/16,cPs,nPs/16,cA1,nA1/16,cEnd
  Melody8:
  .equ cDot=na3/16
  .equ cLong=na3*3/16
  .equ cPause=nPs/16
  .equ cPauseLong=nPs*3/16
  ;   _..
  .dw cA3,cLong,cPs,cPause,cA3,cDot,cPs,cPause,cA3,cDot,cPs,cPauseLong
  ;   __.
  .dw cA3,cLong,cPs,cPause,cA3,cLong,cPs,cPause,cA3,cDot,cPs,cPauseLong
  ; ...._
  .dw cA3,cDot,cPs,cPause,cA3,cDot,cPs,cPause,cA3,cDot,cPs,cPause,cA3,cDot,cPs,cPause,cA3,cLong,cPs,cPauseLong
  ; .._.
  .dw cA3,cDot,cPs,cPause,cA3,cDot,cPs,cPause,cA3,cLong,cPs,cPause,cA3,cDot,cPs,cPauseLong
  ; ._
  .dw cA3,cDot,cPs,cPause,cA3,cLong,cPs,cPauseLong
  ; _._.
  .dw cA3,cLong,cPs,cPause,cA3,cDot,cPs,cPause,ca3,cLong,cPs,cPause,cA3,cDot
  .dw cEnd
  .dw cEnd,cEnd,cEnd,cEnd
MelodiesEnd:
;
; 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
;




Zum Seitenfang


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