Thank you so much Kchriste and Gayan for your valuable suggestions.
I really really appreciate that!
I don't know what you exactly going to do?
Well basically I have a 1kHz 1V-pk AC signal [Halfwave rectified] going into the PIC. I want to write a code which can detect the amplitude of the input signal [1V-pk in this case]. So if I fluctuate the input to 2V, the Output [LEDs on PORTC] now should display 2V accordingly.
To do that, I have already explained my code but my problem is that the sampling does not start at zero all the time.
For detecting the peak value of the signal, I will compare the last sample taken to the current one.
Take ADRESH - save to a memory location, wait 20 microseconds, take a new sample, compare to last sample, if the new sample is larger, throw out the older sample and save the newer one in its place, and so on ... until the newer sample is smaller than the older one. Thus detecting the peak = amplitude. This works fine when sampling starts and the signal is at zero and then each sample values increases. But the code fails when sampling starts and the signal has already crossed its max peak and the sample values are decreasing.
So that's why I suggested to have a zero crossing code so that the sampling can always begin when signal is at zero
Hope this makes sense now.
Ps: Could you please provide any example code or tutorial where I can see the zero crossing routine?
Thanks again.