# ATtiny External Clock not working

So, I've been wanting to break away from the arduino abstraction for a while now. I made a board that has an ATtiny10 on it with a crystal and an output. I cannot for the life of me understand what I am doing wrong.

Here's the problem: When I select the clock source, the AVR stops working.

I had a custom PCB (small one) made to mount everything on. Thinking I didn't do something right (even though schematic looked right)

I changed design and made another one (for the second one, I had traces with clock surrounded by a ground plane (Did not work still) [Also, the first design was using everything but the AVR sourced from ebay. Thinking maybe it was a quality issue the second board is sourced entirely from Mouser]

Lastly, to make sure I wasn't an absolute idiot, I bought a breakout board and just breadboarded the circuit. This still performed just like all the others

It works just fine with the internal oscillator, but as soon as I program to change clock source it stops.

Note that I added R2 to keep the MOSFET pulled down but I do not populate it for programming it, as TPI (Tiny Programming Interface) uses pullups on that line and I cannot populate that if I am going to program the chip

Page 21 of the datasheet regarding changing clock

Page 22 of the datasheet regarding the prescaler (Clock prescaler not timer prescaler)

I am using a MkII programmer from Atmel and these are the fuse bits:

Output External Clock

0xFB

This was set through Atmel studio and I have used a couple different ATtiny's and the chips kept these settings once set, so I am pretty sure the fuses are writing correctly

Now the code: (This is the whole program, the timer portion works (obviously not though once the clock switches))

#define F_CPU   8000000

#include <avr/io.h>
#include <avr/interrupt.h>

void initClock()
{
// Setting CLKPSR does not affect the problem (It doesn't work regardless of what this is set to)
// I have tried this before and after setting CLKMSR

CCP = 0xD8;
CLKPSR = 0;

CCP = 0xD8;
CLKMSR = 0b10;

}

void initPorts()
{

DDRB |= (1 << PORTB0); // PB0 = OCR0A
}

void initTimer()
{
// I posted this code just in case, this works as expected (but only on the internal oscillator)

// We want Compare Output Mode, Clear OC0A on Compare Match
TCCR0A =    (1 << COM0A0);

// Overflow setting
TIMSK0 |= (1 << OCIE0A);

// We will not use a prescaler
// This also starts the timer
TCCR0B =    (1 << CS00) | (1 << WGM02);

// This is the value at which the timer will restart
OCR0A = 8299;

// Set external interrupts
sei();

}

int main(void)
{
initClock();
initPorts();
initTimer();

while(1)
{

}
}


Surely there has to be something I am missing. I've tried to read and reread the especially the clock sections of the datasheet in order to figure it out myself. I am stumped though. Maybe someone could help me understand my mistake.

Thank you! Please let me know if there is any more info I can add to make it easier to understand

Attiny10 Datasheet

• try to write a 0 to CLKM0, if it's a 1 you end up with the 'reserved' combination and who knows what can happen. – Vladimir Cravero Aug 1 '14 at 7:55
• @VladimirCravero I set that bit to 0 and it does not start the external clock – Dan Aug 1 '14 at 7:57
• That bit is zero anyway on reset, but I can't see where in your code you set it to zero. – Vladimir Cravero Aug 1 '14 at 7:59
• @VladimirCravero sorry, I meant I did change that after you asked. I will edit to reflect the change. Sorry for the misunderstanding – Dan Aug 1 '14 at 8:01

## 1 Answer

Using the two leg crystal is not possible with the specific device. There is only the option to use an external clock source, so you can use an external oscillator like the following one and apply the clock pulses to the CLKI pin (pin 1).

Unlike the two leg crystal that needs additional circuitry inside the mcu and two capacitors, this four pin crystal oscillator is a complete oscillator that only needs power to generate the clock.

• I was even thinking that the compiler was optimizing the second write to CCP thus breaking the four clock cycles limit... Nice one. – Vladimir Cravero Aug 1 '14 at 8:05
• Ah! That makes sense. I look at the Atmega328 and see it has XTAL pins, while this one has CLKI and CLK0 pins. Does that naming on AVR chips mean it does not have the internal circuitry? – Dan Aug 1 '14 at 8:08
• @Dan When a device supports operation with a two pin crystal it has a XTAL1 and XTAL2 pin. You can just look at the clock sources section in the datasheet of the device you are interested in, an external clock means an external clock pulse source, an external crystal means the two leg crystal you are trying to use. – alexan_e Aug 1 '14 at 8:14
• Right... The CLKI is used for external clock input, and CLK0 (system clock) can be used as an clock for some other device. – Nazar Aug 1 '14 at 14:01
• "Does that naming on AVR chips mean it does not have the internal circuitry?" If an AVR can drive a crystal directly, it will say so somewhere in the datasheet, such as on p. 29 of the ATmega328P datasheet. The "Clock Sources" section of the ATtiny10 datasheet never mentions the word "crystal" or shows any illustrations showing the two pins of a crystal directly connected to the AVR. That implies that a two pin crystal alone is not enough -- the ATtiny10 requires one of the options described in the datasheet. – davidcary Nov 28 '14 at 1:17