//======================================================
//   (c)2008-9 Matthew Ford
//   Forward Computing and Control Pty. Ltd.
//   www.forward.com.au
//   All rights reserved
//
//   You may freely copy and distribute this program for 
//           commercial and non-commercial use provided:-
//   i) this copyright notice remains attached
//   ii) you clearly mark any changes you make to the code
//======================================================

.include "tn84def.inc"
.def Temp = r16
.def Temp2 = r15
//*************************************************************************
//
//	PROGRAM START - EXECUTION STARTS HERE
//  Before running this program
// i) use the AVR Dragon programming Fuses to turn on CKOUT
// ii) use the AVR Dragon programming Advance tab to Read to clock calibration byte
// iii) Conect a Frequency meter to PB2 and read the clock frequency
// Then set ldi Temp value below and program the uC and check the frequency
// adjust as necessary by no more then 0x20 each step, use more then one
// step if necessary to get to the required value.
// See Typical characteristics in the Attiny 84 specs for
// osc values versus frequency
// between -20degC and +80degC the freq varies by +/-0.2Mhz (+/-2.5%) 
// so if you calibrate for 8Mhz at 25degC you will be well within the
// +/-4% over this temperature range
//
//*************************************************************************
	.cseg
//  INTERRUPT VECTORS 
.org 0x0000
   rjmp RESET


RESET:
    // set clock to 8Mhz i.e. remove div8 that is set by programmer
    ldi Temp, (1<<CLKPCE)
    out CLKPR, Temp  // enable clock change for next 4 cycles
    clr Temp
    out CLKPR,Temp  // set 8Mhz clock

//  this code loads osccal in one step from hard coded value
//  determined from stepping through the table below
// NOTE if you need to go more then 0x20 from the initial value
// do it steps of less then 0x20 say 0x18
// eg. if you need to set 0xCE
//  then use
// ldi Temp, 0xB6
// out OSCCAL, Temp
// ldi Temp, 0xCE   
// out OSCCAL, Temp  // to limit each change to less the 0x20
// 
//  default cal is 9E for 7.77Mhz, 0xA5 gives 8.04Mhz at about 26deg
    ldi Temp, 0xA5
    out OSCCAL, Temp
MAIN_LOOP:
    rjmp MAIN_LOOP 

