Electronic – 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.

Best Answer

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.