arduino timing problem

My problem is this:

my ultrasonic transmitter is connected to the arduino I/o pin which outputs a pulse of 40 kHZ high and low by simple:

digitalWrite (transmitterPin, HIGH); //sets the transmitter to 5 V
delayMicroseconds (9); //first half cycle of 40kHz
digitalWrite (transmitterPin, LOW); //sets the transmitter to 0 V
delayMicroseconds (9); //second half cycle of 40kHz

my adc for the receiver needs a clock pulse of 4 MHz which I want to get from arduino also..

the problem is they have to run simultaneously... meaning while the transmitter is pulsing high and low of 40 kHz, the arduino should also ouptut 4 MhZ clock pulse to drive my ADC IC. WHat should i do?

Thank you

I would look at generating the 40 kHz signal with a hardware timer. If you monkey with the pre-scaler you can probably get the tone library up in that range. You can definitely get the AVR counter/timer hardware to do it.

To get a 4Mhz clock, I think you're going to have to use an external divider to divide down the 16 MHz system clock. If the 40 kHz and 4 Mhz clocks have to be synchronized, you might consider doing it all with a counter. If the 40 kHz clock may be free running, you can use a 555 timer circuit to generate that without creating any load on the Arduino.

To get a 4Mhz clock, I think you're going to have to use an external divider to divide down the 16 MHz system clock. If the 40 kHz and 4 Mhz clocks have to be synchronized, you might consider doing it all with a counter. If the 40 kHz clock may be free running, you can use a 555 timer circuit to generate that without creating any load on the Arduino.

You can configure a 328 timer to generate the 4MHz - it needs to be set up with a prescaler of divide-by-1, TOP = 3, OCR=1 (or perhaps 2?) - but you'll need to mug-up on the timer control registers in the ATmega328p datasheet to do this. I've a few notes that might help:

http://www.mythic-beasts.com/~markt/ATmega-timers.html

thank you very much for the information .. :smiley:
I appreciate it...
another follow up question....

what is the max frequency at which the gpio ports could read digital data?...

I just need to know because as I said, I need 1 MHz pulses for my 12-bit ADC which will return 12-bits digital output to be stored by the arduino?...

thanks..

another thing is that I will just need 1 MHz pulse instead of 4 MHz... :slight_smile:

I think that would be easier. is there any way to directly output 1 MHz pulse from digital pin? I mean something like a function or library that could help me do it?...

So what i just need would be 1 MHz clock where I will use a counter to create 40 kHz

thank you