megaAVR® Oscillator Example Project

 Objective

This page provides a simple project demonstrating dynamic adjustment of the System Clock Frequency via modification of the System Clock Prescaler Register (CLKPR) on megaAVR® devices. The code example runs on the ATmega328PB MCU.

The System Clock Source is set via configuration fuse bits to be the external 16MHz clock provided by the mEDBG chip (see connection diagram below).

The project configures the Timer/Counter1 module to operate in Clear-Timer-On-Compare (CTC) mode, and, on a period match, generates a "tick" interrupt every 100 mS. The timer clock source is configured to be SYS_CLK/64.

The Timer/Counter1 ISR toggles LED0, and increments a Counter. The main program loop monitors the Counter value and updates CLKPR value every 10 seconds to dynamically change the SYS_CLK frequency.

CLKPR is toggled between div/1 and div/4 setting, thereby changing the toggle (interrupt) interval from 100 ms to 400 mS respectively. This can be seen as the LED0 blink rate changes every 10 seconds.

Review the project's main.c file for further detailed comments and description of operation.

 Materials

Hardware Tools

Tool About Purchase
ATmega328PB-XplainedMini-50px.png
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Example Project

We recommend extracting the .zip file to your C:\ folder.

You should see the folder C:\MTT\8avr\mega\code-examples\oscillator-example\8avr-mega-oscillator-example containing the solution 8avr-mega-oscillator-example.atsln

 Connection Diagram

The mEDBG chip controls the programming/debug interface, as well as supplying a 16 MHz clock when the Xplained board is connected via USB cable to a PC.

xplained-mini-connection-diagram-as7.png

The target ATmega328PB CKSEL fuse bits are initially unchangeable on the Xplained Mini board from within Atmel Studio. "Verification" errors will be displayed if programming CLSEL bits with any other setting other than "external clock".

This may be overridden by clearing the mEDBG fuse filter as described in section 1.6.2 of the ATmega328PB Xplained Mini User Guide.

 Procedure

Attach the ATmega328PB Xplained Mini board to your computer using a USB-A-male-to-Micro-B-male cable. Start Atmel Studio 7. If the board has been successfully enumerated, you should see the board image come up in Studio as shown:

xplained-mini-enumeration-success.png

The Xplained mini board is identified by the last four digits in its serial number (see sticker on bottom of board). In the above example, the last four digits are "3352"

1

Open the Solution

In Studio, select File > Open > Project/Solution and navigate to the saved location of the solution, then select the file 8avr-mega-oscillator-example.atsln:

as7-open-solution.png
as7-open-osc-solution-detail.png

2

Rebuild the Solution

as7-rebuild-osc-solution.png

3

Program the Fuses

There are several key hardware configuration settings that need to be configured. The following fuse settings need to be programmed into the device:

  • Ext: 0xFC
  • High: 0xDF
  • Low: 0xC0 (EXT CLK, Fast VDD rise, CLKDIV = 1)

Enter the Device Programming dialog as shown:

as7-programming-fuses-osc-1.png

In the Device Programming dialog box, select Tool, Device and Interface as shown, then click Apply:

as7-program-fuses-2.png

To verify a connection, select Read and verify that a Device Signature is found:

as7-program-fuses-3.png

Select the Fuses sub-section, Enter the 3 Fuse Byte Values above, then click Program as shown:

as7-program-fuses-4.png

4

Program the Hex File

While still in the Device Programming dialog box, select the Memories sub-section as shown. The path to the solution's hex file should already be listed in the dialog. Click Program as shown:

as7-program-hex-osc-example-1.png

 Results

osc-mega-example-results.png

 Conclusions

This project has provided an example of how to dynamically adjust the system clock frequency on the megaAVR® MCU.

 Learn More

 
megaAVR® Oscillator Overview
Learn more >
 
megaAVR® Timer Overview
Learn more >
 
megaAVR® Interrupts Overview
Learn more >
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.