Apparent Problem with Clock Cycle

Normally I'd comb the forums for hours searching for an answer, but i've worked for hours and hours and read through lots of docs with no real success and i hoped someone would be kind (or bored) enough to help me.

After trying to move a programed atmega168 out of an arduino into a "standalone" breadboard setup (without any success), I found when i put the chip back into the arduino it didn't work. I reprogrammed it with the same program and it started working again, but it seems as though the clock is sped up or wrong.

The program I'm using is based on one of the pwm programs to make an led dim and brighten. The problem is now everything looks REALLY choppy. I think i may have hurt the internal clock, but if i'm using the arduino, shouldn't the clock on the board take over?

Also, can you use any external clock for a standalone setup, besides say the standard 16mhz crystal, you used a 25mhz crystal, would it still work? Can anyone provide any extra information about moving the atmega168 into a standalone setup? or point me to a FAQ for troubleshooting? And can you use a USB avr programmer (like the one ladyada sells) to program the bootloader into a fresh 168?

Can't vouch for anything in the first 3 paragraphs, but

If you change the crystal to a 25mhz one, you'd have to modify the bootloader to suit, or else all the timing would be off.

And yes, you can re-program a bootloader via a USB programmer, just plug the 6 pin plug into the Arduino, open the Arduino IDE, Tools > Burn Bootloader > {Programmer model}

Without knowing exactly what you've done, I can't give you much support beyond saying that it's conceivable you accidentally changed mega168's clock fuses. If you set it to run off the internal RC oscillator, it's now running at 8 MHz and ignoring the external crystal. If you set the CKDIV8 fuse, it's now running at 2 MHZ (the external crystal divided by 8). If you changed both fuses, it'd be running at 1 MHz (internal oscillator divided by 8).

I suggest you quantify the problem. For example, run a simple LED blinking program that uses delay(1000) to turn the LED on for 1 second and off for 1 second. Then use a stopwatch to time the duration of the blinks to see if they differ from the expected intervals and by how much if so. For example, if the LED turns on for 2 seconds and off for 2 seconds, you know you're running at 8 MHz. If the LED turns on for 8 seconds and off for 8 seconds, you know you're running at 2 MHz. If the LED blinks at the expected rate, the problem is probably not with your system clock.

Also, can you use any external clock for a standalone setup, besides say the standard 16mhz crystal, you used a 25mhz crystal, would it still work?

You can't use a 25 MHz crystal because the mega168 can only operate up to 20 MHz (and this speed requires a Vcc of 5 V), but you can run a standalone mega168 off of an external crystal or resonator. See section 8 (starting on page 28) of the mega168 datasheet (when you want to know how to do something, the datasheet is a great place to start, though it can be a bit intimidating at first).

you use a USB avr programmer (like the one ladyada sells) to program the bootloader into a fresh 168?

Yes. You can even use it to program your sketch onto the mega168, which means you don't need to sacrifice 2k of flash for the bootloader.

  • Ben

To be clear, you cannot program the bootloader with the arduino. Correct?

Thanks a lot for the info on the datasheet... I'll get to reading :p.

Correct, you need some external programmer to program the bootloader onto a fresh mega168.

  • Ben

So after trying the other 3 PWM pins and they worked fine, I think I may have actually damaged 9,10,and 11.

Since I think that's what happened, does anyone see anything wrong with the following...

I have a 5.13V power supply connected to my circuit setup in the same standalone manner as this page http://itp.nyu.edu/physcomp/Tutorials/ArduinoBreadboard. It's not a perfect 5 volts.

I used a 25khz crystal instead of the 16 (which would be normal).

I didn't have resistors connected to my LEDs on pins 9,10,and 11 because i figured the atmel would use the pull up resistors. I was using the PWM aspect of these pins.

Does anyone see anything up there that would have hurt pins 9, 10, and 11? From being smooth and controlled to choppy... almost laggy? (if you were slowly increasing and decreasing brightness with the analogWrite function?)

Also (sorry to be the annoying guy who doesn't spend all his time reading), is it possible to run the atmega off 3 volts or so? Instead of 5? and if so is there anything special that needs to be done? Lower clocks or special settings?

I used a 25khz crystal instead of the 16 (which would be normal).

Do you mean 25 MHz or 25 kHz? The mega168 has a maximum speed grade of 20 MHz, so you should not be using it with a 25 MHz crystal.

I didn't have resistors connected to my LEDs on pins 9,10,and 11 because i figured the atmel would use the pull up resistors. I was using the PWM aspect of these pins.

Internal pull-ups only come into play when you have the digital I/O pins configured as inputs. There are no internal pull-ups on lines when you are using them as outputs (this wouldn't make sense). If you connected LEDs directly to a pin without a resistor in series and then drove that pin (as is what happens when you use the pin as a PWM output), you could have damaged that pin (it's basically like you shorted that pin to your LED's voltage drop and then told the AVR to try to force it to be 5 V).

Also (sorry to be the annoying guy who doesn't spend all his time reading), is it possible to run the atmega off 3 volts or so? Instead of 5? and if so is there anything special that needs to be done? Lower clocks or special settings?

Look at the first page of the datasheet (the part about speed grades). If you want to run the mega168 off of 3 V, you must use a clock speed that is 10 MHz or lower.

  • Ben

Thanks a lot Ben. I figured I was asking some slightly stupid (or not so knowledgeable) questions and i'm glad to have them corrected.