Path: Home => AVR overview => Applications => Egg-timer => Assembler source code

The ATtiny2313V-Egg-Timer - Assembler source code


; *****************************************************
; * ATtiny2313-Eggwatch, Version 1, 20.10.2006        *
; * (C)2006 by G.Schmidt info!at!avr-asm-tutorial.net *
; *****************************************************
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST
;
; ***************************************
;  Fuses-setting in the ATtiny2313V
; ***************************************
;
;   - Interal RC-Oscillator to 4 Mcs/s, CLKSEL3:0 = 0010, SUT1:0 = 10
;   - Internal Clock prescaler to 8, CKDIV8 = Enabled
;
; ***************************************
;        Hardware - scheme
; ***************************************
;
;             __________
;            /   |_|    |
;    +3V O--|Res     VCC|--O +3V
;           |           |
;    L1K O--|PD0     PB7|--O L10A
;           |           |
;L1A/L2K O--|PD1     PB6|--O L9A/L10K
;           |    AT     |
;        O--|PA1     PB5|--O L8A/L9K
;           |   tiny    |
;        O--|PA0     PB4|--O L7A/L8K
;           |   2313    |
;L2A/L3K O--|PD2     PB3|--O 
;           |    V      |
;L4K/L3A O--|PD3     PB2|--||--O Lsp
;           |     (OC0A)|
;L5K/L4A O--|PD4     PB1|--O 
;           |           |
;L6K/L5A O--|PD5     PB0|--O L0K
;           |           |
;    GND O--|GND     PD6|--O L6A/L7K
;           |___________|
;
;
; ***************************************
;      Define Ports and Portbits
; ***************************************
;
.EQU pOcO = PORTB
.EQU dOcO = DDRB
.EQU bOcO = 2
;
.EQU pL0K = PORTB
.EQU dL0K = DDRB
.EQU bL0K = 0
;
.EQU pL1K = PORTD
.EQU dL1K = DDRD
.EQU bL1K = 0
;
.EQU pL1A = PORTD
.EQU dL1A = DDRD
.EQU bL1A = 1
;
.EQU pL2K = PORTD
.EQU dL2K = DDRD
.EQU bL2K = 1
;
.EQU pL2A = PORTD
.EQU dL2A = DDRD
.EQU bL2A = 2
;
.EQU pL3K = PORTD
.EQU dL3K = DDRD
.EQU bL3K = 2
;
.EQU pL3A = PORTD
.EQU dL3A = DDRD
.EQU bL3A = 3
;
.EQU pL4K = PORTD
.EQU dL4K = DDRD
.EQU bL4K = 3
;
.EQU pL4A = PORTD
.EQU dL4A = DDRD
.EQU bL4A = 4
;
.EQU pL5K = PORTD
.EQU dL5K = DDRD
.EQU bL5K = 4
;
.EQU pL5A = PORTD
.EQU dL5A = DDRD
.EQU bL5A = 5
;
.EQU pL6K = PORTD
.EQU dL6K = DDRD
.EQU bL6K = 5
;
.EQU pL6A = PORTD
.EQU dL6A = DDRD
.EQU bL6A = 6
;
.EQU pL7K = PORTD
.EQU dL7K = DDRD
.EQU bL7K = 6
;
.EQU pL7A = PORTB
.EQU dL7A = DDRB
.EQU bL7A = 4
;
.EQU pL8K = PORTB
.EQU dL8K = DDRB
.EQU bL8K = 4
;
.EQU pL8A = PORTB
.EQU dL8A = DDRB
.EQU bL8A = 5
;
.EQU pL9K = PORTB
.EQU dL9K = DDRB
.EQU bL9K = 5
;
.EQU pL9A = PORTB
.EQU dL9A = DDRB
.EQU bL9A = 6
;
.EQU pL10K = PORTB
.EQU dL10K = DDRB
.EQU bL10K = 6
;
.EQU pL10A = PORTB
.EQU dL10A = DDRB
.EQU bL10A = 7
;
; ***************************************
;       Function of Timing
; ***************************************
;
; Timer1 = time measuring
;  _______      ______     ______      ______     _____     ______
; |RC-Osc.|    | CDiv |500|Presc.|62,5|TC1CNT|5cs| LED |0,5|MinCnt|
; | 4 Mcs |===>| / 8  |==>| / 8  |===>|/12500|==>|Count|==>| / 30 |==>1 Min.
; |_______|    |______|kcs|______|kcs |______|   |_____|cs |______|
;                      |
; Timer0= Audio-gener. |
;                      |   ______     ______
;                      |  |Presc.|   |TC0CNT|992cs/s
;                      |=>| / 8  |==>| / 63 |===> OC0A = Beep-Loudspeaker
;                         |______|   |______|
;
;
; ***************************************
;                 Register
; ***************************************
;
.DEF rmp = R16
.DEF rFlg = R17
.DEF rLed = R18
.DEF rSec = R19
.DEF rMin = R20
.DEF rNb = R21
;
; ***************************************
;      Reset and Interrupt-Vectors
; ***************************************
;
.CSEG
.ORG $0000
	rjmp Main ; Reset
	reti ; INT0 Interrupt
	reti ; INT1 Interrupt
	reti ; TC1Capt Interrupt
	rjmp TC1CmpA ; Timer 1 Compare A match
	reti ; TC1 Overflow Int
	reti ; TC0 Overflow Int
	reti ; UART0 RX Int
	reti ; UART0 TX Int
	reti ; Ana Comp Int
	reti ; PCINT
	reti ; TC1 CompB Int
	reti ; TC0 Compare Match A Int
	reti ; TC0 Compare Match B Int
	reti ; USISTART Int
	reti ; USIOverflow Int
	reti ; EERDY Int
	reti ; WDT Overflow Int
;
; ***************************************
;  TC1 Compare Match A Interrupt Routine
; ***************************************
;
TC1CmpA:
	set
	reti
;
; ***************************************
;             Main program
; ***************************************
;
Main:
	; Stack init
	ldi rmp,LOW(RAMEND) ; Set Stack pointer
	out SPL,rmp
	; Init Register
	ldi rFlg,0 ; Init Flag
	ldi rLed,0 ; Init LEDs
	ldi rSec,0 ; Init 0.5-Second counter
	ldi rMin,1 ; Init Minute counter
	ldi rNb,1  ; Init Beep-counter
	; Init OCR0A-Output for Beep
	sbi dOcO,bOcO
	; Init Timer 1 for time measuring and LED control
	ldi rmp,HIGH(12500) ; CTC-value
	out OCR1AH,rmp
	ldi rmp,LOW(12500)
	out OCR1AL,rmp
	clr rmp ; Controlword A
	out TCCR1A,rmp
	ldi rmp,(1<<WGM12)|(1<<CS11) ; to CTC mode
	out TCCR1B,rmp
	ldi rmp,1<<OCIE1A ; Timer1-Interrupt
	out TIMSK,rmp
	; Init Timer 0 for Beep-Output
	ldi rmp,(1<<COM0A0)|(1<<WGM01) ; CTC, Toggle Compare A
	out TCCR0A,rmp
	ldi rmp,1<<CS01 ; beep on
	out TCCR0B,rmp
	ldi rmp,63 ; CTC-value
	out OCR0A,rmp
	ldi rmp,1<<SE ; sleep idle
	out MCUCR,rmp
	sei ; Enable Ints
;
; ***************************************
;      Main program - Loop
; ***************************************
;
; to sleep mode idle
An:
	sleep ; Sleep on
	nop ; wake up
	brtc An ; T-Flag not activated by Timer
	clt ; T-Flag reset
	sbrc rLed,0 ; Bit 0 is Beep-control
	rjmp BeepAus
	tst rNb ; test beeps = 0
	breq BeepAus ; switch Beeps off
	ldi rmp,1<<CS01 ; beep on
	out TCCR0B,rmp
	dec rNb ; decrement Beeps
	rjmp LedAus
BeepAus:
	ldi rmp,0 ; Beep off, stop Timer
	out TCCR0B,rmp
LedAus:
	ldi ZH,HIGH(TabAus) ; jump table LED off
	ldi ZL,LOW(TabAus)
	add ZL,rLed ; add current LED
	brcc MachAus
	inc ZH
MachAus:
	ijmp ; current LED deactivation
Aus: ; LED is off
	inc rLed ; increase conter
	cpi rLed,10 ; already at ten
	brne Weiter ; no
	clr rLed ; yes, start again
	inc rSec ; inc 0.5-second counter
	cpi rSec,30 ; reached 30?
	brcs Weiter ; no, not yet
	clr rSec ; yes, clear 0.5-second counter
	mov rNb,rMin ; copy Minutes to Beep-counter
	inc rMin ; inc Minute counter
	cpi rMin,11 ; 10 Minutes over?
	brcs Weiter ; no
	clr rMin ; restart Minute counter
	ldi rNb,255 ; Beep for a whole Minute
	sbi dL0K,bL0K ; Output yellow LED on
	cbi pL0K,bL0K ; yellow LED on
Weiter: ; current LED red or green?
	ldi ZH,HIGH(TabRot) ; try with red
	ldi ZL,LOW(TabRot)
	cp rLed,rMin ; current LED smaller than Minute counter
	brcs Weiter1 ; yes
	ldi ZH,HIGH(TabGruen) ; no, it's green
	ldi ZL,LOW(TabGruen)
Weiter1: ; switch LED on
	add ZL,rLed ; add LED to base address
	brcc MachAn
	inc ZH
MachAn: ; switch LED on
	ijmp
;
; ***************************************
;           Jump tables
; ***************************************
;
; Jump table for LEDs red
TabRot:
	rjmp L1R
	rjmp L2R
	rjmp L3R
	rjmp L4R
	rjmp L5R
	rjmp L6R
	rjmp L7R
	rjmp L8R
	rjmp L9R
	rjmp L10R
; Jump table for LEDs green
TabGruen:
	rjmp L1G
	rjmp L2G
	rjmp L3G
	rjmp L4G
	rjmp L5G
	rjmp L6G
	rjmp L7G
	rjmp L8G
	rjmp L9G
	rjmp L10G
; Jump table for LEDs off
TabAus:
	rjmp L1A
	rjmp L2A
	rjmp L3A
	rjmp L4A
	rjmp L5A
	rjmp L6A
	rjmp L7A
	rjmp L8A
	rjmp L9A
	rjmp L10A
;
; ***************************************
;       LED switching routines
; ***************************************
;
; LEDs off
L1A:
	cbi pL1A,bL1A
	cbi pL1K,bL1K
	cbi dL1A,bL1A
	cbi dL1K,bL1K
	rjmp Aus
L2A:
	cbi pL2A,bL2A
	cbi pL2K,bL2K
	cbi dL2A,bL2A
	cbi dL2K,bL2K
	rjmp Aus
L3A:
	cbi pL3A,bL3A
	cbi pL3K,bL3K
	cbi dL3A,bL3A
	cbi dL3K,bL3K
	rjmp Aus
L4A:
	cbi pL4A,bL4A
	cbi pL4K,bL4K
	cbi dL4A,bL4A
	cbi dL4K,bL4K
	rjmp Aus
L5A:
	cbi pL5A,bL5A
	cbi pL5K,bL5K
	cbi dL5A,bL5A
	cbi dL5K,bL5K
	rjmp Aus
L6A:
	cbi pL6A,bL6A
	cbi pL6K,bL6K
	cbi dL6A,bL6A
	cbi dL6K,bL6K
	rjmp Aus
L7A:
	cbi pL7A,bL7A
	cbi pL7K,bL7K
	cbi dL7A,bL7A
	cbi dL7K,bL7K
	rjmp Aus
L8A:
	cbi pL8A,bL8A
	cbi pL8K,bL8K
	cbi dL8A,bL8A
	cbi dL8K,bL8K
	rjmp Aus
L9A:
	cbi pL9A,bL9A
	cbi pL9K,bL9K
	cbi dL9A,bL9A
	cbi dL9K,bL9K
	rjmp Aus
L10A:
	cbi pL10A,bL10A
	cbi pL10K,bL10K
	cbi dL10A,bL10A
	cbi dL10K,bL10K
	rjmp Aus
; LEDs green
L1G:
	sbi pL1A,bL1A
	cbi pL1K,bL1K
	sbi dL1A,bL1A
	sbi dL1K,bL1K
	rjmp An
L2G:
	sbi pL2A,bL2A
	cbi pL2K,bL2K
	sbi dL2A,bL2A
	sbi dL2K,bL2K
	rjmp An
L3G:
	sbi pL3A,bL3A
	cbi pL3K,bL3K
	sbi dL3A,bL3A
	sbi dL3K,bL3K
	rjmp An
L4G:
	sbi pL4A,bL4A
	cbi pL4K,bL4K
	sbi dL4A,bL4A
	sbi dL4K,bL4K
	rjmp An
L5G:
	sbi pL5A,bL5A
	cbi pL5K,bL5K
	sbi dL5A,bL5A
	sbi dL5K,bL5K
	rjmp An
L6G:
	sbi pL6A,bL6A
	cbi pL6K,bL6K
	sbi dL6A,bL6A
	sbi dL6K,bL6K
	rjmp An
L7G:
	sbi pL7A,bL7A
	cbi pL7K,bL7K
	sbi dL7A,bL7A
	sbi dL7K,bL7K
	rjmp An
L8G:
	sbi pL8A,bL8A
	cbi pL8K,bL8K
	sbi dL8A,bL8A
	sbi dL8K,bL8K
	rjmp An
L9G:
	sbi pL9A,bL9A
	cbi pL9K,bL9K
	sbi dL9A,bL9A
	sbi dL9K,bL9K
	rjmp An
L10G:
	sbi pL10A,bL10A
	cbi pL10K,bL10K
	sbi dL10A,bL10A
	sbi dL10K,bL10K
	rjmp An
; LEDs red
L1R:
	cbi pL1A,bL1A
	sbi pL1K,bL1K
	sbi dL1A,bL1A
	sbi dL1K,bL1K
	rjmp An
L2R:
	cbi pL2A,bL2A
	sbi pL2K,bL2K
	sbi dL2A,bL2A
	sbi dL2K,bL2K
	rjmp An
L3R:
	cbi pL3A,bL3A
	sbi pL3K,bL3K
	sbi dL3A,bL3A
	sbi dL3K,bL3K
	rjmp An
L4R:
	cbi pL4A,bL4A
	sbi pL4K,bL4K
	sbi dL4A,bL4A
	sbi dL4K,bL4K
	rjmp An
L5R:
	cbi pL5A,bL5A
	sbi pL5K,bL5K
	sbi dL5A,bL5A
	sbi dL5K,bL5K
	rjmp An
L6R:
	cbi pL6A,bL6A
	sbi pL6K,bL6K
	sbi dL6A,bL6A
	sbi dL6K,bL6K
	rjmp An
L7R:
	cbi pL7A,bL7A
	sbi pL7K,bL7K
	sbi dL7A,bL7A
	sbi dL7K,bL7K
	rjmp An
L8R:
	cbi pL8A,bL8A
	sbi pL8K,bL8K
	sbi dL8A,bL8A
	sbi dL8K,bL8K
	rjmp An
L9R:
	cbi pL9A,bL9A
	sbi pL9K,bL9K
	sbi dL9A,bL9A
	sbi dL9K,bL9K
	rjmp An
L10R:
	cbi pL10A,bL10A
	sbi pL10K,bL10K
	sbi dL10A,bL10A
	sbi dL10K,bL10K
	rjmp An
;
; End of source code
;



To the top of page

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