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

The ATtiny2313V-Egg-Timer - Assembler source code - Version 3


; ***************************************************
; * Eggtimer with ATtiny2313, Version 3 with gamut  *
; * and a melody at start-up                        *
; * (C)2009 by info@avr-asm-tutorial.net            *
; ***************************************************
;
.NOLIST
.INCLUDE "tn2313def.inc"
.LIST
;
;
; ***************************************
;  Fuse-Settings in the ATtiny2313(V)
; ***************************************
;
;   - Internal RC-Oscillator to 4 MHz, CLKSEL3:0 = 0010, SUT1:0 = 10
;   - Internal Clock divider to 8, CKDIV8 = Enabled
;   - The internal Clock divider of 8 will be overwritten by
;     setting CLKPR during init to 2
;
; ***************************************
;        Hardware - Connections
; ***************************************
;
;             __________
;            /   |_|    |
;    +3V O--|Res     VCC|--O +3V
;           |           |
;    L1K O--|PD0     PB7|--O L10A + USCK
;           |           |
;L1A/L2K O--|PD1     PB6|--O L9A/L10K + MISO
;           |    AT     |
;        O--|PA1     PB5|--O L8A/L9K + MOSI
;           |   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
;           |___________|
;
;
; ***************************************
;      Defining Ports and Portbits
; ***************************************
;
.EQU iOcO = PINB
.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
;
.EQU pLRot = PORTB
.EQU dLRot = DDRB
.EQU bLRot = 0
;
; ***************************************
;       Functioning of Timing
; ***************************************
;
; Timer1 = time measuring
;  _______      ______     ______      ______     _____     ______
; |RC-Osc.|    | CDiv | 2 |Presc.|250 |TC1CNT|5cs| LED |0.5|MinCnt|
; | 4 MHz |===>| / 2  |==>| / 1  |===>|/50000|==>|Count|==>| / 30 |==>1 Min.
; |_______|    |______|Mcs|______|kcs |______|   |_____|cs |______|
;                      |
; Timer0 = Audio freq  |
;                      |   ______     ______
;                      |  |Presc.|   | CTC0 |1046-2794 cs
;                      |=>| / 1  |==>|/Tontl|===> OC0A = speaker
;                         |______|   |______|
;
;
; ***************************************
;               Registers
; ***************************************
;
.DEF rmp = R16 ; Multipurpose-Register
.DEF rLmp = R17 ; Lamp counter
.DEF rSec = R18 ; Second counter
.DEF rMin = R19 ; Minute counter
.DEF rFlg = R20 ; Flag-Register
	.EQU fs = 0 ; Begin-Melody-Flag
; uses XH:XL for Melody at start-up
; uses ZH:ZL for gamut
; 
; ***************************************
;      Reset and Interrupt vektors
; ***************************************
;
.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
;
; ***************************************
;             Subroutines
; ***************************************
;
; Set sound to gamut table
;
Ton:
	mov rmp,rMin ; copy minute counter
	cpi rmp,12 ; within table?
	brcs Ton1 ; yes, no correction
	ldi rmp,11 ; correct
Ton1:
	ldi ZH,HIGH(2*Tonleiter) ; Z points to table in flash
	ldi ZL,LOW(2*Tonleiter)
	add ZL,rmp ; point to tone
	brcc Ton2 ; no overflow
	inc ZH ; overflow into MSB of pointer
Ton2:
	lpm ; read divider for tone from flash
	out OCR0A,R0 ; write table value to Compare Register A
	lsr R0 ; half of value
	out OCR0B,R0 ; half value to Compare Register B
	ret ; done
;
Tonleiter:
.DB 237,210,189,178,157,142,126,118,105,95,89,79
; Table for german melody Haenschen klein
mtab:
.db 6,0,4,0,4,0,0,5,0,3,0,3,0,0,2,0,3,0,4,0,5,0
.db 6,0,6,0,6,0,0,6,0,4,0,4,0,0,5,0,3,0,3,0
.db 2,0,4,0,6,0,6,0,2,0,0,0,0,3,0,3,0,3,0,3,0,3
.db 0,4,0,5,0,4,0,4,0,4,0,4,0,4,0,5,0,6,0,6
.db 0,4,0,4,0,0,5,0,3,0,3,0,2,0,4,0,6,0,6,0,1,0
.db 0,0xFF
;
; ***************************************
;             Main program
; ***************************************
;
Main:
	; Stack init
	ldi rmp,LOW(RAMEND) ; Set Stack pointer
	out SPL,rmp
	; set CLKPR to 2 Mcs
	ldi rmp,1<<CLKPCE ; set Clock prescaler change enable Bit
	out CLKPR,rmp
	ldi rmp,4 ;1<<CLKPS1 ; Prescaler to 2
	out CLKPR,rmp
	; Init Registers
	clr rLmp ; clear lamp counter
	clr rSec ; clear second counter
	clr rMin ; clear minute counter
	; Init LED-output
	cbi dLRot,bLRot ; red lamp off
	; Init 8-Bit-Timer 0 for Tone output
	sbr rFlg,1<<fs ; Set Flag Begin Melody
	ldi XH,HIGH(2*mtab); Set to melody start
	ldi XL,LOW(2*mtab)
	ldi rmp,(1<<COM0A0)|(1<<WGM01) ; CTC, Compare A
	out TCCR0A,rmp
	ldi rmp,1<<CS00 ; Timer Prescaler = 1, Timer on
	out TCCR0B,rmp
	; Init OC0A-Port output for speaker
	sbi dOcO,bOcO
	; Init Timer 1 for time and lamp control
	ldi rmp,HIGH(50000) ; CTC value
	out OCR1AH,rmp
	ldi rmp,LOW(50000)
	out OCR1AL,rmp
	clr rmp ; control word A
	out TCCR1A,rmp
	ldi rmp,(1<<WGM12)|(1<<CS10) ; to CTC, prescaler = 1
	out TCCR1B,rmp
	ldi rmp,1<<OCIE1A ; Timer1-Interrupt
	out TIMSK,rmp
	; set sleep mode enable
	ldi rmp,1<<SE ; sleep idle
	out MCUCR,rmp
	sei ; Enable Ints
; main program loop
loop:
	sleep ; sleep again
	nop ; dummy for wake-up
	brtc loop ; no Timer1-Interrupt?
	clt ; clear flag
	sbrc rFlg,fs ; Begin melody?
	rcall Beginn
	sbrc rFlg,fs
	rjmp Loop1
	ldi rmp,0
	out TCCR0B,rmp
Loop1:
	rcall LampOff
	inc rLmp ; inc lamp counter
	rcall LampOn
	cpi rLmp,10 ; lamp counter at 10?
	brcs loop
	clr rLmp
	rcall LampOn
	inc rSec
	cpi rSec,30
	brcs loop
	clr rSec
	sbrc rFlg,fs
	rjmp Loop2
	rcall Ton 
	ldi rmp,1<<CS00 ; Timer Prescaler = 1, Timer on
	out TCCR0B,rmp
Loop2:
	inc rMin
	cpi rMin,10
	brcs loop
	sbi dLRot,bLRot ; red Overflow-Lamp on
	cbi pLRot,bLRot
	sbrc rFlg,fs
	rjmp Loop
	sbr rFlg,fs ; Begin melody on
	ldi XH,HIGH(2*mtab)
	ldi XL,LOW(2*mTab)
	rjmp loop
;
; *******************************
;  Melody at start-up
; *******************************
;
Beginn:
	mov ZH,XH ; copy tone pointer
	mov ZL,XL
	adiw XL,1 ; to next tone
	lpm
	mov rmp,R0 ; copy to rmp
	cpi rmp,0xFF ; last tone?
	brne Beginn2
	cbr rFlg,1<<fs ; Begin melody off
Beginn1:
	clr rmp ; tone off
	out TCCR0B,rmp
	ret
Beginn2:
	subi rmp,1 ; pause?
	brcs Beginn1 ; yes, tone off
	ldi ZH,HIGH(2*Tonleiter) ; tone from gamut
	ldi ZL,LOW(2*Tonleiter)
	add ZL,rmp ; add tone
	ldi rmp,0
	adc ZH,rmp
	lpm
	out OCR0A,R0 ; to OCR0A
	lsr R0 ; half
	out OCR0B,R0 ; to OCR0B
	ldi rmp,1<<CS00 ; tone on
	out TCCR0B,rmp
	ret
;
; ***************************************
;           LED - Switching
; ***************************************
;
; Current LED off
;
LampOff:
	ldi ZH,HIGH(TabAus) ; LED off
	ldi ZL,LOW(TabAus)
	add ZL,rLmp ; add current LED
	brcc LampOff1
	inc ZH
LampOff1:
	ijmp ; disable current LED
;
; Switch current LED on
;
LampOn:
	cp rLmp,rMin ; current lamp < minutes?
	brcc LampGn ; >= minute, lamp green
	ldi ZH,HIGH(TabRot) ; lamp red
	ldi ZL,LOW(TabRot)
	add ZL,rLmp ; add current LED
	brcc LampOn1
	inc ZH
LampOn1:
	ijmp ; activate current LED
LampGn:
	ldi ZH,HIGH(TabGruen) ; lamp green
	ldi ZL,LOW(TabGruen)
	add ZL,rLmp ; add current lamp
	brcc LampGn1
	inc ZH
LampGn1:
	ijmp ; activate current LED
;
; ***************************************
;           Jump tables
; ***************************************
;
; Jump table switching LED off
TabAus:
	rjmp L1A
	rjmp L2A
	rjmp L3A
	rjmp L4A
	rjmp L5A
	rjmp L6A
	rjmp L7A
	rjmp L8A
	rjmp L9A
	rjmp L10A
	ret ; dummies
	ret
	ret
; Jump table for LED red
TabRot:
	rjmp L1R
	rjmp L2R
	rjmp L3R
	rjmp L4R
	rjmp L5R
	rjmp L6R
	rjmp L7R
	rjmp L8R
	rjmp L9R
	rjmp L10R
	ret ; dummies
	ret
	ret
; Jump table for LED green
TabGruen:
	rjmp L1G
	rjmp L2G
	rjmp L3G
	rjmp L4G
	rjmp L5G
	rjmp L6G
	rjmp L7G
	rjmp L8G
	rjmp L9G
	rjmp L10G
	ret ; dummies
	ret
	ret
;
; ***************************************
;       Lamp switching routines
; ***************************************
;
; Lamps off
L1A:
	cbi pL1A,bL1A
	cbi pL1K,bL1K
	cbi dL1A,bL1A
	cbi dL1K,bL1K
	ret
L2A:
	cbi pL2A,bL2A
	cbi pL2K,bL2K
	cbi dL2A,bL2A
	cbi dL2K,bL2K
	ret
L3A:
	cbi pL3A,bL3A
	cbi pL3K,bL3K
	cbi dL3A,bL3A
	cbi dL3K,bL3K
	ret
L4A:
	cbi pL4A,bL4A
	cbi pL4K,bL4K
	cbi dL4A,bL4A
	cbi dL4K,bL4K
	ret
L5A:
	cbi pL5A,bL5A
	cbi pL5K,bL5K
	cbi dL5A,bL5A
	cbi dL5K,bL5K
	ret
L6A:
	cbi pL6A,bL6A
	cbi pL6K,bL6K
	cbi dL6A,bL6A
	cbi dL6K,bL6K
	ret
L7A:
	cbi pL7A,bL7A
	cbi pL7K,bL7K
	cbi dL7A,bL7A
	cbi dL7K,bL7K
	ret
L8A:
	cbi pL8A,bL8A
	cbi pL8K,bL8K
	cbi dL8A,bL8A
	cbi dL8K,bL8K
	ret
L9A:
	cbi pL9A,bL9A
	cbi pL9K,bL9K
	cbi dL9A,bL9A
	cbi dL9K,bL9K
	ret
L10A:
	cbi pL10A,bL10A
	cbi pL10K,bL10K
	cbi dL10A,bL10A
	cbi dL10K,bL10K
	ret
; Lampe auf Gruen
L1G:
	sbi pL1A,bL1A
	cbi pL1K,bL1K
	sbi dL1A,bL1A
	sbi dL1K,bL1K
	ret
L2G:
	sbi pL2A,bL2A
	cbi pL2K,bL2K
	sbi dL2A,bL2A
	sbi dL2K,bL2K
	ret
L3G:
	sbi pL3A,bL3A
	cbi pL3K,bL3K
	sbi dL3A,bL3A
	sbi dL3K,bL3K
	ret
L4G:
	sbi pL4A,bL4A
	cbi pL4K,bL4K
	sbi dL4A,bL4A
	sbi dL4K,bL4K
	ret
L5G:
	sbi pL5A,bL5A
	cbi pL5K,bL5K
	sbi dL5A,bL5A
	sbi dL5K,bL5K
	ret
L6G:
	sbi pL6A,bL6A
	cbi pL6K,bL6K
	sbi dL6A,bL6A
	sbi dL6K,bL6K
	ret
L7G:
	sbi pL7A,bL7A
	cbi pL7K,bL7K
	sbi dL7A,bL7A
	sbi dL7K,bL7K
	ret
L8G:
	sbi pL8A,bL8A
	cbi pL8K,bL8K
	sbi dL8A,bL8A
	sbi dL8K,bL8K
	ret
L9G:
	sbi pL9A,bL9A
	cbi pL9K,bL9K
	sbi dL9A,bL9A
	sbi dL9K,bL9K
	ret
L10G:
	sbi pL10A,bL10A
	cbi pL10K,bL10K
	sbi dL10A,bL10A
	sbi dL10K,bL10K
	ret
; Lampe auf Rot
L1R:
	cbi pL1A,bL1A
	sbi pL1K,bL1K
	sbi dL1A,bL1A
	sbi dL1K,bL1K
	ret
L2R:
	cbi pL2A,bL2A
	sbi pL2K,bL2K
	sbi dL2A,bL2A
	sbi dL2K,bL2K
	ret
L3R:
	cbi pL3A,bL3A
	sbi pL3K,bL3K
	sbi dL3A,bL3A
	sbi dL3K,bL3K
	ret
L4R:
	cbi pL4A,bL4A
	sbi pL4K,bL4K
	sbi dL4A,bL4A
	sbi dL4K,bL4K
	ret
L5R:
	cbi pL5A,bL5A
	sbi pL5K,bL5K
	sbi dL5A,bL5A
	sbi dL5K,bL5K
	ret
L6R:
	cbi pL6A,bL6A
	sbi pL6K,bL6K
	sbi dL6A,bL6A
	sbi dL6K,bL6K
	ret
L7R:
	cbi pL7A,bL7A
	sbi pL7K,bL7K
	sbi dL7A,bL7A
	sbi dL7K,bL7K
	ret
L8R:
	cbi pL8A,bL8A
	sbi pL8K,bL8K
	sbi dL8A,bL8A
	sbi dL8K,bL8K
	ret
L9R:
	cbi pL9A,bL9A
	sbi pL9K,bL9K
	sbi dL9A,bL9A
	sbi dL9K,bL9K
	ret
L10R:
	cbi pL10A,bL10A
	sbi pL10K,bL10K
	sbi dL10A,bL10A
	sbi dL10K,bL10K
	ret
;
; End of source code
;



To the top of page

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