Getting Started

(lily) #1

Chapter 7: Microcontroller Interrupts and Timers


SCCAL_calibration() function – the code:

*************************************************

ameters : None

CLKPR = (1<<CLKPCE); // set Clock Prescaler Change Enable
hz / 8 = 1Mhz
;
TI e OCIE2A and TOIE2
timer2

OC // set timer2 compare value
TIMSK0 = 0; // delete any interrupt sources
aling
aling

i(); // mt __disable_interrupt(); // disable global
interrupt
te TIFR1 flags
e TIFR2 flags

O

/**





  • Function name : OSCCAL_calibration



  • Returns : None



  • Par



  • Purpose : Calibrate the internal OSCCAL byte, using the external

  • 32,768 kHz crystal as reference


  • ***/
    void OSCCAL_calibration(void)
    {
    unsigned char calibrate = FALSE;
    int temp;
    unsigned char tempL;




// set prescaler = 8, Inter RC 8M
CLKPR = (1<<CLKPS1) | (1<<CLKPS0)


MSK2 = 0; //disabl


ASSR = (1<<AS2); //select asynchronous operation of
(32,768kHz)

R2A = 200;


TCCR1B = (1<<CS10); // start timer1 with no presc
TCCR2A = (1<<CS20); // start timer2 with no presc


//wait for TCN2UB and TCR2UB to clear
while((ASSR & 0x01) | (ASSR & 0x04));


Delay(1000); // wait for external crystal to stabilise


while(!calibrate)
{
cl


TIFR1 = 0xFF; // dele
TIFR2 = 0xFF; // delet

Free download pdf