Sampling ADCIN at 1ms for 1 second


Results 1 to 10 of 10

Threaded View

  1. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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 &amp; 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">&quot;[2J&quot;</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">&quot;Ready to take samples: Press any key to begin&quot;</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">&quot; &quot;</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 &quot;1000 255&quot;,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">&quot;ERROR: Loop time too SLOW. T0IF bit already set.&quot;</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)
    Last edited by Darrel Taylor; - 19th May 2009 at 04:27.
    DT

Similar Threads

  1. ADCIN and PIC18F4331 Arghhhhh !
    By GrandPa in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th December 2010, 20:27
  2. How to make adcin stable?
    By Pic2008 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th January 2009, 07:41
  3. ADCIN problems with PIC16F73
    By passion1 in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 26th July 2007, 11:38
  4. ADCIN - AD settings are wrong
    By teverett in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th December 2006, 16:32
  5. help using adcin
    By harryweb in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st January 2006, 07:46

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts