MEGA R3 execution speed

I'd like to port a PIC project in Arduino with this shield. In my old SW I have some critical timing codes (i.e. external Anemometer and lighting sensor) and I adjusted the code considering the cycle time of the CPU (PIC 46K22 @ 32MHz). I wonder how can I estimate this loop cycle timing (call sub; digital.read; if true update two u16 vars; digital.write; return) with the new plarform.
Hope to have been clear enough.

the Mega should be able to replace a system based on a PIC18F46K22 depending on IO requirements
specify IO devices you wish to connect and what interfaces they require ?

You can measure it, using the built in function micros().

Beware that the optimizing compiler can move code around or even eliminate it completely, possibly confusing your measurements. It is possible to turn off optimization, if you mistrust the results.

Both can be done with interrupts,
which makes loop time and to an extend processor clock irrelevant.
Leo..

In my old projects I had 6 devices using SPI - AS3935 (critical), MCP795, DS1307, FM24V10, NRF24L01, mechanical anemometer (critical timing), an I2C BMP085, a 1 Wire DS1820.
I used the interrupt for the RTC, anemometer and AS3935, and this was quite complex.
I was used to estimate the exec time knowing the CPU clock speed, the number of cycles and the format of instructions (addressing...) since I came from PIC Assembling, With a rough idea (compiler permitting) of cycles number I designed the program flow accordingly.
Now I know nothing about the ATmega2560 (instruction set, addressing, num of cycles...) so I'm a little worried about its capability to keep up with the flow; furthermore, I hope that the available 8K memory is enough to store all the data (I used some heavy structure).
I'm sorry to ask this possibly dumb questions, but I really wish to learn and use this new world.

One step at a time. Get each device working independently to your satisfaction, then start adding them together.

If you run out of CPU cycles, stop there. In my opinion, the processor used in the Mega R3 is significantly more powerful than the 18F46K22, so I would not worry.

Why would you use two RTCs?
The DS1307 is ancient, and not very precise.

The Arduino environment has library support for SPI and I2C,
and probably libraries for most if not all of your chips.

Looking at the data sheet, I see some potential pitfalls:
PIC18(L)F2X/4XK22

  • 16-bit Wide Instructions, 8-bit Wide Data Path
    -Mega is 8-bit cpu
  • Four Crystal modes up to 64 MHz
    -- "Arduino core" expects 16MHz
  • 10-bit resolution, up to 30 external channels
    -- 16 Multiplexed Single Ended Input Channels
    ... And so on...

Pull the datasheets, look first at the block diagram and compare, noting concerns.

If you get pass the graphics, then start looking deeper. Both units are RISC, so instruction times should be similar at the same clock...

You have some reading to do...

Be aware, moving from bare-metal to Arduino "core" provides some unexpected abstractions; some you may not appreciate.

The Mega also has 16 bit / 32 bit instructions.

In general, an AVR running at 16MHz is about the same speed as a PIC running at 64MHz, because most AVR instructions execute in one clock cycle, while most PIC instructions take 4 clock cycles. The "free" AVR C compilers are also usually regarded as generating better code than most PIC C Compilers.

However, the Arduino-level function calls like digitalWrite() are infamously much slower than "optimal" code. If you had intended to port highly optimized PIC assembly code to Arduino code, you might have a problem. Fortunately, you have "direct port writes" (bypassing the Arduino functions, while still writing in C) if you need to speed things up. (it is very rare to need assembly language for speed. avr-gcc is really good.)


furthermore, I hope that the available 8K memory is enough to store all the data

The PIC18 architecture is limited to 4k bytes of memory. Twice as much should be enough :slight_smile: (RAM usage shouldn't change much moving architectures.)
And there are several AVRs (with Arduino support) with 16k of RAM.

1 Like

I stand corrected.

Ray

At the top or bottom of loop() put a digitalWrite(spareIOpin, !digitalRead(spareIOpin) );, then slap an o'scope on it and measure directly. This will give you an instant, accurate, loop timing reference. It will allow direct comparison and unambiguous resolution of any and every software change.

First of all, let me beg your pardon...the last 48 hours have been filled with searching info, studying, testing, all during late evening or night, so I recognize I made few errors.
My old project was (positevely) tested in early 2'015 so I'm costrained to my (old) memory since I do not have anymore my lab, not even a scope!, nor the modules I made.
Some quick answers:

  • I reported both the MCP705 and DS1307 (now old), but actually they were in two different module (base station and remote station).
  • My doubts on RAM were incorrect: the PIC actually has 4K, and it was enogh, so the 8k of Mega are almost for sure more than required.
  • Most of the Pic instructions are typically 2 cycles, so I'd say that a mean of 3 cycles is correct; every instruction is divided in 4 phases, so a 32MHz clock becomes a 8Mhz cycle time
  • Your sentence about the AVR compiler being better then MPLAB C cheers me up

Last question, then I will leave rest: where do I find info on "direct port writes"?
Thanks to everybody

Just use the register naming of the datasheet,
the Mega has some more than described above.

1 Like

Many thanks

There is also a special functionality not exposed by the core through a function,
writing to the PINx register flips all bits that are written with a 1.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.