What can an Arduino do in 125us

I was thinking about using multiple PWM outputs. This is very much open now, but I would like to control as many objects as possible at every loop. Loop time is 1ms/16-1ms/256 or so. Like I said everything is open. What is possible depends on the speed of CPU but also on the amount of ram. If I have, lets say, 8*64 outputs, that takes 8 latches and 64 bytes, but how much time it would take to control each output. Now it looks like the code is more critical.

Any suggestions or notes are welcome, but note that I prefer to design and program my self.

Leif

Each instruction takes 62.5 nanoseconds. Two instructions would take 125 nanoseconds. So, 2000 instructions would take 125 microseconds.

Most C++ instructions translate into more than one machine instruction. i++ takes only a few instructions. digitalWrite(somePin, someState) translates into many, many machine instructions.

There is no easy answer to your question. How many C++ instructions translate into 2000 machine instructions depends on which specific C++ statements you are referring to.

PaulS: There is no easy answer to your question. How many C++ instructions translate into 2000 machine instructions depends on which specific C++ statements you are referring to.

True. But your answer is a good start. Next, Are all Arduinos as fast, is it possible to use faster crystal?

Is there an easy? way to count those clock cycles a loop takes, from a commented, neat *.lst file for instance. Dream on?

Are all Arduinos as fast, is it possible to use faster crystal?

No. Some are only half that fast (the 8MHz/3.3.V versions). The Arduino can run up to 20MHz, with a faster crystal. Powers of 2 are much easier to deal with, though.

Is there an easy? way to count those clock cycles a loop takes, from a commented, neat *.lst file for instance.

I don't think that a .lst file contains much more than a listing of the actual code being compiled. There are ways to get assembly level code listings, but mapping that to actual C++ statements is not trivial, because of all the optimization that the compiler performs.

Easiest thing to do is try your code, and see if you get the speed you need.

There is an application called objdump.exe (IIRC) to see the assembly code … then it is just counting instructions …

Powers of 2 are much easier to deal with, though.

Apart from 32.768kHz (commonly used for RTCs), I can't think of too many common powers-of-two crystal frequencies.

If you want to be the fastest then you need to write tight machine code and know how many cycles each instruction takes. Sure, register to register binary ops use like 1 cycle but how about digital read and write? At least those are done directly to registers, AFAIK.
LOL, an analog read takes a fair bit of forever in processor time.

Does the 328 pre-fetch instructions? I would guess so.

Does the 328 pre-fetch instructions? I would guess so.

Guess again

AWOL:

Does the 328 pre-fetch instructions? I would guess so.

Guess again

Heh

I don't think you connected the grounds, Dave. I have seen that, I have done that, I have said that.

AWOL: Apart from 32.768kHz (commonly used for RTCs), I can't think of too many common powers-of-two crystal frequencies.

The following are common powers-of-two crystal oscillator frequencies. These are primarily used for stable serial UART communication.

3.6864 MHz 7.3728 11.0592 14.7456

PaulS:

Are all Arduinos as fast, is it possible to use faster crystal?

No. Some are only half that fast (the 8MHz/3.3.V versions). The Arduino can run up to 20MHz, with a faster crystal. Powers of 2 are much easier to deal with, though.

How much work is it if I change the crystal. Serial bus needs fixing, what else? How much work is it?

Leif

115200 baud isn't fast enough?

If I have, lets say, 8*64 outputs, that takes 8 latches and 64 bytes, but how much time it would take to control each output.

You can output a byte at a time through the i/o pins instead of 1 pin at a time if you want to save cycles. I haven't gone into the machine code but it's still probably at a minimum; load, fire, load, fire. Add to that what you need to do to get your data ready to load, fetch from SRAM, Flash, or EEPROM or maybe calculate on the CPU?

2000 cycles / 64 bytes to go out = 31.25 cycles each, 39 cycles at 20MHz...

Of course what 1 MCU can't do, 2 or more might.

The following are common powers-of-two crystal oscillator frequencies. These are primarily used for stable serial UART communication.

3.6864 MHz 7.3728 11.0592 14.7456

I think perhaps you have different understanding of "powers-of-two" to me.

There are a lot of crystals that divide nicely into standard baud rates, but that doesn't make them a power of 2.

Anyway I think PaulS really meant 2, 4, 8, 16 etc MHz, IE powers of 2 * 1000000


Rob

If you have a 16MHz oscillator then you can use the pre-scaler to divide by powers of 2 from 1 to 256. It's probably a lot easier than changing the crystal.

Going back to your original question, take a look at the ShiftPWM library. I have read that it can PWM about 1000 LEDs at a rate fast enough not to flicker. The trick is to use SPI to shift the data into the shift registers quickly.

[EDIT: to expand on this, you can run SPI at up to 8MHz on a 16MHz atmega mcu. So, if you write the SPI code very carefully, you can in theory send your 8 * 64 bits of data to your shift registers in 64us. You then need to add to this the time it takes to calculate what data you want to send. Then multiply by the number of pwm steps you want to get the overall refresh interval.]

If you have a 16MHz oscillator then you can use the pre-scaler to divide by powers of 2 from 1 to 256. It's probably a lot easier than changing the crystal.

You can't make it faster, though, which is what OP (seems to think he/she) needs.

That's the reason I suggested pumping the data out in parallel and/or using multiple processors.

Could the data be stored on external serial RAM and pumped directly to the led drivers via SPI daisy chain? It might be cheaper to use multiple processors though. Either way is an 'add hardware' approach.

One thing though, if I want to use Serial and have an external crystal it looks like I can only do 6-bits wide on PORTB, C or D of my UNO.

Yes it's unfortunate that there aren't any ports with 8 bits available.


Rob

There's 2 but not if I want to use the standard serial RX and TX (like for my USB connect) and an external crystal. At least that's what I see on the pin map. But I could give the serial up and have 8 bits of PORTD, right?