Took a small break (I went away for some time)from this project and then resumed back to it, and actually decided it will be my best choice to learn registers from the ground up. Now I am at point where I figured out how to make all the timings, and also how to make the ADC sample at the timer's frequency, and all the other details.
Only roadblock right now is that I can't make the ADC trigger at the compare match A of timer/counter0. I successfully made it trigger at the overflow of timer/counter0, but that way I can't adjust where it actually triggers in the timer period(1024us), that's why I specifically need it to trigger at compare match A. I must also add that I successfully configured the timer to output a pulse using the output compare register, the pulse it's rock solid and exactly timed where it's need it to be, I did that for adjusting the compare match position.
Also I really appreciate everyone's replies and time invested in providing the suggested codes.
Now that I have a better understanding of registers, I am more aware the 328p's capabilities. And changed my way to go about this code.
Smajdalf:
Use single autotriggeteing of the ADC. In the ISR for the triggering timer prepare the trigger for the next sampling and change MUX. In ADC ISR collect result. You will probably need low impedance source for the ADC.
They understand it best. Using the ADC's autotrigger function is the only way to go for jitter free sampling. And I'm going to change the analog input each cycle by changing the MUX each cycle with a counter to 3 and after that get ADC value, the ADC conversion and other stuff will be in an interrupt vector triggered by timer2 with the same div factor as timer0 a bit after (for headroom) the ADC has finished it's thing (the best method in my opinion). Instead of previously thinking to sample all 3 analog inputs in 1 cycle (this is unnecessary).
Only thing I need help solving right now is making the ADC autotrigger work with timer/counter0 compare match A.
I have noticed when I use timer/counter0 compare match A it just takes 1 sample at startup and repeats it infinitely.
Sorry if my explanations are messy.
Let me know where im messing up.
Here is the code:
byte timing1 = 45;
byte syncpulse = 20;
int sample;
void setup() {
Serial.begin(9600);
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADATE);
ADCSRB = B00000011; //trigger at timer/counter0 compare match A
DDRD = B01101000; //make pin D5, D6, D3 output //for testing purposes
DDRB = B00001000; //make pin B3 output
//timer0
TCCR0A = B00000011; //Timer Control Register 1, fast pwm mode
TCCR0B = B00000011; //Timer Control Register 2, counter clock divided by 64 (1024us cycle)
//tried using fast PWM with OCRA for TOP, but doesn't help
OCR0A = timing1;
//timer2 //for syncing external hardware
TCCR2A = B10100011; //Timer Control Register 1, both Compare Match Outputs on non inverting mode, fast pwm mode
TCCR2B = B00000100; //Timer Control Register 2, counter clock divided by 128 (1024us cycle)
OCR2A = syncpulse; //sync pulse
}
int readAdc(char chan) //reading adc
{
ADMUX = B01000000; //select input and ref
while (ADCSRA & (1<<ADSC)); //wait for end of conversion
return ADCW;
}
void loop() {
sample = readAdc(0);
Serial.println(sample);
delay(50);
}