ATmega2560 working at half speed when compiling with AS7

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

Hello,

 

I'm working on an Arduino Mega board, trying to make it work with AS7.

 

I'm working on the following code:

 

DDRE |= 0b00110000;

while(1)
{
    PINE |= 0x00110000;
    _delay_ms(5);
}

It make the GPIO 2 and 3 switching on every loop execution.

 

When compiling with Arduino IDE, no problem. But when compiling with AS7, the delay seems to be 10ms instead of the 5ms!

 

I also try to  work on AS7, starting the project from the Arduino Sketch (so there is my application and ArduinoCore, and the init() function from wiring is called), but the behavior is the same: 10ms delay!

 

Something must be missing in the init... any idea what could be wrong?

 

Thank you

Antoine

 

 

 

 

 

 

This topic has a solution.
Last Edited: Tue. May 29, 2018 - 01:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The PIN command toggles the state of the pin the PORT command sets/resets the pin.  Change PINE to PORTE.

 

David

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

AntoineC wrote:
the delay seems to be 10ms instead of the 5ms!
To use _delay_ms requires that F_CPU is defied to the correct value for the actual CPU speed - is it? Say, for example, you have set F_CPU to be 16MHz but the actual CPU is running at 8MHz then for _delay_ms(5) the library will simply have provided a loop that delays enough cycles to last for 5ms at 16MHz but if the CPU is really just 8MHz then that number of cycles would take 10ms to execute.

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

I want "the output to switch, on every loop", not "the output to switch on, every loop". Sorry for my english... So using PINE is correct .

 

The question is, why:

- When using Arduino Ide, the delay between 2 switch is 5ms

- When using AS7, the delay between 2 switch is 10ms

- When using AS7, creating the project from the Arduino Sketch, delay is 10ms too.

 

The init function called is the same in Arduino IDE and AS7 project created from sketch.

 

 

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

AntoineC wrote:
The question is, why: - When using Arduino Ide, the delay between 2 switch is 5ms - When using AS7, the delay between 2 switch is 10ms - When using AS7, creating the project from the Arduino Sketch, delay is 10ms too.
Did you miss my post? The important thing is that at the moment <util/delay.h> is #included the F_CPU symbol is already defined to be the CPU speed in Hz.

 

I suspect you may have set 16MHz but, because of CKSEL, perhaps your chip is only running at 8Mhz.

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

Yes sorry I miss your post! ;-) I start answering when only David answered...

 

So yes, F_CPU is defined at 16000000UL in my project.

 

I thought that the Atmega2560 on Arduino Mega was always working at 16MHz. Is it not the case?

 

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

AntoineC wrote:
So yes, F_CPU is defined at 16000000UL in my project.

Where exactly is it defined.  In your source code, or as a general project option that is passed as a global option to the compiler. This link touches on some of the issues of where to declare a F_CPU so that e.g. _delay_* is compiled with the correct F_CPU value: https://www.avrfreaks.net/comment/850683#comment-850683

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

It is defined as a global option to the compiler in the toolchain properties of the project.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 PINE |= 0x00110000;

BTW if you are using the PIN register "atomic toggle" function the whole point is that it IS atomic. So you don't want to do an RMW on it. Just use:

 PINE = 0x00110000;

that only affects bits 4 and 5.

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

Thank you Clawson for your answers

 

I changed the F_CPU definition to 8000000 and now the timing are correct. So I looked for the CKSEL you are talking about.

The fuse are set to use: Int. RC Osc; Strat-up time: 6CK + 65ms.

I have read that internal oscillator allow the CPU to run only to 8MHz, it explain the behavior of my code...

 

So I would like to use the external oscillator that is present on the Arduino Mega, but there is a lot of different option for the fuse...

 

Option that seems to work is to set the fuse to work as:Ext. Crystal Osc. But there is no option for 16MHz, only 8Mhz.

Do you know how to set the fuse to use the external oscillator to make the CPU work at 16MHz

 

Thank you for the PINE information!

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

Sometimes intervals (say 0.9-4 Mhz) are denoted as half intervals (say 8Mhz -) to mean "above 8Mhz"

I suppose there is a difference between an  "external oscillator" and a Xal (except for the Xal and it capacitors, everything is ** inside** the AVR)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, never mind, there is a display error...

Fuse tools display "8MHz- ", but it seems that it should be "8MHz - 16MHz"

 

So I change to "Ext Crystal Osc. 8.0- MHz ...", change the F_CPU to 16000000UL again, and all seems to be okay!

Thank you!!

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

dbrion0606 wrote:

Sometimes intervals (say 0.9-4 Mhz) are denoted as half intervals (say 8Mhz -) to mean "above 8Mhz"

I suppose there is a difference between an  "external oscillator" and a Xal (except for the Xal and it capacitors, everything is ** inside** the AVR)

 

Yes thank you for the confirmation. I though there was a display error, but it just seems they remove the oscillator maximum value!

Last Edited: Tue. May 29, 2018 - 01:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

AntoineC wrote:
Fuse tools display "8MHz- ", but it seems that it should be "8MHz - 16MHz"
It is impled "8MHz or higher" or "8MHz to maximum".  I'd say intentionally written that way and not a display error.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

AntoineC wrote:
Option that seems to work is to set the fuse to work as:Ext. Crystal Osc. But there is no option for 16MHz,
The point is that the fuses just say "external". They don't specify a speed for external because the speed is determined by the metal can you choose to attach. If you say "external" then connect a 3.6864MHz external can then the CPU runs at 3.6864MHz. If you add an 8MHz can then the CPU runs at 8MHz. If you add a 16MHz can then it runs at 16Mhz and so on. So you need to add a 16MHz crystal can, two capacitors then set the fuses to "external crystal" (and very importantly not "external clock"!!) and now it runs at 16M. When it does then edit you F_CPU value to the right speed so the software "knows" too.

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

So, as a summary if anyone got the same problem:

  • On an Arduino Mega, there is an External Oscillator at 16MHz. To use it, you have the set the Fuse to "Ext. Crystal Osc. 8.0- Mhz"
    • if not defined to External Oscillator, CPU will work with internal oscillator that seems to work at 8Mhz
  • Define F_CPU for the whole projects (Keep in mind that F_CPU gave the compiler the information of the frequency, IT DO NOT SET THE CPU SPEED!!)
  • If your oscillator works at 16Mhz or above, select "8- MHz", which means "8Mhz or above"  (it was not clear!! :-b)

 

Thank you all for your help

 

Last Edited: Tue. May 29, 2018 - 01:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For my personal knowledge, Fuse configuration allow to select different Start-up time.

What is the aim of adding such delay?

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

AntoineC wrote:
For my personal knowledge, Fuse configuration allow to select different Start-up time. What is the aim of adding such delay?
Crystals take a while to start resonating (think of running a wet finger around the rim of a crystal glass - it actually takes a while until the glass starts to "sing"). So with CPU crystals you want to set  the SUT to be as long as you can pick (64K?) to give the crystal as long as possible to settle into resonance. Only ever consider a shorter SUT if there's some reason you really need a "quick start" - but consider it might not have settled into full resonance if you do.

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

"On an Arduino Mega, there is an External Oscillator at 16MHz"

 

No : it is an external Xal ( a huge portion of the oscillators is INTERNAL, inside the atmega)

external oscillators are way more expensive, and need to unsolder the Arduino Xal (their only use I can figure out is to have a very accurate clock, some are temperature compensated, in the purest SiO2 one can find)....

I cannot figure out what would happen if an external oscillator was selected, instead of an external Xal. I bet it would be unpleasant.

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

dbrion0606 wrote:

"On an Arduino Mega, there is an External Oscillator at 16MHz"

 

No : it is an external Xal

For more details on the difference, see: https://www.avrfreaks.net/commen...

 

EDIT

 

Oh - and it's also Tip #4 !

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. May 29, 2018 - 02:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, browsing through avrfreaks post is heroic...

 

Reading tiny charcters, too (I never do; bet other people do not either). when it is redundant with what one knows, it is a huge waste of time.

 

What would be useful would be to ask Atmel / Microchip to withdraw ambiguities (or sources of unuseful/toxic complexity) from menus ... (bubble help; clear separation of Xal/other sources, say).... if they want to attract beginners.