# Setting up an external clock source on an ATmega8

I am attempting to set up V-USB on my ATmega8-16PU (datasheet), and could use some help hooking up an external crystal clock source to it. I have tried to understand the datasheet to the best of my ability, and have drawn a few conclusions which I would appreciate if someone could verify and critique. For reference, I will be using a 16 MHz or 20 MHz crystal, which I also need help deciding between.

Here is my plan so far, based on my understanding:

• Attach my external crystal (two pin one), to the XTAL1 and XTAL2 pins of the IC
• Use two 22pF ceramic capacitors
• Based on Table 2 of Page 26, CKSEL3..0 has to be 1111-1010
• CKOPT will be unprogrammed (based on the description at the top of page 27) because I don't need a large output swing, not am I driving anything else with the crystal
• CKSEL3..1 has to be 101, 110, or 111 because my crystal is greater than 1 MHz
• CKSEL0 has to be set to 1, and SUT1..0 to 11 (based on table 5 on page 28) because I want to allow the maximum start up time to play it safe

Right, so I have taken these notes, and don't know where to go from here. For example:

1. What are all these things (CKSEL3..1, CKSEL3..0, etc.), and how do I know which value to choose for CKSEL3..0 and CKSEL3..1 if I am given an array of options to choose from?
2. What do I need to change, and how do I set the fuse bits to enable these settings? I am using avrdude to program my microcontroller.
3. Do I need to use a watchdog? If I do, it doesn't seem like there are any settings to enable or disable it (bottom of page 26), or is it always enabled?

Thanks, everybody. Sorry about all these questions, I am very new to all of this, but am determined to learn.

CKSEL typically stands for clock select. There are several choices that can be selected via an internal multiplexer-- some for test, others for alternate sources.

Since you are using a xtal, you can provide the code to select that option: which are in the first 2 rows of p 26. You can use 3 bottom rows of table 4 for a crystal. Each is tuned to a particular range of frequencies as shown.If your crystal reference was design to oscillate around 4MHz, you would set the sel bits to code, 111, for example. 16Mhz only works with CKOPT low using any of the three select codes shown in row 4 of the table.

External caps should be 12-22pF not 22uF for the xtal according to the same table. It also only specifies a max oscillator limit of 16MHz, so I would not use a 20MHz xtal.

Looks to me like the watchdog timer functionality is internal.

*note xtal and crystal are used synonymously here.

• Thanks for the answer. Unfortunately, it doesn't answer some of my questions. First, I edited the 22uF to pF, was a mistake, sorry. Second, in the first row on page 26, it says I can choose CKSEL3..0 to be 1111 - 1010. What does that mean, why is it giving me a range? Third, how do you know a frequency around 4 MHz corresponds to 111? Fourth, it seems that the 16 MHz limit is only placed on resonators - is that right? Where does it say a 16 MHz limit applies to external crystals? Finally, what do I do next, once I choose my settings? Thanks again, much appreciated. – capcom Aug 27 '12 at 2:26
• CKSEL has 4 bits you can select via ff bits. The range of those 4 bits is as shown : 1111-1010 for your app (look at row 1 tble 2), the leftmost bit or MSB will always be 1 for you, because you only need choose the right 3 for your frequency range (explained in table 4). Since you said you are going to use >=1MHz and CKOPT=0, then any of the bit codes, 101,110,111 should be adequate for your app. Look closely at table 4, p 27. Note CKSEL 3..1=111 corresponds to range 3.0-8Mhz. You will use the one you want to correspond to 16MhZ which is just below if I understood you. – pat Aug 27 '12 at 2:41
• as mentioned in the datasheet it offers you some internal circuitry to optimize around the external frequency and range you provide. You tell it that value via the bits. If you thought this was useful, you can give me a bump as I'm new here. – pat Aug 27 '12 at 2:52
• Thanks, everything seems to have worked out great. FWIW, here is the avrdude command I used: avrdude -c usbasp -p m8 -U lfuse:w:0b11111111:m -U hfuse:w:0b11001001:m ... may come in use for future readers of this question. To make it clear, this command was used to set up a 16MHz external crystal on my ATMega8 using the USBasp programmer. – capcom Aug 28 '12 at 0:32