Please bear with me until I get to the coding part. I'm trying to make a two channel biphasic pulse sequence generator with variable voltage amplitude, frequency etc. Here's a biphasic pulse:
The pulse width has to be in this range: 200 us - 500 us. I'm passing serial data from two Arduino Uno pins to a DAC. Each DAC has two outputs. Output R and L are determined through every odd 16 bits sent by the Arduino. For clarification about the DAC see this:
For each channel of the pulse generator, the two outputs of the DAC are used and they are added up to make the biphasic pulse. One output which is inverted through an op-amp, for when it is negative and the other for when it is positive.
Now, let's get to the coding part.
//first line of inputs goes to R.OUT of DAC1,second goes to L.OUT of DAC,. 3rd line goes to R.OUT of DAC2,4th goes to L.OUT of DAC12
void WriteRL (int r15_1,int r14_1,int r13_1,int r12_1,int r11_1,int r10_1,int r9_1,int r8_1,int r7_1,int r6_1,int r5_1,int r4_1,int r3_1,int r2_1,int r1_1,int r0_1,
int l15_1,int l14_1,int l13_1,int l12_1,int l11_1,int l10_1,int l9_1,int l8_1,int l7_1,int l6_1,int l5_1,int l4_1,int l3_1,int l2_1,int l1_1,int l0_1,
int r15_2,int r14_2,int r13_2,int r12_2,int r11_2,int r10_2,int r9_2,int r8_2,int r7_2,int r6_2,int r5_2,int r4_2,int r3_2,int r2_2,int r1_2,int r0_2,
int l15_2,int l14_2,int l13_2,int l12_2,int l11_2,int l10_2,int l9_2,int l8_2,int l7_2,int l6_2,int l5_2,int l4_2,int l3_2,int l2_2,int l1_2,int l0_2 )
{
delayMicroseconds(d1);digitalWriteFast(SI,r15_1);digitalWriteFast(SI_2,r15_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r14_1);digitalWriteFast(SI_2,r14_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r13_1);digitalWriteFast(SI_2,r13_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r12_1);digitalWriteFast(SI_2,r12_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r11_1);digitalWriteFast(SI_2,r11_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r10_1);digitalWriteFast(SI_2,r10_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r9_1 );digitalWriteFast(SI_2,r9_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r8_1 );digitalWriteFast(SI_2,r8_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r7_1 );digitalWriteFast(SI_2,r7_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r6_1 );digitalWriteFast(SI_2,r6_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r5_1 );digitalWriteFast(SI_2,r5_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r4_1 );digitalWriteFast(SI_2,r4_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r3_1 );digitalWriteFast(SI_2,r3_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r2_1 );digitalWriteFast(SI_2,r2_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r1_1 );digitalWriteFast(SI_2,r1_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,r0_1 );digitalWriteFast(SI_2,r0_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);
digitalWriteFast(LRCk,1);
// Previous data is written to R.Out. Now that LRCk is 1, the next 16 clocks determine the L.OUT.
// At this point, we don't care about the L.out. For the next 16 clocks, we are going to assign the lowest value to L.OUT.
delayMicroseconds(d1);digitalWriteFast(SI,l15_1);digitalWriteFast(SI_2,l15_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l14_1);digitalWriteFast(SI_2,l14_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l13_1);digitalWriteFast(SI_2,l13_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l12_1);digitalWriteFast(SI_2,l12_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l11_1);digitalWriteFast(SI_2,l11_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l10_1);digitalWriteFast(SI_2,l10_2);delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l9_1 );digitalWriteFast(SI_2,l9_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l8_1 );digitalWriteFast(SI_2,l8_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l7_1 );digitalWriteFast(SI_2,l7_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l6_1 );digitalWriteFast(SI_2,l6_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l5_1 );digitalWriteFast(SI_2,l5_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l4_1 );digitalWriteFast(SI_2,l4_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l3_1 );digitalWriteFast(SI_2,l3_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l2_1 );digitalWriteFast(SI_2,l2_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l1_1 );digitalWriteFast(SI_2,l1_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);delayMicroseconds(d1);digitalWriteFast(SI,l0_1 );digitalWriteFast(SI_2,l0_2 );delayMicroseconds(d1);digitalWriteFast(Clk,1);delayMicroseconds(d2);
digitalWriteFast(Clk,0);
digitalWriteFast(LRCk,0);
}
Each time this function is called, serial input goes to two DACs for 32 clocks. Let's call each 32 clocks an "Interval". Now, I know this much delay along with the two cycles that each digitalwritefast takes already defeats my purpose, but this can be fixed through using NOP instead of the delay. The real problem is that the time my calculations take are also limiting. Basically, first of all after the users enters the parameters for the pulses, I calculate for how many Intervals I need to send data (N). Then a while(counter<N)
loop repeats and based on the counter it is determined weather each output is low,high or zero in the current Interval. and based on that, the function above is called. I put
time = micros();
Serial.println(time);
right before and after the "While". and the difference is more than 300 us. Then, the calculations take up another 600 us and finally the function is called. This is all while I wanted my Interval to take only 100 us. What concerns me the most is the time that while(counter<N) takes because I cannot think of a way to not have to use that. Is there any way to make this faster?