Note that there are some explanatory texts on larger screens.

plurals
  1. POAVR Butterfly - Timer reuse after internal oscillator calibration
    primarykey
    data
    text
    <p>I am calibrating the AVR Butterfly internal oscillator for being able to use USART, based on the sample code provided by AVR (see code below). As I also wanted to use two timer-controlled servo motors, I am wondering whether it is possible to reuse 16-bit timer 1 after the calibration process - I tried resetting the TCCR1A/B but it did not work out (code also below). I hope you can help me out with this.</p> <pre><code>void OSCCAL_Calibrate(void){ unsigned char calibrate = 0; int temp; unsigned char tempL; CLKPR = (1&lt;&lt;CLKPCE); // set Clock Prescaler Change Enable // set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz CLKPR = (1&lt;&lt;CLKPS1) | (1&lt;&lt;CLKPS0); TIMSK2 = 0; //disable OCIE2A and TOIE2 ASSR = (1&lt;&lt;AS2); //select asynchronous operation of timer2 (32,768kHz) OCR2A = 200; // set timer2 compare value TIMSK0 = 0; // delete any interrupt sources TCCR1B = (1&lt;&lt;CS10); // start timer1 with no prescaling TCCR2A = (1&lt;&lt;CS20); // start timer2 with no prescaling while((ASSR &amp; 0x01) | (ASSR &amp; 0x04)); //wait for TCN2UB and TCR2UB to be cleared delayMs(1000); // wait for external crystal to stabilise while(!calibrate) { cli(); // disable global interrupt TIFR1 = 0xFF; // delete TIFR1 flags TIFR2 = 0xFF; // delete TIFR2 flags TCNT1H = 0; // clear timer1 counter TCNT1L = 0; TCNT2 = 0; // clear timer2 counter while ( !(TIFR2 &amp; (1&lt;&lt;OCF2A)) ); // wait for timer2 compareflag TCCR1B = 0; // stop timer1 sei(); // enable global interrupt if ( (TIFR1 &amp; (1&lt;&lt;TOV1)) ) { temp = 0xFFFF; // if timer1 overflows, set the temp to 0xFFFF }else { // read out the timer1 counter value tempL = TCNT1L; temp = TCNT1H; temp = (temp &lt;&lt; 8); temp += tempL; } if (temp &gt; 6250) OSCCAL--; // the internRC oscillator runs to fast, decrease the OSCCAL else if (temp &lt; 6120) OSCCAL++; // the internRC oscillator runs to slow, increase the OSCCAL else calibrate = 1; // the interRC is correct TCCR1B = (1&lt;&lt;CS10); // start timer1 } } void motorInit(){ // reset timer 1 TCCR1A = 0; TCCR1B = 0; // initialize Servo Pins DDRB |= (1&lt;&lt;PB5) | (1&lt;&lt;PB6); ICR1H = ICR_VALUE &gt;&gt; 8; ICR1L = ICR_VALUE &amp; (TOP_VALUE); // reset OCRs setServoSpeed(0, 0); setServoSpeed(1, 0); // Set Timer mode (PWM Phase &amp; Freq. correct, clear on compare match) // and prescaler (8) TCCR1A = ((1&lt;&lt;COM1A1) | (1&lt;&lt;COM1B1)); TCCR1B = ((1&lt;&lt;WGM13) | (0&lt;&lt;CS12) | (1&lt;&lt;CS11) | (0&lt;&lt;CS10)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload