Oh I don't know, doesn't sound that hard to me. 
@ 20mhz, 115200 baud is easy with the USART, higher is also possible, but 115200 is all you need for this.
@115200 the bit period is ~8.7us, and a full byte (10-bits) is ~87us.
The longest HSEROUT line will be "1000 255",13,10 or 10 bytes. for a total of ~870us. (less than 1ms)
The ADCIN statement slows things down for no reason.
Since the ADC is only looking at 1 Channel, you don't have to worry about Acquisition times.
And by controlling the ADC manually, the conversion happens at the same time as the HSEROUT so it's influence will be negligible.
Then using a Timer to measure the 1ms intervals and ...
Here's an example ...
Code:
<font color="#000080">@ DEVICE HS_OSC, WDT_OFF
</font><font color="#008000"><b>DEFINE </b></font><b>OSC </b><font color="#800000"><b>20
</b></font><font color="#0000FF"><b><i>; Set your Analog Pins as needed here
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_RCSTA </b><font color="#800000"><b>90</b></font><b>h </b><font color="#0000FF"><b><i>' Enable serial port & continuous receive
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_TXSTA </b><font color="#800000"><b>24</b></font><b>h </b><font color="#0000FF"><b><i>' Enable transmit, BRGH = 1
</i></b></font><font color="#008000"><b>DEFINE </b></font><b>HSER_SPBRG </b><font color="#800000"><b>10 </b></font><font color="#0000FF"><b><i>' 115200 Baud @ 20MHz, -1.36%
</i></b></font><b>GoDone </b><font color="#008000"><b>VAR </b></font><b>ADCON0</b>.<font color="#800000"><b>2 </b></font><font color="#0000FF"><b><i>; A/D start conversion bit
</i></b></font><b>T0IF </b><font color="#008000"><b>VAR </b></font><b>INTCON</b>.<font color="#800000"><b>2 </b></font><font color="#0000FF"><b><i>; TMR0 overflow flag
</i></b></font><b>x </b><font color="#008000"><b>VAR WORD </b></font><font color="#0000FF"><b><i>; sample counter
</i></b></font><b>TempB </b><font color="#008000"><b>VAR BYTE
</b></font><b>OPTION_REG </b>= <font color="#800000"><b>%11010100 </b></font><font color="#0000FF"><b><i>; TMR0 1:32 prescaler, internal clk
</i></b></font><b>ADCON0 </b>= <font color="#800000"><b>%10001001 </b></font><font color="#0000FF"><b><i>; Fosc/32, AN1, DONE, module ON
</i></b></font><font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>27</b></font>,<font color="#FF0000">"[2J"</font>] <font color="#0000FF"><b><i>; ANSI clear screen
</i></b></font><b>Ready</b>:
<font color="#008000"><b>HSEROUT </b></font>[<font color="#FF0000">"Ready to take samples: Press any key to begin"</font>]
<font color="#008000"><b>HSERIN </b></font>[<b>TempB</b>]
<font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
<font color="#008000"><b>FOR </b></font><b>x </b>= <font color="#800000"><b>1 </b></font><font color="#008000"><b>to </b></font><font color="#800000"><b>1000
</b></font><font color="#008000"><b>WHILE </b></font>!<b>T0IF </b>: <font color="#008000"><b>WEND
</b></font><b>TMR0 </b>= <font color="#800000"><b>100 </b></font><font color="#0000FF"><b><i>; load timer for 1ms
</i></b></font><b>T0IF </b>= <font color="#800000"><b>0 </b></font><font color="#0000FF"><b><i>; reset the overflow flag
</i></b></font><b>GoDone </b>= <font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>; start the A/D conversion
; The time it takes for the A/D conversion (~20us) is less than the
; time to send a single byte from the USART (~87us). So the conversion
; will always be complete by the time it gets to DEC ADRESH
</i></b></font><font color="#008000"><b>HSEROUT </b></font>[<font color="#008000"><b>DEC </b></font><b>x</b>,<font color="#FF0000">" "</font>,<font color="#008000"><b>DEC </b></font><b>ADRESH</b>,<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
<font color="#0000FF"><b><i>; The longest hserout line would be "1000 255",13,10 (10 bytes)
; @ 115200 baud each byte takes 86.8us for a total of 868us (under 1ms)
</i></b></font><font color="#008000"><b>IF </b></font><b>T0IF </b><font color="#008000"><b>THEN HSEROUT </b></font>[<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>, <b>_
</b><font color="#FF0000">"ERROR: Loop time too SLOW. T0IF bit already set."</font>,<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
<font color="#008000"><b>NEXT </b></font><b>x
</b><font color="#008000"><b>GOTO </b></font><b>Ready
</b>
I put a check at the end of the loop to warn if the loop took too long. It never does.
Tested on a 16F737.
Using Timer0, the interval is not EXACTLY 1ms, but the coding was easier. (+/- 6us per sample)
Switching to Timer1 would make it more accurate. (+/- 0.6 us)
Bookmarks