32.768 khz on pin 9 with a 8Mhz arduino

i have a arduino pro mini 3.3 v 8mhz and i need to generate a signal close to 32.768 khz on pin 9 to drive a sensor...

i have seen this

TCCR1B = (TCCR1B & 0xF8) | 1 ; //generates the MCKL signal
  analogWrite (clock, 128) ;

but this is for a 16 mhz clock which i dont have

so for i get is that the instruction loads on the timer a value of 63 488... for what i have been reading this will count until overfloww FFFF... 2047 times... right?

so for a 8 mhz it have to count in half the time so i have to count faster half of 2047 which is 1023?

so i have to load tccr1b with 64511? 0xFB and tccr1a with 0xFF ?

please help me!!!!

void setup ()
{
  pinMode (9, OUTPUT);
  
    // set up Timer 1
  TCCR1A = _BV (COM1A0);  // toggle OC1A on Compare Match
  TCCR1B = _BV(WGM12) | _BV(CS10);   // CTC, no prescaling
  OCR1A =  121;       // output every 122 cycles
}

void loop () {}

That should do it, at 8 MHz clock, it should give you 32786 KHz (on pin 9).

thanks

i will try as soon as i get home :slight_smile:

it worked.. thanks