Examples Index

' =========================================================================
'
'   File...... ADC8.SXB
'   Purpose... Simple 8-bit ADC using the Comparator
'   Author.... (c) Parallax, Inc. -- All Rights Reserved
'   E-mail.... support@parallax.com
'   Started...
'   Updated... 05 JUL 2006
'
' =========================================================================

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' This program uses an RC circuit with PWM and the SX's comparator to
' create a simple 8-bit ADC.  The unknown voltage present on pin RB.1
' is found by applying a known voltage (using PWM) to pin RB.2 and
' examining the analog comparator bit.

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID              "CMP_ADC8"

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

AdcChrg         VAR     RB.3                    ' out to RC circuit

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

adcVal          VAR     Byte                    ' reading from ADC
tmpB1           VAR     Byte                    ' work vars
tmpB2           VAR     Byte
tmpB3           VAR     Byte

WATCH adcVal                                    ' use Debug/Poll to view

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

GET_ADC         SUB     0                       ' 8-bit ADC

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  CMP_B = 0                                     ' enable comparator

Main:
  DO
    adcVal = GET_ADC                            ' get new value
    BREAK                                       ' display in Debug mode
  LOOP

' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------

' Use: aVar = GET_ADC
' -- returns 8-bit value of voltage on RB.1

GET_ADC:
  tmpB1 = 0                                     ' reset result
  tmpB2 = 128                                   ' bias to middle
  DO
    tmpB1 = tmpB1 + tmpB2                       ' create test value
    PWM AdcChrg, tmpB1, 1                       ' charge RC
    CMP_B = tmpB3                               ' compare inputs
    IF tmpB3.0 = 1 THEN                         ' if unknown lower
      tmpB1 = tmpB1 - tmpB2                     '   reduce test value
    ENDIF
    tmpB2 = tmpB2 >> 1                          ' divide bias
  LOOP UNTIL tmpB2 = 0
  RETURN tmpB1                                  ' return ADC value