I have a need to do some logic processing at 200Khz. I would like to accomplish this on Atmega328 (Nano) as a proof of concept and then move to a smaller chip like Attiny1614 or something similar later on.
The main program starts a timer that periodically interrupts the processor at 200Khz approximately. Thereafter, the main program just kills time in an infinite loop.
The timer ISR is triggered once every 200Khz, it reads 3 digital inputs, performs some logic operation and outputs on 2 outputs. Nothing complicated in the logic operation.
Can the processor handle interrupts at 200Khz?
Can it be accomplished using 16Mhz Atmega328?
Thanks and Regards,
Timer0 can be used to generate an interrupt @200Khz. Its an 8 bit timer, MCU clock is 16Mhz so each cycle is 1/16Mhz = 0.0625uS.
200khz is 1/200Khz = 5uS.
No of clock cycles to be counted by timer = 5uS/0.0625uS = 80.
Note:- Timer0 is used by millis, which is really not needed here
It's what you do in between the pulses that matters. If the processing required in your program exceeds the number cycles in between pulses, then you end up with bad data since you'll be starting a new calculation before the previous one is finished.
Take note that jumping to ISRs takes clock cycles too so take that into account
200kHz is about every 130 instructions. So I’d say “no”, you won’t be able to do that.
I get 1 per 80 instructions, 100 if running at 20 mhz (and 160 if using at AVR Dx-series clocked at 32, which works fine at room temp as far as I can tell)… Yeah that’s a pretty tall order. It is do-able if the math is VERY simple, and you’re comfortable in AVR assembly… but it requires attention to detail… and probably no millis either. And while there are tasks running in the background, they ain’t getting much CPU time.
BUT when you say logical operations - is this something that could be done with the CCL on an ATtiny1614 or similar? The 0/1-series only has 2 LUTs, but that might be enough, and AVR128DA is available with 4 or even 6 LUTs… Those can be run asynchronously - generally significantly faster than the processor clock speed. (something like 110 MHz divided by the number of async stages, roughly - in my tests at least)
For reading pins, some logic and writing result 80 CK could be enough time. I think it can even be done in C. But you must avoid superslow digitalRead and digitalWrite. Also if it is the only task of the MCU avoid the ISR overheat. Just wait for the timer flag, clear it, do your task and repeat.
If you're doing any counting/timing tests, a while back I was playing with the input capture and timer2 on an UNO. Self-testing I could accurately count pulses up to 8MHz and up to 3.2MHz from an external source. I didn't have interrupts going that fast as I was gating the pulses with timer 2. The code was crude, just learning, ahem (still am).
I get 1 per 80 instructions
Ah. Somehow 200kHz became 120kHz on the way to the calculator
Doing something in an interrupt in 80 cycles might be "just possible" if you program in assembly language, are very, very, careful, and don't use any of the Arduino functions. Just the ISR enter/return is about 11 cycles, not including the C code (four clocks each for entry and reti, 3 clocks for the jmp in the vector location.
I'd very much stick with my "no, you can't do it" answer - time to get a faster processor and/or think about different ways to solve the problem (such as using hardware timer features, or external hardware.)
(Note that "faster processors" frequently come with increased ISR overhead. The Cortex M0 takes at least 15 cycles to enter an ISR (vs the AVR's 7. But at that point, it's already saved a bunch of state that you'd have to do manually on the AVR, so ... it gets complicated.)
Time for OP to describe the required logic operation. I can imagine
Reading 3 inputs, single port: 1 CK
Simple logic: 10 instructions = 10 CK
Write 2 outputs: 3 CK (read-modify-write)
ISR saving 5 registers: 20 CK
Other ISR stuff: ~ 20 CK
Total: easily less than 60 CK!
I think it is possible when logic operation is simplified.
However, knowledge of AVR Assembly is required.
I have a need to do some logic processing at 200Khz.
Some interesting uses for CCL (reply#4) ... like having a built-in micro CPLD device:
Configurable Custom Logic (HACKADAY)
Combination Lock using CCL
However, knowledge of AVR Assembly is required.
Why? After all 3 inputs mean 8 possible states, very small LUT that can be implemented in C and which will probably be fast enough. Knowledge of ASM would be needed only if the compiler fails miserably on this simple task.
This is exactly the problem the new AVRs have the CCL for. But if you don't mind it consumes nearly all processor time it can be done in SW.
How about a simple ISR:
inputs = PIND & 0xE0; // read port and mask off lower 5 bits
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.