Hello
I have the following code for Arduino mega to output 40 kHz square waves at the analog pins. I need to apply specific phase delays for the signals at each pin though and am struggling to find out how to do this. Any help would be appreciated.
Thanks!
byte TP = 0b10101010; // Every other port receives the inverted signal
void setup() {
DDRF = 0b11111111; // Set all analog ports to be outputs
DDRK = 0b11111111; // Set all analog ports to be outputs
// Initialize Timer1
noInterrupts(); // Disable interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 200; // Set compare register (16MHz / 200 = 80kHz square wave -> 40kHz full wave)
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS10); // Set prescaler to 1 ==> no prescaling
TIMSK1 |= (1 << OCIE1A); // Enable compare timer interrupt
interrupts(); // Enable interrupts
}
ISR(TIMER1_COMPA_vect) {
PORTF = TP; // Send the value of TP to the outputs
PORTK = TP;
TP = ~TP; // Invert TP for the next run
}
void loop() {
// Nothing left to do here :)
}
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Each timer can have a different phase, but each timer controls 2 or 3 analog pins typically, so certain pins have
to stay in phase (although they can be in antiphase on the same timer). Reading the hardware timers sections of the ATmega2560 will explain all this in detail.
hi Tom the phase shifts are so that when emitting the square wave signals from several transducers I can focus the resulting beam in a region of constructive interference.
Hi,
Squarewaves won't produce an interference pattern, sinewaves will.
There will be many regions of constructive interference, are you trying to make an ultrasonic cannon?
What are you using as ultrasonic transmitters?
How much phase/time shift are you trying to get?
Wouldn't just arranging the transducers in a circle cause them to constructively interfere along the central axis? That would be much easier than a phased array if size is not an issue.
Consider which minor phase shifts are required for generating the intended focus, and which phase shifts can be provided by a 16MHz controller. I'd recommend a much faster controller for your project. But for now you can find out how to code the generation of multiple square waves with time shifts.
the phase shifts are so that when emitting the square wave signals from several transducers I can focus the resulting beam in a region of constructive interference.
Yes, that is the basic theory behind common practice, except of course, the transducers are assumed to emit sine waves.
If you can't provide quantitative answers to the questions we are asking, this project may be too difficult for you.
Multiple (acoustic) waves can accumulate in certain places, and extinct each other to some degree somewhere else.
Likewise a single source can be recorded in a number of locations, so that the required phase shift for all transmitters in those locations can be determined. This should work for all waveforms, be sine or square or something distorted in between these, provided equal speed of sound.