Path: Home => AVR overview => Applications => ATtiny with crystals => Sec/Min/Hr timer =gt; Source code   Diese Seite in Deutsch: Flag DE Logo
Tiny XTAL oscillator Applications of
AVR single chip controllers AT90S, ATtiny, ATmega and ATxmega
Source code for the seconds/minutes/hours timer with a crystal

Source code for the seconds, minutes and hour pulses with a crystal

The original source code in assembler format is here, here is a browser display of it.

;
; ******************************************
; * Seconds/Minutes/Hour derived from Xtal *
; * with an ATtiny25, Version 1            *
; * (C)2022 by Gerhard Schmi               *
; ******************************************
;
.nolist
.include "tn25def.inc" ; Define device ATtiny25
.list
;
; ***********************************
;        H A R D W A R E
; ***********************************
;
; Device: ATtiny25, Package: 8-pin-PDIP_SOIC
;            _________
;         1 /         |8
; Reset o--|Reset  Vcc|--o +3..5V
;  Xtal o--|Xtal1  Pb2|--o Hours
;  Xtal o--|Xtal2  Pb1|--o Minutes
;    0V o--|Gnd    Pb0|--o Seconds
;        4 |__________|5
;
; **********************************
;       C O N S T A N T S
; **********************************
;
; Import from LibreOffice-Calc file
; 8-Bit-TC 0
.equ clock = 32768 ; Crystal clock in Hz
.equ cPresc8 = 256 ; Prescaler for 8-Bit-TC
.equ cPrescBits8 = (1<<CS02) ; Prescaler-bits for 8-bit-TC
.equ cCtcDiv8 = 64 ; CTC-divider 8-Bit-TC
.equ cCompA8 = 63 ; Compare-A-value
.equ cSoftCnt8 = 1 ; Software-counter
;
; **********************************
;       R E G I S T E R S
; **********************************
;
; free: R0 to R15
.def rmp = R16 ; Multi purpose register
.def rMin = R17 ; Minute register
.def rHr = R18 ; Hour register
.if (cSoftCnt8 > 1) && (cSoftCnt8 < 257)
  .def rSec = R19
  .endif
; free: R19 to R23
.if cSoftCnt8 > 256
  .def rSecL = R24
  .def rSecH = R25
  .endif
; free: R26 to R31
;
.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 ; PCI0
	reti ; OC1A
	reti ; OVF1
	reti ; OVF0
	reti ; ERDY
	reti ; ACI
	reti ; ADCC
	reti ; OC1B
	rjmp Oc0aIsr ; OC0A
	reti ; OC0B
	reti ; WDT
	reti ; USI_START
	reti ; USI_OVF
;
; **********************************
;  I N T - S E R V I C E   R O U T .
; **********************************
;
Oc0aIsr:
.if (cSoftCnt8 > 1) && (cSoftCnt8 < 257) ; 8-bit-counter
  dec rSec ; 8-bit-count
  brne Oc0aIsrReti
  ldi rSec,cSoftCnt8 ; Restart 8-bit counter
  .endif
.if cSoftCnt8 > 256
  sbiw rSecL,1 ; 16-bit-count
  brne Oc0aIsrReti
  ldi rSecH,High(cSoftCnt8) ; Restart 16-bit-counter
  ldi rSecL,Low(cSoftCnt8)
  .endif
  sbi PINB,0 ; Toggle secnds output
  dec rMin ; Minute count
  brne Oc0aIsrReti
  sbi PINB,1 ; Toggle Minute output
  ldi rMin,60 ; Restart minutes
  dec rHr ; Count hours
  brne Oc0aIsrReti
  sbi PINB,2 ; Toggle hours output
  ldi rHr,60
Oc0aIsrReti:
  reti
;
; **********************************
;  M A I N   P R O G R A M   I N I T
; **********************************
;
Main:
; Stack init for interrupts
.ifdef SPH ; Stack pointer for ATtiny85
  ldi rmp,High(RAMEND) ; Stack pointer MSB
  out SPH,rmp
  .endif
	ldi rmp,Low(RAMEND) ; Stack pointer LSB
	out SPL,rmp ; Init LSB stack pointer
; Portbits as outputs
  sbi DDRB,0 ; Output PB0
  sbi DDRB,1 ; Output PB1
  sbi DDRB,2 ; Output PB2
; Second, Minute- and Hour-register init
.if cSoftCnt8 > 1 ; when larger than 1 software counter
  .if cSoftCnt8 < 257
    ldi rSec,cSoftCnt8 ; 8-bit-software counter
    .else
    ldi rSecH,High(cSoftCnt8) ; 16-bit-software counter
    ldi rSecL,Low(cSoftCnt8)
    .endif
  .endif
  ldi rMin,60 ; 60 seconds per minute, 0.5 s
  ldi rHr,60 ; 60 minutes per hour, 0.5 s
; Timer TC0 init
  ldi rmp,cCompA8 ; CTC-Divider value for 0.5 seconds
  out OCR0A,rmp ; To Compare-Portregister
  ldi rmp,1<<WGM01 ; CTC-Mode TC0
  out TCCR0A,rmp ; To Mode register A TC0
  ldi rmp,cPrescBits8 ; Presdcaler TC0
  out TCCR0B,rmp ; to Mode register B TC0
  ldi rmp,1<<OCIE0A ; Compare match interrupt
  out TIMSK,rmp ; to Timer-Int maske
  ; Sleep enable
  ldi rmp,1<<SE ; Sleep idle
  out MCUCR,rmp
; Enable interrupts
	sei ; Enable interrupts
;
; **********************************
;    P R O G R A M   L O O P
; **********************************
;
Loop:
  sleep
  rjmp loop
;
; End of source code
;
; Copyright information
  .db "(C)2022 by Gerhard Schmidt  " ; Source code readable
  .db "C(2)20 2ybG reahdrS hcimtd  " ; Machine code format
;



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