Using 32kHz external crystal for Timer2 on barebones arduino

Hello,

First of all, I must say that I have found a lot of other discussions about the topic, but none really solved my problem..

So, I'm building a bearbones arduino using an atmega328PU following this schematic: Barebones arduino with no PSU example - Arduino Learning and added a tactile switch for the reset. But instead of using a 16MHz external crystal, I run the Atmega on its internal 8MHz oscillator following the ''Minimal Circuit (Eliminating the External Clock)'' instruction from this page: https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard. At this point, everything works perfectly fine when using Timer2 interrupt (e.g. to make blink a LED).

From this, I wanted to use the Asynchronous mode for Timer2 with an external 32kHz crystal. I use the WATCH crystal from IQD and mount it as it would have been for the 16MHz crystal from the first link but without the capacitors (I found that it wasn't mandatory, but I also tried with and it gaves me the same results). From the Gammon website on the page about Timer, I found that the command to set Timer2 working from an external crystal was : ASSR = bit (AS2);
But when trying with that command and the crystal mounted, Timer2 seems not working anymore. The code I used make use of the Timer2 interrupt (I used TIMER2_COMPA_vect) to make change a LED state at each interruption. I repeat that it works fine when relying on the internal oscillator by simply removing ''ASSR = bit (AS2);''

Also why it doesn't work with the external crystal ? Is it the crystal that is not adapted ? The command I used to set timer2 clocked from external crystal is not enough and some fuses need to be changed ?

Thanks in advance for the help and the answer is may be trivial, I am still discovering the arduino world :smiley:

Are you sure that the external oscillator is working, producing the 32kHz clock?

Maybe you can try the C example at 4.3 here :

It appears to very close to what you are attempting.

Also post your original code.

Post the code, using code tags.

The datasheet has some warnings about switching modes and writing to registers:
18.9 Asynchronous Operation of Timer/Counter2
When Timer/Counter2 operates asynchronously, some considerations must be taken.
Warning: When switching between asynchronous and synchronous clocking of Timer/Counter2, the Timer Registers TCNT2, OCR2x, and TCCR2x might be corrupted. A safe procedure for switching clock source is:
Disable the Timer/Counter2 interrupts by clearing OCIE2x and TOIE2.

  • Select clock source by setting AS2 as appropriate.
  • Write new values to TCNT2, OCR2x, and TCCR2x.
  • To switch to asynchronous operation: Wait for TCN2xUB, OCR2xUB, and TCR2xUB.
  • Clear the Timer/Counter2 Interrupt Flags.
  • Enable interrupts, if needed.

The CPU main clock frequency must be more than four times the Oscillator frequency.
When writing to one of the registers TCNT2, OCR2x, or TCCR2x, the value is transferred to a temporary register, and latched after two positive edges on TOSC1. The user should not write a new value before the contents of the temporary register have been transferred to its destination. Each of the five mentioned registers have their individual temporary register, which means that e.g. writing to TCNT2 does not disturb an OCR2x write in progress. To detect that a transfer to the destination register has taken place, the Asynchronous Status Register – ASSR has been implemented.

Here is what I use to initialize Timer2 for an 1 second interrupt source, using an external 32 kHz crystal and 2x30 pF capacitors. The CPU runs on the 8 MHz internal oscillator.

// initialize Timer2 as asynchronous 32768 Hz timing source

void timer2_init(void) {
  TCCR2B = 0;  //stop Timer 2
  TIMSK2 = 0;	// disable Timer 2 interrupts
  ASSR = (1 << AS2);	// select asynchronous operation of Timer2
  TCNT2 = 0;			// clear Timer 2 counter
  TCCR2A = 0; 		//normal count up mode, no port output
  TCCR2B = (1 << CS22) | (1 << CS20);		// select prescaler 128 => 1 sec between each overflow

  while (ASSR & ((1 << TCN2UB) | (1 << TCR2BUB))); // wait for TCN2UB and TCR2BUB to be cleared

  TIFR2 = (1 << TOV2);			// clear interrupt-flag
  TIMSK2 = (1 << TOIE2);	// enable Timer2 overflow interrupt
}

Thanks all for your advices ! It took me a bit to answer but I wanted to try multiple things before coming back.And finally I succeed creating a 1Hz blinking program using the bare bone arduino with an external 32kHz crystal by just not connecting the crystal to the ground... :cold_sweat:
So, why does it works witout connecting the crystal to the ground ? It needs to be done only if the crystal is used as the chip main clock ? From what I understood in the datasheet it should be the same setup, but I am not sure at all :smiley: And this way, I cannot add any capacitors, so it seems strange. Maybe it is this crystal specifically that allow this setup (datasheet can be found in my first message)

Thanks again !

The crystal is never connected to ground.

It must be connected to the MCU chip in the same way as any other crystal, to the input and output pins of the oscillator section. For accuracy (and often, for the oscillator to function at all), you must use the proper capacitors, which ARE connected to ground. See the MCU datasheet for the details.

The capacitance value to be used is specified in the data sheet for the crystal.

From the ATmega328 data sheet:
osc.png

osc.png

@JRemington: Sorry, I was not clear. The immage you sent is exactly the settup I thought was the right one but it is not working. Instead, just having the crystal between the 2 pin (as it can be seen in the image in attachment) make it works.

I put the datasheet of the crystal I used in attachment. Maybe it will give you more informations. Ah and I saw on the middle of this tread (Timer2 Interrups REFUSE to work! :( - Programming Questions - Arduino Forum) that someone does not connect the crystal to capacitors/ground too.

watch_eng_tds.pdf (127 KB)

osc.png

The data sheet says that the total load capacitance should be 6 to 12.5 pF.

That would be two capacitors of 12 to 25 pF connected as in reply #7, not counting stray circuit capacitance.

Try 2x15 pF or 2x22 pF.

m_pi:
@JRemington: Sorry, I was not clear. The immage you sent is exactly the settup I thought was the right one but it is not working. Instead, just having the crystal between the 2 pin (as it can be seen in the image in attachment) make it works.

On the picture the crystal is not connected to GND. When you are not using caps you use "0 pF" which is open circuit, not short.

The low frequency oscillator has some load capacitance on chip, often no external caps are needed for a watch crystal.

Thanks you for your help ! Finally it is just my noobiness in basic electronics that failed me hahaha I was positioning the capacitors in the same line of the breadboard... I laugh alone when I discover the stupidity of my mistake :roll_eyes: Sorry for wasting some of your time but thanks you again for helping me :slight_smile: