Cheap External Chip Needed

Hello,
I'm looking for a microcontroller (preferably an AVR, since I know how to work with them) that will let me read four digital inputs extremely quickly (ideally read each one within a few microseconds or, even better, have four external interrupts), do a little bit of subtraction, and then output the values to my Arduino for further processing (it would only need to output about every 10-20ms).

I've looked at the attiny, but that appears to just have the SPI interface. Is that easy to work with on both ends?
Is there another microcontroller that has the easy-to-use USART interface? Is there another type of communication that doesn't use many pins and is well-supported?

Thanks for your help!

do a little bit of subtraction

As opposed to what? A whopping big chunk of subtraction? ]:slight_smile:

and then output the values

What sort of values? Floating-point? Discrete? Randomly generated?

I've looked at the attiny, but that appears to just have the SPI interface

The ATtiny2313 (and family) has a UART.

[quote author=Coding Badly link=topic=61206.msg442153#msg442153 date=1305433622]

and then output the values

What sort of values? Floating-point? Discrete? Randomly generated?[/quote]

Integers. I'm getting pulse lengths to read a RC receiver I had lying around.

[quote author=Coding Badly link=topic=61206.msg442153#msg442153 date=1305433622]

I've looked at the attiny, but that appears to just have the SPI interface

The ATtiny2313 (and family) has a UART.[/quote]

Huh. The ATtiny13A datasheet had no mention of it, and neither did the 45. The 2313 seems a little large for my application, but I will definitely consider it.[/quote]

I'm getting pulse lengths to read a RC receiver I had lying around.

What is the price if a pulse is missed?

In your post you mention "cheap" and "four digital inputs". Why not just use four pins on your Arduino and use pulseIn to read the pulses?

Don't care that much if a pulse is missed. Using the Arduino is too slow -- when I tried, it upped my looptime from 1-2ms to 70ms. I probably did something wrong though, since I set the timeout to be 20ms and I was reading two pulses..

I found something that might work:

How accurate do the measurements need to be?

<10 us error

Shortest pulse expected? Longest pulse expected?

Using the Arduino is too slow

The Arduino is just an AVR like most others. If you can do it with a tiny13 you can certainly do it with a mega328.

I probably did something wrong though,

Yep :slight_smile:

If you use 4 pulseIn()s in a row

pulseIn(1,y);
pulseIn(2,y);
pulseIn(3,y);
pulseIn(4,y);
process_results();

you will get consecutive pulses and there will be the inter-pulse delay between each one so the time taken may be quite long. But if you don't mind missing pulses then

pulseIn(1,y);
process_results_for_1();
pulseIn(2,y);
process_results_for_2();
pulseIn(3,y);
process_results_for_3();
pulseIn(4,y);
process_results_for_4();

Should have a pretty fast response.

I found something that might work

168 pages of data sheet, what part exactly might work.


Rob

Graynomad:

Using the Arduino is too slow

The Arduino is just an AVR like most others. If you can do it with a tiny13 you can certainly do it with a mega328.

The Mega328 is already doing lots of things - I basically want a dual-core.

I found something that might work

168 pages of data sheet, what part exactly might work.

The PIC 16F628A

As for the pulseins, what do you mean by an inter-pulse delay? What's happening is that I have a timeout of 20000 us on the pulsein, use two of them, and I end up with a loop that's 70ms slower. The worst that should happen is to be 40ms slower, if it never receives a pulse. Does the command take a while to initialize or something?

pulseIns() waits for an edge then times to the next edge. So it could wait for quite a long time. If you do 4 in a row each one may wait for a long time so in total the procedure takes a very long time.

A lot depends on the nature of your pulses, if they all start at the same time but have varying lengths and a 10mS period each pulseIn() reading could take 10mS worst case. If the pulses are not synchronised you can't determine how long things will take.

How long are these pulses?
What is the period of them?
Are they synchronized or independent?

If you really need to offload this job I don't see why a tiny85 couldn't do it, use SPI to return the data. The 85 interrupts the Arduino when data is ready. The Arduino does N SPI.transfer()s to get the data. Clock the 85 using the clock output pin from the Arduino (PB0 or DIG8) so you have accurate timing.


Rob

Have you scoped the four lines to see if they start simultaneously? If they do, you can hook them all up to to a single hardware interrupt pin (via diodes), and just count the cycles until the pulse ends for each one - the maximum time taken in this case is equal to the maximum pulse length (should be <= 2ms).

Alternatively, you could write some code in the PCINT ISRs - they trigger on every pin, so you'd need to read which pin is being triggered, but it's simple enough to record the micros() when the pulse changes low -> high (beginning of pulse) and again when it changes high -> low (end of pulse). Subtraction will give you the pulse length. This method is used on one of the quadcopter platforms (I forget which one), so it's quite reliable.

Also, if the pulses trigger sequentially, stagger them - pulseIn 1, 3, 2, and then 4 - you will reduce the time spent waiting for the next pulse to start that way.

You could use another ATmega8, 168, or 328 (just like an Arduino chip.) That way you would be REALLY familiar with it.
Bare 328s are less than $5, and I've seen mega8s for about $2. Usually it doesn't make a lot of sense to hunt for cheaper than that unless you're planning on making tens of thousands of units or something.

It appears that the pulses are indeed offset, so I can use the OR gate.

I might write a playground article on how to read servo pulses, since I don't think it's very well document and really isn't all that hard. I'm working on creating a library to do it also.

Thanks for all the help though!

SPI is pretty easy to work with on AVRs - you'll have to use slave mode on the Arduino and master on the pulse detector though which might not be compatible with any other use of SPI.

If you can use pin-change interrupt on your Arduino that might be a better solution.

WizenedEE:
Is there another type of communication that doesn't use many pins and is well-supported?

I'd recommend I2C. It's not very complicated, uses only 2 lines, and you can work with up to 127 devices simultaneously (various sensors, eeprom etc). With SPI, AFAIK you can use only one device at once (you have to switch them if you have more than one in circuit).