Path: Home => AVR overview => Applications => ATtiny with crystals => 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 clocking an ATtiny with a crystal

Source code for ATtiny with a crystal

The original source code in assembler format is here. The source code:

;
; *********************************
; * Xtal oscillator with ATtiny25 *
; * (C)2019 by DG4FAC             *
; *********************************
;
.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 +5 V
; XTAL1 o--|PB3    PB2|--o
; XTAL2 o--|PB4    PB1|--o Osc out 2
;   0 V o--|GND    PB0|--o Osc out 1
;         4|__________|5
;
;
; **********************************
;  A D J U S T A B L E   C O N S T
; **********************************
;
.equ clock = 15000000 ; 15 MHz, your xtal frequency
.equ fOut = 100 ; Your desired frequency
.equ cClkPresc = 1 ; Your clock prescaler value 
.equ cUseOC0B = 1 ; Use OC0B as reverse output
;
; **********************************
;   F I X E D   C O N S T A N T S
; **********************************
;
; Derive prescaler value from clock and frequency
.if (clock/fOut/cClkPresc/2)<=256
  .equ cPresc=1
  .equ cCsPresc=1
  .else
  .if (clock/fOut/cClkPresc/2/8)<=256
    .equ cPresc=8
    .equ cCsPresc=2
    .else
    .if (clock/fOut/cClkPresc/2/64)<=256
      .equ cPresc=64
      .equ cCsPresc=3
      .else
      .if (clock/fOut/cClkPresc/2/256)<=256
        .equ cPresc=256
        .equ cCsPresc=4
        .else
        .if (clock/fOut/cClkPresc/2/1024)>256
          .error "Desired frequency too low!"
          .else
          .equ cPresc=1024
          .equ cCsPresc=5
          .endif
        .endif
      .endif
    .endif
  .endif
;
; Derive divider and compare value
.equ divider = (((((clock+fout/2)/fOut+cClkPresc/2)/cClkPresc+cPresc/2)/cPresc)+1)/2
.equ cCtc = divider - 1 ; CTC value
;
; **********************************
;        R E G I S T E R S
; **********************************
;
.def rmp = R16 ; Multipurpose register
;
; **********************************
;  M A I N   P R O G R A M   I N I T
; **********************************
;
.cseg
.org 000000
;
Main:
  sbi DDRB,DDB0 ; PB0 direction output
  cbi PORTB,PORTB0 ; Clear OC0A output
  ldi rmp,cCtc ; Write CTC value
  out OCR0A,rmp ; to compare register A
  .if cUseOC0B == 1
    sbi DDRB,DDB1 ; PB1 direction output
    sbi PORTB,PORTB1 ; Set OC0B output
    out OCR0B,rmp ; and B
    ldi rmp,(1<<WGM01)|(1<<COM0A0)|(1<<COM0B0) ; CTC mode, toggle OC0A and OC0B
    .else
    ldi rmp,(1<<WGM01)|(1<<COM0A0) ; CTC mode, toggle OC0A
    .endif
  out TCCR0A,rmp ; in TC0 control port A
  ldi rmp,cCsPresc ; Prescaler setting
  out TCCR0B,rmp ; in TC0 control port B
  ldi rmp,1<<SE ; Sleep enable, idle mode
  out MCUCR,rmp
Loop:
  sleep ; Go to sleep
  rjmp loop
;
; End of source code                

15MHz crystal generates 100 Hz This is a simulation with the above listed software setting.


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