Arduino Forum

Using Arduino => Microcontrollers => Topic started by: remout on Sep 13, 2011, 06:34 pm

Title: Two chip sanity check
Post by: remout on Sep 13, 2011, 06:34 pm
I've got two processes which are both time sensitive.

1. Every cycle I loop for .25 sec and wait for pulses coming in via an interrupt. I take the count of the pulses and update a LCD.

2. I continuously listen for signals coming in from an I.R. receiver and stop a timer if it's my I.R. beam coming in. I then update the same LCD

Does this require two chips? I'm using ATmega328s.

Thanks!

Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 13, 2011, 06:53 pm
Nope.  One processor can do an awful liot of stuff in that 250mS.
Title: Re: Two chip sanity check
Post by: remout on Sep 13, 2011, 07:21 pm
I don't understand. Doesn't my 250ms loop, within void loop(){}, block the processor from doing anything with the pin listening for the IR sensor?

Thanks!
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 13, 2011, 10:46 pm
Write your loop to check for IR pulses, or a flag set in the IR for the pulses.
If the flag is set, update your counter, then go back to doing IR stuff.

I have a scoring machine that checks a series of things every 100uS - and most of the time nothing is going on, so it os very responsive when something does occur.
Title: Re: Two chip sanity check
Post by: JChristensen on Sep 13, 2011, 10:50 pm
Are you using the delay() function? If so, then yes that can make doing two things "at once" difficult.

In many cases, we want loop() to run as fast as possible, checking for things to do, and not calling delay(). If for one of those things, we only want to check every 250ms, then get the elapsed time from millis() and save it in a variable. Then check the variable against millis() every time through loop(), and only do the 250ms thing if the current value of millis() is at least 250 more than the value saved in the variable. Then reset the variable to the new current millis() value.

See the famous example in the Arduino IDE, File > Examples > 2.Digital > BlinkWithoutDelay.
Title: Re: Two chip sanity check
Post by: remout on Sep 13, 2011, 11:28 pm
I better check my understanding of loop.

void loop(){

long startTime = millis();
//lets call this counting
while(millis() - startTime < 250)
{
        count something
}

//lets call this blinking

if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;


}

Am I correct in thinking that while counting is running for 250ms blinking is stopped or not running? So whatever the ledState is it will remain until counting is done?

Thanks!
Title: Re: Two chip sanity check
Post by: remout on Sep 14, 2011, 12:01 am
Oh, okay guys.... Thanks. The dim light is coming on.

Can I:
1. use an interrupt on one pin to increment a counter
2. set the counter to 0 and startTime to millis()
3. monitor millis() in the loop for millis()- startTime > 250
4. grab the accumulated value of the counter and do something with it.

The reason is so my code can keep looping and doing other stuff.

Am I getting closer.

Thanks!
Title: Re: Two chip sanity check
Post by: JChristensen on Sep 14, 2011, 03:10 am

Can I:
1. use an interrupt on one pin to increment a counter
2. set the counter to 0 and startTime to millis()
3. monitor millis() in the loop for millis()- startTime > 250
4. grab the accumulated value of the counter and do something with it.


Yes, yes, yes, and yes.

In your previous post, you are correct, while "count something" is running, nothing else is running. At the lowest level, the processor is only executing a single instruction at any given time. As we have only a single processor, it is physically impossible to run more than one instruction at a time. Any appearance to the contrary is just an illusion, caused by the speed of the processor and careful control of program flow and execution. Now if some interrupt is enabled, of course it is entirely possible that "count something" is interrupted, control transferred to the interrupt service routine, and then "count something" resumes when the ISR completes.

If you understand the "BlinkWithoutDelay" sketch, you will be well on your way. To test your comprehension, try writing a sketch that blinks two LEDs independently at different rates. Say, blink one once per second and another twice per second.

Note that there is some protocol to follow when sharing a variable between an interrupt service routine and the main program (loop). Because we only have an 8-bit processor, depending on the data type of the counter, it may take the main program more than one instruction to fetch it, zero it, etc. If the main program is in the middle of doing this when the interrupt fires, we can get invalid results. To avoid this, use the volatile (http://www.arduino.cc/en/Reference/Volatile) qualifier when declaring the variable, and inhibit interrupts any time the variable is being accessed, updated, etc. by the main program. See the doc on the cli() and sei() functions (http://www.arduino.cc/playground/Main/AVR).
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 14, 2011, 03:16 am
You're getting the idea.
You'll need to set a flag or something once startTime has begun so you don't reset the elapsed time at the top of loop every time.
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 14, 2011, 03:17 am
Alternately, see attach and detach interrupts.
Title: Re: Two chip sanity check
Post by: remout on Sep 14, 2011, 03:41 am
Thanks guys!

You've given me much to think about. Back to the drawing board.
Title: Re: Two chip sanity check
Post by: JChristensen on Sep 14, 2011, 03:50 am
Let us know how things work out. OTOH, going back to the title of this thread, I believe CrossRoads may actually have a multi-processor solution  XD
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 14, 2011, 04:41 am
Got it designed, still thinking about about other options before I plop down $25 for boards...
Take your pick, dual '328s or dual '1284s
Title: Re: Two chip sanity check
Post by: JoeO on Sep 14, 2011, 04:50 am
How about adding mounting holes.
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 14, 2011, 05:21 am
This more current layout has the mounting holes and additional ground pins that were discussed in an earlier thread.
Title: Re: Two chip sanity check
Post by: Njay on Sep 14, 2011, 02:52 pm
I wrote these 2 small articles specifically on doing several things at the same time:

How to do several things at the same time (http://embeddeddreams.com/site/2011/07/10/how-to-do-several-things-at-the-same-time/)

More on doing several things at the same time (http://embeddeddreams.com/site/2011/07/23/more-on-doing-several-things-at-the-same-time/)
Title: Re: Two chip sanity check
Post by: cyclegadget on Sep 14, 2011, 03:37 pm
@CrossRoads,

If your going to do it, do it BIG! Dual '1284s would totally rock! I probably would never get full use out of it but, it sure would be a neat little toy!
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 14, 2011, 08:26 pm
Okay, I'll order some boards tonight. Was thinking about how to make a shield type board to go with it, what to do with all the pins that are there, which to bring up or to leave holes for, etc.
Title: Re: Two chip sanity check
Post by: Texy on Sep 14, 2011, 10:24 pm

Got it designed, still thinking about about other options before I plop down $25 for boards...
Take your pick, dual '328s or dual '1284s

Hey crossroads, where do you order your pcbs from?
Texy
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 15, 2011, 01:36 am
My 2 orders have been with iteadstudio. Am waiting for mini-uino's to show up, status on 50-lot of 1.3" x 1.3" PCBs is "departure from outward office of exchange" on 9/4.
Prior order for Bobuino's took a while too.
http://iteadstudio.com/store/index.php?main_page=product_info&cPath=19_20&products_id=175&zenid=edplpd8kf14ntqn128uncg0bd6
and
http://iteadstudio.com/store/index.php?main_page=product_info&cPath=19_20&products_id=173

seeedstudio offers similar
http://www.seeedstudio.com/depot/fusion-pcb-service-p-835.html?cPath=185
Title: Re: Two chip sanity check
Post by: EmilyJane on Sep 15, 2011, 03:11 am
I guess this is sort of on topic, certainly goes with the subject anyway.  :) Back at the beginning of time when microprocessors had 1 MHz clocks and program and data storage was external to the processor, using more than one CPU in a system sometimes was vital rather than just an interesting thing to do. For example, in a data acquisition and processing system, to get the throughput necessary for success, one could have one CPU controlling data acquisition and buffering and another CPU handling computations and HMI. Since early microprocessors had external address and data busses and memory some interesting things were possible.

If the CPU clock was two phase, you could operate the CPUs off of opposite phases and therefore share the same physical program and data storage. Since many times there were functions that were common to both subsystems, sharing the program storage was an economical thing to do, as EPROMs were relatively expensive then. Data storage sharing was particularly compelling because it eliminated the interprocessor communication that would have slowed things down otherwise. A system of semaphores in shared R/W memory kept things synchronized and since the CPUs were on oposite phases of the clock, there was never any interference. Completely separate areas of program and data could also be maintained if that was desired.

Nowadays we just throw another $3 microcontroller at the problem. :D
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 15, 2011, 03:49 am
Well,  maybe a little more - $5.82 at mouser currently  :smiley-mr-green:
Title: Re: Two chip sanity check
Post by: EmilyJane on Sep 15, 2011, 03:57 am
Hah, I used to pay that much for 128 bytes of RAM.  :D
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 15, 2011, 04:34 am
Yeah, prices sure have dropped.
And now a 512K x 8 SRAM is $4.70
http://www.mouser.com/ProductDetail/ISSI/IS61WV5128BLL-10TLI/?qs=sGAEpiMZZMs6Aik9Fp479htyRPPcVL%252bzv89zHNw4tYc%3d
Maybe I'll make a 1M x 16 SRAM for capturing  24 second sound samples at CD quality.
4 chips, 3 8-bit counter chips, a couple of transceiver to mux the 2 bytes & store to  SD card, 16 BIT DAC to play it back for a sound check ...
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 15, 2011, 07:01 am
Didn't get to ordering boards yet- realized I hadn't done the gerbers & checked them out yet. Couple more days...
Title: Re: Two chip sanity check
Post by: Texy on Sep 15, 2011, 08:42 am

My 2 orders have been with iteadstudio. Am waiting for mini-uino's to show up, status on 50-lot of 1.3" x 1.3" PCBs is "departure from outward office of exchange" on 9/4.
Prior order for Bobuino's took a while too.
http://iteadstudio.com/store/index.php?main_page=product_info&cPath=19_20&products_id=175&zenid=edplpd8kf14ntqn128uncg0bd6
and
http://iteadstudio.com/store/index.php?main_page=product_info&cPath=19_20&products_id=173

seeedstudio offers similar
http://www.seeedstudio.com/depot/fusion-pcb-service-p-835.html?cPath=185

10cm x 10cm x 10 pieces, only $24.90 (plus shipping) - WOW.
They also supply an Eagle to Gerber conversion program (I have only ever used Eagle before).

Texy
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 15, 2011, 01:40 pm
Eagle exports Gerbers directly. Downloads itead's 'rules' file, it will provide the setup files needed so you can select in eagle files:export and have the file outputs needed created and check what you will be getting,
Title: Re: Two chip sanity check
Post by: Texy on Sep 15, 2011, 02:02 pm
Many thanks CR - I will download that and give it a go, once I,ve designed my board.
Do I need both the .cam and .dru files?
How long did it take you take get your boards, once ordered?
(I know we will in different countries, but it may just give a ball park figure).

Texy
Title: Re: Two chip sanity check
Post by: JChristensen on Sep 15, 2011, 02:31 pm

Back at the beginning of time...


Good stuff!  6800?  8080?  6502?
Title: Re: Two chip sanity check
Post by: JChristensen on Sep 15, 2011, 02:35 pm

My 2 orders have been with iteadstudio.


Might have to give them a try, gonna be hard to beat their $/in2 rate. @CrossRoads, what sort of turnaround time do you see from them?
Title: Re: Two chip sanity check
Post by: EmilyJane on Sep 15, 2011, 03:53 pm


Back at the beginning of time...


Good stuff!  6800?  8080?  6502?


6800 at first and then mostly 6809 soon after. I really liked the 6809. All those address modes that worked for every instruction. Hated the 8080. You had to keep the fold-up card with all the opcodes with you all the time to check if you could use a particular address mode with a particular instruction. The Z80 was pretty nice.

The 68H16 was probably the cream of that crop. It's a shame it grew out of its DIP shoes. Made it hard to prototype with.
Title: Re: Two chip sanity check
Post by: graynomad on Sep 15, 2011, 04:23 pm
Z80 gets my vote. Closely followed by the 68000 in the "aircraft carrier" package.

______
Rob
Title: Re: Two chip sanity check
Post by: EmilyJane on Sep 15, 2011, 04:36 pm

Z80 gets my vote. Closely followed by the 68000 in the "aircraft carrier" package.

______
Rob


Yes, wasn't that an impressive package? That was a proper microprocessor with external busses and a package you could plug into a breadboard. I had one of those wire-wrap "bed of nails" proto boards that was just a huge XY grid of machined sockets with a 68008 on it.
Title: Re: Two chip sanity check
Post by: CrossRoads on Sep 16, 2011, 03:37 am
Ok, the order I placed for 50 mini-uino's was accepted Aug 26, shipped Sept 1, tracking showed up in China customs Sept 4, arrived here Sept 15 (Boston, MA area).
Bobuino's were about same.