Electronic – How to determine the right microcontroller for your project

embeddedmicrocontroller

I'm curious as to how would someone pick out their microcontroller for their project when it comes down to the horse power?

I am not talking about the hardware (ADC,DAC,SPI,ETC) as its more intuitive. I once posted here asking how to implement a digital filter into an Atmel Atmega328p, but they simply said, it wasn't 'up to speed'.

So what is up to speed? And how can I find out if its up to speed?

For example:

I have an Atmel Atmega328p @ 16MHz and I have it sampling an input sine wave 60Hz using the ADC.

The ADC is setup using an interrupt as well as using the running mode configuration sampling at 76KHz.

At each interrupt trigger I want it to run a task of running a difference equation of

$$Y_i = 0.1441U_i+0.2281U_{i-1}+0.1441U_{i-2}+0.6777Y_{i-1}-0.254Y_{i-2}$$

The way I understand it is that I have an interrupt that will be firing at every 13uS. I will need this task be in an another interrupt that fires right after every 13uS but needs to finish within the next ADC interrupt triggering.

Best Answer

Approach 1 (very coarse) - estimate the number of cycles your algorithm will take. I see that you have 5 floating-point multiplies and 4 additions. On a Cortex-M4F each of these operations take a single clock cycle. You will also need some cycles for overhead, interrupt entry/exit etc. Let's assume 10 cycles for the computation and 30 cycles for the overhead, so you need 40 cycles. Let's also assume a clock frequency of 20 MHz (quite low for an M4F). You have 20'000'000 clock cycles per second and 76'000 samples per second. If you divide these numbers you will get ~263 cycles per sample. You need 40, so 263 will be definitely enough. The system load will be around 15% - this is going to be okay. Best if you can look at the disassembly to count the instructions.

Side note: AVRs are not exactly the best MCUs for heavy number crunching.

Approach 2 - Needs a hardware timer. Develop your algorithm, start a timer, run the algorithm once (without the ADC and interrupts), stop the timer. If the timer is clocked at the same rate as the CPU you will get the approximate number of cycles the algorithm takes (approach 1 can be difficult if the algorithm gets more complex). Cortex-M3 and M4 have cycle counters for exactly that purpose. If you now see that the algorithm takes 300 cycles, while you get a new sample every 263 cycles, then you have a problem and can't process it in real time.

How to figure out which exact MCU (or CPU architecture) to choose? Write your algorithm in pure C and run on some eval boards. I keep a couple of cheapest Cortex-M0, M3 and M4F boards for that purpose, before I commit to a particular chip.