1
\$\begingroup\$

I'm doing some debugging on my project and I need to check if I'm setting my external crystal correctly. I have the ability to print out to a serial monitor via usb, is it possible to retrieve the Clock Speed and whether or not the cpu is running off the internal clock or an external crystal so I can print that out?

\$\endgroup\$
  • \$\begingroup\$ Do you have an oscilloscope? You could toggle an I/O pin and measure the clock speed that way.. \$\endgroup\$ – m.Alin Feb 17 '15 at 9:17
  • \$\begingroup\$ I do not. I need to be able to check in code. \$\endgroup\$ – bwoogie Feb 17 '15 at 15:21
3
\$\begingroup\$

You can measure the current CPU Clock speed by using the internal Real Time Clock driven by the 32.76kHz local oscillator. I don't have an XMEGA handy at a moment so I can not give exact procedure, but the general idea is to set up the RTC to measure some fixed amount of time (say 1ms), and then measure how many clock ticks occur during that time period using one of the 16 bit Counter/Timers. If, say, you count 8,044 ticks during a single 1ms RTC cycle, then your CPU clock speed is 8044/1ms= ~8Mhz.

You can check if you are running off of an internal or external clock by looking at the System Clock Selection (SCLKSEL) bits in the Clock Control Register (CTRL).

\$\endgroup\$
2
\$\begingroup\$

This doesn't require a scope. Does require an I/O pin.

Based on what you think the crystal frequency is, write a routine that you think should toggle an I/O pin every second (500 ms on, 500 ms off), then connect an LED to the pin and count the number of pulses in a minute. Then depending on whether the number comes up 60 or something else you can get a clue what is going on.

I've done this all the time to check my clock settings.

\$\endgroup\$
  • \$\begingroup\$ This works. I got 60 pulses in a minute, but I don't know what frequency it's actually running at or if its running on the internal clock or external clock. \$\endgroup\$ – bwoogie Feb 18 '15 at 3:28
  • \$\begingroup\$ @bwoogie I would do some experiments. First an easy one would be to disconnect the external clock and see if it still runs. Secondly, make a change to your code which you think should make a measurable difference and see what actually happens. Depending on how you wrote your code (whether it is a simple loop or you made use of a timer), it should be possible to work backwards and figure out the frequency. \$\endgroup\$ – tcrosley Feb 18 '15 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.