From: Tobias Girstmair Date: Fri, 21 Dec 2018 18:58:08 +0000 (+0100) Subject: calibrate oscillator X-Git-Tag: attiny9~5 X-Git-Url: https://git.gir.st/Chiptunes.git/commitdiff_plain/2af726bccc686e5f36046dc1cf15721dfcf64018 calibrate oscillator just measure frequency of PORTB2 and fiddle with the number until it's close enough. don't forget to hold your tongue at the right angle! --- diff --git a/foo.S b/foo.S index 566d9fc..84336ca 100644 --- a/foo.S +++ b/foo.S @@ -30,6 +30,7 @@ SPL = 0x3D SPH = 0x3E CCP = 0x3C CLKPSR = 0x36 +OSCCAL = 0x39 WDTCSR = 0x31 SMCR = 0x3A TCCR0A = 0x2E @@ -235,7 +236,7 @@ g: ; g(i, t) -> t end_mul: LSR a1 ;final shift is a common operation for all - MOV t, a1 ;;TODO: use a1 in main() directly + MOV t, a1 ;;TODO: use a1 in loop: directly #undef a1 #undef a2 RET ; TODO: replace CALL/RET with IJMP? @@ -259,6 +260,8 @@ main: ; setup routine LDI x, 0xd8 OUT CCP, x ; change protected ioregs OUT CLKPSR, one ; clock prescaler 1/2 (4Mhz) + LDI x, 0xa7 ; determined by trial-and-error (->PORTB2) + OUT OSCCAL, x ; set oscillator calibration OUT WDTCSR, zero; turn off watchdog ;;TODO: incomplete - see datasheet pg48 ; OUT SMCR, 2 ; sleep mode 'power down' ('idle' (default) has faster response time)