AVR128DA28 How to set clock frequency

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


This datasheet is quite different from the AVR's I am use to seeing. 

Have not been at the bench for a while as I had my L5 S1 fused, finally feel like doing something to keep my brain working.

 

Anyway, Could someone help me with C code to set the frequency of the clock. 

 

Here is the page I don't seem to understand. It looks like I would set bits 5 to 2 as if they were bits 3 to 0 so setting bit 5 to a 1 would be #8 on the list and 20mh?

 

Second thing How do I code this, Here are two attempts

 

CLKCTRL_OSCHFCTRLA = CLKCTRL_FREQSEL_8M_gc;

 

 Compile errors

expected identifier or '(' before 'volitile'

expected ')' before '(' token

 

Program runs and i get 3us width on output

 

    while (1)

    {

        PORTC_DIRTGL = 1;

        PORTC_DIRTGL = 1;

    }

 

 

 

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh I found example code at GitHub 

 

 

_PROTECTED_WRITE (CLKCTRL.OSCHFCTRLA, CLKCTRL_FREQSEL_8M_gc);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Alas How do I get red of the red squiggly lines, compiles fine but I don't know where _Protected_write comes from Find won't find it just where I use it.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

try

Underline Unknown Symbols (Whole Tomato Software)

due to

Visual Assist | Microchip Studio

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This doesn't directly address your question, since it's for the AVR128DB48 and for the external crystal oscillator rather than the internal, but in case it's helpful to you here's a snippet of code that seems to work for me (this code is based on Microchip github code):

 

  //============================================
  // Enable external crystal oscillator for 16 MHz, 4K start up
  //============================================
 	ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, CLKCTRL_RUNSTDBY_bm
	  | CLKCTRL_CSUTHF_4K_gc
	  | CLKCTRL_FRQRANGE_16M_gc
	  | CLKCTRL_SELHF_CRYSTAL_gc
	  | CLKCTRL_ENABLE_bm);
	while(!(CLKCTRL.MCLKSTATUS & CLKCTRL_EXTS_bm)); // Wait for Xtal startup
        ccp_write_io((uint8_t *) &CLKCTRL.MCLKCTRLB, 0x00);  // Clear Main Clock Prescaler
        // Set the main clock to use XOSCHF as source, and enable the CLKOUT pin
	ccp_write_io((uint8_t *) &CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_EXTCLK_gc | CLKCTRL_CLKOUT_bm);
        while(CLKCTRL.MCLKSTATUS & CLKCTRL_SOSC_bm) ; // wait for change to complete
        // Clear RUNSTDBY for power save during sleep
	ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, CLKCTRL.XOSCHFCTRLA & ~CLKCTRL_RUNSTDBY_bm);

As for the defined bit patterns, you can find those in a .h file associated with your compiler.  look for a folder called ioavr128da28.h in your Program Files (if on a PC).  I'm using the DB48 and my file for that is at: "C:\Program Files\Microchip\xc8\v2.31\dfp\xc8\avr\include\avr\ioavr128db48.h"

 

it's not a small file...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>but I don't know where _Protected_write comes from

 

xmega.h, included from io.h

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks curtvm , I just open that file and VAssistX finds it. So Include files that include other header files might be the reason it wont update.

 

Gchapman yes that is the one I found and started using it.

 

Paul where does ccp_write_io come from is it the same as _Protected_write or different?

ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, CLKCTRL_RUNSTDBY_bm

I have to say these are pretty cool chips, I am making a light curtain for a punch press I made that punches out die cut parts. Don't want employees to get their fingers pinched.

I also really like the Microchip Power Debugger lightening fast using UPDI

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First understand that the red squiggle thing is nothing more than eye candy. It won't affect whether the code will build or not. Sometimes there'll be squiggles yet it will build just fine. Most of the time it gets it right but not always.
.
The squiggle generator works by parsing it's way through the #include'd .h files so it knows how things are defined but one thing that often seems to throw its interpretation is where something is conditionally defined. Quite a lot of stuff in headers is "#ifdef DOXYGEN simple_definition #else complex_definition #endif" and it's almost like it cannot "see" / interpret either definition which often leads to red squiggles.
.
I'm not saying it has to be that in this case but it is the kind of problem it has.
.
If it bothers you there'll be an option somewhere to disable it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do find though it helps me when I use code someone else has written to find out more about the code. Really all squiggles if the code is correct should not have squiggles, if opening the .h file with the offending code removes the squiggles then I learn a little more where the actual code comes from instead of just a cut and paste. I have the luxury of time with no deadlines though and the process of learning how stuff works instead of just making it work I find more satisfying as this is really a hobby with some devices sprinkled in that are useful in my manufacturing business.  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

metron9 wrote:
... then I learn a little more where the actual code comes from ...

Project Directories (Whole Tomato Software)

[third paragraph]

The C/C++ Directories node in the options dialog for Visual Assist tells Visual Assist where to find header files, and which of the header files should be re-parsed frequently versus infrequently. 

...

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you look at that data sheets now you have to look out for this:

Property:      Configuration Change Protection